blob: c1eb7b72ab15fb3a09bf3dc236dc7c83043d51d9 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "translation-table.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
Linus Lüssingac4eebd2015-06-16 17:10:24 +020022#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/bug.h>
24#include <linux/byteorder/generic.h>
25#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020026#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/string.h>
44#include <linux/workqueue.h>
45#include <net/net_namespace.h>
46
47#include "bridge_loop_avoidance.h"
48#include "hard-interface.h"
49#include "hash.h"
50#include "multicast.h"
51#include "originator.h"
52#include "packet.h"
53#include "soft-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020054
Antonio Quartullidec05072012-11-10 11:00:32 +010055/* hash class keys */
56static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57static struct lock_class_key batadv_tt_global_hash_lock_class_key;
58
Sven Eckelmann56303d32012-06-05 22:31:31 +020059static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020060 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020061 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020062static void batadv_tt_purge(struct work_struct *work);
63static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020064batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020065static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020068 unsigned short vid, const char *message,
69 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070
Marek Lindner7aadf882011-02-18 12:28:09 +000071/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020072static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000073{
Sven Eckelmann56303d32012-06-05 22:31:31 +020074 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020075 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000076
dingtianhong323813e2013-12-26 19:40:39 +080077 return batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000078}
79
Antonio Quartullic018ad32013-06-04 12:11:39 +020080/**
81 * batadv_choose_tt - return the index of the tt entry in the hash table
82 * @data: pointer to the tt_common_entry object to map
83 * @size: the size of the hash table
84 *
85 * Returns the hash index where the object represented by 'data' should be
86 * stored at.
87 */
88static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
89{
90 struct batadv_tt_common_entry *tt;
91 uint32_t hash = 0;
92
93 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010094 hash = jhash(&tt->addr, ETH_ALEN, hash);
95 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +020096
97 return hash % size;
98}
99
100/**
101 * batadv_tt_hash_find - look for a client in the given hash table
102 * @hash: the hash table to search
103 * @addr: the mac address of the client to look for
104 * @vid: VLAN identifier
105 *
106 * Returns a pointer to the tt_common struct belonging to the searched client if
107 * found, NULL otherwise.
108 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200110batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
111 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000112{
Marek Lindner7aadf882011-02-18 12:28:09 +0000113 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200114 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200115 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000116
117 if (!hash)
118 return NULL;
119
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100120 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200121 to_search.vid = vid;
122
123 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000124 head = &hash->table[index];
125
126 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200127 hlist_for_each_entry_rcu(tt, head, hash_entry) {
128 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000129 continue;
130
Antonio Quartullic018ad32013-06-04 12:11:39 +0200131 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200132 continue;
133
Antonio Quartullic018ad32013-06-04 12:11:39 +0200134 if (!atomic_inc_not_zero(&tt->refcount))
135 continue;
136
137 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000138 break;
139 }
140 rcu_read_unlock();
141
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100143}
144
Antonio Quartullic018ad32013-06-04 12:11:39 +0200145/**
146 * batadv_tt_local_hash_find - search the local table for a given client
147 * @bat_priv: the bat priv with all the soft interface information
148 * @addr: the mac address of the client to look for
149 * @vid: VLAN identifier
150 *
151 * Returns a pointer to the corresponding tt_local_entry struct if the client is
152 * found, NULL otherwise.
153 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200154static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200155batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
156 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100157{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200158 struct batadv_tt_common_entry *tt_common_entry;
159 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100160
Antonio Quartullic018ad32013-06-04 12:11:39 +0200161 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
162 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100163 if (tt_common_entry)
164 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165 struct batadv_tt_local_entry,
166 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100167 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000168}
169
Antonio Quartullic018ad32013-06-04 12:11:39 +0200170/**
171 * batadv_tt_global_hash_find - search the global table for a given client
172 * @bat_priv: the bat priv with all the soft interface information
173 * @addr: the mac address of the client to look for
174 * @vid: VLAN identifier
175 *
176 * Returns a pointer to the corresponding tt_global_entry struct if the client
177 * is found, NULL otherwise.
178 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200179static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200180batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
181 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000182{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200183 struct batadv_tt_common_entry *tt_common_entry;
184 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000185
Antonio Quartullic018ad32013-06-04 12:11:39 +0200186 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
187 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100188 if (tt_common_entry)
189 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200190 struct batadv_tt_global_entry,
191 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100192 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000193}
194
Sven Eckelmanna5130882012-05-16 20:23:16 +0200195static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200196batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200197{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100198 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
199 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200200}
201
Antonio Quartulli21026052013-05-07 00:29:22 +0200202/**
203 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
204 * tt_global_entry and possibly free it
205 * @tt_global_entry: the object to free
206 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200207static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200208batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200209{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200210 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200211 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200212 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200213 }
214}
215
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100216/**
217 * batadv_tt_global_hash_count - count the number of orig entries
218 * @hash: hash table containing the tt entries
219 * @addr: the mac address of the client to count entries for
220 * @vid: VLAN identifier
221 *
222 * Return the number of originators advertising the given address/data
223 * (excluding ourself).
224 */
225int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
226 const uint8_t *addr, unsigned short vid)
227{
228 struct batadv_tt_global_entry *tt_global_entry;
229 int count;
230
231 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
232 if (!tt_global_entry)
233 return 0;
234
235 count = atomic_read(&tt_global_entry->orig_list_count);
236 batadv_tt_global_entry_free_ref(tt_global_entry);
237
238 return count;
239}
240
Sven Eckelmanna5130882012-05-16 20:23:16 +0200241static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200242{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200243 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200244
Sven Eckelmann56303d32012-06-05 22:31:31 +0200245 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800246
247 /* We are in an rcu callback here, therefore we cannot use
248 * batadv_orig_node_free_ref() and its call_rcu():
249 * An rcu_barrier() wouldn't wait for that to finish
250 */
251 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200252 kfree(orig_entry);
253}
254
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200255/**
256 * batadv_tt_local_size_mod - change the size by v of the local table identified
257 * by vid
258 * @bat_priv: the bat priv with all the soft interface information
259 * @vid: the VLAN identifier of the sub-table to change
260 * @v: the amount to sum to the local table size
261 */
262static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
263 unsigned short vid, int v)
264{
265 struct batadv_softif_vlan *vlan;
266
267 vlan = batadv_softif_vlan_get(bat_priv, vid);
268 if (!vlan)
269 return;
270
271 atomic_add(v, &vlan->tt.num_entries);
272
273 batadv_softif_vlan_free_ref(vlan);
274}
275
276/**
277 * batadv_tt_local_size_inc - increase by one the local table size for the given
278 * vid
279 * @bat_priv: the bat priv with all the soft interface information
280 * @vid: the VLAN identifier
281 */
282static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
283 unsigned short vid)
284{
285 batadv_tt_local_size_mod(bat_priv, vid, 1);
286}
287
288/**
289 * batadv_tt_local_size_dec - decrease by one the local table size for the given
290 * vid
291 * @bat_priv: the bat priv with all the soft interface information
292 * @vid: the VLAN identifier
293 */
294static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
295 unsigned short vid)
296{
297 batadv_tt_local_size_mod(bat_priv, vid, -1);
298}
299
300/**
301 * batadv_tt_global_size_mod - change the size by v of the local table
302 * identified by vid
303 * @bat_priv: the bat priv with all the soft interface information
304 * @vid: the VLAN identifier
305 * @v: the amount to sum to the global table size
306 */
307static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
308 unsigned short vid, int v)
309{
310 struct batadv_orig_node_vlan *vlan;
311
312 vlan = batadv_orig_node_vlan_new(orig_node, vid);
313 if (!vlan)
314 return;
315
316 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
317 spin_lock_bh(&orig_node->vlan_list_lock);
318 list_del_rcu(&vlan->list);
319 spin_unlock_bh(&orig_node->vlan_list_lock);
320 batadv_orig_node_vlan_free_ref(vlan);
321 }
322
323 batadv_orig_node_vlan_free_ref(vlan);
324}
325
326/**
327 * batadv_tt_global_size_inc - increase by one the global table size for the
328 * given vid
329 * @orig_node: the originator which global table size has to be decreased
330 * @vid: the vlan identifier
331 */
332static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
333 unsigned short vid)
334{
335 batadv_tt_global_size_mod(orig_node, vid, 1);
336}
337
338/**
339 * batadv_tt_global_size_dec - decrease by one the global table size for the
340 * given vid
341 * @orig_node: the originator which global table size has to be decreased
342 * @vid: the vlan identifier
343 */
344static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
345 unsigned short vid)
346{
347 batadv_tt_global_size_mod(orig_node, vid, -1);
348}
349
Sven Eckelmanna5130882012-05-16 20:23:16 +0200350static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200351batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200352{
Antonio Quartullid657e622012-07-01 14:09:12 +0200353 if (!atomic_dec_and_test(&orig_entry->refcount))
354 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200355
Sven Eckelmanna5130882012-05-16 20:23:16 +0200356 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200357}
358
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200359/**
360 * batadv_tt_local_event - store a local TT event (ADD/DEL)
361 * @bat_priv: the bat priv with all the soft interface information
362 * @tt_local_entry: the TT entry involved in the event
363 * @event_flags: flags to store in the event structure
364 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200365static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200366 struct batadv_tt_local_entry *tt_local_entry,
367 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200368{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200369 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200370 struct batadv_tt_common_entry *common = &tt_local_entry->common;
371 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200372 bool event_removed = false;
373 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200374
375 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200376 if (!tt_change_node)
377 return;
378
Antonio Quartulliff66c972011-06-30 01:14:00 +0200379 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100380 memset(tt_change_node->change.reserved, 0,
381 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100382 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200383 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200384
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200385 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200386
387 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200388 spin_lock_bh(&bat_priv->tt.changes_list_lock);
389 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200390 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200391 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200392 continue;
393
394 /* DEL+ADD in the same orig interval have no effect and can be
395 * removed to avoid silly behaviour on the receiver side. The
396 * other way around (ADD+DEL) can happen in case of roaming of
397 * a client still in the NEW state. Roaming of NEW clients is
398 * now possible due to automatically recognition of "temporary"
399 * clients
400 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200401 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200402 if (!del_op_requested && del_op_entry)
403 goto del;
404 if (del_op_requested && !del_op_entry)
405 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200406
407 /* this is a second add in the same originator interval. It
408 * means that flags have been changed: update them!
409 */
410 if (!del_op_requested && !del_op_entry)
411 entry->change.flags = flags;
412
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200413 continue;
414del:
415 list_del(&entry->list);
416 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000417 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200418 event_removed = true;
419 goto unlock;
420 }
421
Antonio Quartullia73105b2011-04-27 14:27:44 +0200422 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200423 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200424
425unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200426 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200427
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200428 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200430 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200431 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432}
433
Marek Lindner335fbe02013-04-23 21:40:02 +0800434/**
435 * batadv_tt_len - compute length in bytes of given number of tt changes
436 * @changes_num: number of tt changes
437 *
438 * Returns computed length in bytes.
439 */
440static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441{
Marek Lindner335fbe02013-04-23 21:40:02 +0800442 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443}
444
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200445/**
446 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
447 * @tt_len: available space
448 *
449 * Returns the number of entries.
450 */
451static uint16_t batadv_tt_entries(uint16_t tt_len)
452{
453 return tt_len / batadv_tt_len(1);
454}
455
Marek Lindnera19d3d82013-05-27 15:33:25 +0800456/**
457 * batadv_tt_local_table_transmit_size - calculates the local translation table
458 * size when transmitted over the air
459 * @bat_priv: the bat priv with all the soft interface information
460 *
461 * Returns local translation table size in bytes.
462 */
463static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
464{
465 uint16_t num_vlan = 0, tt_local_entries = 0;
466 struct batadv_softif_vlan *vlan;
467 int hdr_size;
468
469 rcu_read_lock();
470 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
471 num_vlan++;
472 tt_local_entries += atomic_read(&vlan->tt.num_entries);
473 }
474 rcu_read_unlock();
475
476 /* header size of tvlv encapsulated tt response payload */
477 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
478 hdr_size += sizeof(struct batadv_tvlv_hdr);
479 hdr_size += sizeof(struct batadv_tvlv_tt_data);
480 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
481
482 return hdr_size + batadv_tt_len(tt_local_entries);
483}
484
Sven Eckelmann56303d32012-06-05 22:31:31 +0200485static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200487 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200488 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmann807736f2012-07-15 22:26:51 +0200490 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491
Sven Eckelmann807736f2012-07-15 22:26:51 +0200492 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200493 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494
Antonio Quartullidec05072012-11-10 11:00:32 +0100495 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
496 &batadv_tt_local_hash_lock_class_key);
497
Sven Eckelmann5346c352012-05-05 13:27:28 +0200498 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499}
500
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200501static void batadv_tt_global_free(struct batadv_priv *bat_priv,
502 struct batadv_tt_global_entry *tt_global,
503 const char *message)
504{
505 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200506 "Deleting global tt entry %pM (vid: %d): %s\n",
507 tt_global->common.addr,
508 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200509
510 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200511 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200512 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200513}
514
Antonio Quartullic018ad32013-06-04 12:11:39 +0200515/**
516 * batadv_tt_local_add - add a new client to the local table or update an
517 * existing client
518 * @soft_iface: netdev struct of the mesh interface
519 * @addr: the mac address of the client to add
520 * @vid: VLAN identifier
521 * @ifindex: index of the interface where the client is connected to (useful to
522 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100523 * @mark: the value contained in the skb->mark field of the received packet (if
524 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800525 *
526 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200527 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800528bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartulli9464d072013-11-16 12:03:48 +0100529 unsigned short vid, int ifindex, uint32_t mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200532 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100533 struct batadv_tt_global_entry *tt_global = NULL;
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200534 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200535 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200536 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200537 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800538 int hash_added, table_size, packet_size_max;
539 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200540 uint8_t remote_flags;
Antonio Quartulli9464d072013-11-16 12:03:48 +0100541 uint32_t match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200543 if (ifindex != BATADV_NULL_IFINDEX)
544 in_dev = dev_get_by_index(&init_net, ifindex);
545
Antonio Quartullic018ad32013-06-04 12:11:39 +0200546 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100547
548 if (!is_multicast_ether_addr(addr))
549 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Antonio Quartulli47c94652012-09-23 22:38:35 +0200551 if (tt_local) {
552 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200553 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
554 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200555 "Re-adding pending client %pM (vid: %d)\n",
556 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200557 /* whatever the reason why the PENDING flag was set,
558 * this is a client which was enqueued to be removed in
559 * this orig_interval. Since it popped up again, the
560 * flag can be reset like it was never enqueued
561 */
562 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
563 goto add_event;
564 }
565
566 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
567 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200568 "Roaming client %pM (vid: %d) came back to its original location\n",
569 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200570 /* the ROAM flag is set because this client roamed away
571 * and the node got a roaming_advertisement message. Now
572 * that the client popped up again at its original
573 * location such flag can be unset
574 */
575 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
576 roamed_back = true;
577 }
578 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 }
580
Marek Lindnera19d3d82013-05-27 15:33:25 +0800581 /* Ignore the client if we cannot send it in a full table response. */
582 table_size = batadv_tt_local_table_transmit_size(bat_priv);
583 table_size += batadv_tt_len(1);
584 packet_size_max = atomic_read(&bat_priv->packet_size_max);
585 if (table_size > packet_size_max) {
586 net_ratelimited_function(batadv_info, soft_iface,
587 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
588 table_size, packet_size_max, addr);
589 goto out;
590 }
591
Antonio Quartulli47c94652012-09-23 22:38:35 +0200592 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
593 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200594 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200595
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200596 /* increase the refcounter of the related vlan */
597 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +0800598 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200599 addr, BATADV_PRINT_VID(vid))) {
600 kfree(tt_local);
601 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800602 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200603 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200604
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200605 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200606 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
607 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200608 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100610 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100611 /* The local entry has to be marked as NEW to avoid to send it in
612 * a full table response going out before the next ttvn increment
613 * (consistency check)
614 */
615 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200616 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200617 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200618 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
619 atomic_set(&tt_local->common.refcount, 2);
620 tt_local->last_seen = jiffies;
621 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100623 /* the batman interface mac and multicast addresses should never be
624 * purged
625 */
626 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
627 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200628 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629
Sven Eckelmann807736f2012-07-15 22:26:51 +0200630 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200631 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200632 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100633
634 if (unlikely(hash_added != 0)) {
635 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200636 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200637 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100638 goto out;
639 }
640
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200641add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200642 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200643
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200644check_roaming:
645 /* Check whether it is a roaming, but don't do anything if the roaming
646 * process has already been handled
647 */
648 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200649 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200650 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200651 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800652 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200653 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200654 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200655 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200656 }
657 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200658 if (roamed_back) {
659 batadv_tt_global_free(bat_priv, tt_global,
660 "Roaming canceled");
661 tt_global = NULL;
662 } else {
663 /* The global entry has to be marked as ROAMING and
664 * has to be kept for consistency purpose
665 */
666 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
667 tt_global->roam_at = jiffies;
668 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200669 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200670
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200671 /* store the current remote flags before altering them. This helps
672 * understanding is flags are changing or not
673 */
674 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800675
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200676 if (batadv_is_wifi_netdev(in_dev))
677 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
678 else
679 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
680
Antonio Quartulli9464d072013-11-16 12:03:48 +0100681 /* check the mark in the skb: if it's equal to the configured
682 * isolation_mark, it means the packet is coming from an isolated
683 * non-mesh client
684 */
685 match_mark = (mark & bat_priv->isolation_mark_mask);
686 if (bat_priv->isolation_mark_mask &&
687 match_mark == bat_priv->isolation_mark)
688 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
689 else
690 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
691
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200692 /* if any "dynamic" flag has been modified, resend an ADD event for this
693 * entry so that all the nodes can get the new flags
694 */
695 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
696 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
697
698 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200699out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200700 if (in_dev)
701 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200702 if (tt_local)
703 batadv_tt_local_entry_free_ref(tt_local);
704 if (tt_global)
705 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800706 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707}
708
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800709/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200710 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
711 * within a TT Response directed to another node
712 * @orig_node: originator for which the TT data has to be prepared
713 * @tt_data: uninitialised pointer to the address of the TVLV buffer
714 * @tt_change: uninitialised pointer to the address of the area where the TT
715 * changed can be stored
716 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
717 * function reserves the amount of space needed to send the entire global TT
718 * table. In case of success the value is updated with the real amount of
719 * reserved bytes
720
721 * Allocate the needed amount of memory for the entire TT TVLV and write its
722 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
723 * objects, one per active VLAN served by the originator node.
724 *
725 * Return the size of the allocated buffer or 0 in case of failure.
726 */
727static uint16_t
728batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
729 struct batadv_tvlv_tt_data **tt_data,
730 struct batadv_tvlv_tt_change **tt_change,
731 int32_t *tt_len)
732{
733 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
734 struct batadv_tvlv_tt_vlan_data *tt_vlan;
735 struct batadv_orig_node_vlan *vlan;
736 uint8_t *tt_change_ptr;
737
738 rcu_read_lock();
739 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
740 num_vlan++;
741 num_entries += atomic_read(&vlan->tt.num_entries);
742 }
743
744 change_offset = sizeof(**tt_data);
745 change_offset += num_vlan * sizeof(*tt_vlan);
746
747 /* if tt_len is negative, allocate the space needed by the full table */
748 if (*tt_len < 0)
749 *tt_len = batadv_tt_len(num_entries);
750
751 tvlv_len = *tt_len;
752 tvlv_len += change_offset;
753
754 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
755 if (!*tt_data) {
756 *tt_len = 0;
757 goto out;
758 }
759
760 (*tt_data)->flags = BATADV_NO_FLAGS;
761 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
762 (*tt_data)->num_vlan = htons(num_vlan);
763
764 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
765 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
766 tt_vlan->vid = htons(vlan->vid);
767 tt_vlan->crc = htonl(vlan->tt.crc);
768
769 tt_vlan++;
770 }
771
772 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
773 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
774
775out:
776 rcu_read_unlock();
777 return tvlv_len;
778}
779
780/**
781 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
782 * node
783 * @bat_priv: the bat priv with all the soft interface information
784 * @tt_data: uninitialised pointer to the address of the TVLV buffer
785 * @tt_change: uninitialised pointer to the address of the area where the TT
786 * changes can be stored
787 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
788 * function reserves the amount of space needed to send the entire local TT
789 * table. In case of success the value is updated with the real amount of
790 * reserved bytes
791 *
792 * Allocate the needed amount of memory for the entire TT TVLV and write its
793 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
794 * objects, one per active VLAN.
795 *
796 * Return the size of the allocated buffer or 0 in case of failure.
797 */
798static uint16_t
799batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
800 struct batadv_tvlv_tt_data **tt_data,
801 struct batadv_tvlv_tt_change **tt_change,
802 int32_t *tt_len)
803{
804 struct batadv_tvlv_tt_vlan_data *tt_vlan;
805 struct batadv_softif_vlan *vlan;
806 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
807 uint8_t *tt_change_ptr;
808 int change_offset;
809
810 rcu_read_lock();
811 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
812 num_vlan++;
813 num_entries += atomic_read(&vlan->tt.num_entries);
814 }
815
816 change_offset = sizeof(**tt_data);
817 change_offset += num_vlan * sizeof(*tt_vlan);
818
819 /* if tt_len is negative, allocate the space needed by the full table */
820 if (*tt_len < 0)
821 *tt_len = batadv_tt_len(num_entries);
822
823 tvlv_len = *tt_len;
824 tvlv_len += change_offset;
825
826 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
827 if (!*tt_data) {
828 tvlv_len = 0;
829 goto out;
830 }
831
832 (*tt_data)->flags = BATADV_NO_FLAGS;
833 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
834 (*tt_data)->num_vlan = htons(num_vlan);
835
836 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
837 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
838 tt_vlan->vid = htons(vlan->vid);
839 tt_vlan->crc = htonl(vlan->tt.crc);
840
841 tt_vlan++;
842 }
843
844 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
845 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
846
847out:
848 rcu_read_unlock();
849 return tvlv_len;
850}
851
852/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800853 * batadv_tt_tvlv_container_update - update the translation table tvlv container
854 * after local tt changes have been committed
855 * @bat_priv: the bat priv with all the soft interface information
856 */
857static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800859 struct batadv_tt_change_node *entry, *safe;
860 struct batadv_tvlv_tt_data *tt_data;
861 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200862 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800863 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200864 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200866 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
867 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800868
869 /* if we have too many changes for one packet don't send any
870 * and wait for the tt table request which will be fragmented
871 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800872 if (tt_diff_len > bat_priv->soft_iface->mtu)
873 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800874
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200875 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
876 &tt_change, &tt_diff_len);
877 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800878 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800879
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800880 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800881
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800882 if (tt_diff_len == 0)
883 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800884
Sven Eckelmann807736f2012-07-15 22:26:51 +0200885 spin_lock_bh(&bat_priv->tt.changes_list_lock);
886 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887
Sven Eckelmann807736f2012-07-15 22:26:51 +0200888 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100889 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800890 if (tt_diff_entries_count < tt_diff_entries_num) {
891 memcpy(tt_change + tt_diff_entries_count,
892 &entry->change,
893 sizeof(struct batadv_tvlv_tt_change));
894 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200896 list_del(&entry->list);
897 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000898 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200899 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900
Antonio Quartullia73105b2011-04-27 14:27:44 +0200901 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200902 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
903 kfree(bat_priv->tt.last_changeset);
904 bat_priv->tt.last_changeset_len = 0;
905 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800906 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800907 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800908 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800909 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200910 * instead of providing the diff
911 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800912 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200913 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800914 memcpy(bat_priv->tt.last_changeset,
915 tt_change, tt_change_len);
916 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200917 }
918 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200919 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800921container_register:
922 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200923 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800924 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000925}
926
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200927int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928{
929 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200930 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200931 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100933 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200934 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200935 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000936 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200937 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200938 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100939 int last_seen_secs;
940 int last_seen_msecs;
941 unsigned long last_seen_jiffies;
942 bool no_purge;
943 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000944
Marek Lindner30da63a2012-08-03 17:15:46 +0200945 primary_if = batadv_seq_print_text_primary_if_get(seq);
946 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200947 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000948
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100949 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200950 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
951 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100952 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200953 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000954
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000955 for (i = 0; i < hash->size; i++) {
956 head = &hash->table[i];
957
Marek Lindner7aadf882011-02-18 12:28:09 +0000958 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800959 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000960 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100961 tt_local = container_of(tt_common_entry,
962 struct batadv_tt_local_entry,
963 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200964 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100965 last_seen_jiffies = jiffies - tt_local->last_seen;
966 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
967 last_seen_secs = last_seen_msecs / 1000;
968 last_seen_msecs = last_seen_msecs % 1000;
969
970 no_purge = tt_common_entry->flags & np_flag;
971
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200972 vlan = batadv_softif_vlan_get(bat_priv, vid);
973 if (!vlan) {
974 seq_printf(seq, "Cannot retrieve VLAN %d\n",
975 BATADV_PRINT_VID(vid));
976 continue;
977 }
978
979 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100980 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100981 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200982 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200983 ((tt_common_entry->flags &
984 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100985 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200986 ((tt_common_entry->flags &
987 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
988 ((tt_common_entry->flags &
989 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
990 ((tt_common_entry->flags &
991 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
992 ((tt_common_entry->flags &
993 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100994 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200995 no_purge ? 0 : last_seen_msecs,
996 vlan->tt.crc);
997
998 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001000 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001002out:
1003 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001004 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001005 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006}
1007
Sven Eckelmann56303d32012-06-05 22:31:31 +02001008static void
1009batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1010 struct batadv_tt_local_entry *tt_local_entry,
1011 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001013 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Antonio Quartulli015758d2011-07-09 17:52:13 +02001015 /* The local client has to be marked as "pending to be removed" but has
1016 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001017 * response issued before the net ttvn increment (consistency check)
1018 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001019 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001020
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001021 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001022 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1023 tt_local_entry->common.addr,
1024 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025}
1026
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001027/**
1028 * batadv_tt_local_remove - logically remove an entry from the local table
1029 * @bat_priv: the bat priv with all the soft interface information
1030 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001031 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001032 * @message: message to append to the log on deletion
1033 * @roaming: true if the deletion is due to a roaming event
1034 *
1035 * Returns the flags assigned to the local entry before being deleted
1036 */
1037uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001038 const uint8_t *addr, unsigned short vid,
1039 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001041 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001042 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001043 struct batadv_softif_vlan *vlan;
Marek Lindneref727062015-06-17 20:01:36 +08001044 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001045
Antonio Quartullic018ad32013-06-04 12:11:39 +02001046 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001047 if (!tt_local_entry)
1048 goto out;
1049
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001050 curr_flags = tt_local_entry->common.flags;
1051
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001052 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001053 /* if this global entry addition is due to a roaming, the node has to
1054 * mark the local entry as "roamed" in order to correctly reroute
1055 * packets later
1056 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001057 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001058 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001059 /* mark the local client as ROAMed */
1060 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1061 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001062
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001063 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1064 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1065 message);
1066 goto out;
1067 }
1068 /* if this client has been added right now, it is possible to
1069 * immediately purge it
1070 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001071 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001072
1073 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1074 batadv_compare_tt,
1075 batadv_choose_tt,
1076 &tt_local_entry->common);
1077 if (!tt_entry_exists)
1078 goto out;
1079
1080 /* extra call to free the local tt entry */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001081 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001082
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001083 /* decrease the reference held for this vlan */
1084 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001085 if (!vlan)
1086 goto out;
1087
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001088 batadv_softif_vlan_free_ref(vlan);
1089 batadv_softif_vlan_free_ref(vlan);
1090
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001091out:
1092 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001093 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001094
1095 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001096}
1097
Marek Lindnera19d3d82013-05-27 15:33:25 +08001098/**
1099 * batadv_tt_local_purge_list - purge inactive tt local entries
1100 * @bat_priv: the bat priv with all the soft interface information
1101 * @head: pointer to the list containing the local tt entries
1102 * @timeout: parameter deciding whether a given tt local entry is considered
1103 * inactive or not
1104 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001105static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001106 struct hlist_head *head,
1107 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001109 struct batadv_tt_local_entry *tt_local_entry;
1110 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001111 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001112
Sasha Levinb67bfe02013-02-27 17:06:00 -08001113 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001114 hash_entry) {
1115 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001116 struct batadv_tt_local_entry,
1117 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001118 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1119 continue;
1120
1121 /* entry already marked for deletion */
1122 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1123 continue;
1124
Marek Lindnera19d3d82013-05-27 15:33:25 +08001125 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001126 continue;
1127
1128 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1129 BATADV_TT_CLIENT_DEL, "timed out");
1130 }
1131}
1132
Marek Lindnera19d3d82013-05-27 15:33:25 +08001133/**
1134 * batadv_tt_local_purge - purge inactive tt local entries
1135 * @bat_priv: the bat priv with all the soft interface information
1136 * @timeout: parameter deciding whether a given tt local entry is considered
1137 * inactive or not
1138 */
1139static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1140 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001141{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001142 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001143 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001144 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001145 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147 for (i = 0; i < hash->size; i++) {
1148 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001149 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001150
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001151 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001152 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001153 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001155}
1156
Sven Eckelmann56303d32012-06-05 22:31:31 +02001157static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001158{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001159 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001160 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001161 struct batadv_tt_common_entry *tt_common_entry;
1162 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001163 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001164 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001165 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001166 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167
Sven Eckelmann807736f2012-07-15 22:26:51 +02001168 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 return;
1170
Sven Eckelmann807736f2012-07-15 22:26:51 +02001171 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001172
1173 for (i = 0; i < hash->size; i++) {
1174 head = &hash->table[i];
1175 list_lock = &hash->list_locks[i];
1176
1177 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001178 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001179 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001180 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181 tt_local = container_of(tt_common_entry,
1182 struct batadv_tt_local_entry,
1183 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001184
1185 /* decrease the reference held for this vlan */
1186 vlan = batadv_softif_vlan_get(bat_priv,
1187 tt_common_entry->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001188 if (vlan) {
1189 batadv_softif_vlan_free_ref(vlan);
1190 batadv_softif_vlan_free_ref(vlan);
1191 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001192
Sven Eckelmann56303d32012-06-05 22:31:31 +02001193 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194 }
1195 spin_unlock_bh(list_lock);
1196 }
1197
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001198 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001199
Sven Eckelmann807736f2012-07-15 22:26:51 +02001200 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001201}
1202
Sven Eckelmann56303d32012-06-05 22:31:31 +02001203static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001204{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001205 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001206 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207
Sven Eckelmann807736f2012-07-15 22:26:51 +02001208 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001209
Sven Eckelmann807736f2012-07-15 22:26:51 +02001210 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001211 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001212
Antonio Quartullidec05072012-11-10 11:00:32 +01001213 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1214 &batadv_tt_global_hash_lock_class_key);
1215
Sven Eckelmann5346c352012-05-05 13:27:28 +02001216 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001217}
1218
Sven Eckelmann56303d32012-06-05 22:31:31 +02001219static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001220{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001221 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001222
Sven Eckelmann807736f2012-07-15 22:26:51 +02001223 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001224
Sven Eckelmann807736f2012-07-15 22:26:51 +02001225 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226 list) {
1227 list_del(&entry->list);
1228 kfree(entry);
1229 }
1230
Sven Eckelmann807736f2012-07-15 22:26:51 +02001231 atomic_set(&bat_priv->tt.local_changes, 0);
1232 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001233}
1234
Antonio Quartullid657e622012-07-01 14:09:12 +02001235/* retrieves the orig_tt_list_entry belonging to orig_node from the
1236 * batadv_tt_global_entry list
1237 *
1238 * returns it with an increased refcounter, NULL if not found
1239 */
1240static struct batadv_tt_orig_list_entry *
1241batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1242 const struct batadv_orig_node *orig_node)
1243{
1244 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1245 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001246
1247 rcu_read_lock();
1248 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001249 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001250 if (tmp_orig_entry->orig_node != orig_node)
1251 continue;
1252 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1253 continue;
1254
1255 orig_entry = tmp_orig_entry;
1256 break;
1257 }
1258 rcu_read_unlock();
1259
1260 return orig_entry;
1261}
1262
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001263/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001264 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001265 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001266static bool
1267batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1268 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001269{
Antonio Quartullid657e622012-07-01 14:09:12 +02001270 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001271 bool found = false;
1272
Antonio Quartullid657e622012-07-01 14:09:12 +02001273 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1274 if (orig_entry) {
1275 found = true;
1276 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001278
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001279 return found;
1280}
1281
Sven Eckelmanna5130882012-05-16 20:23:16 +02001282static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001283batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001284 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001285{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001286 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001287
Antonio Quartullid657e622012-07-01 14:09:12 +02001288 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001289 if (orig_entry) {
1290 /* refresh the ttvn: the current value could be a bogus one that
1291 * was added during a "temporary client detection"
1292 */
1293 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001294 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001295 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001296
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001297 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1298 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001299 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001300
1301 INIT_HLIST_NODE(&orig_entry->list);
1302 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001303 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001304 orig_entry->orig_node = orig_node;
1305 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001306 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001307
Antonio Quartullid657e622012-07-01 14:09:12 +02001308 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001309 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001310 &tt_global->orig_list);
1311 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001312 atomic_inc(&tt_global->orig_list_count);
1313
Antonio Quartullid657e622012-07-01 14:09:12 +02001314out:
1315 if (orig_entry)
1316 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001317}
1318
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001319/**
1320 * batadv_tt_global_add - add a new TT global entry or update an existing one
1321 * @bat_priv: the bat priv with all the soft interface information
1322 * @orig_node: the originator announcing the client
1323 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001324 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001325 * @flags: TT flags that have to be set for this non-mesh client
1326 * @ttvn: the tt version number ever announcing this non-mesh client
1327 *
1328 * Add a new TT global entry for the given originator. If the entry already
1329 * exists add a new reference to the given originator (a global entry can have
1330 * references to multiple originators) and adjust the flags attribute to reflect
1331 * the function argument.
1332 * If a TT local entry exists for this non-mesh client remove it.
1333 *
1334 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001335 *
1336 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001337 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001338static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1339 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001340 const unsigned char *tt_addr,
1341 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001342 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001343{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001344 struct batadv_tt_global_entry *tt_global_entry;
1345 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001346 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001347 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001348 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001349 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001351 /* ignore global entries from backbone nodes */
1352 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1353 return true;
1354
Antonio Quartullic018ad32013-06-04 12:11:39 +02001355 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1356 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001357
1358 /* if the node already has a local client for this entry, it has to wait
1359 * for a roaming advertisement instead of manually messing up the global
1360 * table
1361 */
1362 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1363 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1364 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365
Antonio Quartullia73105b2011-04-27 14:27:44 +02001366 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001367 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001368 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001369 goto out;
1370
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001371 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001372 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001373 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001374
Antonio Quartullid4f44692012-05-25 00:00:54 +02001375 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001376 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001377 /* node must store current time in case of roaming. This is
1378 * needed to purge this entry out on timeout (if nobody claims
1379 * it)
1380 */
1381 if (flags & BATADV_TT_CLIENT_ROAM)
1382 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001383 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001384 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001385
1386 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001387 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001388 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001389
Sven Eckelmann807736f2012-07-15 22:26:51 +02001390 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001391 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001392 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001393 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001394
1395 if (unlikely(hash_added != 0)) {
1396 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001397 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001398 goto out_remove;
1399 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001401 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001402 /* If there is already a global entry, we can use this one for
1403 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001404 * But if we are trying to add a temporary client then here are
1405 * two options at this point:
1406 * 1) the global client is not a temporary client: the global
1407 * client has to be left as it is, temporary information
1408 * should never override any already known client state
1409 * 2) the global client is a temporary client: purge the
1410 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001411 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001412 if (flags & BATADV_TT_CLIENT_TEMP) {
1413 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1414 goto out;
1415 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1416 orig_node))
1417 goto out_remove;
1418 batadv_tt_global_del_orig_list(tt_global_entry);
1419 goto add_orig_entry;
1420 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001421
1422 /* if the client was temporary added before receiving the first
1423 * OGM announcing it, we have to clear the TEMP flag
1424 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001425 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001426
Antonio Quartullie9c001362012-11-07 15:05:33 +01001427 /* the change can carry possible "attribute" flags like the
1428 * TT_CLIENT_WIFI, therefore they have to be copied in the
1429 * client entry
1430 */
1431 tt_global_entry->common.flags |= flags;
1432
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001433 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1434 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001435 * delete + roaming change for this originator.
1436 *
1437 * We should first delete the old originator before adding the
1438 * new one.
1439 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001440 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001441 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001442 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001443 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001444 }
1445 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001446add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001447 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001448 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001449
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001450 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001451 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1452 common->addr, BATADV_PRINT_VID(common->vid),
1453 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001454 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001456out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001457 /* Do not remove multicast addresses from the local hash on
1458 * global additions
1459 */
1460 if (is_multicast_ether_addr(tt_addr))
1461 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001462
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001464 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001465 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001466 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001467 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1468
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001469 if (!(flags & BATADV_TT_CLIENT_ROAM))
1470 /* this is a normal global add. Therefore the client is not in a
1471 * roaming state anymore.
1472 */
1473 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1474
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001475out:
1476 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001477 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001478 if (tt_local_entry)
1479 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001480 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001481}
1482
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001483/**
1484 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001485 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001486 * @tt_global_entry: global translation table entry to be analyzed
1487 *
1488 * This functon assumes the caller holds rcu_read_lock().
1489 * Returns best originator list entry or NULL on errors.
1490 */
1491static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001492batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1493 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001494{
Antonio Quartulli46274562013-09-03 11:10:24 +02001495 struct batadv_neigh_node *router, *best_router = NULL;
1496 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001497 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001498 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001499
1500 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001501 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001502 router = batadv_orig_router_get(orig_entry->orig_node,
1503 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001504 if (!router)
1505 continue;
1506
Antonio Quartulli46274562013-09-03 11:10:24 +02001507 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001508 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1509 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001510 batadv_neigh_node_free_ref(router);
1511 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001512 }
1513
Antonio Quartulli46274562013-09-03 11:10:24 +02001514 /* release the refcount for the "old" best */
1515 if (best_router)
1516 batadv_neigh_node_free_ref(best_router);
1517
1518 best_entry = orig_entry;
1519 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001520 }
1521
Antonio Quartulli46274562013-09-03 11:10:24 +02001522 if (best_router)
1523 batadv_neigh_node_free_ref(best_router);
1524
Sven Eckelmann981d8902012-10-07 13:34:15 +02001525 return best_entry;
1526}
1527
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001528/**
1529 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1530 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001531 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001532 * @tt_global_entry: global translation table entry to be printed
1533 * @seq: debugfs table seq_file struct
1534 *
1535 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001536 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001537static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001538batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1539 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001540 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001541{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001542 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001543 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001544 struct batadv_orig_node_vlan *vlan;
1545 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001546 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001547 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001548
1549 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001550 flags = tt_common_entry->flags;
1551
Antonio Quartulli46274562013-09-03 11:10:24 +02001552 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001553 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001554 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1555 tt_common_entry->vid);
1556 if (!vlan) {
1557 seq_printf(seq,
1558 " * Cannot retrieve VLAN %d for originator %pM\n",
1559 BATADV_PRINT_VID(tt_common_entry->vid),
1560 best_entry->orig_node->orig);
1561 goto print_list;
1562 }
1563
Sven Eckelmann981d8902012-10-07 13:34:15 +02001564 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001565 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001566 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001567 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001568 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001569 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001570 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001571 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1572 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1573 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1574 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001575
1576 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001577 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001578
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001579print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001580 head = &tt_global_entry->orig_list;
1581
Sasha Levinb67bfe02013-02-27 17:06:00 -08001582 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001583 if (best_entry == orig_entry)
1584 continue;
1585
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001586 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1587 tt_common_entry->vid);
1588 if (!vlan) {
1589 seq_printf(seq,
1590 " + Cannot retrieve VLAN %d for originator %pM\n",
1591 BATADV_PRINT_VID(tt_common_entry->vid),
1592 orig_entry->orig_node->orig);
1593 continue;
1594 }
1595
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001596 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001597 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001598 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001599 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001600 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001601 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001602 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001603 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1604 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1605 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1606 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001607
1608 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001609 }
1610}
1611
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001612int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001613{
1614 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001615 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001616 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001617 struct batadv_tt_common_entry *tt_common_entry;
1618 struct batadv_tt_global_entry *tt_global;
1619 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001620 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001621 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001622
Marek Lindner30da63a2012-08-03 17:15:46 +02001623 primary_if = batadv_seq_print_text_primary_if_get(seq);
1624 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001625 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001626
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001627 seq_printf(seq,
1628 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001629 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001630 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1631 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1632 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001633
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001634 for (i = 0; i < hash->size; i++) {
1635 head = &hash->table[i];
1636
Marek Lindner7aadf882011-02-18 12:28:09 +00001637 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001638 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001639 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001640 tt_global = container_of(tt_common_entry,
1641 struct batadv_tt_global_entry,
1642 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001643 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001644 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001645 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001646 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001647out:
1648 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001649 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001650 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001651}
1652
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001653/**
1654 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1655 * @tt_global_entry: the global entry to remove the orig_entry from
1656 * @orig_entry: the orig entry to remove and free
1657 *
1658 * Remove an orig_entry from its list in the given tt_global_entry and
1659 * free this orig_entry afterwards.
1660 */
1661static void
1662batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1663 struct batadv_tt_orig_list_entry *orig_entry)
1664{
1665 batadv_tt_global_size_dec(orig_entry->orig_node,
1666 tt_global_entry->common.vid);
1667 atomic_dec(&tt_global_entry->orig_list_count);
1668 hlist_del_rcu(&orig_entry->list);
1669 batadv_tt_orig_list_entry_free_ref(orig_entry);
1670}
1671
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001672/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001673static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001674batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001675{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001676 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001677 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001678 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001680 spin_lock_bh(&tt_global_entry->list_lock);
1681 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001682 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1683 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001684 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001685}
1686
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001687/**
1688 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1689 * @bat_priv: the bat priv with all the soft interface information
1690 * @tt_global_entry: the global entry to remove the orig_node from
1691 * @orig_node: the originator announcing the client
1692 * @message: message to append to the log on deletion
1693 *
1694 * Remove the given orig_node and its according orig_entry from the given
1695 * global tt entry.
1696 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001697static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001698batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1699 struct batadv_tt_global_entry *tt_global_entry,
1700 struct batadv_orig_node *orig_node,
1701 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001702{
1703 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001704 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001705 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001706 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001707
1708 spin_lock_bh(&tt_global_entry->list_lock);
1709 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001710 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001711 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001712 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001713 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001714 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001715 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001716 tt_global_entry->common.addr,
1717 BATADV_PRINT_VID(vid), message);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001718 batadv_tt_global_del_orig_entry(tt_global_entry,
1719 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001720 }
1721 }
1722 spin_unlock_bh(&tt_global_entry->list_lock);
1723}
1724
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001725/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001726 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1727 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001728 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001729static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001730batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1731 struct batadv_tt_global_entry *tt_global_entry,
1732 struct batadv_orig_node *orig_node,
1733 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001734{
1735 bool last_entry = true;
1736 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001737 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001738
1739 /* no local entry exists, case 1:
1740 * Check if this is the last one or if other entries exist.
1741 */
1742
1743 rcu_read_lock();
1744 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001745 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001746 if (orig_entry->orig_node != orig_node) {
1747 last_entry = false;
1748 break;
1749 }
1750 }
1751 rcu_read_unlock();
1752
1753 if (last_entry) {
1754 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001755 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001756 tt_global_entry->roam_at = jiffies;
1757 } else
1758 /* there is another entry, we can simply delete this
1759 * one and can still use the other one.
1760 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001761 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1762 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001763}
1764
Antonio Quartullic018ad32013-06-04 12:11:39 +02001765/**
1766 * batadv_tt_global_del - remove a client from the global table
1767 * @bat_priv: the bat priv with all the soft interface information
1768 * @orig_node: an originator serving this client
1769 * @addr: the mac address of the client
1770 * @vid: VLAN identifier
1771 * @message: a message explaining the reason for deleting the client to print
1772 * for debugging purpose
1773 * @roaming: true if the deletion has been triggered by a roaming event
1774 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001775static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1776 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001777 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001778 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001779{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001780 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001781 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001782
Antonio Quartullic018ad32013-06-04 12:11:39 +02001783 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001784 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001785 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001787 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001788 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1789 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001790
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001791 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001792 batadv_tt_global_free(bat_priv, tt_global_entry,
1793 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001794
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001795 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001797
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001798 /* if we are deleting a global entry due to a roam
1799 * event, there are two possibilities:
1800 * 1) the client roamed from node A to node B => if there
1801 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001802 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001803 * wait for node B to claim it. In case of timeout
1804 * the entry is purged.
1805 *
1806 * If there are other originators left, we directly delete
1807 * the originator.
1808 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001809 * the global entry, since it is useless now.
1810 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001811 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001812 tt_global_entry->common.addr,
1813 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001814 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001815 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001816 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001817 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001818 } else
1819 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001820 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1821 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001822
Antonio Quartullicc47f662011-04-27 14:27:57 +02001823out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001824 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001825 batadv_tt_global_entry_free_ref(tt_global_entry);
1826 if (local_entry)
1827 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828}
1829
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001830/**
1831 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1832 * given originator matching the provided vid
1833 * @bat_priv: the bat priv with all the soft interface information
1834 * @orig_node: the originator owning the entries to remove
1835 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1836 * removed
1837 * @message: debug message to print as "reason"
1838 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001839void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1840 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001841 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001842 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001844 struct batadv_tt_global_entry *tt_global;
1845 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001846 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001847 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001848 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001849 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001850 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001851 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852
Simon Wunderlich6e801492011-10-19 10:28:26 +02001853 if (!hash)
1854 return;
1855
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856 for (i = 0; i < hash->size; i++) {
1857 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001858 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001859
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001860 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001861 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001862 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001863 /* remove only matching entries */
1864 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1865 continue;
1866
Sven Eckelmann56303d32012-06-05 22:31:31 +02001867 tt_global = container_of(tt_common_entry,
1868 struct batadv_tt_global_entry,
1869 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001870
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001871 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1872 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001873
Sven Eckelmann56303d32012-06-05 22:31:31 +02001874 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001875 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001876 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001877 "Deleting global tt entry %pM (vid: %d): %s\n",
1878 tt_global->common.addr,
1879 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001880 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001881 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001882 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001884 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001885 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02001886 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001887}
1888
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001889static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1890 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001891{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001892 bool purge = false;
1893 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1894 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001895
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001896 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1897 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1898 purge = true;
1899 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001900 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001901
1902 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1903 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1904 purge = true;
1905 *msg = "Temporary client timeout\n";
1906 }
1907
1908 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001909}
1910
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001911static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001912{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001913 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001914 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001915 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001916 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001917 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001918 char *msg = NULL;
1919 struct batadv_tt_common_entry *tt_common;
1920 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001921
Antonio Quartullicc47f662011-04-27 14:27:57 +02001922 for (i = 0; i < hash->size; i++) {
1923 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001924 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001925
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001926 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001927 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001928 hash_entry) {
1929 tt_global = container_of(tt_common,
1930 struct batadv_tt_global_entry,
1931 common);
1932
1933 if (!batadv_tt_global_to_purge(tt_global, &msg))
1934 continue;
1935
1936 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001937 "Deleting global tt entry %pM (vid: %d): %s\n",
1938 tt_global->common.addr,
1939 BATADV_PRINT_VID(tt_global->common.vid),
1940 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001941
Sasha Levinb67bfe02013-02-27 17:06:00 -08001942 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001943
1944 batadv_tt_global_entry_free_ref(tt_global);
1945 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001946 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001947 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001948}
1949
Sven Eckelmann56303d32012-06-05 22:31:31 +02001950static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001951{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001952 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001953 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001954 struct batadv_tt_common_entry *tt_common_entry;
1955 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001956 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001957 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001958 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001959
Sven Eckelmann807736f2012-07-15 22:26:51 +02001960 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001961 return;
1962
Sven Eckelmann807736f2012-07-15 22:26:51 +02001963 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001964
1965 for (i = 0; i < hash->size; i++) {
1966 head = &hash->table[i];
1967 list_lock = &hash->list_locks[i];
1968
1969 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001970 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001971 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001972 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001973 tt_global = container_of(tt_common_entry,
1974 struct batadv_tt_global_entry,
1975 common);
1976 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001977 }
1978 spin_unlock_bh(list_lock);
1979 }
1980
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001981 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001982
Sven Eckelmann807736f2012-07-15 22:26:51 +02001983 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001984}
1985
Sven Eckelmann56303d32012-06-05 22:31:31 +02001986static bool
1987_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1988 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001989{
1990 bool ret = false;
1991
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001992 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1993 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001994 ret = true;
1995
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001996 /* check if the two clients are marked as isolated */
1997 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1998 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1999 ret = true;
2000
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002001 return ret;
2002}
2003
Antonio Quartullic018ad32013-06-04 12:11:39 +02002004/**
2005 * batadv_transtable_search - get the mesh destination for a given client
2006 * @bat_priv: the bat priv with all the soft interface information
2007 * @src: mac address of the source client
2008 * @addr: mac address of the destination client
2009 * @vid: VLAN identifier
2010 *
2011 * Returns a pointer to the originator that was selected as destination in the
2012 * mesh for contacting the client 'addr', NULL otherwise.
2013 * In case of multiple originators serving the same client, the function returns
2014 * the best one (best in terms of metric towards the destination node).
2015 *
2016 * If the two clients are AP isolated the function returns NULL.
2017 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002018struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2019 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002020 const uint8_t *addr,
2021 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002022{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002023 struct batadv_tt_local_entry *tt_local_entry = NULL;
2024 struct batadv_tt_global_entry *tt_global_entry = NULL;
2025 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002026 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002027
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002028 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002029 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002030 if (!tt_local_entry ||
2031 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002032 goto out;
2033 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002034
Antonio Quartullic018ad32013-06-04 12:11:39 +02002035 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002036 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002037 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002038
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002039 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002040 * isolation
2041 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002042 if (tt_local_entry &&
2043 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002044 goto out;
2045
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002046 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002047 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002048 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002049 if (best_entry)
2050 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002051 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2052 orig_node = NULL;
2053 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002054
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002055out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002056 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002057 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002058 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002059 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002060
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002061 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002062}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002063
Antonio Quartulliced72932013-04-24 16:37:51 +02002064/**
2065 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2066 * to the given orig_node
2067 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002068 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002069 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002070 *
2071 * This function computes the checksum for the global table corresponding to a
2072 * specific originator. In particular, the checksum is computed as follows: For
2073 * each client connected to the originator the CRC32C of the MAC address and the
2074 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2075 * together.
2076 *
2077 * The idea behind is that CRC32C should be used as much as possible in order to
2078 * produce a unique hash of the table, but since the order which is used to feed
2079 * the CRC32C function affects the result and since every node in the network
2080 * probably sorts the clients differently, the hash function cannot be directly
2081 * computed over the entire table. Hence the CRC32C is used only on
2082 * the single client entry, while all the results are then xor'ed together
2083 * because the XOR operation can combine them all while trying to reduce the
2084 * noise as much as possible.
2085 *
2086 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002087 */
2088static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002089 struct batadv_orig_node *orig_node,
2090 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002092 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002093 struct batadv_tt_common_entry *tt_common;
2094 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002095 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002096 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002097 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002098 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002099
2100 for (i = 0; i < hash->size; i++) {
2101 head = &hash->table[i];
2102
2103 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002104 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002105 tt_global = container_of(tt_common,
2106 struct batadv_tt_global_entry,
2107 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002108 /* compute the CRC only for entries belonging to the
2109 * VLAN identified by the vid passed as parameter
2110 */
2111 if (tt_common->vid != vid)
2112 continue;
2113
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002114 /* Roaming clients are in the global table for
2115 * consistency only. They don't have to be
2116 * taken into account while computing the
2117 * global crc
2118 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002119 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002120 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002121 /* Temporary clients have not been announced yet, so
2122 * they have to be skipped while computing the global
2123 * crc
2124 */
2125 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2126 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002127
2128 /* find out if this global entry is announced by this
2129 * originator
2130 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002131 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002132 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002133 continue;
2134
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002135 /* use network order to read the VID: this ensures that
2136 * every node reads the bytes in the same order.
2137 */
2138 tmp_vid = htons(tt_common->vid);
2139 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002140
2141 /* compute the CRC on flags that have to be kept in sync
2142 * among nodes
2143 */
2144 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2145 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2146
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002147 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002148 }
2149 rcu_read_unlock();
2150 }
2151
Antonio Quartulliced72932013-04-24 16:37:51 +02002152 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002153}
2154
Antonio Quartulliced72932013-04-24 16:37:51 +02002155/**
2156 * batadv_tt_local_crc - calculates the checksum of the local table
2157 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002158 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002159 *
2160 * For details about the computation, please refer to the documentation for
2161 * batadv_tt_global_crc().
2162 *
2163 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002164 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002165static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2166 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002168 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002169 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002171 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002172 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002173 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002174
2175 for (i = 0; i < hash->size; i++) {
2176 head = &hash->table[i];
2177
2178 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002179 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002180 /* compute the CRC only for entries belonging to the
2181 * VLAN identified by vid
2182 */
2183 if (tt_common->vid != vid)
2184 continue;
2185
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002186 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002187 * account while computing the CRC
2188 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002189 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002190 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002191
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002192 /* use network order to read the VID: this ensures that
2193 * every node reads the bytes in the same order.
2194 */
2195 tmp_vid = htons(tt_common->vid);
2196 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002197
2198 /* compute the CRC on flags that have to be kept in sync
2199 * among nodes
2200 */
2201 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2202 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2203
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002204 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002205 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002206 rcu_read_unlock();
2207 }
2208
Antonio Quartulliced72932013-04-24 16:37:51 +02002209 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002210}
2211
Sven Eckelmann56303d32012-06-05 22:31:31 +02002212static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002213{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002214 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002215
Sven Eckelmann807736f2012-07-15 22:26:51 +02002216 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002217
Sven Eckelmann807736f2012-07-15 22:26:51 +02002218 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner1f155102015-06-22 00:36:28 +08002219 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002220 kfree(node);
2221 }
2222
Sven Eckelmann807736f2012-07-15 22:26:51 +02002223 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002224}
2225
Sven Eckelmann56303d32012-06-05 22:31:31 +02002226static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2227 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002228 const void *tt_buff,
2229 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002230{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002232 * last OGM (the OGM could carry no changes)
2233 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002234 spin_lock_bh(&orig_node->tt_buff_lock);
2235 if (tt_buff_len > 0) {
2236 kfree(orig_node->tt_buff);
2237 orig_node->tt_buff_len = 0;
2238 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2239 if (orig_node->tt_buff) {
2240 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2241 orig_node->tt_buff_len = tt_buff_len;
2242 }
2243 }
2244 spin_unlock_bh(&orig_node->tt_buff_lock);
2245}
2246
Sven Eckelmann56303d32012-06-05 22:31:31 +02002247static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002248{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002249 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002250
Sven Eckelmann807736f2012-07-15 22:26:51 +02002251 spin_lock_bh(&bat_priv->tt.req_list_lock);
2252 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002253 if (batadv_has_timed_out(node->issued_at,
2254 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner1f155102015-06-22 00:36:28 +08002255 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002256 kfree(node);
2257 }
2258 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002259 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002260}
2261
2262/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002263 * has already been issued for this orig_node, NULL otherwise
2264 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002265static struct batadv_tt_req_node *
2266batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2267 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002268{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002269 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002270
Sven Eckelmann807736f2012-07-15 22:26:51 +02002271 spin_lock_bh(&bat_priv->tt.req_list_lock);
2272 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002273 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2274 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002275 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002276 goto unlock;
2277 }
2278
2279 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2280 if (!tt_req_node)
2281 goto unlock;
2282
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002283 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002284 tt_req_node->issued_at = jiffies;
2285
Sven Eckelmann807736f2012-07-15 22:26:51 +02002286 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002287unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002288 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002289 return tt_req_node;
2290}
2291
Marek Lindner335fbe02013-04-23 21:40:02 +08002292/**
2293 * batadv_tt_local_valid - verify that given tt entry is a valid one
2294 * @entry_ptr: to be checked local tt entry
2295 * @data_ptr: not used but definition required to satisfy the callback prototype
2296 *
2297 * Returns 1 if the entry is a valid, 0 otherwise.
2298 */
2299static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002300{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002301 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002302
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002303 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002304 return 0;
2305 return 1;
2306}
2307
Sven Eckelmanna5130882012-05-16 20:23:16 +02002308static int batadv_tt_global_valid(const void *entry_ptr,
2309 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002311 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2312 const struct batadv_tt_global_entry *tt_global_entry;
2313 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002314
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002315 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2316 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002317 return 0;
2318
Sven Eckelmann56303d32012-06-05 22:31:31 +02002319 tt_global_entry = container_of(tt_common_entry,
2320 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002321 common);
2322
Sven Eckelmanna5130882012-05-16 20:23:16 +02002323 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002324}
2325
Marek Lindner335fbe02013-04-23 21:40:02 +08002326/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002327 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2328 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002329 * @bat_priv: the bat priv with all the soft interface information
2330 * @hash: hash table containing the tt entries
2331 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002332 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002333 * @valid_cb: function to filter tt change entries
2334 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002335 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002336static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2337 struct batadv_hashtable *hash,
2338 void *tvlv_buff, uint16_t tt_len,
2339 int (*valid_cb)(const void *, const void *),
2340 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002341{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002342 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002343 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002344 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002345 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002346 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002347
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002348 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002349 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002350
2351 rcu_read_lock();
2352 for (i = 0; i < hash->size; i++) {
2353 head = &hash->table[i];
2354
Sasha Levinb67bfe02013-02-27 17:06:00 -08002355 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002356 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002357 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002358 break;
2359
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002360 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002361 continue;
2362
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002363 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002364 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002365 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002366 memset(tt_change->reserved, 0,
2367 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002368
Marek Lindner335fbe02013-04-23 21:40:02 +08002369 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002370 tt_change++;
2371 }
2372 }
2373 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002374}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002375
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002376/**
2377 * batadv_tt_global_check_crc - check if all the CRCs are correct
2378 * @orig_node: originator for which the CRCs have to be checked
2379 * @tt_vlan: pointer to the first tvlv VLAN entry
2380 * @num_vlan: number of tvlv VLAN entries
2381 * @create: if true, create VLAN objects if not found
2382 *
2383 * Return true if all the received CRCs match the locally stored ones, false
2384 * otherwise
2385 */
2386static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2387 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2388 uint16_t num_vlan)
2389{
2390 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2391 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002392 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002393 int i;
2394
2395 /* check if each received CRC matches the locally stored one */
2396 for (i = 0; i < num_vlan; i++) {
2397 tt_vlan_tmp = tt_vlan + i;
2398
2399 /* if orig_node is a backbone node for this VLAN, don't check
2400 * the CRC as we ignore all the global entries over it
2401 */
2402 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002403 orig_node->orig,
2404 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002405 continue;
2406
2407 vlan = batadv_orig_node_vlan_get(orig_node,
2408 ntohs(tt_vlan_tmp->vid));
2409 if (!vlan)
2410 return false;
2411
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002412 crc = vlan->tt.crc;
2413 batadv_orig_node_vlan_free_ref(vlan);
2414
2415 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002416 return false;
2417 }
2418
2419 return true;
2420}
2421
2422/**
2423 * batadv_tt_local_update_crc - update all the local CRCs
2424 * @bat_priv: the bat priv with all the soft interface information
2425 */
2426static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2427{
2428 struct batadv_softif_vlan *vlan;
2429
2430 /* recompute the global CRC for each VLAN */
2431 rcu_read_lock();
2432 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2433 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2434 }
2435 rcu_read_unlock();
2436}
2437
2438/**
2439 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2440 * @bat_priv: the bat priv with all the soft interface information
2441 * @orig_node: the orig_node for which the CRCs have to be updated
2442 */
2443static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2444 struct batadv_orig_node *orig_node)
2445{
2446 struct batadv_orig_node_vlan *vlan;
2447 uint32_t crc;
2448
2449 /* recompute the global CRC for each VLAN */
2450 rcu_read_lock();
2451 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2452 /* if orig_node is a backbone node for this VLAN, don't compute
2453 * the CRC as we ignore all the global entries over it
2454 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002455 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2456 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002457 continue;
2458
2459 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2460 vlan->tt.crc = crc;
2461 }
2462 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002463}
2464
Antonio Quartulliced72932013-04-24 16:37:51 +02002465/**
2466 * batadv_send_tt_request - send a TT Request message to a given node
2467 * @bat_priv: the bat priv with all the soft interface information
2468 * @dst_orig_node: the destination of the message
2469 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002470 * @tt_vlan: pointer to the first tvlv VLAN object to request
2471 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002472 * @full_table: ask for the entire translation table if true, while only for the
2473 * last TT diff otherwise
2474 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002475static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2476 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002477 uint8_t ttvn,
2478 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2479 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002480{
Marek Lindner335fbe02013-04-23 21:40:02 +08002481 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002482 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002483 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2484 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002485 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002486 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002487
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002488 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002489 if (!primary_if)
2490 goto out;
2491
2492 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002493 * reply from the same orig_node yet
2494 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002495 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002496 if (!tt_req_node)
2497 goto out;
2498
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002499 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2500 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002501 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002502 goto out;
2503
Marek Lindner335fbe02013-04-23 21:40:02 +08002504 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2505 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002506 tvlv_tt_data->num_vlan = htons(num_vlan);
2507
2508 /* send all the CRCs within the request. This is needed by intermediate
2509 * nodes to ensure they have the correct table before replying
2510 */
2511 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2512 for (i = 0; i < num_vlan; i++) {
2513 tt_vlan_req->vid = tt_vlan->vid;
2514 tt_vlan_req->crc = tt_vlan->crc;
2515
2516 tt_vlan_req++;
2517 tt_vlan++;
2518 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002519
2520 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002521 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002522
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002523 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002524 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002525
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002526 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002527 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2528 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002529 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002530 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002531
2532out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002534 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002535 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002536 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner1f155102015-06-22 00:36:28 +08002537 /* list_del_init() verifies tt_req_node still is in the list */
2538 list_del_init(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002539 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002540 kfree(tt_req_node);
2541 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002542 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002543 return ret;
2544}
2545
Marek Lindner335fbe02013-04-23 21:40:02 +08002546/**
2547 * batadv_send_other_tt_response - send reply to tt request concerning another
2548 * node's translation table
2549 * @bat_priv: the bat priv with all the soft interface information
2550 * @tt_data: tt data containing the tt request information
2551 * @req_src: mac address of tt request sender
2552 * @req_dst: mac address of tt request recipient
2553 *
2554 * Returns true if tt request reply was sent, false otherwise.
2555 */
2556static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2557 struct batadv_tvlv_tt_data *tt_data,
2558 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002559{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002560 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002561 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002562 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002563 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002564 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002565 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002566 uint8_t orig_ttvn, req_ttvn;
2567 uint16_t tvlv_len;
2568 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002569
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002570 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002571 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002572 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002573 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002574
2575 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002576 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002577 if (!req_dst_orig_node)
2578 goto out;
2579
Marek Lindner335fbe02013-04-23 21:40:02 +08002580 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002581 if (!res_dst_orig_node)
2582 goto out;
2583
Antonio Quartullia73105b2011-04-27 14:27:44 +02002584 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002585 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002586
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002587 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002588 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002589 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002590 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2591 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002592 goto out;
2593
Antonio Quartulli015758d2011-07-09 17:52:13 +02002594 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002595 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002596 !req_dst_orig_node->tt_buff)
2597 full_table = true;
2598 else
2599 full_table = false;
2600
Marek Lindner335fbe02013-04-23 21:40:02 +08002601 /* TT fragmentation hasn't been implemented yet, so send as many
2602 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002603 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002604 if (!full_table) {
2605 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2606 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002607
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002608 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2609 &tvlv_tt_data,
2610 &tt_change,
2611 &tt_len);
2612 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002613 goto unlock;
2614
Antonio Quartullia73105b2011-04-27 14:27:44 +02002615 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002616 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002617 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002618 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2619 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002620 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2621 * in the initial part
2622 */
2623 tt_len = -1;
2624 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2625 &tvlv_tt_data,
2626 &tt_change,
2627 &tt_len);
2628 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002629 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002630
2631 /* fill the rest of the tvlv with the real TT entries */
2632 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2633 tt_change, tt_len,
2634 batadv_tt_global_valid,
2635 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002636 }
2637
Marek Lindnera19d3d82013-05-27 15:33:25 +08002638 /* Don't send the response, if larger than fragmented packet. */
2639 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2640 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2641 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2642 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2643 res_dst_orig_node->orig);
2644 goto out;
2645 }
2646
Marek Lindner335fbe02013-04-23 21:40:02 +08002647 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2648 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002649
2650 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002651 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002652
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002653 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002654 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2655 res_dst_orig_node->orig, req_dst_orig_node->orig,
2656 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002657
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002658 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002659
Marek Lindner335fbe02013-04-23 21:40:02 +08002660 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002661 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2662 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002663
Marek Lindner335fbe02013-04-23 21:40:02 +08002664 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665 goto out;
2666
2667unlock:
2668 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2669
2670out:
2671 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002672 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002674 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002675 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002677}
Sven Eckelmann96412692012-06-05 22:31:30 +02002678
Marek Lindner335fbe02013-04-23 21:40:02 +08002679/**
2680 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2681 * translation table
2682 * @bat_priv: the bat priv with all the soft interface information
2683 * @tt_data: tt data containing the tt request information
2684 * @req_src: mac address of tt request sender
2685 *
2686 * Returns true if tt request reply was sent, false otherwise.
2687 */
2688static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2689 struct batadv_tvlv_tt_data *tt_data,
2690 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002691{
Marek Lindner335fbe02013-04-23 21:40:02 +08002692 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002693 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002694 struct batadv_tvlv_tt_change *tt_change;
2695 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002696 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002697 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002698 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002699 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002700
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002701 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002702 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002703 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002704 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002706 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002707
Sven Eckelmann807736f2012-07-15 22:26:51 +02002708 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002709 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710
Marek Lindner335fbe02013-04-23 21:40:02 +08002711 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002712 if (!orig_node)
2713 goto out;
2714
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002715 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716 if (!primary_if)
2717 goto out;
2718
2719 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002720 * is too big send the whole local translation table
2721 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002722 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002723 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002724 full_table = true;
2725 else
2726 full_table = false;
2727
Marek Lindner335fbe02013-04-23 21:40:02 +08002728 /* TT fragmentation hasn't been implemented yet, so send as many
2729 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002730 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002731 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002732 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002733
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002734 tt_len = bat_priv->tt.last_changeset_len;
2735 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2736 &tvlv_tt_data,
2737 &tt_change,
2738 &tt_len);
2739 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002740 goto unlock;
2741
Marek Lindner335fbe02013-04-23 21:40:02 +08002742 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002743 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002744 bat_priv->tt.last_changeset_len);
2745 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002746 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002747 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002748
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002749 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2750 * in the initial part
2751 */
2752 tt_len = -1;
2753 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2754 &tvlv_tt_data,
2755 &tt_change,
2756 &tt_len);
2757 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002758 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002759
2760 /* fill the rest of the tvlv with the real TT entries */
2761 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2762 tt_change, tt_len,
2763 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002764 }
2765
Marek Lindner335fbe02013-04-23 21:40:02 +08002766 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2767 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002768
2769 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002770 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002771
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002772 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002773 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2774 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002775
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002776 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002777
Marek Lindner335fbe02013-04-23 21:40:02 +08002778 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002779 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2780 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002781
Antonio Quartullia73105b2011-04-27 14:27:44 +02002782 goto out;
2783
2784unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002785 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002786out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002787 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002788 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002789 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002790 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002791 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002792 kfree(tvlv_tt_data);
2793 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794 return true;
2795}
2796
Marek Lindner335fbe02013-04-23 21:40:02 +08002797/**
2798 * batadv_send_tt_response - send reply to tt request
2799 * @bat_priv: the bat priv with all the soft interface information
2800 * @tt_data: tt data containing the tt request information
2801 * @req_src: mac address of tt request sender
2802 * @req_dst: mac address of tt request recipient
2803 *
2804 * Returns true if tt request reply was sent, false otherwise.
2805 */
2806static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2807 struct batadv_tvlv_tt_data *tt_data,
2808 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002809{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002810 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002811 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002812 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2813 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002814}
2815
Sven Eckelmann56303d32012-06-05 22:31:31 +02002816static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2817 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002818 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002819 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002820{
2821 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002822 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002823
2824 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002825 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2826 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002827 batadv_tt_global_del(bat_priv, orig_node,
2828 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002829 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002830 "tt removed by changes",
2831 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002832 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002833 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002834 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002835 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002836 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837 /* In case of problem while storing a
2838 * global_entry, we stop the updating
2839 * procedure without committing the
2840 * ttvn change. This will avoid to send
2841 * corrupted data on tt_request
2842 */
2843 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002844 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002845 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002846 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002847}
2848
Sven Eckelmann56303d32012-06-05 22:31:31 +02002849static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002850 struct batadv_tvlv_tt_change *tt_change,
2851 uint8_t ttvn, uint8_t *resp_src,
2852 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002853{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002854 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002855
Marek Lindner335fbe02013-04-23 21:40:02 +08002856 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002857 if (!orig_node)
2858 goto out;
2859
2860 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002861 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2862 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002863
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002864 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2865 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002866
2867 spin_lock_bh(&orig_node->tt_buff_lock);
2868 kfree(orig_node->tt_buff);
2869 orig_node->tt_buff_len = 0;
2870 orig_node->tt_buff = NULL;
2871 spin_unlock_bh(&orig_node->tt_buff_lock);
2872
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002873 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002874
2875out:
2876 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002877 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002878}
2879
Sven Eckelmann56303d32012-06-05 22:31:31 +02002880static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2881 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002882 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002883 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002884{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002885 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2886 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002887
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002888 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2889 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002890 atomic_set(&orig_node->last_ttvn, ttvn);
2891}
2892
Antonio Quartullic018ad32013-06-04 12:11:39 +02002893/**
2894 * batadv_is_my_client - check if a client is served by the local node
2895 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002896 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002897 * @vid: VLAN identifier
2898 *
2899 * Returns true if the client is served by this node, false otherwise.
2900 */
2901bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2902 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002903{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002904 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002905 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002906
Antonio Quartullic018ad32013-06-04 12:11:39 +02002907 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002908 if (!tt_local_entry)
2909 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002910 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002911 * consistency purpose)
2912 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002913 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2914 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002915 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002916 ret = true;
2917out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002918 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002919 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002920 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002921}
2922
Marek Lindner335fbe02013-04-23 21:40:02 +08002923/**
2924 * batadv_handle_tt_response - process incoming tt reply
2925 * @bat_priv: the bat priv with all the soft interface information
2926 * @tt_data: tt data containing the tt request information
2927 * @resp_src: mac address of tt reply sender
2928 * @num_entries: number of tt change entries appended to the tt data
2929 */
2930static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2931 struct batadv_tvlv_tt_data *tt_data,
2932 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002933{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002934 struct batadv_tt_req_node *node, *safe;
2935 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002936 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002937 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2938 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002939
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002940 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002941 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002942 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002943 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002944
Marek Lindner335fbe02013-04-23 21:40:02 +08002945 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002946 if (!orig_node)
2947 goto out;
2948
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002949 spin_lock_bh(&orig_node->tt_lock);
2950
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002951 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2952 change_offset *= ntohs(tt_data->num_vlan);
2953 change_offset += sizeof(*tt_data);
2954 tvlv_ptr += change_offset;
2955
2956 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002957 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002958 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2959 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002960 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002961 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2962 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002963 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002964
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002965 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002966 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002967
2968 spin_unlock_bh(&orig_node->tt_lock);
2969
Antonio Quartullia73105b2011-04-27 14:27:44 +02002970 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002971 spin_lock_bh(&bat_priv->tt.req_list_lock);
2972 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002973 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002974 continue;
Marek Lindner1f155102015-06-22 00:36:28 +08002975 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002976 kfree(node);
2977 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002978
Sven Eckelmann807736f2012-07-15 22:26:51 +02002979 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002980out:
2981 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002982 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002983}
2984
Sven Eckelmann56303d32012-06-05 22:31:31 +02002985static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002986{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002987 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002988
Sven Eckelmann807736f2012-07-15 22:26:51 +02002989 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002990
Sven Eckelmann807736f2012-07-15 22:26:51 +02002991 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002992 list_del(&node->list);
2993 kfree(node);
2994 }
2995
Sven Eckelmann807736f2012-07-15 22:26:51 +02002996 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002997}
2998
Sven Eckelmann56303d32012-06-05 22:31:31 +02002999static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003000{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003001 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003002
Sven Eckelmann807736f2012-07-15 22:26:51 +02003003 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3004 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003005 if (!batadv_has_timed_out(node->first_time,
3006 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003007 continue;
3008
3009 list_del(&node->list);
3010 kfree(node);
3011 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003012 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003013}
3014
3015/* This function checks whether the client already reached the
3016 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3017 * will not be sent.
3018 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003019 * returns true if the ROAMING_ADV can be sent, false otherwise
3020 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003021static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02003022 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003023{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003024 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003025 bool ret = false;
3026
Sven Eckelmann807736f2012-07-15 22:26:51 +02003027 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003028 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003029 * reply from the same orig_node yet
3030 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003031 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003032 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003033 continue;
3034
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003035 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003036 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003037 continue;
3038
Sven Eckelmann3e348192012-05-16 20:23:22 +02003039 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003040 /* Sorry, you roamed too many times! */
3041 goto unlock;
3042 ret = true;
3043 break;
3044 }
3045
3046 if (!ret) {
3047 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3048 if (!tt_roam_node)
3049 goto unlock;
3050
3051 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003052 atomic_set(&tt_roam_node->counter,
3053 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003054 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003055
Sven Eckelmann807736f2012-07-15 22:26:51 +02003056 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003057 ret = true;
3058 }
3059
3060unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003061 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003062 return ret;
3063}
3064
Antonio Quartullic018ad32013-06-04 12:11:39 +02003065/**
3066 * batadv_send_roam_adv - send a roaming advertisement message
3067 * @bat_priv: the bat priv with all the soft interface information
3068 * @client: mac address of the roaming client
3069 * @vid: VLAN identifier
3070 * @orig_node: message destination
3071 *
3072 * Send a ROAMING_ADV message to the node which was previously serving this
3073 * client. This is done to inform the node that from now on all traffic destined
3074 * for this particular roamed client has to be forwarded to the sender of the
3075 * roaming message.
3076 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003077static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003078 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003079 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003080{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003081 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003082 struct batadv_tvlv_roam_adv tvlv_roam;
3083
3084 primary_if = batadv_primary_if_get_selected(bat_priv);
3085 if (!primary_if)
3086 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003087
3088 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003089 * already roamed to us too many times
3090 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003091 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003092 goto out;
3093
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003094 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003095 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3096 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003097
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003098 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003099
Marek Lindner122edaa2013-04-23 21:40:03 +08003100 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003101 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003102
3103 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3104 orig_node->orig, BATADV_TVLV_ROAM, 1,
3105 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003106
3107out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003108 if (primary_if)
3109 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003110}
3111
Sven Eckelmanna5130882012-05-16 20:23:16 +02003112static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003113{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003114 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003115 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003116 struct batadv_priv *bat_priv;
3117
3118 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003119 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3120 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003121
Marek Lindnera19d3d82013-05-27 15:33:25 +08003122 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003123 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003124 batadv_tt_req_purge(bat_priv);
3125 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003126
Antonio Quartulli72414442012-12-25 13:14:37 +01003127 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3128 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003129}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003130
Sven Eckelmann56303d32012-06-05 22:31:31 +02003131void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003132{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003133 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3134 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3135
Sven Eckelmann807736f2012-07-15 22:26:51 +02003136 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003137
Sven Eckelmanna5130882012-05-16 20:23:16 +02003138 batadv_tt_local_table_free(bat_priv);
3139 batadv_tt_global_table_free(bat_priv);
3140 batadv_tt_req_list_free(bat_priv);
3141 batadv_tt_changes_list_free(bat_priv);
3142 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003143
Sven Eckelmann807736f2012-07-15 22:26:51 +02003144 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003145}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003146
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003147/**
3148 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3149 * table and possibly count them in the TT size
3150 * @bat_priv: the bat priv with all the soft interface information
3151 * @flags: the flag to switch
3152 * @enable: whether to set or unset the flag
3153 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003154 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003155static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3156 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003157{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003158 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3159 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003160 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003161 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003162 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003163
3164 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003165 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003166
3167 for (i = 0; i < hash->size; i++) {
3168 head = &hash->table[i];
3169
3170 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003171 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003172 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003173 if (enable) {
3174 if ((tt_common_entry->flags & flags) == flags)
3175 continue;
3176 tt_common_entry->flags |= flags;
3177 } else {
3178 if (!(tt_common_entry->flags & flags))
3179 continue;
3180 tt_common_entry->flags &= ~flags;
3181 }
3182 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003183
3184 if (!count)
3185 continue;
3186
3187 batadv_tt_local_size_inc(bat_priv,
3188 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003189 }
3190 rcu_read_unlock();
3191 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003192}
3193
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003194/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003195static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003196{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003197 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003198 struct batadv_tt_common_entry *tt_common;
3199 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003200 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003201 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003202 struct hlist_head *head;
3203 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003204 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003205
3206 if (!hash)
3207 return;
3208
3209 for (i = 0; i < hash->size; i++) {
3210 head = &hash->table[i];
3211 list_lock = &hash->list_locks[i];
3212
3213 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003214 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003215 hash_entry) {
3216 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003217 continue;
3218
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003219 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003220 "Deleting local tt entry (%pM, vid: %d): pending\n",
3221 tt_common->addr,
3222 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003223
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003224 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003225 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003226 tt_local = container_of(tt_common,
3227 struct batadv_tt_local_entry,
3228 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003229
3230 /* decrease the reference held for this vlan */
3231 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08003232 if (vlan) {
3233 batadv_softif_vlan_free_ref(vlan);
3234 batadv_softif_vlan_free_ref(vlan);
3235 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003236
Sven Eckelmann56303d32012-06-05 22:31:31 +02003237 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003238 }
3239 spin_unlock_bh(list_lock);
3240 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003241}
3242
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003243/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003244 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3245 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003246 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003247 *
3248 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003249 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003250static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003251{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003252 /* Update multicast addresses in local translation table */
3253 batadv_mcast_mla_update(bat_priv);
3254
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003255 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3256 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3257 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003258 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003259 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003260
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003261 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003262
Sven Eckelmanna5130882012-05-16 20:23:16 +02003263 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003264 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003265
3266 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003267 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003268 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003269 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003270 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003271
3272 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003273 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003274 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003275}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003276
Marek Lindnera19d3d82013-05-27 15:33:25 +08003277/**
3278 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3279 * have been queued in the time since the last commit
3280 * @bat_priv: the bat priv with all the soft interface information
3281 */
3282void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3283{
3284 spin_lock_bh(&bat_priv->tt.commit_lock);
3285 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003286 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003287}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003288
Sven Eckelmann56303d32012-06-05 22:31:31 +02003289bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003290 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003291{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003292 struct batadv_tt_local_entry *tt_local_entry = NULL;
3293 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003294 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003295 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003296
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003297 vlan = batadv_softif_vlan_get(bat_priv, vid);
3298 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003299 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003300
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003301 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003302 if (!tt_local_entry)
3303 goto out;
3304
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003305 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003306 if (!tt_global_entry)
3307 goto out;
3308
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003309 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003310 goto out;
3311
Marek Lindner5870adc2012-06-20 17:16:05 +02003312 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003313
3314out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003315 if (vlan)
3316 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003317 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003318 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003319 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003320 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003321 return ret;
3322}
Marek Lindnera943cac2011-07-30 13:10:18 +02003323
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003324/**
3325 * batadv_tt_update_orig - update global translation table with new tt
3326 * information received via ogms
3327 * @bat_priv: the bat priv with all the soft interface information
3328 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003329 * @tt_vlan: pointer to the first tvlv VLAN entry
3330 * @tt_num_vlan: number of tvlv VLAN entries
3331 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003332 * @tt_num_changes: number of tt changes inside the tt buffer
3333 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003334 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003335 */
3336static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3337 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003338 const void *tt_buff, uint16_t tt_num_vlan,
3339 struct batadv_tvlv_tt_change *tt_change,
3340 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003341{
3342 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003343 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003344 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003345 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003346
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003347 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003348 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3349 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003350
Antonio Quartulli17071572011-11-07 16:36:40 +01003351 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003352 * increased by one -> we can apply the attached changes
3353 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003354 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003355 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003356 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3357 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003358 * In this case send a tt request
3359 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003360 if (!tt_num_changes) {
3361 full_table = false;
3362 goto request_table;
3363 }
3364
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003365 spin_lock_bh(&orig_node->tt_lock);
3366
Sven Eckelmanna5130882012-05-16 20:23:16 +02003367 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003368 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003369
3370 /* Even if we received the precomputed crc with the OGM, we
3371 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003372 * in the global table
3373 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003374 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003375
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003376 spin_unlock_bh(&orig_node->tt_lock);
3377
Marek Lindnera943cac2011-07-30 13:10:18 +02003378 /* The ttvn alone is not enough to guarantee consistency
3379 * because a single value could represent different states
3380 * (due to the wrap around). Thus a node has to check whether
3381 * the resulting table (after applying the changes) is still
3382 * consistent or not. E.g. a node could disconnect while its
3383 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3384 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003385 * inconsistency
3386 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003387 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3388 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003389 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003390 } else {
3391 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003392 * in sync anymore -> request fresh tt data
3393 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003394 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003395 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3396 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003397request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003398 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003399 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3400 orig_node->orig, ttvn, orig_ttvn,
3401 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003402 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003403 tt_vlan, tt_num_vlan,
3404 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003405 return;
3406 }
3407 }
3408}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003409
Antonio Quartullic018ad32013-06-04 12:11:39 +02003410/**
3411 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3412 * @bat_priv: the bat priv with all the soft interface information
3413 * @addr: the mac address of the client to check
3414 * @vid: VLAN identifier
3415 *
3416 * Returns true if we know that the client has moved from its old originator
3417 * to another one. This entry is still kept for consistency purposes and will be
3418 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003419 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003420bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003421 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003422{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003423 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003424 bool ret = false;
3425
Antonio Quartullic018ad32013-06-04 12:11:39 +02003426 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003427 if (!tt_global_entry)
3428 goto out;
3429
Antonio Quartullic1d07432013-01-15 22:17:19 +10003430 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003431 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003432out:
3433 return ret;
3434}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003435
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003436/**
3437 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3438 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003439 * @addr: the mac address of the local client to query
3440 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003441 *
3442 * Returns true if the local client is known to be roaming (it is not served by
3443 * this node anymore) or not. If yes, the client is still present in the table
3444 * to keep the latter consistent with the node TTVN
3445 */
3446bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003447 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003448{
3449 struct batadv_tt_local_entry *tt_local_entry;
3450 bool ret = false;
3451
Antonio Quartullic018ad32013-06-04 12:11:39 +02003452 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003453 if (!tt_local_entry)
3454 goto out;
3455
3456 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3457 batadv_tt_local_entry_free_ref(tt_local_entry);
3458out:
3459 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003460}
3461
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003462bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3463 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003464 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003465 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003466{
3467 bool ret = false;
3468
Antonio Quartulli16052782013-06-04 12:11:41 +02003469 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003470 BATADV_TT_CLIENT_TEMP,
3471 atomic_read(&orig_node->last_ttvn)))
3472 goto out;
3473
3474 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003475 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3476 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003477 ret = true;
3478out:
3479 return ret;
3480}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003481
3482/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003483 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3484 * maximum packet size that can be transported through the mesh
3485 * @soft_iface: netdev struct of the mesh interface
3486 *
3487 * Remove entries older than 'timeout' and half timeout if more entries need
3488 * to be removed.
3489 */
3490void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3491{
3492 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3493 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3494 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3495 bool reduced = false;
3496
3497 spin_lock_bh(&bat_priv->tt.commit_lock);
3498
3499 while (true) {
3500 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3501 if (packet_size_max >= table_size)
3502 break;
3503
3504 batadv_tt_local_purge(bat_priv, timeout);
3505 batadv_tt_local_purge_pending_clients(bat_priv);
3506
3507 timeout /= 2;
3508 reduced = true;
3509 net_ratelimited_function(batadv_info, soft_iface,
3510 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3511 packet_size_max);
3512 }
3513
3514 /* commit these changes immediately, to avoid synchronization problem
3515 * with the TTVN
3516 */
3517 if (reduced)
3518 batadv_tt_local_commit_changes_nolock(bat_priv);
3519
3520 spin_unlock_bh(&bat_priv->tt.commit_lock);
3521}
3522
3523/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003524 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3525 * @bat_priv: the bat priv with all the soft interface information
3526 * @orig: the orig_node of the ogm
3527 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3528 * @tvlv_value: tvlv buffer containing the gateway data
3529 * @tvlv_value_len: tvlv buffer length
3530 */
3531static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3532 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003533 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003534 uint16_t tvlv_value_len)
3535{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003536 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3537 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003538 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003539 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003540
3541 if (tvlv_value_len < sizeof(*tt_data))
3542 return;
3543
3544 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3545 tvlv_value_len -= sizeof(*tt_data);
3546
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003547 num_vlan = ntohs(tt_data->num_vlan);
3548
3549 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3550 return;
3551
3552 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3553 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3554 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3555
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003556 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003557
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003558 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3559 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003560}
3561
3562/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003563 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3564 * container
3565 * @bat_priv: the bat priv with all the soft interface information
3566 * @src: mac address of tt tvlv sender
3567 * @dst: mac address of tt tvlv recipient
3568 * @tvlv_value: tvlv buffer containing the tt data
3569 * @tvlv_value_len: tvlv buffer length
3570 *
3571 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3572 * otherwise.
3573 */
3574static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3575 uint8_t *src, uint8_t *dst,
3576 void *tvlv_value,
3577 uint16_t tvlv_value_len)
3578{
3579 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003580 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003581 char tt_flag;
3582 bool ret;
3583
3584 if (tvlv_value_len < sizeof(*tt_data))
3585 return NET_RX_SUCCESS;
3586
3587 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3588 tvlv_value_len -= sizeof(*tt_data);
3589
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003590 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3591 tt_vlan_len *= ntohs(tt_data->num_vlan);
3592
3593 if (tvlv_value_len < tt_vlan_len)
3594 return NET_RX_SUCCESS;
3595
3596 tvlv_value_len -= tt_vlan_len;
3597 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003598
3599 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3600 case BATADV_TT_REQUEST:
3601 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3602
3603 /* If this node cannot provide a TT response the tt_request is
3604 * forwarded
3605 */
3606 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3607 if (!ret) {
3608 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3609 tt_flag = 'F';
3610 else
3611 tt_flag = '.';
3612
3613 batadv_dbg(BATADV_DBG_TT, bat_priv,
3614 "Routing TT_REQUEST to %pM [%c]\n",
3615 dst, tt_flag);
3616 /* tvlv API will re-route the packet */
3617 return NET_RX_DROP;
3618 }
3619 break;
3620 case BATADV_TT_RESPONSE:
3621 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3622
3623 if (batadv_is_my_mac(bat_priv, dst)) {
3624 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003625 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003626 return NET_RX_SUCCESS;
3627 }
3628
3629 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3630 tt_flag = 'F';
3631 else
3632 tt_flag = '.';
3633
3634 batadv_dbg(BATADV_DBG_TT, bat_priv,
3635 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3636
3637 /* tvlv API will re-route the packet */
3638 return NET_RX_DROP;
3639 }
3640
3641 return NET_RX_SUCCESS;
3642}
3643
3644/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003645 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3646 * @bat_priv: the bat priv with all the soft interface information
3647 * @src: mac address of tt tvlv sender
3648 * @dst: mac address of tt tvlv recipient
3649 * @tvlv_value: tvlv buffer containing the tt data
3650 * @tvlv_value_len: tvlv buffer length
3651 *
3652 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3653 * otherwise.
3654 */
3655static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3656 uint8_t *src, uint8_t *dst,
3657 void *tvlv_value,
3658 uint16_t tvlv_value_len)
3659{
3660 struct batadv_tvlv_roam_adv *roaming_adv;
3661 struct batadv_orig_node *orig_node = NULL;
3662
3663 /* If this node is not the intended recipient of the
3664 * roaming advertisement the packet is forwarded
3665 * (the tvlv API will re-route the packet).
3666 */
3667 if (!batadv_is_my_mac(bat_priv, dst))
3668 return NET_RX_DROP;
3669
Marek Lindner122edaa2013-04-23 21:40:03 +08003670 if (tvlv_value_len < sizeof(*roaming_adv))
3671 goto out;
3672
3673 orig_node = batadv_orig_hash_find(bat_priv, src);
3674 if (!orig_node)
3675 goto out;
3676
3677 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3678 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3679
3680 batadv_dbg(BATADV_DBG_TT, bat_priv,
3681 "Received ROAMING_ADV from %pM (client %pM)\n",
3682 src, roaming_adv->client);
3683
3684 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003685 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003686 atomic_read(&orig_node->last_ttvn) + 1);
3687
3688out:
3689 if (orig_node)
3690 batadv_orig_node_free_ref(orig_node);
3691 return NET_RX_SUCCESS;
3692}
3693
3694/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003695 * batadv_tt_init - initialise the translation table internals
3696 * @bat_priv: the bat priv with all the soft interface information
3697 *
3698 * Return 0 on success or negative error number in case of failure.
3699 */
3700int batadv_tt_init(struct batadv_priv *bat_priv)
3701{
3702 int ret;
3703
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003704 /* synchronized flags must be remote */
3705 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3706
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003707 ret = batadv_tt_local_init(bat_priv);
3708 if (ret < 0)
3709 return ret;
3710
3711 ret = batadv_tt_global_init(bat_priv);
3712 if (ret < 0)
3713 return ret;
3714
3715 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003716 batadv_tt_tvlv_unicast_handler_v1,
3717 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003718
Marek Lindner122edaa2013-04-23 21:40:03 +08003719 batadv_tvlv_handler_register(bat_priv, NULL,
3720 batadv_roam_tvlv_unicast_handler_v1,
3721 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3722
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003723 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3724 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3725 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3726
3727 return 1;
3728}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003729
3730/**
3731 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3732 * @bat_priv: the bat priv with all the soft interface information
3733 * @addr: the mac address of the client
3734 * @vid: the identifier of the VLAN where this client is connected
3735 *
3736 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3737 * otherwise
3738 */
3739bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3740 const uint8_t *addr, unsigned short vid)
3741{
3742 struct batadv_tt_global_entry *tt;
3743 bool ret;
3744
3745 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3746 if (!tt)
3747 return false;
3748
3749 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3750
3751 batadv_tt_global_entry_free_ref(tt);
3752
3753 return ret;
3754}