blob: db40ca6243b3b66c61feca2344e4faa142ee1a26 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2007-2017 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 Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/bug.h>
25#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>
40#include <linux/netdevice.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020041#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include <linux/rculist.h>
43#include <linux/rcupdate.h>
44#include <linux/seq_file.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020045#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020046#include <linux/slab.h>
47#include <linux/spinlock.h>
48#include <linux/stddef.h>
49#include <linux/string.h>
50#include <linux/workqueue.h>
Matthias Schifferd34f0552016-07-03 13:31:37 +020051#include <net/genetlink.h>
52#include <net/netlink.h>
53#include <net/sock.h>
54#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055
56#include "bridge_loop_avoidance.h"
57#include "hard-interface.h"
58#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020059#include "log.h"
Matthias Schifferd34f0552016-07-03 13:31:37 +020060#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020061#include "originator.h"
62#include "packet.h"
63#include "soft-interface.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020064#include "tvlv.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020065
Sven Eckelmann86452f82016-06-25 16:44:06 +020066static struct kmem_cache *batadv_tl_cache __read_mostly;
67static struct kmem_cache *batadv_tg_cache __read_mostly;
68static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
69static struct kmem_cache *batadv_tt_change_cache __read_mostly;
70static struct kmem_cache *batadv_tt_req_cache __read_mostly;
71static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
72
Antonio Quartullidec05072012-11-10 11:00:32 +010073/* hash class keys */
74static struct lock_class_key batadv_tt_local_hash_lock_class_key;
75static struct lock_class_key batadv_tt_global_hash_lock_class_key;
76
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020077static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020078 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020079 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020080static void batadv_tt_purge(struct work_struct *work);
81static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020082batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020083static void batadv_tt_global_del(struct batadv_priv *bat_priv,
84 struct batadv_orig_node *orig_node,
85 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020086 unsigned short vid, const char *message,
87 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088
Sven Eckelmann62fe7102015-09-15 19:00:48 +020089/**
Antonio Quartullid15cd622015-11-17 16:40:52 +080090 * batadv_compare_tt - check if two TT entries are the same
91 * @node: the list element pointer of the first TT entry
92 * @data2: pointer to the tt_common_entry of the second TT entry
Sven Eckelmann62fe7102015-09-15 19:00:48 +020093 *
Antonio Quartullid15cd622015-11-17 16:40:52 +080094 * Compare the MAC address and the VLAN ID of the two TT entries and check if
95 * they are the same TT client.
Sven Eckelmann4b426b12016-02-22 21:02:39 +010096 * Return: true if the two TT clients are the same, false otherwise
Sven Eckelmann62fe7102015-09-15 19:00:48 +020097 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010098static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000099{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200100 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200101 hash_entry);
Marek Lindner4c718952015-08-06 10:38:54 +0200102 const struct batadv_tt_common_entry *tt1 = data1;
103 const struct batadv_tt_common_entry *tt2 = data2;
Marek Lindner7aadf882011-02-18 12:28:09 +0000104
Marek Lindner4c718952015-08-06 10:38:54 +0200105 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +0000106}
107
Antonio Quartullic018ad32013-06-04 12:11:39 +0200108/**
109 * batadv_choose_tt - return the index of the tt entry in the hash table
110 * @data: pointer to the tt_common_entry object to map
111 * @size: the size of the hash table
112 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200113 * Return: the hash index where the object represented by 'data' should be
Antonio Quartullic018ad32013-06-04 12:11:39 +0200114 * stored at.
115 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200116static inline u32 batadv_choose_tt(const void *data, u32 size)
Antonio Quartullic018ad32013-06-04 12:11:39 +0200117{
118 struct batadv_tt_common_entry *tt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200119 u32 hash = 0;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200120
121 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100122 hash = jhash(&tt->addr, ETH_ALEN, hash);
123 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200124
125 return hash % size;
126}
127
128/**
129 * batadv_tt_hash_find - look for a client in the given hash table
130 * @hash: the hash table to search
131 * @addr: the mac address of the client to look for
132 * @vid: VLAN identifier
133 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200134 * Return: a pointer to the tt_common struct belonging to the searched client if
Antonio Quartullic018ad32013-06-04 12:11:39 +0200135 * found, NULL otherwise.
136 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200137static struct batadv_tt_common_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200138batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200139 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000140{
Marek Lindner7aadf882011-02-18 12:28:09 +0000141 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200143 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000144
145 if (!hash)
146 return NULL;
147
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100148 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200149 to_search.vid = vid;
150
151 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000152 head = &hash->table[index];
153
154 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200155 hlist_for_each_entry_rcu(tt, head, hash_entry) {
156 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000157 continue;
158
Antonio Quartullic018ad32013-06-04 12:11:39 +0200159 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200160 continue;
161
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100162 if (!kref_get_unless_zero(&tt->refcount))
Antonio Quartullic018ad32013-06-04 12:11:39 +0200163 continue;
164
165 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000166 break;
167 }
168 rcu_read_unlock();
169
Antonio Quartullic018ad32013-06-04 12:11:39 +0200170 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100171}
172
Antonio Quartullic018ad32013-06-04 12:11:39 +0200173/**
174 * batadv_tt_local_hash_find - search the local table for a given client
175 * @bat_priv: the bat priv with all the soft interface information
176 * @addr: the mac address of the client to look for
177 * @vid: VLAN identifier
178 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200179 * Return: a pointer to the corresponding tt_local_entry struct if the client is
Antonio Quartullic018ad32013-06-04 12:11:39 +0200180 * found, NULL otherwise.
181 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200182static struct batadv_tt_local_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200183batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200184 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100185{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186 struct batadv_tt_common_entry *tt_common_entry;
187 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100188
Antonio Quartullic018ad32013-06-04 12:11:39 +0200189 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
190 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100191 if (tt_common_entry)
192 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200193 struct batadv_tt_local_entry,
194 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100195 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000196}
197
Antonio Quartullic018ad32013-06-04 12:11:39 +0200198/**
199 * batadv_tt_global_hash_find - search the global table for a given client
200 * @bat_priv: the bat priv with all the soft interface information
201 * @addr: the mac address of the client to look for
202 * @vid: VLAN identifier
203 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200204 * Return: a pointer to the corresponding tt_global_entry struct if the client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200205 * is found, NULL otherwise.
206 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207static struct batadv_tt_global_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200208batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200209 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000210{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211 struct batadv_tt_common_entry *tt_common_entry;
212 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000213
Antonio Quartullic018ad32013-06-04 12:11:39 +0200214 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
215 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100216 if (tt_common_entry)
217 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200218 struct batadv_tt_global_entry,
219 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100220 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000221}
222
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100223/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200224 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
225 * @rcu: rcu pointer of the tt_local_entry
226 */
227static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
228{
229 struct batadv_tt_local_entry *tt_local_entry;
230
231 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
232 common.rcu);
233
234 kmem_cache_free(batadv_tl_cache, tt_local_entry);
235}
236
237/**
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100238 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
239 * for free after rcu grace period
240 * @ref: kref pointer of the nc_node
241 */
242static void batadv_tt_local_entry_release(struct kref *ref)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200243{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100244 struct batadv_tt_local_entry *tt_local_entry;
245
246 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
247 common.refcount);
248
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100249 batadv_softif_vlan_put(tt_local_entry->vlan);
250
Sven Eckelmann86452f82016-06-25 16:44:06 +0200251 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200252}
253
Antonio Quartulli21026052013-05-07 00:29:22 +0200254/**
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100255 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100256 * possibly release it
257 * @tt_local_entry: tt_local_entry to be free'd
258 */
259static void
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100260batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100261{
262 kref_put(&tt_local_entry->common.refcount,
263 batadv_tt_local_entry_release);
264}
265
266/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200267 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
268 * @rcu: rcu pointer of the tt_global_entry
269 */
270static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
271{
272 struct batadv_tt_global_entry *tt_global_entry;
273
274 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
275 common.rcu);
276
277 kmem_cache_free(batadv_tg_cache, tt_global_entry);
278}
279
280/**
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100281 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
282 * for free after rcu grace period
283 * @ref: kref pointer of the nc_node
284 */
285static void batadv_tt_global_entry_release(struct kref *ref)
286{
287 struct batadv_tt_global_entry *tt_global_entry;
288
289 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
290 common.refcount);
291
292 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200293
294 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100295}
296
297/**
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100298 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
299 * possibly release it
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100300 * @tt_global_entry: tt_global_entry to be free'd
Antonio Quartulli21026052013-05-07 00:29:22 +0200301 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200302static void
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100303batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200304{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100305 kref_put(&tt_global_entry->common.refcount,
306 batadv_tt_global_entry_release);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200307}
308
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100309/**
310 * batadv_tt_global_hash_count - count the number of orig entries
Antonio Quartullid15cd622015-11-17 16:40:52 +0800311 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100312 * @addr: the mac address of the client to count entries for
313 * @vid: VLAN identifier
314 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200315 * Return: the number of originators advertising the given address/data
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100316 * (excluding ourself).
317 */
318int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200319 const u8 *addr, unsigned short vid)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100320{
321 struct batadv_tt_global_entry *tt_global_entry;
322 int count;
323
324 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
325 if (!tt_global_entry)
326 return 0;
327
328 count = atomic_read(&tt_global_entry->orig_list_count);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100329 batadv_tt_global_entry_put(tt_global_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100330
331 return count;
332}
333
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200334/**
335 * batadv_tt_local_size_mod - change the size by v of the local table identified
336 * by vid
337 * @bat_priv: the bat priv with all the soft interface information
338 * @vid: the VLAN identifier of the sub-table to change
339 * @v: the amount to sum to the local table size
340 */
341static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
342 unsigned short vid, int v)
343{
344 struct batadv_softif_vlan *vlan;
345
346 vlan = batadv_softif_vlan_get(bat_priv, vid);
347 if (!vlan)
348 return;
349
350 atomic_add(v, &vlan->tt.num_entries);
351
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100352 batadv_softif_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200353}
354
355/**
356 * batadv_tt_local_size_inc - increase by one the local table size for the given
357 * vid
358 * @bat_priv: the bat priv with all the soft interface information
359 * @vid: the VLAN identifier
360 */
361static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
362 unsigned short vid)
363{
364 batadv_tt_local_size_mod(bat_priv, vid, 1);
365}
366
367/**
368 * batadv_tt_local_size_dec - decrease by one the local table size for the given
369 * vid
370 * @bat_priv: the bat priv with all the soft interface information
371 * @vid: the VLAN identifier
372 */
373static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
374 unsigned short vid)
375{
376 batadv_tt_local_size_mod(bat_priv, vid, -1);
377}
378
379/**
Antonio Quartullid15cd622015-11-17 16:40:52 +0800380 * batadv_tt_global_size_mod - change the size by v of the global table
381 * for orig_node identified by vid
382 * @orig_node: the originator for which the table has to be modified
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200383 * @vid: the VLAN identifier
384 * @v: the amount to sum to the global table size
385 */
386static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
387 unsigned short vid, int v)
388{
389 struct batadv_orig_node_vlan *vlan;
390
391 vlan = batadv_orig_node_vlan_new(orig_node, vid);
392 if (!vlan)
393 return;
394
395 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
396 spin_lock_bh(&orig_node->vlan_list_lock);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100397 if (!hlist_unhashed(&vlan->list)) {
398 hlist_del_init_rcu(&vlan->list);
Sven Eckelmann21754e22016-01-17 11:01:24 +0100399 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100400 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200401 spin_unlock_bh(&orig_node->vlan_list_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200402 }
403
Sven Eckelmann21754e22016-01-17 11:01:24 +0100404 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200405}
406
407/**
408 * batadv_tt_global_size_inc - increase by one the global table size for the
409 * given vid
410 * @orig_node: the originator which global table size has to be decreased
411 * @vid: the vlan identifier
412 */
413static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
414 unsigned short vid)
415{
416 batadv_tt_global_size_mod(orig_node, vid, 1);
417}
418
419/**
420 * batadv_tt_global_size_dec - decrease by one the global table size for the
421 * given vid
422 * @orig_node: the originator which global table size has to be decreased
423 * @vid: the vlan identifier
424 */
425static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
426 unsigned short vid)
427{
428 batadv_tt_global_size_mod(orig_node, vid, -1);
429}
430
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100431/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200432 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
433 * @rcu: rcu pointer of the orig_entry
434 */
435static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
436{
437 struct batadv_tt_orig_list_entry *orig_entry;
438
439 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
440
441 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
442}
443
444/**
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100445 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
446 * queue for free after rcu grace period
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100447 * @ref: kref pointer of the tt orig entry
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100448 */
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100449static void batadv_tt_orig_list_entry_release(struct kref *ref)
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100450{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100451 struct batadv_tt_orig_list_entry *orig_entry;
452
453 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
454 refcount);
455
Sven Eckelmann5d967312016-01-17 11:01:09 +0100456 batadv_orig_node_put(orig_entry->orig_node);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200457 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100458}
459
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100460/**
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100461 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
462 * possibly release it
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100463 * @orig_entry: tt orig entry to be free'd
464 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200465static void
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100466batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200467{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100468 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200469}
470
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200471/**
472 * batadv_tt_local_event - store a local TT event (ADD/DEL)
473 * @bat_priv: the bat priv with all the soft interface information
474 * @tt_local_entry: the TT entry involved in the event
475 * @event_flags: flags to store in the event structure
476 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200477static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200478 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200479 u8 event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200480{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200482 struct batadv_tt_common_entry *common = &tt_local_entry->common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200483 u8 flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200484 bool event_removed = false;
485 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200486
Sven Eckelmann86452f82016-06-25 16:44:06 +0200487 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200488 if (!tt_change_node)
489 return;
490
Antonio Quartulliff66c972011-06-30 01:14:00 +0200491 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100492 memset(tt_change_node->change.reserved, 0,
493 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100494 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200495 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200496
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200497 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200498
499 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200500 spin_lock_bh(&bat_priv->tt.changes_list_lock);
501 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200502 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200503 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200504 continue;
505
506 /* DEL+ADD in the same orig interval have no effect and can be
507 * removed to avoid silly behaviour on the receiver side. The
508 * other way around (ADD+DEL) can happen in case of roaming of
509 * a client still in the NEW state. Roaming of NEW clients is
510 * now possible due to automatically recognition of "temporary"
511 * clients
512 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200513 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200514 if (!del_op_requested && del_op_entry)
515 goto del;
516 if (del_op_requested && !del_op_entry)
517 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200518
519 /* this is a second add in the same originator interval. It
520 * means that flags have been changed: update them!
521 */
522 if (!del_op_requested && !del_op_entry)
523 entry->change.flags = flags;
524
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200525 continue;
526del:
527 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200528 kmem_cache_free(batadv_tt_change_cache, entry);
529 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200530 event_removed = true;
531 goto unlock;
532 }
533
Antonio Quartullia73105b2011-04-27 14:27:44 +0200534 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200535 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200536
537unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200538 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200540 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200541 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200542 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200543 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200544}
545
Marek Lindner335fbe02013-04-23 21:40:02 +0800546/**
547 * batadv_tt_len - compute length in bytes of given number of tt changes
548 * @changes_num: number of tt changes
549 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200550 * Return: computed length in bytes.
Marek Lindner335fbe02013-04-23 21:40:02 +0800551 */
552static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200553{
Marek Lindner335fbe02013-04-23 21:40:02 +0800554 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200555}
556
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200557/**
558 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
559 * @tt_len: available space
560 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200561 * Return: the number of entries.
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200562 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200563static u16 batadv_tt_entries(u16 tt_len)
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200564{
565 return tt_len / batadv_tt_len(1);
566}
567
Marek Lindnera19d3d82013-05-27 15:33:25 +0800568/**
569 * batadv_tt_local_table_transmit_size - calculates the local translation table
570 * size when transmitted over the air
571 * @bat_priv: the bat priv with all the soft interface information
572 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200573 * Return: local translation table size in bytes.
Marek Lindnera19d3d82013-05-27 15:33:25 +0800574 */
575static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
576{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200577 u16 num_vlan = 0;
578 u16 tt_local_entries = 0;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800579 struct batadv_softif_vlan *vlan;
580 int hdr_size;
581
582 rcu_read_lock();
583 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
584 num_vlan++;
585 tt_local_entries += atomic_read(&vlan->tt.num_entries);
586 }
587 rcu_read_unlock();
588
589 /* header size of tvlv encapsulated tt response payload */
590 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
591 hdr_size += sizeof(struct batadv_tvlv_hdr);
592 hdr_size += sizeof(struct batadv_tvlv_tt_data);
593 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
594
595 return hdr_size + batadv_tt_len(tt_local_entries);
596}
597
Sven Eckelmann56303d32012-06-05 22:31:31 +0200598static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200600 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200601 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602
Sven Eckelmann807736f2012-07-15 22:26:51 +0200603 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604
Sven Eckelmann807736f2012-07-15 22:26:51 +0200605 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200606 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607
Antonio Quartullidec05072012-11-10 11:00:32 +0100608 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
609 &batadv_tt_local_hash_lock_class_key);
610
Sven Eckelmann5346c352012-05-05 13:27:28 +0200611 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612}
613
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200614static void batadv_tt_global_free(struct batadv_priv *bat_priv,
615 struct batadv_tt_global_entry *tt_global,
616 const char *message)
617{
618 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200619 "Deleting global tt entry %pM (vid: %d): %s\n",
620 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100621 batadv_print_vid(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200622
623 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200624 batadv_choose_tt, &tt_global->common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100625 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200626}
627
Antonio Quartullic018ad32013-06-04 12:11:39 +0200628/**
629 * batadv_tt_local_add - add a new client to the local table or update an
630 * existing client
631 * @soft_iface: netdev struct of the mesh interface
632 * @addr: the mac address of the client to add
633 * @vid: VLAN identifier
634 * @ifindex: index of the interface where the client is connected to (useful to
635 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100636 * @mark: the value contained in the skb->mark field of the received packet (if
637 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800638 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200639 * Return: true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200640 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200641bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
642 unsigned short vid, int ifindex, u32 mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200644 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200645 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100646 struct batadv_tt_global_entry *tt_global = NULL;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200647 struct net *net = dev_net(soft_iface);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200648 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200649 struct net_device *in_dev = NULL;
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200650 struct batadv_hard_iface *in_hardif = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200651 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800653 int hash_added, table_size, packet_size_max;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200654 bool ret = false;
655 bool roamed_back = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200656 u8 remote_flags;
657 u32 match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200659 if (ifindex != BATADV_NULL_IFINDEX)
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200660 in_dev = dev_get_by_index(net, ifindex);
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200661
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200662 if (in_dev)
663 in_hardif = batadv_hardif_get_by_netdev(in_dev);
664
Antonio Quartullic018ad32013-06-04 12:11:39 +0200665 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100666
667 if (!is_multicast_ether_addr(addr))
668 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669
Antonio Quartulli47c94652012-09-23 22:38:35 +0200670 if (tt_local) {
671 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200672 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
673 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200674 "Re-adding pending client %pM (vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100675 addr, batadv_print_vid(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200676 /* whatever the reason why the PENDING flag was set,
677 * this is a client which was enqueued to be removed in
678 * this orig_interval. Since it popped up again, the
679 * flag can be reset like it was never enqueued
680 */
681 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
682 goto add_event;
683 }
684
685 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
686 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200687 "Roaming client %pM (vid: %d) came back to its original location\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100688 addr, batadv_print_vid(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200689 /* the ROAM flag is set because this client roamed away
690 * and the node got a roaming_advertisement message. Now
691 * that the client popped up again at its original
692 * location such flag can be unset
693 */
694 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
695 roamed_back = true;
696 }
697 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698 }
699
Marek Lindnera19d3d82013-05-27 15:33:25 +0800700 /* Ignore the client if we cannot send it in a full table response. */
701 table_size = batadv_tt_local_table_transmit_size(bat_priv);
702 table_size += batadv_tt_len(1);
703 packet_size_max = atomic_read(&bat_priv->packet_size_max);
704 if (table_size > packet_size_max) {
705 net_ratelimited_function(batadv_info, soft_iface,
706 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
707 table_size, packet_size_max, addr);
708 goto out;
709 }
710
Sven Eckelmann86452f82016-06-25 16:44:06 +0200711 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200712 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200713 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200714
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200715 /* increase the refcounter of the related vlan */
716 vlan = batadv_softif_vlan_get(bat_priv, vid);
Simon Wunderlich0b3dd7d2016-06-26 11:16:09 +0200717 if (!vlan) {
718 net_ratelimited_function(batadv_info, soft_iface,
719 "adding TT local entry %pM to non-existent VLAN %d\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100720 addr, batadv_print_vid(vid));
Sven Eckelmann86452f82016-06-25 16:44:06 +0200721 kmem_cache_free(batadv_tl_cache, tt_local);
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200722 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800723 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200724 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200725
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200726 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200727 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100728 addr, batadv_print_vid(vid),
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200729 (u8)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100731 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100732 /* The local entry has to be marked as NEW to avoid to send it in
733 * a full table response going out before the next ttvn increment
734 * (consistency check)
735 */
736 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200737 tt_local->common.vid = vid;
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200738 if (batadv_is_wifi_hardif(in_hardif))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200739 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100740 kref_init(&tt_local->common.refcount);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200741 tt_local->last_seen = jiffies;
742 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100743 tt_local->vlan = vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100745 /* the batman interface mac and multicast addresses should never be
746 * purged
747 */
748 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
749 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200750 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751
Sven Eckelmanne3387b22016-07-15 17:39:22 +0200752 kref_get(&tt_local->common.refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200753 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200754 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200755 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100756
757 if (unlikely(hash_added != 0)) {
758 /* remove the reference for the hash */
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100759 batadv_tt_local_entry_put(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100760 goto out;
761 }
762
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200763add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200764 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200765
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200766check_roaming:
767 /* Check whether it is a roaming, but don't do anything if the roaming
768 * process has already been handled
769 */
770 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200771 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200772 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200773 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800774 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200775 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200776 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200777 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200778 }
779 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200780 if (roamed_back) {
781 batadv_tt_global_free(bat_priv, tt_global,
782 "Roaming canceled");
783 tt_global = NULL;
784 } else {
785 /* The global entry has to be marked as ROAMING and
786 * has to be kept for consistency purpose
787 */
788 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
789 tt_global->roam_at = jiffies;
790 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200791 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200792
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200793 /* store the current remote flags before altering them. This helps
794 * understanding is flags are changing or not
795 */
796 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800797
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200798 if (batadv_is_wifi_hardif(in_hardif))
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200799 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
800 else
801 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
802
Antonio Quartulli9464d072013-11-16 12:03:48 +0100803 /* check the mark in the skb: if it's equal to the configured
804 * isolation_mark, it means the packet is coming from an isolated
805 * non-mesh client
806 */
807 match_mark = (mark & bat_priv->isolation_mark_mask);
808 if (bat_priv->isolation_mark_mask &&
809 match_mark == bat_priv->isolation_mark)
810 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
811 else
812 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
813
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200814 /* if any "dynamic" flag has been modified, resend an ADD event for this
815 * entry so that all the nodes can get the new flags
816 */
817 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
818 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
819
820 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200821out:
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +0200822 if (in_hardif)
823 batadv_hardif_put(in_hardif);
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200824 if (in_dev)
825 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200826 if (tt_local)
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100827 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200828 if (tt_global)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100829 batadv_tt_global_entry_put(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800830 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831}
832
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800833/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200834 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
835 * within a TT Response directed to another node
836 * @orig_node: originator for which the TT data has to be prepared
837 * @tt_data: uninitialised pointer to the address of the TVLV buffer
838 * @tt_change: uninitialised pointer to the address of the area where the TT
839 * changed can be stored
840 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
841 * function reserves the amount of space needed to send the entire global TT
842 * table. In case of success the value is updated with the real amount of
843 * reserved bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200844 * Allocate the needed amount of memory for the entire TT TVLV and write its
845 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
846 * objects, one per active VLAN served by the originator node.
847 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200848 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200849 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200850static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200851batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
852 struct batadv_tvlv_tt_data **tt_data,
853 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200854 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200855{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200856 u16 num_vlan = 0;
857 u16 num_entries = 0;
858 u16 change_offset;
859 u16 tvlv_len;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200860 struct batadv_tvlv_tt_vlan_data *tt_vlan;
861 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200862 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200863
864 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800865 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200866 num_vlan++;
867 num_entries += atomic_read(&vlan->tt.num_entries);
868 }
869
870 change_offset = sizeof(**tt_data);
871 change_offset += num_vlan * sizeof(*tt_vlan);
872
873 /* if tt_len is negative, allocate the space needed by the full table */
874 if (*tt_len < 0)
875 *tt_len = batadv_tt_len(num_entries);
876
877 tvlv_len = *tt_len;
878 tvlv_len += change_offset;
879
880 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
881 if (!*tt_data) {
882 *tt_len = 0;
883 goto out;
884 }
885
886 (*tt_data)->flags = BATADV_NO_FLAGS;
887 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
888 (*tt_data)->num_vlan = htons(num_vlan);
889
890 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800891 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200892 tt_vlan->vid = htons(vlan->vid);
893 tt_vlan->crc = htonl(vlan->tt.crc);
894
895 tt_vlan++;
896 }
897
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200898 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200899 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
900
901out:
902 rcu_read_unlock();
903 return tvlv_len;
904}
905
906/**
907 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
908 * node
909 * @bat_priv: the bat priv with all the soft interface information
910 * @tt_data: uninitialised pointer to the address of the TVLV buffer
911 * @tt_change: uninitialised pointer to the address of the area where the TT
912 * changes can be stored
913 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
914 * function reserves the amount of space needed to send the entire local TT
915 * table. In case of success the value is updated with the real amount of
916 * reserved bytes
917 *
918 * Allocate the needed amount of memory for the entire TT TVLV and write its
919 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
920 * objects, one per active VLAN.
921 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200922 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200923 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200924static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200925batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
926 struct batadv_tvlv_tt_data **tt_data,
927 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200928 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200929{
930 struct batadv_tvlv_tt_vlan_data *tt_vlan;
931 struct batadv_softif_vlan *vlan;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200932 u16 num_vlan = 0;
933 u16 num_entries = 0;
934 u16 tvlv_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200935 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200936 int change_offset;
937
938 rcu_read_lock();
939 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
940 num_vlan++;
941 num_entries += atomic_read(&vlan->tt.num_entries);
942 }
943
944 change_offset = sizeof(**tt_data);
945 change_offset += num_vlan * sizeof(*tt_vlan);
946
947 /* if tt_len is negative, allocate the space needed by the full table */
948 if (*tt_len < 0)
949 *tt_len = batadv_tt_len(num_entries);
950
951 tvlv_len = *tt_len;
952 tvlv_len += change_offset;
953
954 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
955 if (!*tt_data) {
956 tvlv_len = 0;
957 goto out;
958 }
959
960 (*tt_data)->flags = BATADV_NO_FLAGS;
961 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
962 (*tt_data)->num_vlan = htons(num_vlan);
963
964 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
965 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
966 tt_vlan->vid = htons(vlan->vid);
967 tt_vlan->crc = htonl(vlan->tt.crc);
968
969 tt_vlan++;
970 }
971
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200972 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200973 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
974
975out:
976 rcu_read_unlock();
977 return tvlv_len;
978}
979
980/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800981 * batadv_tt_tvlv_container_update - update the translation table tvlv container
982 * after local tt changes have been committed
983 * @bat_priv: the bat priv with all the soft interface information
984 */
985static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000986{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800987 struct batadv_tt_change_node *entry, *safe;
988 struct batadv_tvlv_tt_data *tt_data;
989 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200990 int tt_diff_len, tt_change_len = 0;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200991 int tt_diff_entries_num = 0;
992 int tt_diff_entries_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200993 u16 tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200995 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
996 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800997
998 /* if we have too many changes for one packet don't send any
999 * and wait for the tt table request which will be fragmented
1000 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001001 if (tt_diff_len > bat_priv->soft_iface->mtu)
1002 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001003
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001004 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1005 &tt_change, &tt_diff_len);
1006 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001007 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001008
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001009 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001010
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001011 if (tt_diff_len == 0)
1012 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001013
Sven Eckelmann807736f2012-07-15 22:26:51 +02001014 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1015 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016
Sven Eckelmann807736f2012-07-15 22:26:51 +02001017 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001018 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001019 if (tt_diff_entries_count < tt_diff_entries_num) {
1020 memcpy(tt_change + tt_diff_entries_count,
1021 &entry->change,
1022 sizeof(struct batadv_tvlv_tt_change));
1023 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001025 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001026 kmem_cache_free(batadv_tt_change_cache, entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001028 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
Antonio Quartullia73105b2011-04-27 14:27:44 +02001030 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001031 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1032 kfree(bat_priv->tt.last_changeset);
1033 bat_priv->tt.last_changeset_len = 0;
1034 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001035 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001036 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001037 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001038 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +02001039 * instead of providing the diff
1040 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001041 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001042 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001043 memcpy(bat_priv->tt.last_changeset,
1044 tt_change, tt_change_len);
1045 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001046 }
1047 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001048 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001050container_register:
1051 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001052 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001053 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054}
1055
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001056#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001057int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058{
1059 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001060 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001061 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001063 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001064 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001065 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001066 u32 i;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001067 int last_seen_secs;
1068 int last_seen_msecs;
1069 unsigned long last_seen_jiffies;
1070 bool no_purge;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001071 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072
Marek Lindner30da63a2012-08-03 17:15:46 +02001073 primary_if = batadv_seq_print_text_primary_if_get(seq);
1074 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001075 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001077 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001078 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001079 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001080 seq_puts(seq,
1081 " Client VID Flags Last seen (CRC )\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083 for (i = 0; i < hash->size; i++) {
1084 head = &hash->table[i];
1085
Marek Lindner7aadf882011-02-18 12:28:09 +00001086 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001088 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +01001089 tt_local = container_of(tt_common_entry,
1090 struct batadv_tt_local_entry,
1091 common);
1092 last_seen_jiffies = jiffies - tt_local->last_seen;
1093 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1094 last_seen_secs = last_seen_msecs / 1000;
1095 last_seen_msecs = last_seen_msecs % 1000;
1096
1097 no_purge = tt_common_entry->flags & np_flag;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001098 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001099 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001100 tt_common_entry->addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001101 batadv_print_vid(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001102 ((tt_common_entry->flags &
1103 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +01001104 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001105 ((tt_common_entry->flags &
1106 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1107 ((tt_common_entry->flags &
1108 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1109 ((tt_common_entry->flags &
1110 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1111 ((tt_common_entry->flags &
1112 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +01001113 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001114 no_purge ? 0 : last_seen_msecs,
Sven Eckelmanna33d9702016-03-11 16:44:05 +01001115 tt_local->vlan->tt.crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001117 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001119out:
1120 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001121 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001122 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001124#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125
Matthias Schifferd34f0552016-07-03 13:31:37 +02001126/**
1127 * batadv_tt_local_dump_entry - Dump one TT local entry into a message
1128 * @msg :Netlink message to dump into
1129 * @portid: Port making netlink request
1130 * @seq: Sequence number of netlink message
1131 * @bat_priv: The bat priv with all the soft interface information
1132 * @common: tt local & tt global common data
1133 *
1134 * Return: Error code, or 0 on success
1135 */
1136static int
1137batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1138 struct batadv_priv *bat_priv,
1139 struct batadv_tt_common_entry *common)
1140{
1141 void *hdr;
1142 struct batadv_softif_vlan *vlan;
1143 struct batadv_tt_local_entry *local;
1144 unsigned int last_seen_msecs;
1145 u32 crc;
1146
1147 local = container_of(common, struct batadv_tt_local_entry, common);
1148 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1149
1150 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1151 if (!vlan)
1152 return 0;
1153
1154 crc = vlan->tt.crc;
1155
1156 batadv_softif_vlan_put(vlan);
1157
1158 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1159 NLM_F_MULTI,
1160 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1161 if (!hdr)
1162 return -ENOBUFS;
1163
1164 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1165 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1166 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1167 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1168 goto nla_put_failure;
1169
1170 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1171 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1172 goto nla_put_failure;
1173
1174 genlmsg_end(msg, hdr);
1175 return 0;
1176
1177 nla_put_failure:
1178 genlmsg_cancel(msg, hdr);
1179 return -EMSGSIZE;
1180}
1181
1182/**
1183 * batadv_tt_local_dump_bucket - Dump one TT local bucket into a message
1184 * @msg: Netlink message to dump into
1185 * @portid: Port making netlink request
1186 * @seq: Sequence number of netlink message
1187 * @bat_priv: The bat priv with all the soft interface information
1188 * @head: Pointer to the list containing the local tt entries
1189 * @idx_s: Number of entries to skip
1190 *
1191 * Return: Error code, or 0 on success
1192 */
1193static int
1194batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1195 struct batadv_priv *bat_priv,
1196 struct hlist_head *head, int *idx_s)
1197{
1198 struct batadv_tt_common_entry *common;
1199 int idx = 0;
1200
1201 rcu_read_lock();
1202 hlist_for_each_entry_rcu(common, head, hash_entry) {
1203 if (idx++ < *idx_s)
1204 continue;
1205
1206 if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
1207 common)) {
1208 rcu_read_unlock();
1209 *idx_s = idx - 1;
1210 return -EMSGSIZE;
1211 }
1212 }
1213 rcu_read_unlock();
1214
1215 *idx_s = 0;
1216 return 0;
1217}
1218
1219/**
1220 * batadv_tt_local_dump - Dump TT local entries into a message
1221 * @msg: Netlink message to dump into
1222 * @cb: Parameters from query
1223 *
1224 * Return: Error code, or 0 on success
1225 */
1226int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1227{
1228 struct net *net = sock_net(cb->skb->sk);
1229 struct net_device *soft_iface;
1230 struct batadv_priv *bat_priv;
1231 struct batadv_hard_iface *primary_if = NULL;
1232 struct batadv_hashtable *hash;
1233 struct hlist_head *head;
1234 int ret;
1235 int ifindex;
1236 int bucket = cb->args[0];
1237 int idx = cb->args[1];
1238 int portid = NETLINK_CB(cb->skb).portid;
1239
1240 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1241 if (!ifindex)
1242 return -EINVAL;
1243
1244 soft_iface = dev_get_by_index(net, ifindex);
1245 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1246 ret = -ENODEV;
1247 goto out;
1248 }
1249
1250 bat_priv = netdev_priv(soft_iface);
1251
1252 primary_if = batadv_primary_if_get_selected(bat_priv);
1253 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1254 ret = -ENOENT;
1255 goto out;
1256 }
1257
1258 hash = bat_priv->tt.local_hash;
1259
1260 while (bucket < hash->size) {
1261 head = &hash->table[bucket];
1262
1263 if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
1264 bat_priv, head, &idx))
1265 break;
1266
1267 bucket++;
1268 }
1269
1270 ret = msg->len;
1271
1272 out:
1273 if (primary_if)
1274 batadv_hardif_put(primary_if);
1275 if (soft_iface)
1276 dev_put(soft_iface);
1277
1278 cb->args[0] = bucket;
1279 cb->args[1] = idx;
1280
1281 return ret;
1282}
1283
Sven Eckelmann56303d32012-06-05 22:31:31 +02001284static void
1285batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1286 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001287 u16 flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001288{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001289 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001290
Antonio Quartulli015758d2011-07-09 17:52:13 +02001291 /* The local client has to be marked as "pending to be removed" but has
1292 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001293 * response issued before the net ttvn increment (consistency check)
1294 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001295 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001296
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001297 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001298 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1299 tt_local_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001300 batadv_print_vid(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001301}
1302
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001303/**
1304 * batadv_tt_local_remove - logically remove an entry from the local table
1305 * @bat_priv: the bat priv with all the soft interface information
1306 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001307 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001308 * @message: message to append to the log on deletion
1309 * @roaming: true if the deletion is due to a roaming event
1310 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001311 * Return: the flags assigned to the local entry before being deleted
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001312 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001313u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1314 unsigned short vid, const char *message,
1315 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001316{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001317 struct batadv_tt_local_entry *tt_local_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001318 u16 flags, curr_flags = BATADV_NO_FLAGS;
Marek Lindneref727062015-06-17 20:01:36 +08001319 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001320
Antonio Quartullic018ad32013-06-04 12:11:39 +02001321 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001322 if (!tt_local_entry)
1323 goto out;
1324
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001325 curr_flags = tt_local_entry->common.flags;
1326
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001327 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001328 /* if this global entry addition is due to a roaming, the node has to
1329 * mark the local entry as "roamed" in order to correctly reroute
1330 * packets later
1331 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001332 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001333 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001334 /* mark the local client as ROAMed */
1335 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1336 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001337
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001338 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1339 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1340 message);
1341 goto out;
1342 }
1343 /* if this client has been added right now, it is possible to
1344 * immediately purge it
1345 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001346 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001347
1348 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1349 batadv_compare_tt,
1350 batadv_choose_tt,
1351 &tt_local_entry->common);
1352 if (!tt_entry_exists)
1353 goto out;
1354
1355 /* extra call to free the local tt entry */
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001356 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001357
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001358out:
1359 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001360 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001361
1362 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001363}
1364
Marek Lindnera19d3d82013-05-27 15:33:25 +08001365/**
1366 * batadv_tt_local_purge_list - purge inactive tt local entries
1367 * @bat_priv: the bat priv with all the soft interface information
1368 * @head: pointer to the list containing the local tt entries
1369 * @timeout: parameter deciding whether a given tt local entry is considered
1370 * inactive or not
1371 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001372static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001373 struct hlist_head *head,
1374 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001375{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001376 struct batadv_tt_local_entry *tt_local_entry;
1377 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001379
Sasha Levinb67bfe02013-02-27 17:06:00 -08001380 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001381 hash_entry) {
1382 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001383 struct batadv_tt_local_entry,
1384 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001385 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1386 continue;
1387
1388 /* entry already marked for deletion */
1389 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1390 continue;
1391
Marek Lindnera19d3d82013-05-27 15:33:25 +08001392 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001393 continue;
1394
1395 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1396 BATADV_TT_CLIENT_DEL, "timed out");
1397 }
1398}
1399
Marek Lindnera19d3d82013-05-27 15:33:25 +08001400/**
1401 * batadv_tt_local_purge - purge inactive tt local entries
1402 * @bat_priv: the bat priv with all the soft interface information
1403 * @timeout: parameter deciding whether a given tt local entry is considered
1404 * inactive or not
1405 */
1406static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1407 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001408{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001409 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001410 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001411 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001412 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001413
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001414 for (i = 0; i < hash->size; i++) {
1415 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001416 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001417
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001418 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001419 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001420 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001421 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001422}
1423
Sven Eckelmann56303d32012-06-05 22:31:31 +02001424static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001425{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001426 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001427 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001428 struct batadv_tt_common_entry *tt_common_entry;
1429 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001430 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001431 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001432 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433
Sven Eckelmann807736f2012-07-15 22:26:51 +02001434 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001435 return;
1436
Sven Eckelmann807736f2012-07-15 22:26:51 +02001437 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001438
1439 for (i = 0; i < hash->size; i++) {
1440 head = &hash->table[i];
1441 list_lock = &hash->list_locks[i];
1442
1443 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001444 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001446 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001447 tt_local = container_of(tt_common_entry,
1448 struct batadv_tt_local_entry,
1449 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001450
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001451 batadv_tt_local_entry_put(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 }
1453 spin_unlock_bh(list_lock);
1454 }
1455
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001456 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457
Sven Eckelmann807736f2012-07-15 22:26:51 +02001458 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001459}
1460
Sven Eckelmann56303d32012-06-05 22:31:31 +02001461static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001462{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001463 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001464 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001465
Sven Eckelmann807736f2012-07-15 22:26:51 +02001466 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001467
Sven Eckelmann807736f2012-07-15 22:26:51 +02001468 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001469 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001470
Antonio Quartullidec05072012-11-10 11:00:32 +01001471 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1472 &batadv_tt_global_hash_lock_class_key);
1473
Sven Eckelmann5346c352012-05-05 13:27:28 +02001474 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001475}
1476
Sven Eckelmann56303d32012-06-05 22:31:31 +02001477static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001479 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480
Sven Eckelmann807736f2012-07-15 22:26:51 +02001481 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484 list) {
1485 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001486 kmem_cache_free(batadv_tt_change_cache, entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487 }
1488
Sven Eckelmann807736f2012-07-15 22:26:51 +02001489 atomic_set(&bat_priv->tt.local_changes, 0);
1490 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491}
1492
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001493/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001494 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1495 * @entry: the TT global entry where the orig_list_entry has to be
1496 * extracted from
1497 * @orig_node: the originator for which the orig_list_entry has to be found
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001498 *
Antonio Quartullid15cd622015-11-17 16:40:52 +08001499 * retrieve the orig_tt_list_entry belonging to orig_node from the
Antonio Quartullid657e622012-07-01 14:09:12 +02001500 * batadv_tt_global_entry list
1501 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001502 * Return: it with an increased refcounter, NULL if not found
Antonio Quartullid657e622012-07-01 14:09:12 +02001503 */
1504static struct batadv_tt_orig_list_entry *
1505batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1506 const struct batadv_orig_node *orig_node)
1507{
1508 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1509 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001510
1511 rcu_read_lock();
1512 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001513 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001514 if (tmp_orig_entry->orig_node != orig_node)
1515 continue;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001516 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
Antonio Quartullid657e622012-07-01 14:09:12 +02001517 continue;
1518
1519 orig_entry = tmp_orig_entry;
1520 break;
1521 }
1522 rcu_read_unlock();
1523
1524 return orig_entry;
1525}
1526
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001527/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001528 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1529 * by a given originator
1530 * @entry: the TT global entry to check
1531 * @orig_node: the originator to search in the list
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001532 *
1533 * find out if an orig_node is already in the list of a tt_global_entry.
1534 *
1535 * Return: true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001536 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001537static bool
1538batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1539 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001540{
Antonio Quartullid657e622012-07-01 14:09:12 +02001541 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001542 bool found = false;
1543
Antonio Quartullid657e622012-07-01 14:09:12 +02001544 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1545 if (orig_entry) {
1546 found = true;
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001547 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001548 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001549
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001550 return found;
1551}
1552
Linus Lüssing54e22f22017-07-06 07:02:25 +02001553/**
1554 * batadv_tt_global_sync_flags - update TT sync flags
1555 * @tt_global: the TT global entry to update sync flags in
1556 *
1557 * Updates the sync flag bits in the tt_global flag attribute with a logical
1558 * OR of all sync flags from any of its TT orig entries.
1559 */
1560static void
1561batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1562{
1563 struct batadv_tt_orig_list_entry *orig_entry;
1564 const struct hlist_head *head;
1565 u16 flags = BATADV_NO_FLAGS;
1566
1567 rcu_read_lock();
1568 head = &tt_global->orig_list;
1569 hlist_for_each_entry_rcu(orig_entry, head, list)
1570 flags |= orig_entry->flags;
1571 rcu_read_unlock();
1572
1573 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1574 tt_global->common.flags = flags;
1575}
1576
1577/**
1578 * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1579 * @tt_global: the TT global entry to add an orig entry in
1580 * @orig_node: the originator to add an orig entry for
1581 * @ttvn: translation table version number of this changeset
1582 * @flags: TT sync flags
1583 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001584static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001585batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Linus Lüssing54e22f22017-07-06 07:02:25 +02001586 struct batadv_orig_node *orig_node, int ttvn,
1587 u8 flags)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001588{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001589 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001590
Antonio Quartullid657e622012-07-01 14:09:12 +02001591 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001592 if (orig_entry) {
1593 /* refresh the ttvn: the current value could be a bogus one that
1594 * was added during a "temporary client detection"
1595 */
1596 orig_entry->ttvn = ttvn;
Linus Lüssing54e22f22017-07-06 07:02:25 +02001597 orig_entry->flags = flags;
1598 goto sync_flags;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001599 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001600
Sven Eckelmann86452f82016-06-25 16:44:06 +02001601 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001602 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001603 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001604
1605 INIT_HLIST_NODE(&orig_entry->list);
Sven Eckelmann7c124392016-01-16 10:29:56 +01001606 kref_get(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001607 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001608 orig_entry->orig_node = orig_node;
1609 orig_entry->ttvn = ttvn;
Linus Lüssing54e22f22017-07-06 07:02:25 +02001610 orig_entry->flags = flags;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001611 kref_init(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001612
Antonio Quartullid657e622012-07-01 14:09:12 +02001613 spin_lock_bh(&tt_global->list_lock);
Sven Eckelmann23f55482016-07-15 17:39:18 +02001614 kref_get(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001615 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001616 &tt_global->orig_list);
1617 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001618 atomic_inc(&tt_global->orig_list_count);
1619
Linus Lüssing54e22f22017-07-06 07:02:25 +02001620sync_flags:
1621 batadv_tt_global_sync_flags(tt_global);
Antonio Quartullid657e622012-07-01 14:09:12 +02001622out:
1623 if (orig_entry)
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001624 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001625}
1626
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001627/**
1628 * batadv_tt_global_add - add a new TT global entry or update an existing one
1629 * @bat_priv: the bat priv with all the soft interface information
1630 * @orig_node: the originator announcing the client
1631 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001632 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001633 * @flags: TT flags that have to be set for this non-mesh client
1634 * @ttvn: the tt version number ever announcing this non-mesh client
1635 *
1636 * Add a new TT global entry for the given originator. If the entry already
1637 * exists add a new reference to the given originator (a global entry can have
1638 * references to multiple originators) and adjust the flags attribute to reflect
1639 * the function argument.
1640 * If a TT local entry exists for this non-mesh client remove it.
1641 *
1642 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001643 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001644 * Return: true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001645 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001646static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1647 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001648 const unsigned char *tt_addr,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001649 unsigned short vid, u16 flags, u8 ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001650{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001651 struct batadv_tt_global_entry *tt_global_entry;
1652 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001653 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001654 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001655 struct batadv_tt_common_entry *common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001656 u16 local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001657
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001658 /* ignore global entries from backbone nodes */
1659 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1660 return true;
1661
Antonio Quartullic018ad32013-06-04 12:11:39 +02001662 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1663 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001664
1665 /* if the node already has a local client for this entry, it has to wait
1666 * for a roaming advertisement instead of manually messing up the global
1667 * table
1668 */
1669 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1670 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1671 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001672
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 if (!tt_global_entry) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02001674 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1675 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001677 goto out;
1678
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001679 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001680 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001681 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001682
Antonio Quartullid4f44692012-05-25 00:00:54 +02001683 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001684 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001685 /* node must store current time in case of roaming. This is
1686 * needed to purge this entry out on timeout (if nobody claims
1687 * it)
1688 */
1689 if (flags & BATADV_TT_CLIENT_ROAM)
1690 tt_global_entry->roam_at = jiffies;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +01001691 kref_init(&common->refcount);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001692 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001693
1694 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001695 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001696 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001697
Sven Eckelmann15d5ffd2016-07-15 17:39:23 +02001698 kref_get(&common->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001699 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001700 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001701 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001702 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001703
1704 if (unlikely(hash_added != 0)) {
1705 /* remove the reference for the hash */
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001706 batadv_tt_global_entry_put(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001707 goto out_remove;
1708 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001709 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001710 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001711 /* If there is already a global entry, we can use this one for
1712 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001713 * But if we are trying to add a temporary client then here are
1714 * two options at this point:
1715 * 1) the global client is not a temporary client: the global
1716 * client has to be left as it is, temporary information
1717 * should never override any already known client state
1718 * 2) the global client is a temporary client: purge the
1719 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001720 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001721 if (flags & BATADV_TT_CLIENT_TEMP) {
1722 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1723 goto out;
1724 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1725 orig_node))
1726 goto out_remove;
1727 batadv_tt_global_del_orig_list(tt_global_entry);
1728 goto add_orig_entry;
1729 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001730
1731 /* if the client was temporary added before receiving the first
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001732 * OGM announcing it, we have to clear the TEMP flag. Also,
1733 * remove the previous temporary orig node and re-add it
1734 * if required. If the orig entry changed, the new one which
1735 * is a non-temporary entry is preferred.
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001736 */
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001737 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1738 batadv_tt_global_del_orig_list(tt_global_entry);
1739 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1740 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001741
Antonio Quartullie9c001362012-11-07 15:05:33 +01001742 /* the change can carry possible "attribute" flags like the
Linus Lüssing54e22f22017-07-06 07:02:25 +02001743 * TT_CLIENT_TEMP, therefore they have to be copied in the
Antonio Quartullie9c001362012-11-07 15:05:33 +01001744 * client entry
1745 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02001746 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
Antonio Quartullie9c001362012-11-07 15:05:33 +01001747
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001748 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1749 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001750 * delete + roaming change for this originator.
1751 *
1752 * We should first delete the old originator before adding the
1753 * new one.
1754 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001755 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001756 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001757 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001758 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001759 }
1760 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001761add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001762 /* add the new orig_entry (if needed) or update it */
Linus Lüssing54e22f22017-07-06 07:02:25 +02001763 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1764 flags & BATADV_TT_SYNC_MASK);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001766 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001767 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001768 common->addr, batadv_print_vid(common->vid),
Antonio Quartulli16052782013-06-04 12:11:41 +02001769 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001770 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001772out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001773 /* Do not remove multicast addresses from the local hash on
1774 * global additions
1775 */
1776 if (is_multicast_ether_addr(tt_addr))
1777 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001778
Antonio Quartullia73105b2011-04-27 14:27:44 +02001779 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001780 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001781 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001782 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001783 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1784
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001785 if (!(flags & BATADV_TT_CLIENT_ROAM))
1786 /* this is a normal global add. Therefore the client is not in a
1787 * roaming state anymore.
1788 */
1789 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1790
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001791out:
1792 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001793 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001794 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001795 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001796 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001797}
1798
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001799/**
1800 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001801 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001802 * @tt_global_entry: global translation table entry to be analyzed
1803 *
1804 * This functon assumes the caller holds rcu_read_lock().
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001805 * Return: best originator list entry or NULL on errors.
Sven Eckelmann981d8902012-10-07 13:34:15 +02001806 */
1807static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001808batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1809 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001810{
Antonio Quartulli46274562013-09-03 11:10:24 +02001811 struct batadv_neigh_node *router, *best_router = NULL;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001812 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001813 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001814 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001815
1816 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001817 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001818 router = batadv_orig_router_get(orig_entry->orig_node,
1819 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001820 if (!router)
1821 continue;
1822
Antonio Quartulli46274562013-09-03 11:10:24 +02001823 if (best_router &&
Antonio Quartulli29824a52016-05-25 23:27:31 +08001824 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1825 BATADV_IF_DEFAULT) <= 0) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001826 batadv_neigh_node_put(router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001827 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001828 }
1829
Antonio Quartulli46274562013-09-03 11:10:24 +02001830 /* release the refcount for the "old" best */
1831 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001832 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001833
1834 best_entry = orig_entry;
1835 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001836 }
1837
Antonio Quartulli46274562013-09-03 11:10:24 +02001838 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001839 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001840
Sven Eckelmann981d8902012-10-07 13:34:15 +02001841 return best_entry;
1842}
1843
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001844#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001845/**
1846 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1847 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001848 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001849 * @tt_global_entry: global translation table entry to be printed
1850 * @seq: debugfs table seq_file struct
1851 *
1852 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001853 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001854static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001855batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1856 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001857 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001858{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001859 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001860 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001861 struct batadv_orig_node_vlan *vlan;
1862 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001863 u8 last_ttvn;
1864 u16 flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001865
1866 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001867 flags = tt_common_entry->flags;
1868
Antonio Quartulli46274562013-09-03 11:10:24 +02001869 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001870 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001871 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1872 tt_common_entry->vid);
1873 if (!vlan) {
1874 seq_printf(seq,
1875 " * Cannot retrieve VLAN %d for originator %pM\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001876 batadv_print_vid(tt_common_entry->vid),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001877 best_entry->orig_node->orig);
1878 goto print_list;
1879 }
1880
Sven Eckelmann981d8902012-10-07 13:34:15 +02001881 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001882 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001883 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001884 '*', tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001885 batadv_print_vid(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001886 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001887 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001888 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1889 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1890 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1891 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001892
Sven Eckelmann21754e22016-01-17 11:01:24 +01001893 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001894 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001895
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001896print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001897 head = &tt_global_entry->orig_list;
1898
Sasha Levinb67bfe02013-02-27 17:06:00 -08001899 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001900 if (best_entry == orig_entry)
1901 continue;
1902
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001903 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1904 tt_common_entry->vid);
1905 if (!vlan) {
1906 seq_printf(seq,
1907 " + Cannot retrieve VLAN %d for originator %pM\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001908 batadv_print_vid(tt_common_entry->vid),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001909 orig_entry->orig_node->orig);
1910 continue;
1911 }
1912
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001913 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001914 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001915 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001916 '+', tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01001917 batadv_print_vid(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001918 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001919 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001920 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1921 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1922 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1923 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001924
Sven Eckelmann21754e22016-01-17 11:01:24 +01001925 batadv_orig_node_vlan_put(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001926 }
1927}
1928
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001929int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001930{
1931 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001932 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001933 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001934 struct batadv_tt_common_entry *tt_common_entry;
1935 struct batadv_tt_global_entry *tt_global;
1936 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001937 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001938 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001939
Marek Lindner30da63a2012-08-03 17:15:46 +02001940 primary_if = batadv_seq_print_text_primary_if_get(seq);
1941 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001942 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001943
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001944 seq_printf(seq,
1945 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001946 net_dev->name);
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001947 seq_puts(seq,
1948 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001949
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001950 for (i = 0; i < hash->size; i++) {
1951 head = &hash->table[i];
1952
Marek Lindner7aadf882011-02-18 12:28:09 +00001953 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001954 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001955 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001956 tt_global = container_of(tt_common_entry,
1957 struct batadv_tt_global_entry,
1958 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001959 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001960 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001961 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001962 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001963out:
1964 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001965 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001966 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001967}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001968#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001969
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001970/**
Matthias Schifferd34f0552016-07-03 13:31:37 +02001971 * batadv_tt_global_dump_subentry - Dump all TT local entries into a message
1972 * @msg: Netlink message to dump into
1973 * @portid: Port making netlink request
1974 * @seq: Sequence number of netlink message
1975 * @common: tt local & tt global common data
1976 * @orig: Originator node announcing a non-mesh client
1977 * @best: Is the best originator for the TT entry
1978 *
1979 * Return: Error code, or 0 on success
1980 */
1981static int
1982batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1983 struct batadv_tt_common_entry *common,
1984 struct batadv_tt_orig_list_entry *orig,
1985 bool best)
1986{
Linus Lüssing54e22f22017-07-06 07:02:25 +02001987 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
Matthias Schifferd34f0552016-07-03 13:31:37 +02001988 void *hdr;
1989 struct batadv_orig_node_vlan *vlan;
1990 u8 last_ttvn;
1991 u32 crc;
1992
1993 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1994 common->vid);
1995 if (!vlan)
1996 return 0;
1997
1998 crc = vlan->tt.crc;
1999
2000 batadv_orig_node_vlan_put(vlan);
2001
2002 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2003 NLM_F_MULTI,
2004 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2005 if (!hdr)
2006 return -ENOBUFS;
2007
2008 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2009
2010 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2011 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2012 orig->orig_node->orig) ||
2013 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2014 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2015 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2016 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
Linus Lüssing54e22f22017-07-06 07:02:25 +02002017 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
Matthias Schifferd34f0552016-07-03 13:31:37 +02002018 goto nla_put_failure;
2019
2020 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2021 goto nla_put_failure;
2022
2023 genlmsg_end(msg, hdr);
2024 return 0;
2025
2026 nla_put_failure:
2027 genlmsg_cancel(msg, hdr);
2028 return -EMSGSIZE;
2029}
2030
2031/**
2032 * batadv_tt_global_dump_entry - Dump one TT global entry into a message
2033 * @msg: Netlink message to dump into
2034 * @portid: Port making netlink request
2035 * @seq: Sequence number of netlink message
2036 * @bat_priv: The bat priv with all the soft interface information
2037 * @common: tt local & tt global common data
2038 * @sub_s: Number of entries to skip
2039 *
2040 * This function assumes the caller holds rcu_read_lock().
2041 *
2042 * Return: Error code, or 0 on success
2043 */
2044static int
2045batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2046 struct batadv_priv *bat_priv,
2047 struct batadv_tt_common_entry *common, int *sub_s)
2048{
2049 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2050 struct batadv_tt_global_entry *global;
2051 struct hlist_head *head;
2052 int sub = 0;
2053 bool best;
2054
2055 global = container_of(common, struct batadv_tt_global_entry, common);
2056 best_entry = batadv_transtable_best_orig(bat_priv, global);
2057 head = &global->orig_list;
2058
2059 hlist_for_each_entry_rcu(orig_entry, head, list) {
2060 if (sub++ < *sub_s)
2061 continue;
2062
2063 best = (orig_entry == best_entry);
2064
2065 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2066 orig_entry, best)) {
2067 *sub_s = sub - 1;
2068 return -EMSGSIZE;
2069 }
2070 }
2071
2072 *sub_s = 0;
2073 return 0;
2074}
2075
2076/**
2077 * batadv_tt_global_dump_bucket - Dump one TT local bucket into a message
2078 * @msg: Netlink message to dump into
2079 * @portid: Port making netlink request
2080 * @seq: Sequence number of netlink message
2081 * @bat_priv: The bat priv with all the soft interface information
2082 * @head: Pointer to the list containing the global tt entries
2083 * @idx_s: Number of entries to skip
2084 * @sub: Number of entries to skip
2085 *
2086 * Return: Error code, or 0 on success
2087 */
2088static int
2089batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2090 struct batadv_priv *bat_priv,
2091 struct hlist_head *head, int *idx_s, int *sub)
2092{
2093 struct batadv_tt_common_entry *common;
2094 int idx = 0;
2095
2096 rcu_read_lock();
2097 hlist_for_each_entry_rcu(common, head, hash_entry) {
2098 if (idx++ < *idx_s)
2099 continue;
2100
2101 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2102 common, sub)) {
2103 rcu_read_unlock();
2104 *idx_s = idx - 1;
2105 return -EMSGSIZE;
2106 }
2107 }
2108 rcu_read_unlock();
2109
2110 *idx_s = 0;
2111 *sub = 0;
2112 return 0;
2113}
2114
2115/**
2116 * batadv_tt_global_dump - Dump TT global entries into a message
2117 * @msg: Netlink message to dump into
2118 * @cb: Parameters from query
2119 *
2120 * Return: Error code, or length of message on success
2121 */
2122int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2123{
2124 struct net *net = sock_net(cb->skb->sk);
2125 struct net_device *soft_iface;
2126 struct batadv_priv *bat_priv;
2127 struct batadv_hard_iface *primary_if = NULL;
2128 struct batadv_hashtable *hash;
2129 struct hlist_head *head;
2130 int ret;
2131 int ifindex;
2132 int bucket = cb->args[0];
2133 int idx = cb->args[1];
2134 int sub = cb->args[2];
2135 int portid = NETLINK_CB(cb->skb).portid;
2136
2137 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2138 if (!ifindex)
2139 return -EINVAL;
2140
2141 soft_iface = dev_get_by_index(net, ifindex);
2142 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2143 ret = -ENODEV;
2144 goto out;
2145 }
2146
2147 bat_priv = netdev_priv(soft_iface);
2148
2149 primary_if = batadv_primary_if_get_selected(bat_priv);
2150 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2151 ret = -ENOENT;
2152 goto out;
2153 }
2154
2155 hash = bat_priv->tt.global_hash;
2156
2157 while (bucket < hash->size) {
2158 head = &hash->table[bucket];
2159
2160 if (batadv_tt_global_dump_bucket(msg, portid,
2161 cb->nlh->nlmsg_seq, bat_priv,
2162 head, &idx, &sub))
2163 break;
2164
2165 bucket++;
2166 }
2167
2168 ret = msg->len;
2169
2170 out:
2171 if (primary_if)
2172 batadv_hardif_put(primary_if);
2173 if (soft_iface)
2174 dev_put(soft_iface);
2175
2176 cb->args[0] = bucket;
2177 cb->args[1] = idx;
2178 cb->args[2] = sub;
2179
2180 return ret;
2181}
2182
2183/**
Marek Lindner433ff98f2015-06-18 16:11:07 +08002184 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002185 * @tt_global_entry: the global entry to remove the orig_entry from
2186 * @orig_entry: the orig entry to remove and free
2187 *
2188 * Remove an orig_entry from its list in the given tt_global_entry and
2189 * free this orig_entry afterwards.
Marek Lindner433ff98f2015-06-18 16:11:07 +08002190 *
2191 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2192 * part of a list.
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002193 */
2194static void
Marek Lindner433ff98f2015-06-18 16:11:07 +08002195_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2196 struct batadv_tt_orig_list_entry *orig_entry)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002197{
Sven Eckelmann2c72d652015-06-21 14:45:14 +02002198 lockdep_assert_held(&tt_global_entry->list_lock);
2199
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002200 batadv_tt_global_size_dec(orig_entry->orig_node,
2201 tt_global_entry->common.vid);
2202 atomic_dec(&tt_global_entry->orig_list_count);
Marek Lindner433ff98f2015-06-18 16:11:07 +08002203 /* requires holding tt_global_entry->list_lock and orig_entry->list
2204 * being part of a list
2205 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002206 hlist_del_rcu(&orig_entry->list);
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01002207 batadv_tt_orig_list_entry_put(orig_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002208}
2209
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002210/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002211static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02002212batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002213{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002214 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002215 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002216 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002217
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002218 spin_lock_bh(&tt_global_entry->list_lock);
2219 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002220 hlist_for_each_entry_safe(orig_entry, safe, head, list)
Marek Lindner433ff98f2015-06-18 16:11:07 +08002221 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002222 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002223}
2224
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002225/**
2226 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
2227 * @bat_priv: the bat priv with all the soft interface information
2228 * @tt_global_entry: the global entry to remove the orig_node from
2229 * @orig_node: the originator announcing the client
2230 * @message: message to append to the log on deletion
2231 *
2232 * Remove the given orig_node and its according orig_entry from the given
2233 * global tt entry.
2234 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002235static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002236batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2237 struct batadv_tt_global_entry *tt_global_entry,
2238 struct batadv_orig_node *orig_node,
2239 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002240{
2241 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002242 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002243 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02002244 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002245
2246 spin_lock_bh(&tt_global_entry->list_lock);
2247 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002248 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002249 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02002250 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002251 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002252 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002253 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02002254 tt_global_entry->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002255 batadv_print_vid(vid), message);
Marek Lindner433ff98f2015-06-18 16:11:07 +08002256 _batadv_tt_global_del_orig_entry(tt_global_entry,
2257 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002258 }
2259 }
2260 spin_unlock_bh(&tt_global_entry->list_lock);
2261}
2262
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002263/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002264 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2265 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002266 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002267static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02002268batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2269 struct batadv_tt_global_entry *tt_global_entry,
2270 struct batadv_orig_node *orig_node,
2271 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002272{
2273 bool last_entry = true;
2274 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002275 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002276
2277 /* no local entry exists, case 1:
2278 * Check if this is the last one or if other entries exist.
2279 */
2280
2281 rcu_read_lock();
2282 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002283 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002284 if (orig_entry->orig_node != orig_node) {
2285 last_entry = false;
2286 break;
2287 }
2288 }
2289 rcu_read_unlock();
2290
2291 if (last_entry) {
2292 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002293 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002294 tt_global_entry->roam_at = jiffies;
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002295 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002296 /* there is another entry, we can simply delete this
2297 * one and can still use the other one.
2298 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002299 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2300 orig_node, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002301 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002302}
2303
Antonio Quartullic018ad32013-06-04 12:11:39 +02002304/**
2305 * batadv_tt_global_del - remove a client from the global table
2306 * @bat_priv: the bat priv with all the soft interface information
2307 * @orig_node: an originator serving this client
2308 * @addr: the mac address of the client
2309 * @vid: VLAN identifier
2310 * @message: a message explaining the reason for deleting the client to print
2311 * for debugging purpose
2312 * @roaming: true if the deletion has been triggered by a roaming event
2313 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002314static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2315 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002316 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002317 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002318{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002319 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002320 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002321
Antonio Quartullic018ad32013-06-04 12:11:39 +02002322 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002323 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002324 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002325
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002326 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002327 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2328 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002329
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002330 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02002331 batadv_tt_global_free(bat_priv, tt_global_entry,
2332 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002333
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002334 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002335 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08002336
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002337 /* if we are deleting a global entry due to a roam
2338 * event, there are two possibilities:
2339 * 1) the client roamed from node A to node B => if there
2340 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002341 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002342 * wait for node B to claim it. In case of timeout
2343 * the entry is purged.
2344 *
2345 * If there are other originators left, we directly delete
2346 * the originator.
2347 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002348 * the global entry, since it is useless now.
2349 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002350 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002351 tt_global_entry->common.addr,
2352 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002353 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002354 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002355 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02002356 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002357 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002358 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002359 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2360 orig_node, message);
Sven Eckelmann1fda4c02017-02-22 17:16:40 +01002361 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002362
Antonio Quartullicc47f662011-04-27 14:27:57 +02002363out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002364 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002365 batadv_tt_global_entry_put(tt_global_entry);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002366 if (local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01002367 batadv_tt_local_entry_put(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002368}
2369
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002370/**
2371 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
2372 * given originator matching the provided vid
2373 * @bat_priv: the bat priv with all the soft interface information
2374 * @orig_node: the originator owning the entries to remove
2375 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2376 * removed
2377 * @message: debug message to print as "reason"
2378 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002379void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2380 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002381 s32 match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002382 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002383{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002384 struct batadv_tt_global_entry *tt_global;
2385 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002386 u32 i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002387 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002388 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002389 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002390 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02002391 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002392
Simon Wunderlich6e801492011-10-19 10:28:26 +02002393 if (!hash)
2394 return;
2395
Antonio Quartullia73105b2011-04-27 14:27:44 +02002396 for (i = 0; i < hash->size; i++) {
2397 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002398 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002399
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002400 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002401 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01002402 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002403 /* remove only matching entries */
2404 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2405 continue;
2406
Sven Eckelmann56303d32012-06-05 22:31:31 +02002407 tt_global = container_of(tt_common_entry,
2408 struct batadv_tt_global_entry,
2409 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002410
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01002411 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2412 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002413
Sven Eckelmann56303d32012-06-05 22:31:31 +02002414 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02002415 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002416 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002417 "Deleting global tt entry %pM (vid: %d): %s\n",
2418 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002419 batadv_print_vid(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002420 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002421 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002422 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002423 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002424 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002425 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002426 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002427}
2428
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002429static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2430 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002431{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002432 bool purge = false;
2433 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2434 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002435
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002436 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2437 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2438 purge = true;
2439 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002440 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002441
2442 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2443 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2444 purge = true;
2445 *msg = "Temporary client timeout\n";
2446 }
2447
2448 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002449}
2450
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002451static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002452{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002453 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002454 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002455 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002456 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002457 u32 i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002458 char *msg = NULL;
2459 struct batadv_tt_common_entry *tt_common;
2460 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002461
Antonio Quartullicc47f662011-04-27 14:27:57 +02002462 for (i = 0; i < hash->size; i++) {
2463 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002464 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02002465
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002466 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002467 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002468 hash_entry) {
2469 tt_global = container_of(tt_common,
2470 struct batadv_tt_global_entry,
2471 common);
2472
2473 if (!batadv_tt_global_to_purge(tt_global, &msg))
2474 continue;
2475
2476 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002477 "Deleting global tt entry %pM (vid: %d): %s\n",
2478 tt_global->common.addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01002479 batadv_print_vid(tt_global->common.vid),
Antonio Quartulli16052782013-06-04 12:11:41 +02002480 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002481
Sasha Levinb67bfe02013-02-27 17:06:00 -08002482 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002483
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002484 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002485 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002486 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002487 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02002488}
2489
Sven Eckelmann56303d32012-06-05 22:31:31 +02002490static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002491{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002492 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002493 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002494 struct batadv_tt_common_entry *tt_common_entry;
2495 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002496 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002497 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002498 u32 i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002499
Sven Eckelmann807736f2012-07-15 22:26:51 +02002500 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002501 return;
2502
Sven Eckelmann807736f2012-07-15 22:26:51 +02002503 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002504
2505 for (i = 0; i < hash->size; i++) {
2506 head = &hash->table[i];
2507 list_lock = &hash->list_locks[i];
2508
2509 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002510 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002511 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002512 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002513 tt_global = container_of(tt_common_entry,
2514 struct batadv_tt_global_entry,
2515 common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002516 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002517 }
2518 spin_unlock_bh(list_lock);
2519 }
2520
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02002521 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002522
Sven Eckelmann807736f2012-07-15 22:26:51 +02002523 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002524}
2525
Sven Eckelmann56303d32012-06-05 22:31:31 +02002526static bool
2527_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2528 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002529{
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002530 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2531 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002532 return true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002533
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002534 /* check if the two clients are marked as isolated */
2535 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2536 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002537 return true;
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002538
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02002539 return false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002540}
2541
Antonio Quartullic018ad32013-06-04 12:11:39 +02002542/**
2543 * batadv_transtable_search - get the mesh destination for a given client
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @src: mac address of the source client
2546 * @addr: mac address of the destination client
2547 * @vid: VLAN identifier
2548 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002549 * Return: a pointer to the originator that was selected as destination in the
Antonio Quartullic018ad32013-06-04 12:11:39 +02002550 * mesh for contacting the client 'addr', NULL otherwise.
2551 * In case of multiple originators serving the same client, the function returns
2552 * the best one (best in terms of metric towards the destination node).
2553 *
2554 * If the two clients are AP isolated the function returns NULL.
2555 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002556struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002557 const u8 *src,
2558 const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002559 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002560{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002561 struct batadv_tt_local_entry *tt_local_entry = NULL;
2562 struct batadv_tt_global_entry *tt_global_entry = NULL;
2563 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002564 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002565
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002566 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002567 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002568 if (!tt_local_entry ||
2569 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002570 goto out;
2571 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002572
Antonio Quartullic018ad32013-06-04 12:11:39 +02002573 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002574 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002575 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002576
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002577 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002578 * isolation
2579 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002580 if (tt_local_entry &&
2581 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002582 goto out;
2583
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002584 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002585 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002586 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002587 if (best_entry)
2588 orig_node = best_entry->orig_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +01002589 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002590 orig_node = NULL;
2591 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002592
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002593out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002594 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002595 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002596 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01002597 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002598
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002599 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002600}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002601
Antonio Quartulliced72932013-04-24 16:37:51 +02002602/**
2603 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2604 * to the given orig_node
2605 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002606 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002607 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002608 *
2609 * This function computes the checksum for the global table corresponding to a
2610 * specific originator. In particular, the checksum is computed as follows: For
2611 * each client connected to the originator the CRC32C of the MAC address and the
2612 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2613 * together.
2614 *
2615 * The idea behind is that CRC32C should be used as much as possible in order to
2616 * produce a unique hash of the table, but since the order which is used to feed
2617 * the CRC32C function affects the result and since every node in the network
2618 * probably sorts the clients differently, the hash function cannot be directly
2619 * computed over the entire table. Hence the CRC32C is used only on
2620 * the single client entry, while all the results are then xor'ed together
2621 * because the XOR operation can combine them all while trying to reduce the
2622 * noise as much as possible.
2623 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002624 * Return: the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002625 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002626static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2627 struct batadv_orig_node *orig_node,
2628 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002629{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002630 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Linus Lüssing54e22f22017-07-06 07:02:25 +02002631 struct batadv_tt_orig_list_entry *tt_orig;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002632 struct batadv_tt_common_entry *tt_common;
2633 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002634 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002635 u32 i, crc_tmp, crc = 0;
2636 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002637 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002638
2639 for (i = 0; i < hash->size; i++) {
2640 head = &hash->table[i];
2641
2642 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002643 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002644 tt_global = container_of(tt_common,
2645 struct batadv_tt_global_entry,
2646 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002647 /* compute the CRC only for entries belonging to the
2648 * VLAN identified by the vid passed as parameter
2649 */
2650 if (tt_common->vid != vid)
2651 continue;
2652
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002653 /* Roaming clients are in the global table for
2654 * consistency only. They don't have to be
2655 * taken into account while computing the
2656 * global crc
2657 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002658 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002659 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002660 /* Temporary clients have not been announced yet, so
2661 * they have to be skipped while computing the global
2662 * crc
2663 */
2664 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2665 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002666
2667 /* find out if this global entry is announced by this
2668 * originator
2669 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02002670 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2671 orig_node);
2672 if (!tt_orig)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002673 continue;
2674
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002675 /* use network order to read the VID: this ensures that
2676 * every node reads the bytes in the same order.
2677 */
2678 tmp_vid = htons(tt_common->vid);
2679 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002680
2681 /* compute the CRC on flags that have to be kept in sync
2682 * among nodes
2683 */
Linus Lüssing54e22f22017-07-06 07:02:25 +02002684 flags = tt_orig->flags;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002685 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2686
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002687 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Linus Lüssing54e22f22017-07-06 07:02:25 +02002688
2689 batadv_tt_orig_list_entry_put(tt_orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690 }
2691 rcu_read_unlock();
2692 }
2693
Antonio Quartulliced72932013-04-24 16:37:51 +02002694 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002695}
2696
Antonio Quartulliced72932013-04-24 16:37:51 +02002697/**
2698 * batadv_tt_local_crc - calculates the checksum of the local table
2699 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002700 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002701 *
2702 * For details about the computation, please refer to the documentation for
2703 * batadv_tt_global_crc().
2704 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002705 * Return: the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002706 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002707static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2708 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002709{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002710 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002711 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002712 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002713 u32 i, crc_tmp, crc = 0;
2714 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002715 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716
2717 for (i = 0; i < hash->size; i++) {
2718 head = &hash->table[i];
2719
2720 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002721 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002722 /* compute the CRC only for entries belonging to the
2723 * VLAN identified by vid
2724 */
2725 if (tt_common->vid != vid)
2726 continue;
2727
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002728 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002729 * account while computing the CRC
2730 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002731 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002732 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002733
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002734 /* use network order to read the VID: this ensures that
2735 * every node reads the bytes in the same order.
2736 */
2737 tmp_vid = htons(tt_common->vid);
2738 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002739
2740 /* compute the CRC on flags that have to be kept in sync
2741 * among nodes
2742 */
2743 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2744 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2745
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002746 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002747 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002748 rcu_read_unlock();
2749 }
2750
Antonio Quartulliced72932013-04-24 16:37:51 +02002751 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002752}
2753
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002754/**
2755 * batadv_tt_req_node_release - free tt_req node entry
2756 * @ref: kref pointer of the tt req_node entry
2757 */
2758static void batadv_tt_req_node_release(struct kref *ref)
2759{
2760 struct batadv_tt_req_node *tt_req_node;
2761
2762 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2763
Sven Eckelmann86452f82016-06-25 16:44:06 +02002764 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002765}
2766
2767/**
2768 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2769 * possibly release it
2770 * @tt_req_node: tt_req_node to be free'd
2771 */
2772static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2773{
2774 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2775}
2776
Sven Eckelmann56303d32012-06-05 22:31:31 +02002777static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002778{
Marek Lindner7c26a532015-06-28 22:16:06 +08002779 struct batadv_tt_req_node *node;
2780 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002781
Sven Eckelmann807736f2012-07-15 22:26:51 +02002782 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002783
Marek Lindner7c26a532015-06-28 22:16:06 +08002784 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2785 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002786 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002787 }
2788
Sven Eckelmann807736f2012-07-15 22:26:51 +02002789 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002790}
2791
Sven Eckelmann56303d32012-06-05 22:31:31 +02002792static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2793 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002794 const void *tt_buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002795 u16 tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002796{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002798 * last OGM (the OGM could carry no changes)
2799 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002800 spin_lock_bh(&orig_node->tt_buff_lock);
2801 if (tt_buff_len > 0) {
2802 kfree(orig_node->tt_buff);
2803 orig_node->tt_buff_len = 0;
2804 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2805 if (orig_node->tt_buff) {
2806 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2807 orig_node->tt_buff_len = tt_buff_len;
2808 }
2809 }
2810 spin_unlock_bh(&orig_node->tt_buff_lock);
2811}
2812
Sven Eckelmann56303d32012-06-05 22:31:31 +02002813static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002814{
Marek Lindner7c26a532015-06-28 22:16:06 +08002815 struct batadv_tt_req_node *node;
2816 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817
Sven Eckelmann807736f2012-07-15 22:26:51 +02002818 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002819 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002820 if (batadv_has_timed_out(node->issued_at,
2821 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner7c26a532015-06-28 22:16:06 +08002822 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002823 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002824 }
2825 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002826 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827}
2828
Marek Lindner383b8632015-06-18 16:24:24 +08002829/**
2830 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2831 * @bat_priv: the bat priv with all the soft interface information
2832 * @orig_node: orig node this request is being issued for
2833 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002834 * Return: the pointer to the new tt_req_node struct if no request
Marek Lindner383b8632015-06-18 16:24:24 +08002835 * has already been issued for this orig_node, NULL otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002836 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002837static struct batadv_tt_req_node *
Marek Lindner383b8632015-06-18 16:24:24 +08002838batadv_tt_req_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002839 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002841 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002842
Sven Eckelmann807736f2012-07-15 22:26:51 +02002843 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002844 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002845 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2846 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002847 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002848 goto unlock;
2849 }
2850
Sven Eckelmann86452f82016-06-25 16:44:06 +02002851 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002852 if (!tt_req_node)
2853 goto unlock;
2854
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002855 kref_init(&tt_req_node->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002856 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002857 tt_req_node->issued_at = jiffies;
2858
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002859 kref_get(&tt_req_node->refcount);
Marek Lindner7c26a532015-06-28 22:16:06 +08002860 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002861unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002862 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002863 return tt_req_node;
2864}
2865
Marek Lindner335fbe02013-04-23 21:40:02 +08002866/**
2867 * batadv_tt_local_valid - verify that given tt entry is a valid one
2868 * @entry_ptr: to be checked local tt entry
2869 * @data_ptr: not used but definition required to satisfy the callback prototype
2870 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002871 * Return: true if the entry is a valid, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002872 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002873static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002874{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002875 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002876
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002877 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002878 return false;
2879 return true;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002880}
2881
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002882static bool batadv_tt_global_valid(const void *entry_ptr,
2883 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002884{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002885 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2886 const struct batadv_tt_global_entry *tt_global_entry;
2887 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002888
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002889 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2890 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002891 return false;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002892
Sven Eckelmann56303d32012-06-05 22:31:31 +02002893 tt_global_entry = container_of(tt_common_entry,
2894 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002895 common);
2896
Sven Eckelmanna5130882012-05-16 20:23:16 +02002897 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002898}
2899
Marek Lindner335fbe02013-04-23 21:40:02 +08002900/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002901 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2902 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002903 * @bat_priv: the bat priv with all the soft interface information
2904 * @hash: hash table containing the tt entries
2905 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002906 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002907 * @valid_cb: function to filter tt change entries
2908 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002909 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002910static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2911 struct batadv_hashtable *hash,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002912 void *tvlv_buff, u16 tt_len,
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002913 bool (*valid_cb)(const void *,
2914 const void *),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002915 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002916{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002917 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002918 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002919 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002920 u16 tt_tot, tt_num_entries = 0;
2921 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002922
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002923 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002924 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002925
2926 rcu_read_lock();
2927 for (i = 0; i < hash->size; i++) {
2928 head = &hash->table[i];
2929
Sasha Levinb67bfe02013-02-27 17:06:00 -08002930 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002931 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002932 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002933 break;
2934
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002935 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002936 continue;
2937
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002938 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002939 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002940 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002941 memset(tt_change->reserved, 0,
2942 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002943
Marek Lindner335fbe02013-04-23 21:40:02 +08002944 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002945 tt_change++;
2946 }
2947 }
2948 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002949}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002950
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002951/**
2952 * batadv_tt_global_check_crc - check if all the CRCs are correct
2953 * @orig_node: originator for which the CRCs have to be checked
2954 * @tt_vlan: pointer to the first tvlv VLAN entry
2955 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002956 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002957 * Return: true if all the received CRCs match the locally stored ones, false
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002958 * otherwise
2959 */
2960static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2961 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002962 u16 num_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002963{
2964 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2965 struct batadv_orig_node_vlan *vlan;
Simon Wunderlichc169c592015-09-02 20:09:56 +02002966 int i, orig_num_vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002967 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002968
2969 /* check if each received CRC matches the locally stored one */
2970 for (i = 0; i < num_vlan; i++) {
2971 tt_vlan_tmp = tt_vlan + i;
2972
2973 /* if orig_node is a backbone node for this VLAN, don't check
2974 * the CRC as we ignore all the global entries over it
2975 */
2976 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002977 orig_node->orig,
2978 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002979 continue;
2980
2981 vlan = batadv_orig_node_vlan_get(orig_node,
2982 ntohs(tt_vlan_tmp->vid));
2983 if (!vlan)
2984 return false;
2985
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002986 crc = vlan->tt.crc;
Sven Eckelmann21754e22016-01-17 11:01:24 +01002987 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002988
2989 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002990 return false;
2991 }
2992
Simon Wunderlichc169c592015-09-02 20:09:56 +02002993 /* check if any excess VLANs exist locally for the originator
2994 * which are not mentioned in the TVLV from the originator.
2995 */
2996 rcu_read_lock();
2997 orig_num_vlan = 0;
2998 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2999 orig_num_vlan++;
3000 rcu_read_unlock();
3001
3002 if (orig_num_vlan > num_vlan)
3003 return false;
3004
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003005 return true;
3006}
3007
3008/**
3009 * batadv_tt_local_update_crc - update all the local CRCs
3010 * @bat_priv: the bat priv with all the soft interface information
3011 */
3012static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3013{
3014 struct batadv_softif_vlan *vlan;
3015
3016 /* recompute the global CRC for each VLAN */
3017 rcu_read_lock();
3018 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3019 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3020 }
3021 rcu_read_unlock();
3022}
3023
3024/**
3025 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
3026 * @bat_priv: the bat priv with all the soft interface information
3027 * @orig_node: the orig_node for which the CRCs have to be updated
3028 */
3029static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3030 struct batadv_orig_node *orig_node)
3031{
3032 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003033 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003034
3035 /* recompute the global CRC for each VLAN */
3036 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08003037 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003038 /* if orig_node is a backbone node for this VLAN, don't compute
3039 * the CRC as we ignore all the global entries over it
3040 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02003041 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3042 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003043 continue;
3044
3045 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3046 vlan->tt.crc = crc;
3047 }
3048 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02003049}
3050
Antonio Quartulliced72932013-04-24 16:37:51 +02003051/**
3052 * batadv_send_tt_request - send a TT Request message to a given node
3053 * @bat_priv: the bat priv with all the soft interface information
3054 * @dst_orig_node: the destination of the message
3055 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003056 * @tt_vlan: pointer to the first tvlv VLAN object to request
3057 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02003058 * @full_table: ask for the entire translation table if true, while only for the
3059 * last TT diff otherwise
Antonio Quartullid15cd622015-11-17 16:40:52 +08003060 *
3061 * Return: true if the TT Request was sent, false otherwise
Antonio Quartulliced72932013-04-24 16:37:51 +02003062 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01003063static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3064 struct batadv_orig_node *dst_orig_node,
3065 u8 ttvn,
3066 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3067 u16 num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003068{
Marek Lindner335fbe02013-04-23 21:40:02 +08003069 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003070 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003071 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3072 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08003073 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003074 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003075
Sven Eckelmanne5d89252012-05-12 13:48:54 +02003076 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003077 if (!primary_if)
3078 goto out;
3079
3080 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003081 * reply from the same orig_node yet
3082 */
Marek Lindner383b8632015-06-18 16:24:24 +08003083 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003084 if (!tt_req_node)
3085 goto out;
3086
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003087 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3088 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08003089 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003090 goto out;
3091
Marek Lindner335fbe02013-04-23 21:40:02 +08003092 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3093 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003094 tvlv_tt_data->num_vlan = htons(num_vlan);
3095
3096 /* send all the CRCs within the request. This is needed by intermediate
3097 * nodes to ensure they have the correct table before replying
3098 */
3099 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3100 for (i = 0; i < num_vlan; i++) {
3101 tt_vlan_req->vid = tt_vlan->vid;
3102 tt_vlan_req->crc = tt_vlan->crc;
3103
3104 tt_vlan_req++;
3105 tt_vlan++;
3106 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003107
3108 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003109 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003110
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02003111 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003112 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02003113
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003114 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08003115 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3116 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003117 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08003118 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003119
3120out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02003121 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003122 batadv_hardif_put(primary_if);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003123
Antonio Quartullia73105b2011-04-27 14:27:44 +02003124 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02003125 spin_lock_bh(&bat_priv->tt.req_list_lock);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003126 if (!hlist_unhashed(&tt_req_node->list)) {
3127 hlist_del_init(&tt_req_node->list);
3128 batadv_tt_req_node_put(tt_req_node);
3129 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003130 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003131 }
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003132
3133 if (tt_req_node)
3134 batadv_tt_req_node_put(tt_req_node);
3135
Marek Lindner335fbe02013-04-23 21:40:02 +08003136 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003137 return ret;
3138}
3139
Marek Lindner335fbe02013-04-23 21:40:02 +08003140/**
3141 * batadv_send_other_tt_response - send reply to tt request concerning another
3142 * node's translation table
3143 * @bat_priv: the bat priv with all the soft interface information
3144 * @tt_data: tt data containing the tt request information
3145 * @req_src: mac address of tt request sender
3146 * @req_dst: mac address of tt request recipient
3147 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003148 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003149 */
3150static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3151 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003152 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003153{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003154 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003155 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003156 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08003157 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003158 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08003159 bool ret = false, full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003160 u8 orig_ttvn, req_ttvn;
3161 u16 tvlv_len;
3162 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003163
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003164 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003165 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003166 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003167 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003168
3169 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08003170 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003171 if (!req_dst_orig_node)
3172 goto out;
3173
Marek Lindner335fbe02013-04-23 21:40:02 +08003174 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003175 if (!res_dst_orig_node)
3176 goto out;
3177
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003178 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08003179 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003180
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003181 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08003182 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003183 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003184 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3185 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003186 goto out;
3187
Antonio Quartulli015758d2011-07-09 17:52:13 +02003188 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08003189 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02003190 !req_dst_orig_node->tt_buff)
3191 full_table = true;
3192 else
3193 full_table = false;
3194
Marek Lindner335fbe02013-04-23 21:40:02 +08003195 /* TT fragmentation hasn't been implemented yet, so send as many
3196 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003197 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003198 if (!full_table) {
3199 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3200 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003201
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003202 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3203 &tvlv_tt_data,
3204 &tt_change,
3205 &tt_len);
3206 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003207 goto unlock;
3208
Antonio Quartullia73105b2011-04-27 14:27:44 +02003209 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003210 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02003211 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003212 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3213 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003214 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3215 * in the initial part
3216 */
3217 tt_len = -1;
3218 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 out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003224
3225 /* fill the rest of the tvlv with the real TT entries */
3226 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3227 tt_change, tt_len,
3228 batadv_tt_global_valid,
3229 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003230 }
3231
Marek Lindnera19d3d82013-05-27 15:33:25 +08003232 /* Don't send the response, if larger than fragmented packet. */
3233 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3234 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3235 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3236 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3237 res_dst_orig_node->orig);
3238 goto out;
3239 }
3240
Marek Lindner335fbe02013-04-23 21:40:02 +08003241 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3242 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003243
3244 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003245 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003246
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003247 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08003248 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3249 res_dst_orig_node->orig, req_dst_orig_node->orig,
3250 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003251
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003252 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003253
Marek Lindner335fbe02013-04-23 21:40:02 +08003254 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003255 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3256 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02003257
Marek Lindner335fbe02013-04-23 21:40:02 +08003258 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003259 goto out;
3260
3261unlock:
3262 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3263
3264out:
3265 if (res_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003266 batadv_orig_node_put(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003267 if (req_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003268 batadv_orig_node_put(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08003269 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003270 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003271}
Sven Eckelmann96412692012-06-05 22:31:30 +02003272
Marek Lindner335fbe02013-04-23 21:40:02 +08003273/**
3274 * batadv_send_my_tt_response - send reply to tt request concerning this node's
3275 * translation table
3276 * @bat_priv: the bat priv with all the soft interface information
3277 * @tt_data: tt data containing the tt request information
3278 * @req_src: mac address of tt request sender
3279 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003280 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003281 */
3282static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3283 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003284 u8 *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003285{
Marek Lindner335fbe02013-04-23 21:40:02 +08003286 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003287 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003288 struct batadv_tvlv_tt_change *tt_change;
3289 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003290 u8 my_ttvn, req_ttvn;
3291 u16 tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003292 bool full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003293 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003294
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003295 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003296 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003297 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003298 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003299
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003300 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003301
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003302 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08003303 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003304
Marek Lindner335fbe02013-04-23 21:40:02 +08003305 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003306 if (!orig_node)
3307 goto out;
3308
Sven Eckelmanne5d89252012-05-12 13:48:54 +02003309 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003310 if (!primary_if)
3311 goto out;
3312
3313 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003314 * is too big send the whole local translation table
3315 */
Marek Lindner335fbe02013-04-23 21:40:02 +08003316 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02003317 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003318 full_table = true;
3319 else
3320 full_table = false;
3321
Marek Lindner335fbe02013-04-23 21:40:02 +08003322 /* TT fragmentation hasn't been implemented yet, so send as many
3323 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003324 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003325 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02003326 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003327
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003328 tt_len = bat_priv->tt.last_changeset_len;
3329 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3330 &tvlv_tt_data,
3331 &tt_change,
3332 &tt_len);
Sven Eckelmannc2d0f482016-11-30 21:47:09 +01003333 if (!tt_len || !tvlv_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003334 goto unlock;
3335
Marek Lindner335fbe02013-04-23 21:40:02 +08003336 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003337 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02003338 bat_priv->tt.last_changeset_len);
3339 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003340 } else {
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003341 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003342
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003343 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3344 * in the initial part
3345 */
3346 tt_len = -1;
3347 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3348 &tvlv_tt_data,
3349 &tt_change,
3350 &tt_len);
Sven Eckelmannc2d0f482016-11-30 21:47:09 +01003351 if (!tt_len || !tvlv_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003352 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003353
3354 /* fill the rest of the tvlv with the real TT entries */
3355 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3356 tt_change, tt_len,
3357 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003358 }
3359
Marek Lindner335fbe02013-04-23 21:40:02 +08003360 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3361 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003362
3363 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08003364 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003365
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003366 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08003367 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3368 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003369
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003370 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003371
Marek Lindner335fbe02013-04-23 21:40:02 +08003372 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003373 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3374 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003375
Antonio Quartullia73105b2011-04-27 14:27:44 +02003376 goto out;
3377
3378unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003379 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003380out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003381 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003382 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003383 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003384 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003385 batadv_hardif_put(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08003386 kfree(tvlv_tt_data);
3387 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02003388 return true;
3389}
3390
Marek Lindner335fbe02013-04-23 21:40:02 +08003391/**
3392 * batadv_send_tt_response - send reply to tt request
3393 * @bat_priv: the bat priv with all the soft interface information
3394 * @tt_data: tt data containing the tt request information
3395 * @req_src: mac address of tt request sender
3396 * @req_dst: mac address of tt request recipient
3397 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003398 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08003399 */
3400static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3401 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003402 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003403{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02003404 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08003405 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02003406 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3407 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003408}
3409
Sven Eckelmann56303d32012-06-05 22:31:31 +02003410static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3411 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08003412 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003413 u16 tt_num_changes, u8 ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003414{
3415 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003416 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003417
3418 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003419 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3420 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003421 batadv_tt_global_del(bat_priv, orig_node,
3422 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003423 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02003424 "tt removed by changes",
3425 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003426 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003427 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02003428 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003429 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02003430 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003431 /* In case of problem while storing a
3432 * global_entry, we stop the updating
3433 * procedure without committing the
3434 * ttvn change. This will avoid to send
3435 * corrupted data on tt_request
3436 */
3437 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003438 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003439 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003440 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003441}
3442
Sven Eckelmann56303d32012-06-05 22:31:31 +02003443static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003444 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003445 u8 ttvn, u8 *resp_src,
3446 u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003447{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003448 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003449
Marek Lindner335fbe02013-04-23 21:40:02 +08003450 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003451 if (!orig_node)
3452 goto out;
3453
3454 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02003455 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3456 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02003457
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003458 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3459 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003460
3461 spin_lock_bh(&orig_node->tt_buff_lock);
3462 kfree(orig_node->tt_buff);
3463 orig_node->tt_buff_len = 0;
3464 orig_node->tt_buff = NULL;
3465 spin_unlock_bh(&orig_node->tt_buff_lock);
3466
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003467 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003468
3469out:
3470 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003471 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003472}
3473
Sven Eckelmann56303d32012-06-05 22:31:31 +02003474static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3475 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003476 u16 tt_num_changes, u8 ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08003477 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003478{
Sven Eckelmanna5130882012-05-16 20:23:16 +02003479 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3480 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003481
Antonio Quartullie8cf2342013-05-28 13:14:28 +02003482 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3483 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003484 atomic_set(&orig_node->last_ttvn, ttvn);
3485}
3486
Antonio Quartullic018ad32013-06-04 12:11:39 +02003487/**
3488 * batadv_is_my_client - check if a client is served by the local node
3489 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01003490 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02003491 * @vid: VLAN identifier
3492 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003493 * Return: true if the client is served by this node, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +02003494 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003495bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003496 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003497{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003498 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003499 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003500
Antonio Quartullic018ad32013-06-04 12:11:39 +02003501 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003502 if (!tt_local_entry)
3503 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003504 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003505 * consistency purpose)
3506 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003507 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3508 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003509 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003510 ret = true;
3511out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02003512 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003513 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003514 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003515}
3516
Marek Lindner335fbe02013-04-23 21:40:02 +08003517/**
3518 * batadv_handle_tt_response - process incoming tt reply
3519 * @bat_priv: the bat priv with all the soft interface information
3520 * @tt_data: tt data containing the tt request information
3521 * @resp_src: mac address of tt reply sender
3522 * @num_entries: number of tt change entries appended to the tt data
3523 */
3524static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3525 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003526 u8 *resp_src, u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003527{
Marek Lindner7c26a532015-06-28 22:16:06 +08003528 struct batadv_tt_req_node *node;
3529 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003530 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08003531 struct batadv_tvlv_tt_change *tt_change;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003532 u8 *tvlv_ptr = (u8 *)tt_data;
3533 u16 change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003534
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003535 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003536 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003537 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003538 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003539
Marek Lindner335fbe02013-04-23 21:40:02 +08003540 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003541 if (!orig_node)
3542 goto out;
3543
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003544 spin_lock_bh(&orig_node->tt_lock);
3545
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003546 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3547 change_offset *= ntohs(tt_data->num_vlan);
3548 change_offset += sizeof(*tt_data);
3549 tvlv_ptr += change_offset;
3550
3551 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08003552 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003553 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3554 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02003555 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08003556 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3557 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02003558 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003559
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003560 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003561 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003562
3563 spin_unlock_bh(&orig_node->tt_lock);
3564
Antonio Quartullia73105b2011-04-27 14:27:44 +02003565 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003566 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08003567 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08003568 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003569 continue;
Marek Lindner7c26a532015-06-28 22:16:06 +08003570 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003571 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003572 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003573
Sven Eckelmann807736f2012-07-15 22:26:51 +02003574 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003575out:
3576 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003577 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003578}
3579
Sven Eckelmann56303d32012-06-05 22:31:31 +02003580static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003581{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003582 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003583
Sven Eckelmann807736f2012-07-15 22:26:51 +02003584 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003585
Sven Eckelmann807736f2012-07-15 22:26:51 +02003586 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02003587 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003588 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003589 }
3590
Sven Eckelmann807736f2012-07-15 22:26:51 +02003591 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003592}
3593
Sven Eckelmann56303d32012-06-05 22:31:31 +02003594static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003595{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003596 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003597
Sven Eckelmann807736f2012-07-15 22:26:51 +02003598 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3599 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003600 if (!batadv_has_timed_out(node->first_time,
3601 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003602 continue;
3603
3604 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003605 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003606 }
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 Eckelmann62fe7102015-09-15 19:00:48 +02003610/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08003611 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3612 * @bat_priv: the bat priv with all the soft interface information
3613 * @client: mac address of the roaming client
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003614 *
3615 * This function checks whether the client already reached the
Antonio Quartullicc47f662011-04-27 14:27:57 +02003616 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3617 * will not be sent.
3618 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003619 * Return: true if the ROAMING_ADV can be sent, false otherwise
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003620 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003621static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003622{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003623 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003624 bool ret = false;
3625
Sven Eckelmann807736f2012-07-15 22:26:51 +02003626 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003627 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003628 * reply from the same orig_node yet
3629 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003630 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003631 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003632 continue;
3633
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003634 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003635 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003636 continue;
3637
Sven Eckelmann3e348192012-05-16 20:23:22 +02003638 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003639 /* Sorry, you roamed too many times! */
3640 goto unlock;
3641 ret = true;
3642 break;
3643 }
3644
3645 if (!ret) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02003646 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3647 GFP_ATOMIC);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003648 if (!tt_roam_node)
3649 goto unlock;
3650
3651 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003652 atomic_set(&tt_roam_node->counter,
3653 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003654 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003655
Sven Eckelmann807736f2012-07-15 22:26:51 +02003656 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003657 ret = true;
3658 }
3659
3660unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003661 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003662 return ret;
3663}
3664
Antonio Quartullic018ad32013-06-04 12:11:39 +02003665/**
3666 * batadv_send_roam_adv - send a roaming advertisement message
3667 * @bat_priv: the bat priv with all the soft interface information
3668 * @client: mac address of the roaming client
3669 * @vid: VLAN identifier
3670 * @orig_node: message destination
3671 *
3672 * Send a ROAMING_ADV message to the node which was previously serving this
3673 * client. This is done to inform the node that from now on all traffic destined
3674 * for this particular roamed client has to be forwarded to the sender of the
3675 * roaming message.
3676 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003677static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003678 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003679 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003680{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003681 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003682 struct batadv_tvlv_roam_adv tvlv_roam;
3683
3684 primary_if = batadv_primary_if_get_selected(bat_priv);
3685 if (!primary_if)
3686 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003687
3688 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003689 * already roamed to us too many times
3690 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003691 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003692 goto out;
3693
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003694 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003695 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01003696 orig_node->orig, client, batadv_print_vid(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003697
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003698 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003699
Marek Lindner122edaa2013-04-23 21:40:03 +08003700 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003701 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003702
3703 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3704 orig_node->orig, BATADV_TVLV_ROAM, 1,
3705 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003706
3707out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003708 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003709 batadv_hardif_put(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003710}
3711
Sven Eckelmanna5130882012-05-16 20:23:16 +02003712static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003713{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003714 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003715 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003716 struct batadv_priv *bat_priv;
3717
Geliang Tang4ba4bc02015-12-28 23:43:37 +08003718 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003719 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3720 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003721
Marek Lindnera19d3d82013-05-27 15:33:25 +08003722 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003723 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003724 batadv_tt_req_purge(bat_priv);
3725 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003726
Antonio Quartulli72414442012-12-25 13:14:37 +01003727 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3728 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003729}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003730
Sven Eckelmann56303d32012-06-05 22:31:31 +02003731void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003732{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003733 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3734 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3735
Sven Eckelmann807736f2012-07-15 22:26:51 +02003736 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003737
Sven Eckelmanna5130882012-05-16 20:23:16 +02003738 batadv_tt_local_table_free(bat_priv);
3739 batadv_tt_global_table_free(bat_priv);
3740 batadv_tt_req_list_free(bat_priv);
3741 batadv_tt_changes_list_free(bat_priv);
3742 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003743
Sven Eckelmann807736f2012-07-15 22:26:51 +02003744 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003745}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003746
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003747/**
3748 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3749 * table and possibly count them in the TT size
3750 * @bat_priv: the bat priv with all the soft interface information
3751 * @flags: the flag to switch
3752 * @enable: whether to set or unset the flag
3753 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003754 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003755static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3756 bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003757{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003758 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3759 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003760 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003761 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003762
3763 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003764 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003765
3766 for (i = 0; i < hash->size; i++) {
3767 head = &hash->table[i];
3768
3769 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003770 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003771 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003772 if (enable) {
3773 if ((tt_common_entry->flags & flags) == flags)
3774 continue;
3775 tt_common_entry->flags |= flags;
3776 } else {
3777 if (!(tt_common_entry->flags & flags))
3778 continue;
3779 tt_common_entry->flags &= ~flags;
3780 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003781
3782 if (!count)
3783 continue;
3784
3785 batadv_tt_local_size_inc(bat_priv,
3786 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003787 }
3788 rcu_read_unlock();
3789 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003790}
3791
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003792/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003793static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003794{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003795 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003796 struct batadv_tt_common_entry *tt_common;
3797 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003798 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003799 struct hlist_head *head;
3800 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003801 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003802
3803 if (!hash)
3804 return;
3805
3806 for (i = 0; i < hash->size; i++) {
3807 head = &hash->table[i];
3808 list_lock = &hash->list_locks[i];
3809
3810 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003811 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003812 hash_entry) {
3813 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003814 continue;
3815
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003816 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003817 "Deleting local tt entry (%pM, vid: %d): pending\n",
3818 tt_common->addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01003819 batadv_print_vid(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003820
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003821 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003822 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003823 tt_local = container_of(tt_common,
3824 struct batadv_tt_local_entry,
3825 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003826
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003827 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003828 }
3829 spin_unlock_bh(list_lock);
3830 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003831}
3832
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003833/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003834 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3835 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003836 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003837 *
3838 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003839 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003840static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003841{
Sven Eckelmann5274cd62015-06-21 14:45:15 +02003842 lockdep_assert_held(&bat_priv->tt.commit_lock);
3843
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003844 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3845 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3846 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003847 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003848 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003849
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003850 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003851
Sven Eckelmanna5130882012-05-16 20:23:16 +02003852 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003853 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003854
3855 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003856 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003857 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003858 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003859 (u8)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003860
3861 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003862 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003863 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003864}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003865
Marek Lindnera19d3d82013-05-27 15:33:25 +08003866/**
3867 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3868 * have been queued in the time since the last commit
3869 * @bat_priv: the bat priv with all the soft interface information
3870 */
3871void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3872{
3873 spin_lock_bh(&bat_priv->tt.commit_lock);
3874 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003875 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003876}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003877
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003878bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3879 unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003880{
Markus Elfring393b2992016-03-11 13:10:20 +01003881 struct batadv_tt_local_entry *tt_local_entry;
3882 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003883 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003884 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003885
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003886 vlan = batadv_softif_vlan_get(bat_priv, vid);
Markus Elfringe087f342015-11-03 19:20:34 +01003887 if (!vlan)
3888 return false;
3889
3890 if (!atomic_read(&vlan->ap_isolation))
Markus Elfring393b2992016-03-11 13:10:20 +01003891 goto vlan_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003892
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003893 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003894 if (!tt_local_entry)
Markus Elfring393b2992016-03-11 13:10:20 +01003895 goto vlan_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003896
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003897 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003898 if (!tt_global_entry)
Markus Elfring393b2992016-03-11 13:10:20 +01003899 goto local_entry_put;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003900
Markus Elfring393b2992016-03-11 13:10:20 +01003901 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3902 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003903
Markus Elfring393b2992016-03-11 13:10:20 +01003904 batadv_tt_global_entry_put(tt_global_entry);
3905local_entry_put:
3906 batadv_tt_local_entry_put(tt_local_entry);
3907vlan_put:
Sven Eckelmann9c3bf082016-01-17 11:01:21 +01003908 batadv_softif_vlan_put(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003909 return ret;
3910}
Marek Lindnera943cac2011-07-30 13:10:18 +02003911
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003912/**
3913 * batadv_tt_update_orig - update global translation table with new tt
3914 * information received via ogms
3915 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmanne51f0392015-09-06 21:38:51 +02003916 * @orig_node: the orig_node of the ogm
3917 * @tt_buff: pointer to the first tvlv VLAN entry
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003918 * @tt_num_vlan: number of tvlv VLAN entries
3919 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003920 * @tt_num_changes: number of tt changes inside the tt buffer
3921 * @ttvn: translation table version number of this changeset
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003922 */
3923static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3924 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003925 const void *tt_buff, u16 tt_num_vlan,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003926 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003927 u16 tt_num_changes, u8 ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003928{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003929 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003930 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003931 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003932 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003933
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003934 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003935 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3936 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003937
Antonio Quartulli17071572011-11-07 16:36:40 +01003938 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003939 * increased by one -> we can apply the attached changes
3940 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003941 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003942 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003943 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3944 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003945 * In this case send a tt request
3946 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003947 if (!tt_num_changes) {
3948 full_table = false;
3949 goto request_table;
3950 }
3951
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003952 spin_lock_bh(&orig_node->tt_lock);
3953
Sven Eckelmanna5130882012-05-16 20:23:16 +02003954 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003955 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003956
3957 /* Even if we received the precomputed crc with the OGM, we
3958 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003959 * in the global table
3960 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003961 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003962
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003963 spin_unlock_bh(&orig_node->tt_lock);
3964
Marek Lindnera943cac2011-07-30 13:10:18 +02003965 /* The ttvn alone is not enough to guarantee consistency
3966 * because a single value could represent different states
3967 * (due to the wrap around). Thus a node has to check whether
3968 * the resulting table (after applying the changes) is still
3969 * consistent or not. E.g. a node could disconnect while its
3970 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3971 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003972 * inconsistency
3973 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003974 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3975 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003976 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003977 } else {
3978 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003979 * in sync anymore -> request fresh tt data
3980 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003981 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003982 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3983 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003984request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003985 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003986 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3987 orig_node->orig, ttvn, orig_ttvn,
3988 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003989 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003990 tt_vlan, tt_num_vlan,
3991 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003992 return;
3993 }
3994 }
3995}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003996
Antonio Quartullic018ad32013-06-04 12:11:39 +02003997/**
3998 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3999 * @bat_priv: the bat priv with all the soft interface information
4000 * @addr: the mac address of the client to check
4001 * @vid: VLAN identifier
4002 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004003 * Return: true if we know that the client has moved from its old originator
Antonio Quartullic018ad32013-06-04 12:11:39 +02004004 * to another one. This entry is still kept for consistency purposes and will be
4005 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004006 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02004007bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004008 u8 *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004009{
Sven Eckelmann56303d32012-06-05 22:31:31 +02004010 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004011 bool ret = false;
4012
Antonio Quartullic018ad32013-06-04 12:11:39 +02004013 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004014 if (!tt_global_entry)
4015 goto out;
4016
Antonio Quartullic1d07432013-01-15 22:17:19 +10004017 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01004018 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01004019out:
4020 return ret;
4021}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004022
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004023/**
4024 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
4025 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02004026 * @addr: the mac address of the local client to query
4027 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004028 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004029 * Return: true if the local client is known to be roaming (it is not served by
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004030 * this node anymore) or not. If yes, the client is still present in the table
4031 * to keep the latter consistent with the node TTVN
4032 */
4033bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004034 u8 *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004035{
4036 struct batadv_tt_local_entry *tt_local_entry;
4037 bool ret = false;
4038
Antonio Quartullic018ad32013-06-04 12:11:39 +02004039 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004040 if (!tt_local_entry)
4041 goto out;
4042
4043 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann95c0db92016-01-17 11:01:25 +01004044 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004045out:
4046 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02004047}
4048
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004049bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4050 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02004051 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02004052 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004053{
Simon Wunderlich1227c9a2017-06-01 17:11:24 +02004054 /* ignore loop detect macs, they are not supposed to be in the tt local
4055 * data as well.
4056 */
4057 if (batadv_bla_is_loopdetect_mac(addr))
4058 return false;
4059
Antonio Quartulli16052782013-06-04 12:11:41 +02004060 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004061 BATADV_TT_CLIENT_TEMP,
4062 atomic_read(&orig_node->last_ttvn)))
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02004063 return false;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004064
4065 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02004066 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +01004067 addr, batadv_print_vid(vid), orig_node->orig);
Simon Wunderlich75ae84a2017-06-01 17:11:26 +02004068
4069 return true;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02004070}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004071
4072/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08004073 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
4074 * maximum packet size that can be transported through the mesh
4075 * @soft_iface: netdev struct of the mesh interface
4076 *
4077 * Remove entries older than 'timeout' and half timeout if more entries need
4078 * to be removed.
4079 */
4080void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4081{
4082 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4083 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4084 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4085 bool reduced = false;
4086
4087 spin_lock_bh(&bat_priv->tt.commit_lock);
4088
4089 while (true) {
4090 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4091 if (packet_size_max >= table_size)
4092 break;
4093
4094 batadv_tt_local_purge(bat_priv, timeout);
4095 batadv_tt_local_purge_pending_clients(bat_priv);
4096
4097 timeout /= 2;
4098 reduced = true;
4099 net_ratelimited_function(batadv_info, soft_iface,
4100 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4101 packet_size_max);
4102 }
4103
4104 /* commit these changes immediately, to avoid synchronization problem
4105 * with the TTVN
4106 */
4107 if (reduced)
4108 batadv_tt_local_commit_changes_nolock(bat_priv);
4109
4110 spin_unlock_bh(&bat_priv->tt.commit_lock);
4111}
4112
4113/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004114 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
4115 * @bat_priv: the bat priv with all the soft interface information
4116 * @orig: the orig_node of the ogm
4117 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4118 * @tvlv_value: tvlv buffer containing the gateway data
4119 * @tvlv_value_len: tvlv buffer length
4120 */
4121static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4122 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004123 u8 flags, void *tvlv_value,
4124 u16 tvlv_value_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004125{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004126 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4127 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004128 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004129 u16 num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004130
4131 if (tvlv_value_len < sizeof(*tt_data))
4132 return;
4133
4134 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4135 tvlv_value_len -= sizeof(*tt_data);
4136
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004137 num_vlan = ntohs(tt_data->num_vlan);
4138
4139 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4140 return;
4141
4142 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4143 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4144 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4145
Antonio Quartulli298e6e62013-05-28 13:14:27 +02004146 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004147
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004148 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4149 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004150}
4151
4152/**
Marek Lindner335fbe02013-04-23 21:40:02 +08004153 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
4154 * container
4155 * @bat_priv: the bat priv with all the soft interface information
4156 * @src: mac address of tt tvlv sender
4157 * @dst: mac address of tt tvlv recipient
4158 * @tvlv_value: tvlv buffer containing the tt data
4159 * @tvlv_value_len: tvlv buffer length
4160 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004161 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner335fbe02013-04-23 21:40:02 +08004162 * otherwise.
4163 */
4164static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004165 u8 *src, u8 *dst,
Marek Lindner335fbe02013-04-23 21:40:02 +08004166 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004167 u16 tvlv_value_len)
Marek Lindner335fbe02013-04-23 21:40:02 +08004168{
4169 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004170 u16 tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08004171 char tt_flag;
4172 bool ret;
4173
4174 if (tvlv_value_len < sizeof(*tt_data))
4175 return NET_RX_SUCCESS;
4176
4177 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4178 tvlv_value_len -= sizeof(*tt_data);
4179
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004180 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4181 tt_vlan_len *= ntohs(tt_data->num_vlan);
4182
4183 if (tvlv_value_len < tt_vlan_len)
4184 return NET_RX_SUCCESS;
4185
4186 tvlv_value_len -= tt_vlan_len;
4187 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08004188
4189 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4190 case BATADV_TT_REQUEST:
4191 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4192
4193 /* If this node cannot provide a TT response the tt_request is
4194 * forwarded
4195 */
4196 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4197 if (!ret) {
4198 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4199 tt_flag = 'F';
4200 else
4201 tt_flag = '.';
4202
4203 batadv_dbg(BATADV_DBG_TT, bat_priv,
4204 "Routing TT_REQUEST to %pM [%c]\n",
4205 dst, tt_flag);
4206 /* tvlv API will re-route the packet */
4207 return NET_RX_DROP;
4208 }
4209 break;
4210 case BATADV_TT_RESPONSE:
4211 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4212
4213 if (batadv_is_my_mac(bat_priv, dst)) {
4214 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02004215 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08004216 return NET_RX_SUCCESS;
4217 }
4218
4219 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4220 tt_flag = 'F';
4221 else
4222 tt_flag = '.';
4223
4224 batadv_dbg(BATADV_DBG_TT, bat_priv,
4225 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4226
4227 /* tvlv API will re-route the packet */
4228 return NET_RX_DROP;
4229 }
4230
4231 return NET_RX_SUCCESS;
4232}
4233
4234/**
Marek Lindner122edaa2013-04-23 21:40:03 +08004235 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
4236 * @bat_priv: the bat priv with all the soft interface information
4237 * @src: mac address of tt tvlv sender
4238 * @dst: mac address of tt tvlv recipient
4239 * @tvlv_value: tvlv buffer containing the tt data
4240 * @tvlv_value_len: tvlv buffer length
4241 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004242 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner122edaa2013-04-23 21:40:03 +08004243 * otherwise.
4244 */
4245static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004246 u8 *src, u8 *dst,
Marek Lindner122edaa2013-04-23 21:40:03 +08004247 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004248 u16 tvlv_value_len)
Marek Lindner122edaa2013-04-23 21:40:03 +08004249{
4250 struct batadv_tvlv_roam_adv *roaming_adv;
4251 struct batadv_orig_node *orig_node = NULL;
4252
4253 /* If this node is not the intended recipient of the
4254 * roaming advertisement the packet is forwarded
4255 * (the tvlv API will re-route the packet).
4256 */
4257 if (!batadv_is_my_mac(bat_priv, dst))
4258 return NET_RX_DROP;
4259
Marek Lindner122edaa2013-04-23 21:40:03 +08004260 if (tvlv_value_len < sizeof(*roaming_adv))
4261 goto out;
4262
4263 orig_node = batadv_orig_hash_find(bat_priv, src);
4264 if (!orig_node)
4265 goto out;
4266
4267 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4268 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4269
4270 batadv_dbg(BATADV_DBG_TT, bat_priv,
4271 "Received ROAMING_ADV from %pM (client %pM)\n",
4272 src, roaming_adv->client);
4273
4274 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02004275 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08004276 atomic_read(&orig_node->last_ttvn) + 1);
4277
4278out:
4279 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01004280 batadv_orig_node_put(orig_node);
Marek Lindner122edaa2013-04-23 21:40:03 +08004281 return NET_RX_SUCCESS;
4282}
4283
4284/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004285 * batadv_tt_init - initialise the translation table internals
4286 * @bat_priv: the bat priv with all the soft interface information
4287 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004288 * Return: 0 on success or negative error number in case of failure.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004289 */
4290int batadv_tt_init(struct batadv_priv *bat_priv)
4291{
4292 int ret;
4293
Antonio Quartulli0eb015682013-10-13 02:50:20 +02004294 /* synchronized flags must be remote */
4295 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4296
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004297 ret = batadv_tt_local_init(bat_priv);
4298 if (ret < 0)
4299 return ret;
4300
4301 ret = batadv_tt_global_init(bat_priv);
4302 if (ret < 0)
4303 return ret;
4304
4305 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08004306 batadv_tt_tvlv_unicast_handler_v1,
4307 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004308
Marek Lindner122edaa2013-04-23 21:40:03 +08004309 batadv_tvlv_handler_register(bat_priv, NULL,
4310 batadv_roam_tvlv_unicast_handler_v1,
4311 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4312
Marek Lindnere1bf0c12013-04-23 21:40:01 +08004313 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4314 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4315 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4316
4317 return 1;
4318}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004319
4320/**
4321 * batadv_tt_global_is_isolated - check if a client is marked as isolated
4322 * @bat_priv: the bat priv with all the soft interface information
4323 * @addr: the mac address of the client
4324 * @vid: the identifier of the VLAN where this client is connected
4325 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02004326 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004327 * otherwise
4328 */
4329bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02004330 const u8 *addr, unsigned short vid)
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004331{
4332 struct batadv_tt_global_entry *tt;
4333 bool ret;
4334
4335 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4336 if (!tt)
4337 return false;
4338
4339 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4340
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01004341 batadv_tt_global_entry_put(tt);
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01004342
4343 return ret;
4344}
Sven Eckelmann86452f82016-06-25 16:44:06 +02004345
4346/**
4347 * batadv_tt_cache_init - Initialize tt memory object cache
4348 *
4349 * Return: 0 on success or negative error number in case of failure.
4350 */
4351int __init batadv_tt_cache_init(void)
4352{
4353 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4354 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4355 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4356 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4357 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4358 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4359
4360 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4361 SLAB_HWCACHE_ALIGN, NULL);
4362 if (!batadv_tl_cache)
4363 return -ENOMEM;
4364
4365 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4366 SLAB_HWCACHE_ALIGN, NULL);
4367 if (!batadv_tg_cache)
4368 goto err_tt_tl_destroy;
4369
4370 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4371 tt_orig_size, 0,
4372 SLAB_HWCACHE_ALIGN, NULL);
4373 if (!batadv_tt_orig_cache)
4374 goto err_tt_tg_destroy;
4375
4376 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4377 tt_change_size, 0,
4378 SLAB_HWCACHE_ALIGN, NULL);
4379 if (!batadv_tt_change_cache)
4380 goto err_tt_orig_destroy;
4381
4382 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4383 tt_req_size, 0,
4384 SLAB_HWCACHE_ALIGN, NULL);
4385 if (!batadv_tt_req_cache)
4386 goto err_tt_change_destroy;
4387
4388 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4389 tt_roam_size, 0,
4390 SLAB_HWCACHE_ALIGN, NULL);
4391 if (!batadv_tt_roam_cache)
4392 goto err_tt_req_destroy;
4393
4394 return 0;
4395
4396err_tt_req_destroy:
4397 kmem_cache_destroy(batadv_tt_req_cache);
4398 batadv_tt_req_cache = NULL;
4399err_tt_change_destroy:
4400 kmem_cache_destroy(batadv_tt_change_cache);
4401 batadv_tt_change_cache = NULL;
4402err_tt_orig_destroy:
4403 kmem_cache_destroy(batadv_tt_orig_cache);
4404 batadv_tt_orig_cache = NULL;
4405err_tt_tg_destroy:
4406 kmem_cache_destroy(batadv_tg_cache);
4407 batadv_tg_cache = NULL;
4408err_tt_tl_destroy:
4409 kmem_cache_destroy(batadv_tl_cache);
4410 batadv_tl_cache = NULL;
4411
4412 return -ENOMEM;
4413}
4414
4415/**
4416 * batadv_tt_cache_destroy - Destroy tt memory object cache
4417 */
4418void batadv_tt_cache_destroy(void)
4419{
4420 kmem_cache_destroy(batadv_tl_cache);
4421 kmem_cache_destroy(batadv_tg_cache);
4422 kmem_cache_destroy(batadv_tt_orig_cache);
4423 kmem_cache_destroy(batadv_tt_change_cache);
4424 kmem_cache_destroy(batadv_tt_req_cache);
4425 kmem_cache_destroy(batadv_tt_roam_cache);
4426}