blob: cd35bb846582a90fcce035cc6ff62055a76a1605 [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);
598
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200599 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200600 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
601 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200602 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100604 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100605 /* The local entry has to be marked as NEW to avoid to send it in
606 * a full table response going out before the next ttvn increment
607 * (consistency check)
608 */
609 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200610 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200611 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200612 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
613 atomic_set(&tt_local->common.refcount, 2);
614 tt_local->last_seen = jiffies;
615 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000616
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100617 /* the batman interface mac and multicast addresses should never be
618 * purged
619 */
620 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
621 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200622 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623
Sven Eckelmann807736f2012-07-15 22:26:51 +0200624 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200625 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200626 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100627
628 if (unlikely(hash_added != 0)) {
629 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200630 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200631 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100632 goto out;
633 }
634
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200635add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200636 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200637
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200638check_roaming:
639 /* Check whether it is a roaming, but don't do anything if the roaming
640 * process has already been handled
641 */
642 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200643 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200644 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200645 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800646 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200647 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200648 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200649 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200650 }
651 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200652 if (roamed_back) {
653 batadv_tt_global_free(bat_priv, tt_global,
654 "Roaming canceled");
655 tt_global = NULL;
656 } else {
657 /* The global entry has to be marked as ROAMING and
658 * has to be kept for consistency purpose
659 */
660 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
661 tt_global->roam_at = jiffies;
662 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200663 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200664
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200665 /* store the current remote flags before altering them. This helps
666 * understanding is flags are changing or not
667 */
668 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800669
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200670 if (batadv_is_wifi_netdev(in_dev))
671 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
672 else
673 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
674
Antonio Quartulli9464d072013-11-16 12:03:48 +0100675 /* check the mark in the skb: if it's equal to the configured
676 * isolation_mark, it means the packet is coming from an isolated
677 * non-mesh client
678 */
679 match_mark = (mark & bat_priv->isolation_mark_mask);
680 if (bat_priv->isolation_mark_mask &&
681 match_mark == bat_priv->isolation_mark)
682 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
683 else
684 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
685
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200686 /* if any "dynamic" flag has been modified, resend an ADD event for this
687 * entry so that all the nodes can get the new flags
688 */
689 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
690 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
691
692 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200693out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200694 if (in_dev)
695 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200696 if (tt_local)
697 batadv_tt_local_entry_free_ref(tt_local);
698 if (tt_global)
699 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800700 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701}
702
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800703/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200704 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
705 * within a TT Response directed to another node
706 * @orig_node: originator for which the TT data has to be prepared
707 * @tt_data: uninitialised pointer to the address of the TVLV buffer
708 * @tt_change: uninitialised pointer to the address of the area where the TT
709 * changed can be stored
710 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
711 * function reserves the amount of space needed to send the entire global TT
712 * table. In case of success the value is updated with the real amount of
713 * reserved bytes
714
715 * Allocate the needed amount of memory for the entire TT TVLV and write its
716 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
717 * objects, one per active VLAN served by the originator node.
718 *
719 * Return the size of the allocated buffer or 0 in case of failure.
720 */
721static uint16_t
722batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
723 struct batadv_tvlv_tt_data **tt_data,
724 struct batadv_tvlv_tt_change **tt_change,
725 int32_t *tt_len)
726{
727 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
728 struct batadv_tvlv_tt_vlan_data *tt_vlan;
729 struct batadv_orig_node_vlan *vlan;
730 uint8_t *tt_change_ptr;
731
732 rcu_read_lock();
733 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
734 num_vlan++;
735 num_entries += atomic_read(&vlan->tt.num_entries);
736 }
737
738 change_offset = sizeof(**tt_data);
739 change_offset += num_vlan * sizeof(*tt_vlan);
740
741 /* if tt_len is negative, allocate the space needed by the full table */
742 if (*tt_len < 0)
743 *tt_len = batadv_tt_len(num_entries);
744
745 tvlv_len = *tt_len;
746 tvlv_len += change_offset;
747
748 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
749 if (!*tt_data) {
750 *tt_len = 0;
751 goto out;
752 }
753
754 (*tt_data)->flags = BATADV_NO_FLAGS;
755 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
756 (*tt_data)->num_vlan = htons(num_vlan);
757
758 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
759 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
760 tt_vlan->vid = htons(vlan->vid);
761 tt_vlan->crc = htonl(vlan->tt.crc);
762
763 tt_vlan++;
764 }
765
766 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
767 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
768
769out:
770 rcu_read_unlock();
771 return tvlv_len;
772}
773
774/**
775 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
776 * node
777 * @bat_priv: the bat priv with all the soft interface information
778 * @tt_data: uninitialised pointer to the address of the TVLV buffer
779 * @tt_change: uninitialised pointer to the address of the area where the TT
780 * changes can be stored
781 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
782 * function reserves the amount of space needed to send the entire local TT
783 * table. In case of success the value is updated with the real amount of
784 * reserved bytes
785 *
786 * Allocate the needed amount of memory for the entire TT TVLV and write its
787 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
788 * objects, one per active VLAN.
789 *
790 * Return the size of the allocated buffer or 0 in case of failure.
791 */
792static uint16_t
793batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
794 struct batadv_tvlv_tt_data **tt_data,
795 struct batadv_tvlv_tt_change **tt_change,
796 int32_t *tt_len)
797{
798 struct batadv_tvlv_tt_vlan_data *tt_vlan;
799 struct batadv_softif_vlan *vlan;
800 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
801 uint8_t *tt_change_ptr;
802 int change_offset;
803
804 rcu_read_lock();
805 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
806 num_vlan++;
807 num_entries += atomic_read(&vlan->tt.num_entries);
808 }
809
810 change_offset = sizeof(**tt_data);
811 change_offset += num_vlan * sizeof(*tt_vlan);
812
813 /* if tt_len is negative, allocate the space needed by the full table */
814 if (*tt_len < 0)
815 *tt_len = batadv_tt_len(num_entries);
816
817 tvlv_len = *tt_len;
818 tvlv_len += change_offset;
819
820 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
821 if (!*tt_data) {
822 tvlv_len = 0;
823 goto out;
824 }
825
826 (*tt_data)->flags = BATADV_NO_FLAGS;
827 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
828 (*tt_data)->num_vlan = htons(num_vlan);
829
830 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
831 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
832 tt_vlan->vid = htons(vlan->vid);
833 tt_vlan->crc = htonl(vlan->tt.crc);
834
835 tt_vlan++;
836 }
837
838 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
839 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
840
841out:
842 rcu_read_unlock();
843 return tvlv_len;
844}
845
846/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800847 * batadv_tt_tvlv_container_update - update the translation table tvlv container
848 * after local tt changes have been committed
849 * @bat_priv: the bat priv with all the soft interface information
850 */
851static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000852{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800853 struct batadv_tt_change_node *entry, *safe;
854 struct batadv_tvlv_tt_data *tt_data;
855 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200856 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800857 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200858 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200860 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
861 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800862
863 /* if we have too many changes for one packet don't send any
864 * and wait for the tt table request which will be fragmented
865 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800866 if (tt_diff_len > bat_priv->soft_iface->mtu)
867 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800868
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200869 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
870 &tt_change, &tt_diff_len);
871 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800872 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800873
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800874 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800875
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800876 if (tt_diff_len == 0)
877 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800878
Sven Eckelmann807736f2012-07-15 22:26:51 +0200879 spin_lock_bh(&bat_priv->tt.changes_list_lock);
880 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881
Sven Eckelmann807736f2012-07-15 22:26:51 +0200882 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100883 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800884 if (tt_diff_entries_count < tt_diff_entries_num) {
885 memcpy(tt_change + tt_diff_entries_count,
886 &entry->change,
887 sizeof(struct batadv_tvlv_tt_change));
888 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200890 list_del(&entry->list);
891 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200893 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
Antonio Quartullia73105b2011-04-27 14:27:44 +0200895 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200896 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
897 kfree(bat_priv->tt.last_changeset);
898 bat_priv->tt.last_changeset_len = 0;
899 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800900 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800901 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800902 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800903 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200904 * instead of providing the diff
905 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800906 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200907 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800908 memcpy(bat_priv->tt.last_changeset,
909 tt_change, tt_change_len);
910 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200911 }
912 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200913 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000914
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800915container_register:
916 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200917 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800918 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919}
920
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200921int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922{
923 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200924 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200925 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200926 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100927 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200928 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200929 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000930 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200931 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200932 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100933 int last_seen_secs;
934 int last_seen_msecs;
935 unsigned long last_seen_jiffies;
936 bool no_purge;
937 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000938
Marek Lindner30da63a2012-08-03 17:15:46 +0200939 primary_if = batadv_seq_print_text_primary_if_get(seq);
940 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200941 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100943 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200944 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
945 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100946 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200947 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000948
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000949 for (i = 0; i < hash->size; i++) {
950 head = &hash->table[i];
951
Marek Lindner7aadf882011-02-18 12:28:09 +0000952 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800953 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000954 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100955 tt_local = container_of(tt_common_entry,
956 struct batadv_tt_local_entry,
957 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200958 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100959 last_seen_jiffies = jiffies - tt_local->last_seen;
960 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
961 last_seen_secs = last_seen_msecs / 1000;
962 last_seen_msecs = last_seen_msecs % 1000;
963
964 no_purge = tt_common_entry->flags & np_flag;
965
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200966 vlan = batadv_softif_vlan_get(bat_priv, vid);
967 if (!vlan) {
968 seq_printf(seq, "Cannot retrieve VLAN %d\n",
969 BATADV_PRINT_VID(vid));
970 continue;
971 }
972
973 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100974 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100975 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200976 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200977 ((tt_common_entry->flags &
978 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100979 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200980 ((tt_common_entry->flags &
981 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
982 ((tt_common_entry->flags &
983 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
984 ((tt_common_entry->flags &
985 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
986 ((tt_common_entry->flags &
987 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100988 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200989 no_purge ? 0 : last_seen_msecs,
990 vlan->tt.crc);
991
992 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000993 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000994 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200996out:
997 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200998 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200999 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001000}
1001
Sven Eckelmann56303d32012-06-05 22:31:31 +02001002static void
1003batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1004 struct batadv_tt_local_entry *tt_local_entry,
1005 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001007 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008
Antonio Quartulli015758d2011-07-09 17:52:13 +02001009 /* The local client has to be marked as "pending to be removed" but has
1010 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001011 * response issued before the net ttvn increment (consistency check)
1012 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001013 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001014
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001015 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001016 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1017 tt_local_entry->common.addr,
1018 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019}
1020
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001021/**
1022 * batadv_tt_local_remove - logically remove an entry from the local table
1023 * @bat_priv: the bat priv with all the soft interface information
1024 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001025 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001026 * @message: message to append to the log on deletion
1027 * @roaming: true if the deletion is due to a roaming event
1028 *
1029 * Returns the flags assigned to the local entry before being deleted
1030 */
1031uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001032 const uint8_t *addr, unsigned short vid,
1033 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001035 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001036 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001037 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001038
Antonio Quartullic018ad32013-06-04 12:11:39 +02001039 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001040 if (!tt_local_entry)
1041 goto out;
1042
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001043 curr_flags = tt_local_entry->common.flags;
1044
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001045 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001046 /* if this global entry addition is due to a roaming, the node has to
1047 * mark the local entry as "roamed" in order to correctly reroute
1048 * packets later
1049 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001050 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001051 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001052 /* mark the local client as ROAMed */
1053 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1054 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001055
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001056 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1057 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1058 message);
1059 goto out;
1060 }
1061 /* if this client has been added right now, it is possible to
1062 * immediately purge it
1063 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001064 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001065 hlist_del_rcu(&tt_local_entry->common.hash_entry);
1066 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001067
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001068 /* decrease the reference held for this vlan */
1069 vlan = batadv_softif_vlan_get(bat_priv, vid);
1070 batadv_softif_vlan_free_ref(vlan);
1071 batadv_softif_vlan_free_ref(vlan);
1072
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001073out:
1074 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001075 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001076
1077 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001078}
1079
Marek Lindnera19d3d82013-05-27 15:33:25 +08001080/**
1081 * batadv_tt_local_purge_list - purge inactive tt local entries
1082 * @bat_priv: the bat priv with all the soft interface information
1083 * @head: pointer to the list containing the local tt entries
1084 * @timeout: parameter deciding whether a given tt local entry is considered
1085 * inactive or not
1086 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001087static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001088 struct hlist_head *head,
1089 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001091 struct batadv_tt_local_entry *tt_local_entry;
1092 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001093 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001094
Sasha Levinb67bfe02013-02-27 17:06:00 -08001095 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001096 hash_entry) {
1097 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001098 struct batadv_tt_local_entry,
1099 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001100 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1101 continue;
1102
1103 /* entry already marked for deletion */
1104 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1105 continue;
1106
Marek Lindnera19d3d82013-05-27 15:33:25 +08001107 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001108 continue;
1109
1110 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1111 BATADV_TT_CLIENT_DEL, "timed out");
1112 }
1113}
1114
Marek Lindnera19d3d82013-05-27 15:33:25 +08001115/**
1116 * batadv_tt_local_purge - purge inactive tt local entries
1117 * @bat_priv: the bat priv with all the soft interface information
1118 * @timeout: parameter deciding whether a given tt local entry is considered
1119 * inactive or not
1120 */
1121static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1122 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001123{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001124 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001126 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001127 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129 for (i = 0; i < hash->size; i++) {
1130 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001131 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001132
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001133 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001134 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001135 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001136 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137}
1138
Sven Eckelmann56303d32012-06-05 22:31:31 +02001139static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001140{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001141 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001142 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143 struct batadv_tt_common_entry *tt_common_entry;
1144 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001145 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001146 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001147 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001148 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001149
Sven Eckelmann807736f2012-07-15 22:26:51 +02001150 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151 return;
1152
Sven Eckelmann807736f2012-07-15 22:26:51 +02001153 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001154
1155 for (i = 0; i < hash->size; i++) {
1156 head = &hash->table[i];
1157 list_lock = &hash->list_locks[i];
1158
1159 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001160 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001161 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001162 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001163 tt_local = container_of(tt_common_entry,
1164 struct batadv_tt_local_entry,
1165 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001166
1167 /* decrease the reference held for this vlan */
1168 vlan = batadv_softif_vlan_get(bat_priv,
1169 tt_common_entry->vid);
1170 batadv_softif_vlan_free_ref(vlan);
1171 batadv_softif_vlan_free_ref(vlan);
1172
Sven Eckelmann56303d32012-06-05 22:31:31 +02001173 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001174 }
1175 spin_unlock_bh(list_lock);
1176 }
1177
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001178 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001179
Sven Eckelmann807736f2012-07-15 22:26:51 +02001180 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001181}
1182
Sven Eckelmann56303d32012-06-05 22:31:31 +02001183static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001185 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001186 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001187
Sven Eckelmann807736f2012-07-15 22:26:51 +02001188 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001189
Sven Eckelmann807736f2012-07-15 22:26:51 +02001190 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001191 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001192
Antonio Quartullidec05072012-11-10 11:00:32 +01001193 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1194 &batadv_tt_global_hash_lock_class_key);
1195
Sven Eckelmann5346c352012-05-05 13:27:28 +02001196 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001197}
1198
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001201 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001202
Sven Eckelmann807736f2012-07-15 22:26:51 +02001203 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001204
Sven Eckelmann807736f2012-07-15 22:26:51 +02001205 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001206 list) {
1207 list_del(&entry->list);
1208 kfree(entry);
1209 }
1210
Sven Eckelmann807736f2012-07-15 22:26:51 +02001211 atomic_set(&bat_priv->tt.local_changes, 0);
1212 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001213}
1214
Antonio Quartullid657e622012-07-01 14:09:12 +02001215/* retrieves the orig_tt_list_entry belonging to orig_node from the
1216 * batadv_tt_global_entry list
1217 *
1218 * returns it with an increased refcounter, NULL if not found
1219 */
1220static struct batadv_tt_orig_list_entry *
1221batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1222 const struct batadv_orig_node *orig_node)
1223{
1224 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1225 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001226
1227 rcu_read_lock();
1228 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001230 if (tmp_orig_entry->orig_node != orig_node)
1231 continue;
1232 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1233 continue;
1234
1235 orig_entry = tmp_orig_entry;
1236 break;
1237 }
1238 rcu_read_unlock();
1239
1240 return orig_entry;
1241}
1242
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001243/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001244 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001245 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001246static bool
1247batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1248 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001249{
Antonio Quartullid657e622012-07-01 14:09:12 +02001250 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001251 bool found = false;
1252
Antonio Quartullid657e622012-07-01 14:09:12 +02001253 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1254 if (orig_entry) {
1255 found = true;
1256 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001257 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001258
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001259 return found;
1260}
1261
Sven Eckelmanna5130882012-05-16 20:23:16 +02001262static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001263batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001264 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001265{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001266 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267
Antonio Quartullid657e622012-07-01 14:09:12 +02001268 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001269 if (orig_entry) {
1270 /* refresh the ttvn: the current value could be a bogus one that
1271 * was added during a "temporary client detection"
1272 */
1273 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001274 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001275 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001276
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1278 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001279 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001280
1281 INIT_HLIST_NODE(&orig_entry->list);
1282 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001283 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001284 orig_entry->orig_node = orig_node;
1285 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001286 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001287
Antonio Quartullid657e622012-07-01 14:09:12 +02001288 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001289 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001290 &tt_global->orig_list);
1291 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001292 atomic_inc(&tt_global->orig_list_count);
1293
Antonio Quartullid657e622012-07-01 14:09:12 +02001294out:
1295 if (orig_entry)
1296 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001297}
1298
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001299/**
1300 * batadv_tt_global_add - add a new TT global entry or update an existing one
1301 * @bat_priv: the bat priv with all the soft interface information
1302 * @orig_node: the originator announcing the client
1303 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001304 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001305 * @flags: TT flags that have to be set for this non-mesh client
1306 * @ttvn: the tt version number ever announcing this non-mesh client
1307 *
1308 * Add a new TT global entry for the given originator. If the entry already
1309 * exists add a new reference to the given originator (a global entry can have
1310 * references to multiple originators) and adjust the flags attribute to reflect
1311 * the function argument.
1312 * If a TT local entry exists for this non-mesh client remove it.
1313 *
1314 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001315 *
1316 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001317 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001318static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1319 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001320 const unsigned char *tt_addr,
1321 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001322 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001323{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001324 struct batadv_tt_global_entry *tt_global_entry;
1325 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001326 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001327 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001328 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001329 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001330
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001331 /* ignore global entries from backbone nodes */
1332 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1333 return true;
1334
Antonio Quartullic018ad32013-06-04 12:11:39 +02001335 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1336 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001337
1338 /* if the node already has a local client for this entry, it has to wait
1339 * for a roaming advertisement instead of manually messing up the global
1340 * table
1341 */
1342 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1343 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1344 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001345
Antonio Quartullia73105b2011-04-27 14:27:44 +02001346 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001347 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001348 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001349 goto out;
1350
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001351 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001352 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001353 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001354
Antonio Quartullid4f44692012-05-25 00:00:54 +02001355 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001356 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001357 /* node must store current time in case of roaming. This is
1358 * needed to purge this entry out on timeout (if nobody claims
1359 * it)
1360 */
1361 if (flags & BATADV_TT_CLIENT_ROAM)
1362 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001363 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001364 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001365
1366 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001367 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001368 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001369
Sven Eckelmann807736f2012-07-15 22:26:51 +02001370 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001371 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001372 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001373 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001374
1375 if (unlikely(hash_added != 0)) {
1376 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001377 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001378 goto out_remove;
1379 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001380 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001381 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001382 /* If there is already a global entry, we can use this one for
1383 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001384 * But if we are trying to add a temporary client then here are
1385 * two options at this point:
1386 * 1) the global client is not a temporary client: the global
1387 * client has to be left as it is, temporary information
1388 * should never override any already known client state
1389 * 2) the global client is a temporary client: purge the
1390 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001391 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001392 if (flags & BATADV_TT_CLIENT_TEMP) {
1393 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1394 goto out;
1395 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1396 orig_node))
1397 goto out_remove;
1398 batadv_tt_global_del_orig_list(tt_global_entry);
1399 goto add_orig_entry;
1400 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001401
1402 /* if the client was temporary added before receiving the first
1403 * OGM announcing it, we have to clear the TEMP flag
1404 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001405 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001406
Antonio Quartullie9c001362012-11-07 15:05:33 +01001407 /* the change can carry possible "attribute" flags like the
1408 * TT_CLIENT_WIFI, therefore they have to be copied in the
1409 * client entry
1410 */
1411 tt_global_entry->common.flags |= flags;
1412
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001413 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1414 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001415 * delete + roaming change for this originator.
1416 *
1417 * We should first delete the old originator before adding the
1418 * new one.
1419 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001420 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001421 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001422 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001423 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001424 }
1425 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001426add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001427 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001428 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001429
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001430 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001431 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1432 common->addr, BATADV_PRINT_VID(common->vid),
1433 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001434 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001436out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001437 /* Do not remove multicast addresses from the local hash on
1438 * global additions
1439 */
1440 if (is_multicast_ether_addr(tt_addr))
1441 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001442
Antonio Quartullia73105b2011-04-27 14:27:44 +02001443 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001444 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001445 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001446 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001447 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1448
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001449 if (!(flags & BATADV_TT_CLIENT_ROAM))
1450 /* this is a normal global add. Therefore the client is not in a
1451 * roaming state anymore.
1452 */
1453 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1454
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001455out:
1456 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001457 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001458 if (tt_local_entry)
1459 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001460 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001461}
1462
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001463/**
1464 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001465 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001466 * @tt_global_entry: global translation table entry to be analyzed
1467 *
1468 * This functon assumes the caller holds rcu_read_lock().
1469 * Returns best originator list entry or NULL on errors.
1470 */
1471static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001472batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1473 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001474{
Antonio Quartulli46274562013-09-03 11:10:24 +02001475 struct batadv_neigh_node *router, *best_router = NULL;
1476 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001477 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001478 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001479
1480 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001481 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001482 router = batadv_orig_router_get(orig_entry->orig_node,
1483 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001484 if (!router)
1485 continue;
1486
Antonio Quartulli46274562013-09-03 11:10:24 +02001487 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001488 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1489 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001490 batadv_neigh_node_free_ref(router);
1491 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001492 }
1493
Antonio Quartulli46274562013-09-03 11:10:24 +02001494 /* release the refcount for the "old" best */
1495 if (best_router)
1496 batadv_neigh_node_free_ref(best_router);
1497
1498 best_entry = orig_entry;
1499 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001500 }
1501
Antonio Quartulli46274562013-09-03 11:10:24 +02001502 if (best_router)
1503 batadv_neigh_node_free_ref(best_router);
1504
Sven Eckelmann981d8902012-10-07 13:34:15 +02001505 return best_entry;
1506}
1507
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001508/**
1509 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1510 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001511 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001512 * @tt_global_entry: global translation table entry to be printed
1513 * @seq: debugfs table seq_file struct
1514 *
1515 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001516 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001517static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001518batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1519 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001520 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001521{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001522 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001523 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001524 struct batadv_orig_node_vlan *vlan;
1525 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001526 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001527 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001528
1529 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001530 flags = tt_common_entry->flags;
1531
Antonio Quartulli46274562013-09-03 11:10:24 +02001532 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001533 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001534 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1535 tt_common_entry->vid);
1536 if (!vlan) {
1537 seq_printf(seq,
1538 " * Cannot retrieve VLAN %d for originator %pM\n",
1539 BATADV_PRINT_VID(tt_common_entry->vid),
1540 best_entry->orig_node->orig);
1541 goto print_list;
1542 }
1543
Sven Eckelmann981d8902012-10-07 13:34:15 +02001544 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001545 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001546 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001547 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001548 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001549 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001550 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001551 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1552 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1553 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1554 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001555
1556 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001557 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001558
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001559print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001560 head = &tt_global_entry->orig_list;
1561
Sasha Levinb67bfe02013-02-27 17:06:00 -08001562 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001563 if (best_entry == orig_entry)
1564 continue;
1565
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001566 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1567 tt_common_entry->vid);
1568 if (!vlan) {
1569 seq_printf(seq,
1570 " + Cannot retrieve VLAN %d for originator %pM\n",
1571 BATADV_PRINT_VID(tt_common_entry->vid),
1572 orig_entry->orig_node->orig);
1573 continue;
1574 }
1575
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001576 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001577 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001578 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001579 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001580 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001581 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001582 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001583 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1584 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1585 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1586 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001587
1588 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001589 }
1590}
1591
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001592int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001593{
1594 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001595 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001596 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001597 struct batadv_tt_common_entry *tt_common_entry;
1598 struct batadv_tt_global_entry *tt_global;
1599 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001600 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001601 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001602
Marek Lindner30da63a2012-08-03 17:15:46 +02001603 primary_if = batadv_seq_print_text_primary_if_get(seq);
1604 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001605 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001606
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001607 seq_printf(seq,
1608 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001609 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001610 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1611 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1612 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001613
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001614 for (i = 0; i < hash->size; i++) {
1615 head = &hash->table[i];
1616
Marek Lindner7aadf882011-02-18 12:28:09 +00001617 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001618 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001619 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001620 tt_global = container_of(tt_common_entry,
1621 struct batadv_tt_global_entry,
1622 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001623 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001624 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001625 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001626 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001627out:
1628 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001629 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001630 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001631}
1632
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001633/**
1634 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1635 * @tt_global_entry: the global entry to remove the orig_entry from
1636 * @orig_entry: the orig entry to remove and free
1637 *
1638 * Remove an orig_entry from its list in the given tt_global_entry and
1639 * free this orig_entry afterwards.
1640 */
1641static void
1642batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1643 struct batadv_tt_orig_list_entry *orig_entry)
1644{
1645 batadv_tt_global_size_dec(orig_entry->orig_node,
1646 tt_global_entry->common.vid);
1647 atomic_dec(&tt_global_entry->orig_list_count);
1648 hlist_del_rcu(&orig_entry->list);
1649 batadv_tt_orig_list_entry_free_ref(orig_entry);
1650}
1651
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001652/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001653static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001654batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001655{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001656 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001657 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001658 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001660 spin_lock_bh(&tt_global_entry->list_lock);
1661 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001662 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1663 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001664 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001665}
1666
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001667/**
1668 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1669 * @bat_priv: the bat priv with all the soft interface information
1670 * @tt_global_entry: the global entry to remove the orig_node from
1671 * @orig_node: the originator announcing the client
1672 * @message: message to append to the log on deletion
1673 *
1674 * Remove the given orig_node and its according orig_entry from the given
1675 * global tt entry.
1676 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001677static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001678batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1679 struct batadv_tt_global_entry *tt_global_entry,
1680 struct batadv_orig_node *orig_node,
1681 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001682{
1683 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001684 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001685 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001686 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001687
1688 spin_lock_bh(&tt_global_entry->list_lock);
1689 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001690 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001691 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001692 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001693 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001694 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001695 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001696 tt_global_entry->common.addr,
1697 BATADV_PRINT_VID(vid), message);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001698 batadv_tt_global_del_orig_entry(tt_global_entry,
1699 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001700 }
1701 }
1702 spin_unlock_bh(&tt_global_entry->list_lock);
1703}
1704
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001705/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001706 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1707 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001708 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001709static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001710batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1711 struct batadv_tt_global_entry *tt_global_entry,
1712 struct batadv_orig_node *orig_node,
1713 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001714{
1715 bool last_entry = true;
1716 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001717 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001718
1719 /* no local entry exists, case 1:
1720 * Check if this is the last one or if other entries exist.
1721 */
1722
1723 rcu_read_lock();
1724 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001725 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001726 if (orig_entry->orig_node != orig_node) {
1727 last_entry = false;
1728 break;
1729 }
1730 }
1731 rcu_read_unlock();
1732
1733 if (last_entry) {
1734 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001735 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001736 tt_global_entry->roam_at = jiffies;
1737 } else
1738 /* there is another entry, we can simply delete this
1739 * one and can still use the other one.
1740 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001741 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1742 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001743}
1744
Antonio Quartullic018ad32013-06-04 12:11:39 +02001745/**
1746 * batadv_tt_global_del - remove a client from the global table
1747 * @bat_priv: the bat priv with all the soft interface information
1748 * @orig_node: an originator serving this client
1749 * @addr: the mac address of the client
1750 * @vid: VLAN identifier
1751 * @message: a message explaining the reason for deleting the client to print
1752 * for debugging purpose
1753 * @roaming: true if the deletion has been triggered by a roaming event
1754 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001755static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1756 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001757 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001758 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001759{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001760 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001761 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001762
Antonio Quartullic018ad32013-06-04 12:11:39 +02001763 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001764 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001765 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001766
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001767 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001768 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1769 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001770
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001771 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001772 batadv_tt_global_free(bat_priv, tt_global_entry,
1773 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001774
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001775 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001777
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001778 /* if we are deleting a global entry due to a roam
1779 * event, there are two possibilities:
1780 * 1) the client roamed from node A to node B => if there
1781 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001782 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001783 * wait for node B to claim it. In case of timeout
1784 * the entry is purged.
1785 *
1786 * If there are other originators left, we directly delete
1787 * the originator.
1788 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001789 * the global entry, since it is useless now.
1790 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001791 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001792 tt_global_entry->common.addr,
1793 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001794 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001795 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001796 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001797 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001798 } else
1799 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001800 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1801 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001802
Antonio Quartullicc47f662011-04-27 14:27:57 +02001803out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001804 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001805 batadv_tt_global_entry_free_ref(tt_global_entry);
1806 if (local_entry)
1807 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808}
1809
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001810/**
1811 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1812 * given originator matching the provided vid
1813 * @bat_priv: the bat priv with all the soft interface information
1814 * @orig_node: the originator owning the entries to remove
1815 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1816 * removed
1817 * @message: debug message to print as "reason"
1818 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001819void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1820 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001821 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001822 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001824 struct batadv_tt_global_entry *tt_global;
1825 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001826 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001827 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001828 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001830 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001831 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832
Simon Wunderlich6e801492011-10-19 10:28:26 +02001833 if (!hash)
1834 return;
1835
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836 for (i = 0; i < hash->size; i++) {
1837 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001838 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001839
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001840 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001841 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001842 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001843 /* remove only matching entries */
1844 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1845 continue;
1846
Sven Eckelmann56303d32012-06-05 22:31:31 +02001847 tt_global = container_of(tt_common_entry,
1848 struct batadv_tt_global_entry,
1849 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001850
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001851 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1852 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001853
Sven Eckelmann56303d32012-06-05 22:31:31 +02001854 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001855 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001856 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001857 "Deleting global tt entry %pM (vid: %d): %s\n",
1858 tt_global->common.addr,
1859 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001860 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001861 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001862 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001864 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001865 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02001866 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001867}
1868
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001869static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1870 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001871{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001872 bool purge = false;
1873 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1874 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001875
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001876 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1877 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1878 purge = true;
1879 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001880 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001881
1882 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1883 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1884 purge = true;
1885 *msg = "Temporary client timeout\n";
1886 }
1887
1888 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001889}
1890
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001891static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001892{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001893 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001894 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001895 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001896 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001897 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001898 char *msg = NULL;
1899 struct batadv_tt_common_entry *tt_common;
1900 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001901
Antonio Quartullicc47f662011-04-27 14:27:57 +02001902 for (i = 0; i < hash->size; i++) {
1903 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001904 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001905
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001906 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001907 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001908 hash_entry) {
1909 tt_global = container_of(tt_common,
1910 struct batadv_tt_global_entry,
1911 common);
1912
1913 if (!batadv_tt_global_to_purge(tt_global, &msg))
1914 continue;
1915
1916 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001917 "Deleting global tt entry %pM (vid: %d): %s\n",
1918 tt_global->common.addr,
1919 BATADV_PRINT_VID(tt_global->common.vid),
1920 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001921
Sasha Levinb67bfe02013-02-27 17:06:00 -08001922 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001923
1924 batadv_tt_global_entry_free_ref(tt_global);
1925 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001926 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001927 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001928}
1929
Sven Eckelmann56303d32012-06-05 22:31:31 +02001930static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001931{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001932 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001933 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001934 struct batadv_tt_common_entry *tt_common_entry;
1935 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001936 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001937 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001938 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001939
Sven Eckelmann807736f2012-07-15 22:26:51 +02001940 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001941 return;
1942
Sven Eckelmann807736f2012-07-15 22:26:51 +02001943 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001944
1945 for (i = 0; i < hash->size; i++) {
1946 head = &hash->table[i];
1947 list_lock = &hash->list_locks[i];
1948
1949 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001950 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001951 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001952 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001953 tt_global = container_of(tt_common_entry,
1954 struct batadv_tt_global_entry,
1955 common);
1956 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001957 }
1958 spin_unlock_bh(list_lock);
1959 }
1960
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001961 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001962
Sven Eckelmann807736f2012-07-15 22:26:51 +02001963 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001964}
1965
Sven Eckelmann56303d32012-06-05 22:31:31 +02001966static bool
1967_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1968 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001969{
1970 bool ret = false;
1971
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001972 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1973 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001974 ret = true;
1975
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001976 /* check if the two clients are marked as isolated */
1977 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1978 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1979 ret = true;
1980
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001981 return ret;
1982}
1983
Antonio Quartullic018ad32013-06-04 12:11:39 +02001984/**
1985 * batadv_transtable_search - get the mesh destination for a given client
1986 * @bat_priv: the bat priv with all the soft interface information
1987 * @src: mac address of the source client
1988 * @addr: mac address of the destination client
1989 * @vid: VLAN identifier
1990 *
1991 * Returns a pointer to the originator that was selected as destination in the
1992 * mesh for contacting the client 'addr', NULL otherwise.
1993 * In case of multiple originators serving the same client, the function returns
1994 * the best one (best in terms of metric towards the destination node).
1995 *
1996 * If the two clients are AP isolated the function returns NULL.
1997 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001998struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1999 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002000 const uint8_t *addr,
2001 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002002{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002003 struct batadv_tt_local_entry *tt_local_entry = NULL;
2004 struct batadv_tt_global_entry *tt_global_entry = NULL;
2005 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002006 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002007
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002008 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002009 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002010 if (!tt_local_entry ||
2011 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002012 goto out;
2013 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002014
Antonio Quartullic018ad32013-06-04 12:11:39 +02002015 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002016 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002017 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002018
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002019 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002020 * isolation
2021 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002022 if (tt_local_entry &&
2023 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002024 goto out;
2025
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002026 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002027 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002028 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002029 if (best_entry)
2030 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002031 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2032 orig_node = NULL;
2033 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002034
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002035out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002036 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002037 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002038 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002039 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002040
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002041 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002042}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043
Antonio Quartulliced72932013-04-24 16:37:51 +02002044/**
2045 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2046 * to the given orig_node
2047 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002048 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002049 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002050 *
2051 * This function computes the checksum for the global table corresponding to a
2052 * specific originator. In particular, the checksum is computed as follows: For
2053 * each client connected to the originator the CRC32C of the MAC address and the
2054 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2055 * together.
2056 *
2057 * The idea behind is that CRC32C should be used as much as possible in order to
2058 * produce a unique hash of the table, but since the order which is used to feed
2059 * the CRC32C function affects the result and since every node in the network
2060 * probably sorts the clients differently, the hash function cannot be directly
2061 * computed over the entire table. Hence the CRC32C is used only on
2062 * the single client entry, while all the results are then xor'ed together
2063 * because the XOR operation can combine them all while trying to reduce the
2064 * noise as much as possible.
2065 *
2066 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002067 */
2068static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002069 struct batadv_orig_node *orig_node,
2070 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002072 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002073 struct batadv_tt_common_entry *tt_common;
2074 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002075 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002076 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002077 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002078 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002079
2080 for (i = 0; i < hash->size; i++) {
2081 head = &hash->table[i];
2082
2083 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002084 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002085 tt_global = container_of(tt_common,
2086 struct batadv_tt_global_entry,
2087 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002088 /* compute the CRC only for entries belonging to the
2089 * VLAN identified by the vid passed as parameter
2090 */
2091 if (tt_common->vid != vid)
2092 continue;
2093
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002094 /* Roaming clients are in the global table for
2095 * consistency only. They don't have to be
2096 * taken into account while computing the
2097 * global crc
2098 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002099 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002100 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002101 /* Temporary clients have not been announced yet, so
2102 * they have to be skipped while computing the global
2103 * crc
2104 */
2105 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2106 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002107
2108 /* find out if this global entry is announced by this
2109 * originator
2110 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002111 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002112 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002113 continue;
2114
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002115 /* use network order to read the VID: this ensures that
2116 * every node reads the bytes in the same order.
2117 */
2118 tmp_vid = htons(tt_common->vid);
2119 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002120
2121 /* compute the CRC on flags that have to be kept in sync
2122 * among nodes
2123 */
2124 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2125 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2126
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002127 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002128 }
2129 rcu_read_unlock();
2130 }
2131
Antonio Quartulliced72932013-04-24 16:37:51 +02002132 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002133}
2134
Antonio Quartulliced72932013-04-24 16:37:51 +02002135/**
2136 * batadv_tt_local_crc - calculates the checksum of the local table
2137 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002138 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002139 *
2140 * For details about the computation, please refer to the documentation for
2141 * batadv_tt_global_crc().
2142 *
2143 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002144 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002145static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2146 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002147{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002148 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002149 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002150 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002151 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002152 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002153 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002154
2155 for (i = 0; i < hash->size; i++) {
2156 head = &hash->table[i];
2157
2158 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002159 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002160 /* compute the CRC only for entries belonging to the
2161 * VLAN identified by vid
2162 */
2163 if (tt_common->vid != vid)
2164 continue;
2165
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002166 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002167 * account while computing the CRC
2168 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002169 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002170 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002171
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002172 /* use network order to read the VID: this ensures that
2173 * every node reads the bytes in the same order.
2174 */
2175 tmp_vid = htons(tt_common->vid);
2176 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002177
2178 /* compute the CRC on flags that have to be kept in sync
2179 * among nodes
2180 */
2181 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2182 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2183
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002184 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002185 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002186 rcu_read_unlock();
2187 }
2188
Antonio Quartulliced72932013-04-24 16:37:51 +02002189 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002190}
2191
Sven Eckelmann56303d32012-06-05 22:31:31 +02002192static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002193{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002194 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002195
Sven Eckelmann807736f2012-07-15 22:26:51 +02002196 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002197
Sven Eckelmann807736f2012-07-15 22:26:51 +02002198 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner1f155102015-06-22 00:36:28 +08002199 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002200 kfree(node);
2201 }
2202
Sven Eckelmann807736f2012-07-15 22:26:51 +02002203 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002204}
2205
Sven Eckelmann56303d32012-06-05 22:31:31 +02002206static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2207 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002208 const void *tt_buff,
2209 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002210{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002211 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002212 * last OGM (the OGM could carry no changes)
2213 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002214 spin_lock_bh(&orig_node->tt_buff_lock);
2215 if (tt_buff_len > 0) {
2216 kfree(orig_node->tt_buff);
2217 orig_node->tt_buff_len = 0;
2218 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2219 if (orig_node->tt_buff) {
2220 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2221 orig_node->tt_buff_len = tt_buff_len;
2222 }
2223 }
2224 spin_unlock_bh(&orig_node->tt_buff_lock);
2225}
2226
Sven Eckelmann56303d32012-06-05 22:31:31 +02002227static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002228{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002229 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002230
Sven Eckelmann807736f2012-07-15 22:26:51 +02002231 spin_lock_bh(&bat_priv->tt.req_list_lock);
2232 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002233 if (batadv_has_timed_out(node->issued_at,
2234 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner1f155102015-06-22 00:36:28 +08002235 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002236 kfree(node);
2237 }
2238 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002239 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002240}
2241
2242/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002243 * has already been issued for this orig_node, NULL otherwise
2244 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002245static struct batadv_tt_req_node *
2246batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2247 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002248{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002249 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
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(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002253 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2254 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002255 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002256 goto unlock;
2257 }
2258
2259 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2260 if (!tt_req_node)
2261 goto unlock;
2262
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002263 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002264 tt_req_node->issued_at = jiffies;
2265
Sven Eckelmann807736f2012-07-15 22:26:51 +02002266 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002268 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002269 return tt_req_node;
2270}
2271
Marek Lindner335fbe02013-04-23 21:40:02 +08002272/**
2273 * batadv_tt_local_valid - verify that given tt entry is a valid one
2274 * @entry_ptr: to be checked local tt entry
2275 * @data_ptr: not used but definition required to satisfy the callback prototype
2276 *
2277 * Returns 1 if the entry is a valid, 0 otherwise.
2278 */
2279static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002280{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002281 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002282
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002283 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002284 return 0;
2285 return 1;
2286}
2287
Sven Eckelmanna5130882012-05-16 20:23:16 +02002288static int batadv_tt_global_valid(const void *entry_ptr,
2289 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002290{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002291 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2292 const struct batadv_tt_global_entry *tt_global_entry;
2293 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002294
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002295 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2296 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002297 return 0;
2298
Sven Eckelmann56303d32012-06-05 22:31:31 +02002299 tt_global_entry = container_of(tt_common_entry,
2300 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002301 common);
2302
Sven Eckelmanna5130882012-05-16 20:23:16 +02002303 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002304}
2305
Marek Lindner335fbe02013-04-23 21:40:02 +08002306/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002307 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2308 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002309 * @bat_priv: the bat priv with all the soft interface information
2310 * @hash: hash table containing the tt entries
2311 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002312 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002313 * @valid_cb: function to filter tt change entries
2314 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002315 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002316static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2317 struct batadv_hashtable *hash,
2318 void *tvlv_buff, uint16_t tt_len,
2319 int (*valid_cb)(const void *, const void *),
2320 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002321{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002322 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002323 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002324 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002325 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002326 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002327
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002328 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002329 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002330
2331 rcu_read_lock();
2332 for (i = 0; i < hash->size; i++) {
2333 head = &hash->table[i];
2334
Sasha Levinb67bfe02013-02-27 17:06:00 -08002335 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002336 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002337 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002338 break;
2339
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002340 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002341 continue;
2342
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002343 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002344 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002345 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002346 memset(tt_change->reserved, 0,
2347 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002348
Marek Lindner335fbe02013-04-23 21:40:02 +08002349 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002350 tt_change++;
2351 }
2352 }
2353 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002354}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002355
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002356/**
2357 * batadv_tt_global_check_crc - check if all the CRCs are correct
2358 * @orig_node: originator for which the CRCs have to be checked
2359 * @tt_vlan: pointer to the first tvlv VLAN entry
2360 * @num_vlan: number of tvlv VLAN entries
2361 * @create: if true, create VLAN objects if not found
2362 *
2363 * Return true if all the received CRCs match the locally stored ones, false
2364 * otherwise
2365 */
2366static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2367 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2368 uint16_t num_vlan)
2369{
2370 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2371 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002372 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002373 int i;
2374
2375 /* check if each received CRC matches the locally stored one */
2376 for (i = 0; i < num_vlan; i++) {
2377 tt_vlan_tmp = tt_vlan + i;
2378
2379 /* if orig_node is a backbone node for this VLAN, don't check
2380 * the CRC as we ignore all the global entries over it
2381 */
2382 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002383 orig_node->orig,
2384 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002385 continue;
2386
2387 vlan = batadv_orig_node_vlan_get(orig_node,
2388 ntohs(tt_vlan_tmp->vid));
2389 if (!vlan)
2390 return false;
2391
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002392 crc = vlan->tt.crc;
2393 batadv_orig_node_vlan_free_ref(vlan);
2394
2395 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002396 return false;
2397 }
2398
2399 return true;
2400}
2401
2402/**
2403 * batadv_tt_local_update_crc - update all the local CRCs
2404 * @bat_priv: the bat priv with all the soft interface information
2405 */
2406static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2407{
2408 struct batadv_softif_vlan *vlan;
2409
2410 /* recompute the global CRC for each VLAN */
2411 rcu_read_lock();
2412 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2413 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2414 }
2415 rcu_read_unlock();
2416}
2417
2418/**
2419 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2420 * @bat_priv: the bat priv with all the soft interface information
2421 * @orig_node: the orig_node for which the CRCs have to be updated
2422 */
2423static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2424 struct batadv_orig_node *orig_node)
2425{
2426 struct batadv_orig_node_vlan *vlan;
2427 uint32_t crc;
2428
2429 /* recompute the global CRC for each VLAN */
2430 rcu_read_lock();
2431 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2432 /* if orig_node is a backbone node for this VLAN, don't compute
2433 * the CRC as we ignore all the global entries over it
2434 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002435 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2436 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002437 continue;
2438
2439 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2440 vlan->tt.crc = crc;
2441 }
2442 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002443}
2444
Antonio Quartulliced72932013-04-24 16:37:51 +02002445/**
2446 * batadv_send_tt_request - send a TT Request message to a given node
2447 * @bat_priv: the bat priv with all the soft interface information
2448 * @dst_orig_node: the destination of the message
2449 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002450 * @tt_vlan: pointer to the first tvlv VLAN object to request
2451 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002452 * @full_table: ask for the entire translation table if true, while only for the
2453 * last TT diff otherwise
2454 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002455static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2456 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002457 uint8_t ttvn,
2458 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2459 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002460{
Marek Lindner335fbe02013-04-23 21:40:02 +08002461 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002462 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002463 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2464 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002465 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002466 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002467
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002468 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002469 if (!primary_if)
2470 goto out;
2471
2472 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002473 * reply from the same orig_node yet
2474 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002475 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002476 if (!tt_req_node)
2477 goto out;
2478
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002479 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2480 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002481 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002482 goto out;
2483
Marek Lindner335fbe02013-04-23 21:40:02 +08002484 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2485 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002486 tvlv_tt_data->num_vlan = htons(num_vlan);
2487
2488 /* send all the CRCs within the request. This is needed by intermediate
2489 * nodes to ensure they have the correct table before replying
2490 */
2491 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2492 for (i = 0; i < num_vlan; i++) {
2493 tt_vlan_req->vid = tt_vlan->vid;
2494 tt_vlan_req->crc = tt_vlan->crc;
2495
2496 tt_vlan_req++;
2497 tt_vlan++;
2498 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002499
2500 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002501 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002502
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002503 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002504 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002505
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002506 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002507 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2508 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002509 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002510 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002511
2512out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002513 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002514 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002515 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002516 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner1f155102015-06-22 00:36:28 +08002517 /* list_del_init() verifies tt_req_node still is in the list */
2518 list_del_init(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002519 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002520 kfree(tt_req_node);
2521 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002522 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002523 return ret;
2524}
2525
Marek Lindner335fbe02013-04-23 21:40:02 +08002526/**
2527 * batadv_send_other_tt_response - send reply to tt request concerning another
2528 * node's translation table
2529 * @bat_priv: the bat priv with all the soft interface information
2530 * @tt_data: tt data containing the tt request information
2531 * @req_src: mac address of tt request sender
2532 * @req_dst: mac address of tt request recipient
2533 *
2534 * Returns true if tt request reply was sent, false otherwise.
2535 */
2536static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2537 struct batadv_tvlv_tt_data *tt_data,
2538 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002539{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002540 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002541 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002542 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002543 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002544 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002545 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002546 uint8_t orig_ttvn, req_ttvn;
2547 uint16_t tvlv_len;
2548 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002549
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002550 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002551 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002552 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002553 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002554
2555 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002556 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002557 if (!req_dst_orig_node)
2558 goto out;
2559
Marek Lindner335fbe02013-04-23 21:40:02 +08002560 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002561 if (!res_dst_orig_node)
2562 goto out;
2563
Antonio Quartullia73105b2011-04-27 14:27:44 +02002564 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002565 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002566
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002567 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002568 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002569 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002570 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2571 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002572 goto out;
2573
Antonio Quartulli015758d2011-07-09 17:52:13 +02002574 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002575 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002576 !req_dst_orig_node->tt_buff)
2577 full_table = true;
2578 else
2579 full_table = false;
2580
Marek Lindner335fbe02013-04-23 21:40:02 +08002581 /* TT fragmentation hasn't been implemented yet, so send as many
2582 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002583 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002584 if (!full_table) {
2585 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2586 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002587
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002588 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2589 &tvlv_tt_data,
2590 &tt_change,
2591 &tt_len);
2592 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002593 goto unlock;
2594
Antonio Quartullia73105b2011-04-27 14:27:44 +02002595 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002596 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002597 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002598 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2599 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002600 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2601 * in the initial part
2602 */
2603 tt_len = -1;
2604 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2605 &tvlv_tt_data,
2606 &tt_change,
2607 &tt_len);
2608 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002609 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002610
2611 /* fill the rest of the tvlv with the real TT entries */
2612 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2613 tt_change, tt_len,
2614 batadv_tt_global_valid,
2615 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616 }
2617
Marek Lindnera19d3d82013-05-27 15:33:25 +08002618 /* Don't send the response, if larger than fragmented packet. */
2619 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2620 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2621 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2622 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2623 res_dst_orig_node->orig);
2624 goto out;
2625 }
2626
Marek Lindner335fbe02013-04-23 21:40:02 +08002627 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2628 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002629
2630 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002631 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002632
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002633 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002634 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2635 res_dst_orig_node->orig, req_dst_orig_node->orig,
2636 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002637
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002638 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002639
Marek Lindner335fbe02013-04-23 21:40:02 +08002640 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002641 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2642 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002643
Marek Lindner335fbe02013-04-23 21:40:02 +08002644 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002645 goto out;
2646
2647unlock:
2648 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2649
2650out:
2651 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002652 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002653 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002654 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002655 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002656 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002657}
Sven Eckelmann96412692012-06-05 22:31:30 +02002658
Marek Lindner335fbe02013-04-23 21:40:02 +08002659/**
2660 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2661 * translation table
2662 * @bat_priv: the bat priv with all the soft interface information
2663 * @tt_data: tt data containing the tt request information
2664 * @req_src: mac address of tt request sender
2665 *
2666 * Returns true if tt request reply was sent, false otherwise.
2667 */
2668static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2669 struct batadv_tvlv_tt_data *tt_data,
2670 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002671{
Marek Lindner335fbe02013-04-23 21:40:02 +08002672 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002673 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002674 struct batadv_tvlv_tt_change *tt_change;
2675 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002676 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002677 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002678 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002679 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002680
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002681 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002682 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002683 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002684 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002685
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002686 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002687
Sven Eckelmann807736f2012-07-15 22:26:51 +02002688 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002689 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690
Marek Lindner335fbe02013-04-23 21:40:02 +08002691 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002692 if (!orig_node)
2693 goto out;
2694
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002695 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002696 if (!primary_if)
2697 goto out;
2698
2699 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002700 * is too big send the whole local translation table
2701 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002702 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002703 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002704 full_table = true;
2705 else
2706 full_table = false;
2707
Marek Lindner335fbe02013-04-23 21:40:02 +08002708 /* TT fragmentation hasn't been implemented yet, so send as many
2709 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002710 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002711 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002712 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002713
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002714 tt_len = bat_priv->tt.last_changeset_len;
2715 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2716 &tvlv_tt_data,
2717 &tt_change,
2718 &tt_len);
2719 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002720 goto unlock;
2721
Marek Lindner335fbe02013-04-23 21:40:02 +08002722 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002723 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002724 bat_priv->tt.last_changeset_len);
2725 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002726 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002727 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002729 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2730 * in the initial part
2731 */
2732 tt_len = -1;
2733 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2734 &tvlv_tt_data,
2735 &tt_change,
2736 &tt_len);
2737 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002738 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002739
2740 /* fill the rest of the tvlv with the real TT entries */
2741 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2742 tt_change, tt_len,
2743 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002744 }
2745
Marek Lindner335fbe02013-04-23 21:40:02 +08002746 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2747 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002748
2749 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002750 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002751
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002752 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002753 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2754 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002755
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002756 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002757
Marek Lindner335fbe02013-04-23 21:40:02 +08002758 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002759 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2760 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002761
Antonio Quartullia73105b2011-04-27 14:27:44 +02002762 goto out;
2763
2764unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002765 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002766out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002767 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002768 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002769 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002770 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002771 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002772 kfree(tvlv_tt_data);
2773 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002774 return true;
2775}
2776
Marek Lindner335fbe02013-04-23 21:40:02 +08002777/**
2778 * batadv_send_tt_response - send reply to tt request
2779 * @bat_priv: the bat priv with all the soft interface information
2780 * @tt_data: tt data containing the tt request information
2781 * @req_src: mac address of tt request sender
2782 * @req_dst: mac address of tt request recipient
2783 *
2784 * Returns true if tt request reply was sent, false otherwise.
2785 */
2786static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2787 struct batadv_tvlv_tt_data *tt_data,
2788 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002789{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002790 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002791 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002792 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2793 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794}
2795
Sven Eckelmann56303d32012-06-05 22:31:31 +02002796static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2797 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002798 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002799 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002800{
2801 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002802 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002803
2804 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002805 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2806 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002807 batadv_tt_global_del(bat_priv, orig_node,
2808 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002809 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002810 "tt removed by changes",
2811 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002812 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002813 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002814 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002815 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002816 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817 /* In case of problem while storing a
2818 * global_entry, we stop the updating
2819 * procedure without committing the
2820 * ttvn change. This will avoid to send
2821 * corrupted data on tt_request
2822 */
2823 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002824 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002825 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002826 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827}
2828
Sven Eckelmann56303d32012-06-05 22:31:31 +02002829static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002830 struct batadv_tvlv_tt_change *tt_change,
2831 uint8_t ttvn, uint8_t *resp_src,
2832 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002834 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002835
Marek Lindner335fbe02013-04-23 21:40:02 +08002836 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837 if (!orig_node)
2838 goto out;
2839
2840 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002841 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2842 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002844 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2845 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002846
2847 spin_lock_bh(&orig_node->tt_buff_lock);
2848 kfree(orig_node->tt_buff);
2849 orig_node->tt_buff_len = 0;
2850 orig_node->tt_buff = NULL;
2851 spin_unlock_bh(&orig_node->tt_buff_lock);
2852
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002853 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002854
2855out:
2856 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002857 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002858}
2859
Sven Eckelmann56303d32012-06-05 22:31:31 +02002860static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2861 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002862 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002863 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002864{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002865 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2866 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002867
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002868 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2869 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002870 atomic_set(&orig_node->last_ttvn, ttvn);
2871}
2872
Antonio Quartullic018ad32013-06-04 12:11:39 +02002873/**
2874 * batadv_is_my_client - check if a client is served by the local node
2875 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002876 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002877 * @vid: VLAN identifier
2878 *
2879 * Returns true if the client is served by this node, false otherwise.
2880 */
2881bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2882 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002883{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002884 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002885 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002886
Antonio Quartullic018ad32013-06-04 12:11:39 +02002887 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002888 if (!tt_local_entry)
2889 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002890 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002891 * consistency purpose)
2892 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002893 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2894 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002895 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002896 ret = true;
2897out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002898 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002899 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002900 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002901}
2902
Marek Lindner335fbe02013-04-23 21:40:02 +08002903/**
2904 * batadv_handle_tt_response - process incoming tt reply
2905 * @bat_priv: the bat priv with all the soft interface information
2906 * @tt_data: tt data containing the tt request information
2907 * @resp_src: mac address of tt reply sender
2908 * @num_entries: number of tt change entries appended to the tt data
2909 */
2910static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2911 struct batadv_tvlv_tt_data *tt_data,
2912 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002913{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002914 struct batadv_tt_req_node *node, *safe;
2915 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002916 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002917 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2918 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002919
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002920 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002921 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002922 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002923 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002924
Marek Lindner335fbe02013-04-23 21:40:02 +08002925 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002926 if (!orig_node)
2927 goto out;
2928
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002929 spin_lock_bh(&orig_node->tt_lock);
2930
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002931 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2932 change_offset *= ntohs(tt_data->num_vlan);
2933 change_offset += sizeof(*tt_data);
2934 tvlv_ptr += change_offset;
2935
2936 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002937 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002938 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2939 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002940 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002941 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2942 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002943 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002944
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002945 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002946 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002947
2948 spin_unlock_bh(&orig_node->tt_lock);
2949
Antonio Quartullia73105b2011-04-27 14:27:44 +02002950 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002951 spin_lock_bh(&bat_priv->tt.req_list_lock);
2952 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002953 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002954 continue;
Marek Lindner1f155102015-06-22 00:36:28 +08002955 list_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002956 kfree(node);
2957 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002958
Sven Eckelmann807736f2012-07-15 22:26:51 +02002959 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002960out:
2961 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002962 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002963}
2964
Sven Eckelmann56303d32012-06-05 22:31:31 +02002965static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002966{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002967 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002968
Sven Eckelmann807736f2012-07-15 22:26:51 +02002969 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002970
Sven Eckelmann807736f2012-07-15 22:26:51 +02002971 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002972 list_del(&node->list);
2973 kfree(node);
2974 }
2975
Sven Eckelmann807736f2012-07-15 22:26:51 +02002976 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002977}
2978
Sven Eckelmann56303d32012-06-05 22:31:31 +02002979static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002980{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002981 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002982
Sven Eckelmann807736f2012-07-15 22:26:51 +02002983 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2984 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002985 if (!batadv_has_timed_out(node->first_time,
2986 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002987 continue;
2988
2989 list_del(&node->list);
2990 kfree(node);
2991 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002992 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002993}
2994
2995/* This function checks whether the client already reached the
2996 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2997 * will not be sent.
2998 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002999 * returns true if the ROAMING_ADV can be sent, false otherwise
3000 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003001static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02003002 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003003{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003004 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003005 bool ret = false;
3006
Sven Eckelmann807736f2012-07-15 22:26:51 +02003007 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003008 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003009 * reply from the same orig_node yet
3010 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003011 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003012 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003013 continue;
3014
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003015 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003016 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003017 continue;
3018
Sven Eckelmann3e348192012-05-16 20:23:22 +02003019 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003020 /* Sorry, you roamed too many times! */
3021 goto unlock;
3022 ret = true;
3023 break;
3024 }
3025
3026 if (!ret) {
3027 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3028 if (!tt_roam_node)
3029 goto unlock;
3030
3031 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003032 atomic_set(&tt_roam_node->counter,
3033 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003034 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003035
Sven Eckelmann807736f2012-07-15 22:26:51 +02003036 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003037 ret = true;
3038 }
3039
3040unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003041 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003042 return ret;
3043}
3044
Antonio Quartullic018ad32013-06-04 12:11:39 +02003045/**
3046 * batadv_send_roam_adv - send a roaming advertisement message
3047 * @bat_priv: the bat priv with all the soft interface information
3048 * @client: mac address of the roaming client
3049 * @vid: VLAN identifier
3050 * @orig_node: message destination
3051 *
3052 * Send a ROAMING_ADV message to the node which was previously serving this
3053 * client. This is done to inform the node that from now on all traffic destined
3054 * for this particular roamed client has to be forwarded to the sender of the
3055 * roaming message.
3056 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003057static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003058 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003059 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003060{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003061 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003062 struct batadv_tvlv_roam_adv tvlv_roam;
3063
3064 primary_if = batadv_primary_if_get_selected(bat_priv);
3065 if (!primary_if)
3066 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003067
3068 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003069 * already roamed to us too many times
3070 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003071 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003072 goto out;
3073
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003074 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003075 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3076 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003077
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003078 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003079
Marek Lindner122edaa2013-04-23 21:40:03 +08003080 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003081 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003082
3083 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3084 orig_node->orig, BATADV_TVLV_ROAM, 1,
3085 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003086
3087out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003088 if (primary_if)
3089 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003090}
3091
Sven Eckelmanna5130882012-05-16 20:23:16 +02003092static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003093{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003094 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003095 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003096 struct batadv_priv *bat_priv;
3097
3098 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003099 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3100 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003101
Marek Lindnera19d3d82013-05-27 15:33:25 +08003102 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003103 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003104 batadv_tt_req_purge(bat_priv);
3105 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003106
Antonio Quartulli72414442012-12-25 13:14:37 +01003107 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3108 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003109}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003110
Sven Eckelmann56303d32012-06-05 22:31:31 +02003111void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003112{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003113 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3114 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3115
Sven Eckelmann807736f2012-07-15 22:26:51 +02003116 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003117
Sven Eckelmanna5130882012-05-16 20:23:16 +02003118 batadv_tt_local_table_free(bat_priv);
3119 batadv_tt_global_table_free(bat_priv);
3120 batadv_tt_req_list_free(bat_priv);
3121 batadv_tt_changes_list_free(bat_priv);
3122 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003123
Sven Eckelmann807736f2012-07-15 22:26:51 +02003124 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003125}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003126
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003127/**
3128 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3129 * table and possibly count them in the TT size
3130 * @bat_priv: the bat priv with all the soft interface information
3131 * @flags: the flag to switch
3132 * @enable: whether to set or unset the flag
3133 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003134 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003135static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3136 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003137{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003138 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3139 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003140 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003141 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003142 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003143
3144 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003145 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003146
3147 for (i = 0; i < hash->size; i++) {
3148 head = &hash->table[i];
3149
3150 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003151 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003152 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003153 if (enable) {
3154 if ((tt_common_entry->flags & flags) == flags)
3155 continue;
3156 tt_common_entry->flags |= flags;
3157 } else {
3158 if (!(tt_common_entry->flags & flags))
3159 continue;
3160 tt_common_entry->flags &= ~flags;
3161 }
3162 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003163
3164 if (!count)
3165 continue;
3166
3167 batadv_tt_local_size_inc(bat_priv,
3168 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003169 }
3170 rcu_read_unlock();
3171 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003172}
3173
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003174/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003175static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003176{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003177 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003178 struct batadv_tt_common_entry *tt_common;
3179 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003180 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003181 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003182 struct hlist_head *head;
3183 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003184 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003185
3186 if (!hash)
3187 return;
3188
3189 for (i = 0; i < hash->size; i++) {
3190 head = &hash->table[i];
3191 list_lock = &hash->list_locks[i];
3192
3193 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003194 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003195 hash_entry) {
3196 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003197 continue;
3198
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003199 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003200 "Deleting local tt entry (%pM, vid: %d): pending\n",
3201 tt_common->addr,
3202 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003203
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003204 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003205 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003206 tt_local = container_of(tt_common,
3207 struct batadv_tt_local_entry,
3208 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003209
3210 /* decrease the reference held for this vlan */
3211 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3212 batadv_softif_vlan_free_ref(vlan);
3213 batadv_softif_vlan_free_ref(vlan);
3214
Sven Eckelmann56303d32012-06-05 22:31:31 +02003215 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003216 }
3217 spin_unlock_bh(list_lock);
3218 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003219}
3220
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003221/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003222 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3223 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003224 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003225 *
3226 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003227 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003228static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003229{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003230 /* Update multicast addresses in local translation table */
3231 batadv_mcast_mla_update(bat_priv);
3232
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003233 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3234 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3235 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003236 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003237 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003238
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003239 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003240
Sven Eckelmanna5130882012-05-16 20:23:16 +02003241 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003242 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003243
3244 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003245 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003246 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003247 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003248 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003249
3250 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003251 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003252 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003253}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003254
Marek Lindnera19d3d82013-05-27 15:33:25 +08003255/**
3256 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3257 * have been queued in the time since the last commit
3258 * @bat_priv: the bat priv with all the soft interface information
3259 */
3260void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3261{
3262 spin_lock_bh(&bat_priv->tt.commit_lock);
3263 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003264 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003265}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003266
Sven Eckelmann56303d32012-06-05 22:31:31 +02003267bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003268 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003269{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003270 struct batadv_tt_local_entry *tt_local_entry = NULL;
3271 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003272 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003273 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003274
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003275 vlan = batadv_softif_vlan_get(bat_priv, vid);
3276 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003277 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003278
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003279 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003280 if (!tt_local_entry)
3281 goto out;
3282
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003283 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003284 if (!tt_global_entry)
3285 goto out;
3286
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003287 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003288 goto out;
3289
Marek Lindner5870adc2012-06-20 17:16:05 +02003290 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003291
3292out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003293 if (vlan)
3294 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003295 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003296 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003297 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003298 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003299 return ret;
3300}
Marek Lindnera943cac2011-07-30 13:10:18 +02003301
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003302/**
3303 * batadv_tt_update_orig - update global translation table with new tt
3304 * information received via ogms
3305 * @bat_priv: the bat priv with all the soft interface information
3306 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003307 * @tt_vlan: pointer to the first tvlv VLAN entry
3308 * @tt_num_vlan: number of tvlv VLAN entries
3309 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003310 * @tt_num_changes: number of tt changes inside the tt buffer
3311 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003312 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003313 */
3314static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3315 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003316 const void *tt_buff, uint16_t tt_num_vlan,
3317 struct batadv_tvlv_tt_change *tt_change,
3318 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003319{
3320 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003321 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003322 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003323 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003324
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003325 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003326 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3327 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003328
Antonio Quartulli17071572011-11-07 16:36:40 +01003329 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003330 * increased by one -> we can apply the attached changes
3331 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003332 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003333 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003334 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3335 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003336 * In this case send a tt request
3337 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003338 if (!tt_num_changes) {
3339 full_table = false;
3340 goto request_table;
3341 }
3342
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003343 spin_lock_bh(&orig_node->tt_lock);
3344
Sven Eckelmanna5130882012-05-16 20:23:16 +02003345 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003346 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003347
3348 /* Even if we received the precomputed crc with the OGM, we
3349 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003350 * in the global table
3351 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003352 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003353
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003354 spin_unlock_bh(&orig_node->tt_lock);
3355
Marek Lindnera943cac2011-07-30 13:10:18 +02003356 /* The ttvn alone is not enough to guarantee consistency
3357 * because a single value could represent different states
3358 * (due to the wrap around). Thus a node has to check whether
3359 * the resulting table (after applying the changes) is still
3360 * consistent or not. E.g. a node could disconnect while its
3361 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3362 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003363 * inconsistency
3364 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003365 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3366 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003367 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003368 } else {
3369 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003370 * in sync anymore -> request fresh tt data
3371 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003372 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003373 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3374 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003375request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003376 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003377 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3378 orig_node->orig, ttvn, orig_ttvn,
3379 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003380 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003381 tt_vlan, tt_num_vlan,
3382 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003383 return;
3384 }
3385 }
3386}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003387
Antonio Quartullic018ad32013-06-04 12:11:39 +02003388/**
3389 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3390 * @bat_priv: the bat priv with all the soft interface information
3391 * @addr: the mac address of the client to check
3392 * @vid: VLAN identifier
3393 *
3394 * Returns true if we know that the client has moved from its old originator
3395 * to another one. This entry is still kept for consistency purposes and will be
3396 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003397 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003398bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003399 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003400{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003401 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003402 bool ret = false;
3403
Antonio Quartullic018ad32013-06-04 12:11:39 +02003404 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003405 if (!tt_global_entry)
3406 goto out;
3407
Antonio Quartullic1d07432013-01-15 22:17:19 +10003408 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003409 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003410out:
3411 return ret;
3412}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003413
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003414/**
3415 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3416 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003417 * @addr: the mac address of the local client to query
3418 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003419 *
3420 * Returns true if the local client is known to be roaming (it is not served by
3421 * this node anymore) or not. If yes, the client is still present in the table
3422 * to keep the latter consistent with the node TTVN
3423 */
3424bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003425 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003426{
3427 struct batadv_tt_local_entry *tt_local_entry;
3428 bool ret = false;
3429
Antonio Quartullic018ad32013-06-04 12:11:39 +02003430 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003431 if (!tt_local_entry)
3432 goto out;
3433
3434 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3435 batadv_tt_local_entry_free_ref(tt_local_entry);
3436out:
3437 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003438}
3439
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003440bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3441 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003442 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003443 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003444{
3445 bool ret = false;
3446
Antonio Quartulli16052782013-06-04 12:11:41 +02003447 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003448 BATADV_TT_CLIENT_TEMP,
3449 atomic_read(&orig_node->last_ttvn)))
3450 goto out;
3451
3452 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003453 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3454 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003455 ret = true;
3456out:
3457 return ret;
3458}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003459
3460/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003461 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3462 * maximum packet size that can be transported through the mesh
3463 * @soft_iface: netdev struct of the mesh interface
3464 *
3465 * Remove entries older than 'timeout' and half timeout if more entries need
3466 * to be removed.
3467 */
3468void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3469{
3470 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3471 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3472 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3473 bool reduced = false;
3474
3475 spin_lock_bh(&bat_priv->tt.commit_lock);
3476
3477 while (true) {
3478 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3479 if (packet_size_max >= table_size)
3480 break;
3481
3482 batadv_tt_local_purge(bat_priv, timeout);
3483 batadv_tt_local_purge_pending_clients(bat_priv);
3484
3485 timeout /= 2;
3486 reduced = true;
3487 net_ratelimited_function(batadv_info, soft_iface,
3488 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3489 packet_size_max);
3490 }
3491
3492 /* commit these changes immediately, to avoid synchronization problem
3493 * with the TTVN
3494 */
3495 if (reduced)
3496 batadv_tt_local_commit_changes_nolock(bat_priv);
3497
3498 spin_unlock_bh(&bat_priv->tt.commit_lock);
3499}
3500
3501/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003502 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3503 * @bat_priv: the bat priv with all the soft interface information
3504 * @orig: the orig_node of the ogm
3505 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3506 * @tvlv_value: tvlv buffer containing the gateway data
3507 * @tvlv_value_len: tvlv buffer length
3508 */
3509static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3510 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003511 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003512 uint16_t tvlv_value_len)
3513{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003514 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3515 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003516 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003517 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003518
3519 if (tvlv_value_len < sizeof(*tt_data))
3520 return;
3521
3522 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3523 tvlv_value_len -= sizeof(*tt_data);
3524
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003525 num_vlan = ntohs(tt_data->num_vlan);
3526
3527 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3528 return;
3529
3530 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3531 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3532 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3533
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003534 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003535
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003536 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3537 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003538}
3539
3540/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003541 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3542 * container
3543 * @bat_priv: the bat priv with all the soft interface information
3544 * @src: mac address of tt tvlv sender
3545 * @dst: mac address of tt tvlv recipient
3546 * @tvlv_value: tvlv buffer containing the tt data
3547 * @tvlv_value_len: tvlv buffer length
3548 *
3549 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3550 * otherwise.
3551 */
3552static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3553 uint8_t *src, uint8_t *dst,
3554 void *tvlv_value,
3555 uint16_t tvlv_value_len)
3556{
3557 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003558 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003559 char tt_flag;
3560 bool ret;
3561
3562 if (tvlv_value_len < sizeof(*tt_data))
3563 return NET_RX_SUCCESS;
3564
3565 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3566 tvlv_value_len -= sizeof(*tt_data);
3567
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003568 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3569 tt_vlan_len *= ntohs(tt_data->num_vlan);
3570
3571 if (tvlv_value_len < tt_vlan_len)
3572 return NET_RX_SUCCESS;
3573
3574 tvlv_value_len -= tt_vlan_len;
3575 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003576
3577 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3578 case BATADV_TT_REQUEST:
3579 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3580
3581 /* If this node cannot provide a TT response the tt_request is
3582 * forwarded
3583 */
3584 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3585 if (!ret) {
3586 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3587 tt_flag = 'F';
3588 else
3589 tt_flag = '.';
3590
3591 batadv_dbg(BATADV_DBG_TT, bat_priv,
3592 "Routing TT_REQUEST to %pM [%c]\n",
3593 dst, tt_flag);
3594 /* tvlv API will re-route the packet */
3595 return NET_RX_DROP;
3596 }
3597 break;
3598 case BATADV_TT_RESPONSE:
3599 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3600
3601 if (batadv_is_my_mac(bat_priv, dst)) {
3602 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003603 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003604 return NET_RX_SUCCESS;
3605 }
3606
3607 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3608 tt_flag = 'F';
3609 else
3610 tt_flag = '.';
3611
3612 batadv_dbg(BATADV_DBG_TT, bat_priv,
3613 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3614
3615 /* tvlv API will re-route the packet */
3616 return NET_RX_DROP;
3617 }
3618
3619 return NET_RX_SUCCESS;
3620}
3621
3622/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003623 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3624 * @bat_priv: the bat priv with all the soft interface information
3625 * @src: mac address of tt tvlv sender
3626 * @dst: mac address of tt tvlv recipient
3627 * @tvlv_value: tvlv buffer containing the tt data
3628 * @tvlv_value_len: tvlv buffer length
3629 *
3630 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3631 * otherwise.
3632 */
3633static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3634 uint8_t *src, uint8_t *dst,
3635 void *tvlv_value,
3636 uint16_t tvlv_value_len)
3637{
3638 struct batadv_tvlv_roam_adv *roaming_adv;
3639 struct batadv_orig_node *orig_node = NULL;
3640
3641 /* If this node is not the intended recipient of the
3642 * roaming advertisement the packet is forwarded
3643 * (the tvlv API will re-route the packet).
3644 */
3645 if (!batadv_is_my_mac(bat_priv, dst))
3646 return NET_RX_DROP;
3647
Marek Lindner122edaa2013-04-23 21:40:03 +08003648 if (tvlv_value_len < sizeof(*roaming_adv))
3649 goto out;
3650
3651 orig_node = batadv_orig_hash_find(bat_priv, src);
3652 if (!orig_node)
3653 goto out;
3654
3655 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3656 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3657
3658 batadv_dbg(BATADV_DBG_TT, bat_priv,
3659 "Received ROAMING_ADV from %pM (client %pM)\n",
3660 src, roaming_adv->client);
3661
3662 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003663 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003664 atomic_read(&orig_node->last_ttvn) + 1);
3665
3666out:
3667 if (orig_node)
3668 batadv_orig_node_free_ref(orig_node);
3669 return NET_RX_SUCCESS;
3670}
3671
3672/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003673 * batadv_tt_init - initialise the translation table internals
3674 * @bat_priv: the bat priv with all the soft interface information
3675 *
3676 * Return 0 on success or negative error number in case of failure.
3677 */
3678int batadv_tt_init(struct batadv_priv *bat_priv)
3679{
3680 int ret;
3681
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003682 /* synchronized flags must be remote */
3683 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3684
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003685 ret = batadv_tt_local_init(bat_priv);
3686 if (ret < 0)
3687 return ret;
3688
3689 ret = batadv_tt_global_init(bat_priv);
3690 if (ret < 0)
3691 return ret;
3692
3693 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003694 batadv_tt_tvlv_unicast_handler_v1,
3695 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003696
Marek Lindner122edaa2013-04-23 21:40:03 +08003697 batadv_tvlv_handler_register(bat_priv, NULL,
3698 batadv_roam_tvlv_unicast_handler_v1,
3699 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3700
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003701 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3702 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3703 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3704
3705 return 1;
3706}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003707
3708/**
3709 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3710 * @bat_priv: the bat priv with all the soft interface information
3711 * @addr: the mac address of the client
3712 * @vid: the identifier of the VLAN where this client is connected
3713 *
3714 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3715 * otherwise
3716 */
3717bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3718 const uint8_t *addr, unsigned short vid)
3719{
3720 struct batadv_tt_global_entry *tt;
3721 bool ret;
3722
3723 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3724 if (!tt)
3725 return false;
3726
3727 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3728
3729 batadv_tt_global_entry_free_ref(tt);
3730
3731 return ret;
3732}