blob: 5e953297d3b2bda513cd9ad90e7e634928f34a20 [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>
22#include <linux/bug.h>
23#include <linux/byteorder/generic.h>
24#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020025#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026#include <linux/errno.h>
27#include <linux/etherdevice.h>
28#include <linux/fs.h>
29#include <linux/if_ether.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/kernel.h>
33#include <linux/list.h>
34#include <linux/lockdep.h>
35#include <linux/netdevice.h>
36#include <linux/rculist.h>
37#include <linux/rcupdate.h>
38#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/spinlock.h>
41#include <linux/stddef.h>
42#include <linux/string.h>
43#include <linux/workqueue.h>
44#include <net/net_namespace.h>
45
46#include "bridge_loop_avoidance.h"
47#include "hard-interface.h"
48#include "hash.h"
49#include "multicast.h"
50#include "originator.h"
51#include "packet.h"
52#include "soft-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020053
Antonio Quartullidec05072012-11-10 11:00:32 +010054/* hash class keys */
55static struct lock_class_key batadv_tt_local_hash_lock_class_key;
56static struct lock_class_key batadv_tt_global_hash_lock_class_key;
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020059 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020060 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020061static void batadv_tt_purge(struct work_struct *work);
62static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020063batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020064static void batadv_tt_global_del(struct batadv_priv *bat_priv,
65 struct batadv_orig_node *orig_node,
66 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020067 unsigned short vid, const char *message,
68 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069
Marek Lindner7aadf882011-02-18 12:28:09 +000070/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020071static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000072{
Sven Eckelmann56303d32012-06-05 22:31:31 +020073 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020074 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000075
dingtianhong323813e2013-12-26 19:40:39 +080076 return batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000077}
78
Antonio Quartullic018ad32013-06-04 12:11:39 +020079/**
80 * batadv_choose_tt - return the index of the tt entry in the hash table
81 * @data: pointer to the tt_common_entry object to map
82 * @size: the size of the hash table
83 *
84 * Returns the hash index where the object represented by 'data' should be
85 * stored at.
86 */
87static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
88{
89 struct batadv_tt_common_entry *tt;
90 uint32_t hash = 0;
91
92 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010093 hash = jhash(&tt->addr, ETH_ALEN, hash);
94 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +020095
96 return hash % size;
97}
98
99/**
100 * batadv_tt_hash_find - look for a client in the given hash table
101 * @hash: the hash table to search
102 * @addr: the mac address of the client to look for
103 * @vid: VLAN identifier
104 *
105 * Returns a pointer to the tt_common struct belonging to the searched client if
106 * found, NULL otherwise.
107 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200108static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200109batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
110 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000111{
Marek Lindner7aadf882011-02-18 12:28:09 +0000112 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200113 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200114 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000115
116 if (!hash)
117 return NULL;
118
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100119 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200120 to_search.vid = vid;
121
122 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000123 head = &hash->table[index];
124
125 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200126 hlist_for_each_entry_rcu(tt, head, hash_entry) {
127 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000128 continue;
129
Antonio Quartullic018ad32013-06-04 12:11:39 +0200130 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200131 continue;
132
Antonio Quartullic018ad32013-06-04 12:11:39 +0200133 if (!atomic_inc_not_zero(&tt->refcount))
134 continue;
135
136 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000137 break;
138 }
139 rcu_read_unlock();
140
Antonio Quartullic018ad32013-06-04 12:11:39 +0200141 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142}
143
Antonio Quartullic018ad32013-06-04 12:11:39 +0200144/**
145 * batadv_tt_local_hash_find - search the local table for a given client
146 * @bat_priv: the bat priv with all the soft interface information
147 * @addr: the mac address of the client to look for
148 * @vid: VLAN identifier
149 *
150 * Returns a pointer to the corresponding tt_local_entry struct if the client is
151 * found, NULL otherwise.
152 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200153static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200154batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
155 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100156{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157 struct batadv_tt_common_entry *tt_common_entry;
158 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100159
Antonio Quartullic018ad32013-06-04 12:11:39 +0200160 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
161 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100162 if (tt_common_entry)
163 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164 struct batadv_tt_local_entry,
165 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100166 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000167}
168
Antonio Quartullic018ad32013-06-04 12:11:39 +0200169/**
170 * batadv_tt_global_hash_find - search the global table for a given client
171 * @bat_priv: the bat priv with all the soft interface information
172 * @addr: the mac address of the client to look for
173 * @vid: VLAN identifier
174 *
175 * Returns a pointer to the corresponding tt_global_entry struct if the client
176 * is found, NULL otherwise.
177 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200178static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200179batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
180 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000181{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200182 struct batadv_tt_common_entry *tt_common_entry;
183 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000184
Antonio Quartullic018ad32013-06-04 12:11:39 +0200185 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
186 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100187 if (tt_common_entry)
188 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200189 struct batadv_tt_global_entry,
190 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100191 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000192}
193
Sven Eckelmanna5130882012-05-16 20:23:16 +0200194static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200195batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200196{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100197 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
198 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199}
200
Antonio Quartulli21026052013-05-07 00:29:22 +0200201/**
202 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
203 * tt_global_entry and possibly free it
204 * @tt_global_entry: the object to free
205 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200206static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200208{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200209 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200210 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200211 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200212 }
213}
214
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100215/**
216 * batadv_tt_global_hash_count - count the number of orig entries
217 * @hash: hash table containing the tt entries
218 * @addr: the mac address of the client to count entries for
219 * @vid: VLAN identifier
220 *
221 * Return the number of originators advertising the given address/data
222 * (excluding ourself).
223 */
224int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
225 const uint8_t *addr, unsigned short vid)
226{
227 struct batadv_tt_global_entry *tt_global_entry;
228 int count;
229
230 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
231 if (!tt_global_entry)
232 return 0;
233
234 count = atomic_read(&tt_global_entry->orig_list_count);
235 batadv_tt_global_entry_free_ref(tt_global_entry);
236
237 return count;
238}
239
Sven Eckelmanna5130882012-05-16 20:23:16 +0200240static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200241{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200242 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200243
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800245
246 /* We are in an rcu callback here, therefore we cannot use
247 * batadv_orig_node_free_ref() and its call_rcu():
248 * An rcu_barrier() wouldn't wait for that to finish
249 */
250 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200251 kfree(orig_entry);
252}
253
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200254/**
255 * batadv_tt_local_size_mod - change the size by v of the local table identified
256 * by vid
257 * @bat_priv: the bat priv with all the soft interface information
258 * @vid: the VLAN identifier of the sub-table to change
259 * @v: the amount to sum to the local table size
260 */
261static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
262 unsigned short vid, int v)
263{
264 struct batadv_softif_vlan *vlan;
265
266 vlan = batadv_softif_vlan_get(bat_priv, vid);
267 if (!vlan)
268 return;
269
270 atomic_add(v, &vlan->tt.num_entries);
271
272 batadv_softif_vlan_free_ref(vlan);
273}
274
275/**
276 * batadv_tt_local_size_inc - increase by one the local table size for the given
277 * vid
278 * @bat_priv: the bat priv with all the soft interface information
279 * @vid: the VLAN identifier
280 */
281static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
282 unsigned short vid)
283{
284 batadv_tt_local_size_mod(bat_priv, vid, 1);
285}
286
287/**
288 * batadv_tt_local_size_dec - decrease by one the local table size for the given
289 * vid
290 * @bat_priv: the bat priv with all the soft interface information
291 * @vid: the VLAN identifier
292 */
293static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
294 unsigned short vid)
295{
296 batadv_tt_local_size_mod(bat_priv, vid, -1);
297}
298
299/**
300 * batadv_tt_global_size_mod - change the size by v of the local table
301 * identified by vid
302 * @bat_priv: the bat priv with all the soft interface information
303 * @vid: the VLAN identifier
304 * @v: the amount to sum to the global table size
305 */
306static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
307 unsigned short vid, int v)
308{
309 struct batadv_orig_node_vlan *vlan;
310
311 vlan = batadv_orig_node_vlan_new(orig_node, vid);
312 if (!vlan)
313 return;
314
315 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
316 spin_lock_bh(&orig_node->vlan_list_lock);
317 list_del_rcu(&vlan->list);
318 spin_unlock_bh(&orig_node->vlan_list_lock);
319 batadv_orig_node_vlan_free_ref(vlan);
320 }
321
322 batadv_orig_node_vlan_free_ref(vlan);
323}
324
325/**
326 * batadv_tt_global_size_inc - increase by one the global table size for the
327 * given vid
328 * @orig_node: the originator which global table size has to be decreased
329 * @vid: the vlan identifier
330 */
331static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
332 unsigned short vid)
333{
334 batadv_tt_global_size_mod(orig_node, vid, 1);
335}
336
337/**
338 * batadv_tt_global_size_dec - decrease by one the global table size for the
339 * given vid
340 * @orig_node: the originator which global table size has to be decreased
341 * @vid: the vlan identifier
342 */
343static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
344 unsigned short vid)
345{
346 batadv_tt_global_size_mod(orig_node, vid, -1);
347}
348
Sven Eckelmanna5130882012-05-16 20:23:16 +0200349static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200350batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200351{
Antonio Quartullid657e622012-07-01 14:09:12 +0200352 if (!atomic_dec_and_test(&orig_entry->refcount))
353 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200354
Sven Eckelmanna5130882012-05-16 20:23:16 +0200355 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200356}
357
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200358/**
359 * batadv_tt_local_event - store a local TT event (ADD/DEL)
360 * @bat_priv: the bat priv with all the soft interface information
361 * @tt_local_entry: the TT entry involved in the event
362 * @event_flags: flags to store in the event structure
363 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200364static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200365 struct batadv_tt_local_entry *tt_local_entry,
366 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200367{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200369 struct batadv_tt_common_entry *common = &tt_local_entry->common;
370 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200371 bool event_removed = false;
372 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200373
374 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200375 if (!tt_change_node)
376 return;
377
Antonio Quartulliff66c972011-06-30 01:14:00 +0200378 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100379 memset(tt_change_node->change.reserved, 0,
380 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100381 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200382 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200383
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200384 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200385
386 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200387 spin_lock_bh(&bat_priv->tt.changes_list_lock);
388 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200389 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200390 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200391 continue;
392
393 /* DEL+ADD in the same orig interval have no effect and can be
394 * removed to avoid silly behaviour on the receiver side. The
395 * other way around (ADD+DEL) can happen in case of roaming of
396 * a client still in the NEW state. Roaming of NEW clients is
397 * now possible due to automatically recognition of "temporary"
398 * clients
399 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200400 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200401 if (!del_op_requested && del_op_entry)
402 goto del;
403 if (del_op_requested && !del_op_entry)
404 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200405
406 /* this is a second add in the same originator interval. It
407 * means that flags have been changed: update them!
408 */
409 if (!del_op_requested && !del_op_entry)
410 entry->change.flags = flags;
411
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200412 continue;
413del:
414 list_del(&entry->list);
415 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000416 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200417 event_removed = true;
418 goto unlock;
419 }
420
Antonio Quartullia73105b2011-04-27 14:27:44 +0200421 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200422 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200423
424unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200425 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200426
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200427 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200428 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200429 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200430 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200431}
432
Marek Lindner335fbe02013-04-23 21:40:02 +0800433/**
434 * batadv_tt_len - compute length in bytes of given number of tt changes
435 * @changes_num: number of tt changes
436 *
437 * Returns computed length in bytes.
438 */
439static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440{
Marek Lindner335fbe02013-04-23 21:40:02 +0800441 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200442}
443
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200444/**
445 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
446 * @tt_len: available space
447 *
448 * Returns the number of entries.
449 */
450static uint16_t batadv_tt_entries(uint16_t tt_len)
451{
452 return tt_len / batadv_tt_len(1);
453}
454
Marek Lindnera19d3d82013-05-27 15:33:25 +0800455/**
456 * batadv_tt_local_table_transmit_size - calculates the local translation table
457 * size when transmitted over the air
458 * @bat_priv: the bat priv with all the soft interface information
459 *
460 * Returns local translation table size in bytes.
461 */
462static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
463{
464 uint16_t num_vlan = 0, tt_local_entries = 0;
465 struct batadv_softif_vlan *vlan;
466 int hdr_size;
467
468 rcu_read_lock();
469 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
470 num_vlan++;
471 tt_local_entries += atomic_read(&vlan->tt.num_entries);
472 }
473 rcu_read_unlock();
474
475 /* header size of tvlv encapsulated tt response payload */
476 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
477 hdr_size += sizeof(struct batadv_tvlv_hdr);
478 hdr_size += sizeof(struct batadv_tvlv_tt_data);
479 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
480
481 return hdr_size + batadv_tt_len(tt_local_entries);
482}
483
Sven Eckelmann56303d32012-06-05 22:31:31 +0200484static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200486 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200487 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
Sven Eckelmann807736f2012-07-15 22:26:51 +0200489 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490
Sven Eckelmann807736f2012-07-15 22:26:51 +0200491 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200492 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
Antonio Quartullidec05072012-11-10 11:00:32 +0100494 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
495 &batadv_tt_local_hash_lock_class_key);
496
Sven Eckelmann5346c352012-05-05 13:27:28 +0200497 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498}
499
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200500static void batadv_tt_global_free(struct batadv_priv *bat_priv,
501 struct batadv_tt_global_entry *tt_global,
502 const char *message)
503{
504 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200505 "Deleting global tt entry %pM (vid: %d): %s\n",
506 tt_global->common.addr,
507 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200508
509 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200510 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200511 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200512}
513
Antonio Quartullic018ad32013-06-04 12:11:39 +0200514/**
515 * batadv_tt_local_add - add a new client to the local table or update an
516 * existing client
517 * @soft_iface: netdev struct of the mesh interface
518 * @addr: the mac address of the client to add
519 * @vid: VLAN identifier
520 * @ifindex: index of the interface where the client is connected to (useful to
521 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100522 * @mark: the value contained in the skb->mark field of the received packet (if
523 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800524 *
525 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200526 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800527bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartulli9464d072013-11-16 12:03:48 +0100528 unsigned short vid, int ifindex, uint32_t mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200530 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200531 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100532 struct batadv_tt_global_entry *tt_global = NULL;
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200533 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200534 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200535 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200536 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800537 int hash_added, table_size, packet_size_max;
538 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200539 uint8_t remote_flags;
Antonio Quartulli9464d072013-11-16 12:03:48 +0100540 uint32_t match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200542 if (ifindex != BATADV_NULL_IFINDEX)
543 in_dev = dev_get_by_index(&init_net, ifindex);
544
Antonio Quartullic018ad32013-06-04 12:11:39 +0200545 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100546
547 if (!is_multicast_ether_addr(addr))
548 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
Antonio Quartulli47c94652012-09-23 22:38:35 +0200550 if (tt_local) {
551 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200552 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
553 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200554 "Re-adding pending client %pM (vid: %d)\n",
555 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200556 /* whatever the reason why the PENDING flag was set,
557 * this is a client which was enqueued to be removed in
558 * this orig_interval. Since it popped up again, the
559 * flag can be reset like it was never enqueued
560 */
561 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
562 goto add_event;
563 }
564
565 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
566 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200567 "Roaming client %pM (vid: %d) came back to its original location\n",
568 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200569 /* the ROAM flag is set because this client roamed away
570 * and the node got a roaming_advertisement message. Now
571 * that the client popped up again at its original
572 * location such flag can be unset
573 */
574 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
575 roamed_back = true;
576 }
577 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578 }
579
Marek Lindnera19d3d82013-05-27 15:33:25 +0800580 /* Ignore the client if we cannot send it in a full table response. */
581 table_size = batadv_tt_local_table_transmit_size(bat_priv);
582 table_size += batadv_tt_len(1);
583 packet_size_max = atomic_read(&bat_priv->packet_size_max);
584 if (table_size > packet_size_max) {
585 net_ratelimited_function(batadv_info, soft_iface,
586 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
587 table_size, packet_size_max, addr);
588 goto out;
589 }
590
Antonio Quartulli47c94652012-09-23 22:38:35 +0200591 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
592 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200593 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200594
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200595 /* increase the refcounter of the related vlan */
596 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +0800597 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
598 addr, BATADV_PRINT_VID(vid)))
599 goto out;
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200600
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200601 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200602 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
603 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200604 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100606 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100607 /* The local entry has to be marked as NEW to avoid to send it in
608 * a full table response going out before the next ttvn increment
609 * (consistency check)
610 */
611 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200612 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200613 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200614 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
615 atomic_set(&tt_local->common.refcount, 2);
616 tt_local->last_seen = jiffies;
617 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100619 /* the batman interface mac and multicast addresses should never be
620 * purged
621 */
622 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
623 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200624 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625
Sven Eckelmann807736f2012-07-15 22:26:51 +0200626 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200627 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200628 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100629
630 if (unlikely(hash_added != 0)) {
631 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200632 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200633 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100634 goto out;
635 }
636
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200637add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200638 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200639
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200640check_roaming:
641 /* Check whether it is a roaming, but don't do anything if the roaming
642 * process has already been handled
643 */
644 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200645 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200646 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200647 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800648 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200649 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200650 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200651 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200652 }
653 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200654 if (roamed_back) {
655 batadv_tt_global_free(bat_priv, tt_global,
656 "Roaming canceled");
657 tt_global = NULL;
658 } else {
659 /* The global entry has to be marked as ROAMING and
660 * has to be kept for consistency purpose
661 */
662 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
663 tt_global->roam_at = jiffies;
664 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200665 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200666
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200667 /* store the current remote flags before altering them. This helps
668 * understanding is flags are changing or not
669 */
670 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800671
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200672 if (batadv_is_wifi_netdev(in_dev))
673 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
674 else
675 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
676
Antonio Quartulli9464d072013-11-16 12:03:48 +0100677 /* check the mark in the skb: if it's equal to the configured
678 * isolation_mark, it means the packet is coming from an isolated
679 * non-mesh client
680 */
681 match_mark = (mark & bat_priv->isolation_mark_mask);
682 if (bat_priv->isolation_mark_mask &&
683 match_mark == bat_priv->isolation_mark)
684 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
685 else
686 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
687
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200688 /* if any "dynamic" flag has been modified, resend an ADD event for this
689 * entry so that all the nodes can get the new flags
690 */
691 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
692 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
693
694 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200695out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200696 if (in_dev)
697 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200698 if (tt_local)
699 batadv_tt_local_entry_free_ref(tt_local);
700 if (tt_global)
701 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800702 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703}
704
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800705/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200706 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
707 * within a TT Response directed to another node
708 * @orig_node: originator for which the TT data has to be prepared
709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changed can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire global TT
714 * table. In case of success the value is updated with the real amount of
715 * reserved bytes
716
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN served by the originator node.
720 *
721 * Return the size of the allocated buffer or 0 in case of failure.
722 */
723static uint16_t
724batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
725 struct batadv_tvlv_tt_data **tt_data,
726 struct batadv_tvlv_tt_change **tt_change,
727 int32_t *tt_len)
728{
729 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
730 struct batadv_tvlv_tt_vlan_data *tt_vlan;
731 struct batadv_orig_node_vlan *vlan;
732 uint8_t *tt_change_ptr;
733
734 rcu_read_lock();
735 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
736 num_vlan++;
737 num_entries += atomic_read(&vlan->tt.num_entries);
738 }
739
740 change_offset = sizeof(**tt_data);
741 change_offset += num_vlan * sizeof(*tt_vlan);
742
743 /* if tt_len is negative, allocate the space needed by the full table */
744 if (*tt_len < 0)
745 *tt_len = batadv_tt_len(num_entries);
746
747 tvlv_len = *tt_len;
748 tvlv_len += change_offset;
749
750 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
751 if (!*tt_data) {
752 *tt_len = 0;
753 goto out;
754 }
755
756 (*tt_data)->flags = BATADV_NO_FLAGS;
757 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
758 (*tt_data)->num_vlan = htons(num_vlan);
759
760 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
761 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
762 tt_vlan->vid = htons(vlan->vid);
763 tt_vlan->crc = htonl(vlan->tt.crc);
764
765 tt_vlan++;
766 }
767
768 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
769 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
770
771out:
772 rcu_read_unlock();
773 return tvlv_len;
774}
775
776/**
777 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
778 * node
779 * @bat_priv: the bat priv with all the soft interface information
780 * @tt_data: uninitialised pointer to the address of the TVLV buffer
781 * @tt_change: uninitialised pointer to the address of the area where the TT
782 * changes can be stored
783 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
784 * function reserves the amount of space needed to send the entire local TT
785 * table. In case of success the value is updated with the real amount of
786 * reserved bytes
787 *
788 * Allocate the needed amount of memory for the entire TT TVLV and write its
789 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
790 * objects, one per active VLAN.
791 *
792 * Return the size of the allocated buffer or 0 in case of failure.
793 */
794static uint16_t
795batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
796 struct batadv_tvlv_tt_data **tt_data,
797 struct batadv_tvlv_tt_change **tt_change,
798 int32_t *tt_len)
799{
800 struct batadv_tvlv_tt_vlan_data *tt_vlan;
801 struct batadv_softif_vlan *vlan;
802 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
803 uint8_t *tt_change_ptr;
804 int change_offset;
805
806 rcu_read_lock();
807 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
808 num_vlan++;
809 num_entries += atomic_read(&vlan->tt.num_entries);
810 }
811
812 change_offset = sizeof(**tt_data);
813 change_offset += num_vlan * sizeof(*tt_vlan);
814
815 /* if tt_len is negative, allocate the space needed by the full table */
816 if (*tt_len < 0)
817 *tt_len = batadv_tt_len(num_entries);
818
819 tvlv_len = *tt_len;
820 tvlv_len += change_offset;
821
822 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
823 if (!*tt_data) {
824 tvlv_len = 0;
825 goto out;
826 }
827
828 (*tt_data)->flags = BATADV_NO_FLAGS;
829 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
830 (*tt_data)->num_vlan = htons(num_vlan);
831
832 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
833 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
834 tt_vlan->vid = htons(vlan->vid);
835 tt_vlan->crc = htonl(vlan->tt.crc);
836
837 tt_vlan++;
838 }
839
840 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
841 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
842
843out:
844 rcu_read_unlock();
845 return tvlv_len;
846}
847
848/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800849 * batadv_tt_tvlv_container_update - update the translation table tvlv container
850 * after local tt changes have been committed
851 * @bat_priv: the bat priv with all the soft interface information
852 */
853static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000854{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800855 struct batadv_tt_change_node *entry, *safe;
856 struct batadv_tvlv_tt_data *tt_data;
857 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200858 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800859 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200860 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200862 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
863 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800864
865 /* if we have too many changes for one packet don't send any
866 * and wait for the tt table request which will be fragmented
867 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800868 if (tt_diff_len > bat_priv->soft_iface->mtu)
869 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800870
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200871 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
872 &tt_change, &tt_diff_len);
873 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800874 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800875
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800876 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800877
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800878 if (tt_diff_len == 0)
879 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800880
Sven Eckelmann807736f2012-07-15 22:26:51 +0200881 spin_lock_bh(&bat_priv->tt.changes_list_lock);
882 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883
Sven Eckelmann807736f2012-07-15 22:26:51 +0200884 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100885 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800886 if (tt_diff_entries_count < tt_diff_entries_num) {
887 memcpy(tt_change + tt_diff_entries_count,
888 &entry->change,
889 sizeof(struct batadv_tvlv_tt_change));
890 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200892 list_del(&entry->list);
893 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200895 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896
Antonio Quartullia73105b2011-04-27 14:27:44 +0200897 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200898 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
899 kfree(bat_priv->tt.last_changeset);
900 bat_priv->tt.last_changeset_len = 0;
901 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800902 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800903 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800904 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800905 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200906 * instead of providing the diff
907 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800908 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200909 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800910 memcpy(bat_priv->tt.last_changeset,
911 tt_change, tt_change_len);
912 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200913 }
914 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200915 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000916
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800917container_register:
918 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200919 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800920 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921}
922
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200923int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924{
925 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200926 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200927 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200928 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100929 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200930 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200931 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000932 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200933 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200934 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100935 int last_seen_secs;
936 int last_seen_msecs;
937 unsigned long last_seen_jiffies;
938 bool no_purge;
939 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000940
Marek Lindner30da63a2012-08-03 17:15:46 +0200941 primary_if = batadv_seq_print_text_primary_if_get(seq);
942 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200943 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000944
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100945 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200946 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
947 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100948 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200949 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000951 for (i = 0; i < hash->size; i++) {
952 head = &hash->table[i];
953
Marek Lindner7aadf882011-02-18 12:28:09 +0000954 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800955 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000956 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100957 tt_local = container_of(tt_common_entry,
958 struct batadv_tt_local_entry,
959 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200960 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100961 last_seen_jiffies = jiffies - tt_local->last_seen;
962 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
963 last_seen_secs = last_seen_msecs / 1000;
964 last_seen_msecs = last_seen_msecs % 1000;
965
966 no_purge = tt_common_entry->flags & np_flag;
967
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200968 vlan = batadv_softif_vlan_get(bat_priv, vid);
969 if (!vlan) {
970 seq_printf(seq, "Cannot retrieve VLAN %d\n",
971 BATADV_PRINT_VID(vid));
972 continue;
973 }
974
975 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100976 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100977 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200978 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200979 ((tt_common_entry->flags &
980 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100981 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200982 ((tt_common_entry->flags &
983 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
984 ((tt_common_entry->flags &
985 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
986 ((tt_common_entry->flags &
987 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
988 ((tt_common_entry->flags &
989 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100990 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200991 no_purge ? 0 : last_seen_msecs,
992 vlan->tt.crc);
993
994 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000996 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200998out:
999 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001000 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001001 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001002}
1003
Sven Eckelmann56303d32012-06-05 22:31:31 +02001004static void
1005batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1006 struct batadv_tt_local_entry *tt_local_entry,
1007 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001009 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010
Antonio Quartulli015758d2011-07-09 17:52:13 +02001011 /* The local client has to be marked as "pending to be removed" but has
1012 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001013 * response issued before the net ttvn increment (consistency check)
1014 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001015 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001016
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001017 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001018 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1019 tt_local_entry->common.addr,
1020 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021}
1022
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001023/**
1024 * batadv_tt_local_remove - logically remove an entry from the local table
1025 * @bat_priv: the bat priv with all the soft interface information
1026 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001027 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001028 * @message: message to append to the log on deletion
1029 * @roaming: true if the deletion is due to a roaming event
1030 *
1031 * Returns the flags assigned to the local entry before being deleted
1032 */
1033uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001034 const uint8_t *addr, unsigned short vid,
1035 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001037 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001038 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001039 struct batadv_softif_vlan *vlan;
Marek Lindneref727062015-06-17 20:01:36 +08001040 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041
Antonio Quartullic018ad32013-06-04 12:11:39 +02001042 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001043 if (!tt_local_entry)
1044 goto out;
1045
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001046 curr_flags = tt_local_entry->common.flags;
1047
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001048 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001049 /* if this global entry addition is due to a roaming, the node has to
1050 * mark the local entry as "roamed" in order to correctly reroute
1051 * packets later
1052 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001053 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001054 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001055 /* mark the local client as ROAMed */
1056 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1057 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001058
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001059 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1060 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1061 message);
1062 goto out;
1063 }
1064 /* if this client has been added right now, it is possible to
1065 * immediately purge it
1066 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001067 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001068
1069 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1070 batadv_compare_tt,
1071 batadv_choose_tt,
1072 &tt_local_entry->common);
1073 if (!tt_entry_exists)
1074 goto out;
1075
1076 /* extra call to free the local tt entry */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001077 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001078
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001079 /* decrease the reference held for this vlan */
1080 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001081 if (!vlan)
1082 goto out;
1083
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001084 batadv_softif_vlan_free_ref(vlan);
1085 batadv_softif_vlan_free_ref(vlan);
1086
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001087out:
1088 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001089 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001090
1091 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001092}
1093
Marek Lindnera19d3d82013-05-27 15:33:25 +08001094/**
1095 * batadv_tt_local_purge_list - purge inactive tt local entries
1096 * @bat_priv: the bat priv with all the soft interface information
1097 * @head: pointer to the list containing the local tt entries
1098 * @timeout: parameter deciding whether a given tt local entry is considered
1099 * inactive or not
1100 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001101static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001102 struct hlist_head *head,
1103 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001105 struct batadv_tt_local_entry *tt_local_entry;
1106 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001107 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001108
Sasha Levinb67bfe02013-02-27 17:06:00 -08001109 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001110 hash_entry) {
1111 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001112 struct batadv_tt_local_entry,
1113 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001114 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1115 continue;
1116
1117 /* entry already marked for deletion */
1118 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1119 continue;
1120
Marek Lindnera19d3d82013-05-27 15:33:25 +08001121 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001122 continue;
1123
1124 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1125 BATADV_TT_CLIENT_DEL, "timed out");
1126 }
1127}
1128
Marek Lindnera19d3d82013-05-27 15:33:25 +08001129/**
1130 * batadv_tt_local_purge - purge inactive tt local entries
1131 * @bat_priv: the bat priv with all the soft interface information
1132 * @timeout: parameter deciding whether a given tt local entry is considered
1133 * inactive or not
1134 */
1135static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1136 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001137{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001138 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001139 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001140 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001141 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001142
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001143 for (i = 0; i < hash->size; i++) {
1144 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001145 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001147 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001148 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001149 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001150 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151}
1152
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001155 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001156 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001157 struct batadv_tt_common_entry *tt_common_entry;
1158 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001159 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001160 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001161 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001162 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163
Sven Eckelmann807736f2012-07-15 22:26:51 +02001164 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001165 return;
1166
Sven Eckelmann807736f2012-07-15 22:26:51 +02001167 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001168
1169 for (i = 0; i < hash->size; i++) {
1170 head = &hash->table[i];
1171 list_lock = &hash->list_locks[i];
1172
1173 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001174 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001175 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001176 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001177 tt_local = container_of(tt_common_entry,
1178 struct batadv_tt_local_entry,
1179 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001180
1181 /* decrease the reference held for this vlan */
1182 vlan = batadv_softif_vlan_get(bat_priv,
1183 tt_common_entry->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001184 if (vlan) {
1185 batadv_softif_vlan_free_ref(vlan);
1186 batadv_softif_vlan_free_ref(vlan);
1187 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001188
Sven Eckelmann56303d32012-06-05 22:31:31 +02001189 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001190 }
1191 spin_unlock_bh(list_lock);
1192 }
1193
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001194 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001195
Sven Eckelmann807736f2012-07-15 22:26:51 +02001196 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001197}
1198
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001200{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001201 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001202 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001203
Sven Eckelmann807736f2012-07-15 22:26:51 +02001204 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001205
Sven Eckelmann807736f2012-07-15 22:26:51 +02001206 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001207 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001208
Antonio Quartullidec05072012-11-10 11:00:32 +01001209 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1210 &batadv_tt_global_hash_lock_class_key);
1211
Sven Eckelmann5346c352012-05-05 13:27:28 +02001212 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001213}
1214
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001216{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001217 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001218
Sven Eckelmann807736f2012-07-15 22:26:51 +02001219 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001220
Sven Eckelmann807736f2012-07-15 22:26:51 +02001221 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001222 list) {
1223 list_del(&entry->list);
1224 kfree(entry);
1225 }
1226
Sven Eckelmann807736f2012-07-15 22:26:51 +02001227 atomic_set(&bat_priv->tt.local_changes, 0);
1228 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001229}
1230
Antonio Quartullid657e622012-07-01 14:09:12 +02001231/* retrieves the orig_tt_list_entry belonging to orig_node from the
1232 * batadv_tt_global_entry list
1233 *
1234 * returns it with an increased refcounter, NULL if not found
1235 */
1236static struct batadv_tt_orig_list_entry *
1237batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1238 const struct batadv_orig_node *orig_node)
1239{
1240 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1241 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001242
1243 rcu_read_lock();
1244 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001245 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001246 if (tmp_orig_entry->orig_node != orig_node)
1247 continue;
1248 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1249 continue;
1250
1251 orig_entry = tmp_orig_entry;
1252 break;
1253 }
1254 rcu_read_unlock();
1255
1256 return orig_entry;
1257}
1258
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001259/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001260 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001261 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001262static bool
1263batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1264 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001265{
Antonio Quartullid657e622012-07-01 14:09:12 +02001266 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267 bool found = false;
1268
Antonio Quartullid657e622012-07-01 14:09:12 +02001269 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1270 if (orig_entry) {
1271 found = true;
1272 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001273 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001274
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001275 return found;
1276}
1277
Sven Eckelmanna5130882012-05-16 20:23:16 +02001278static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001279batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001280 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001281{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001282 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001283
Antonio Quartullid657e622012-07-01 14:09:12 +02001284 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001285 if (orig_entry) {
1286 /* refresh the ttvn: the current value could be a bogus one that
1287 * was added during a "temporary client detection"
1288 */
1289 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001290 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001291 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001292
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001293 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1294 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001295 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001296
1297 INIT_HLIST_NODE(&orig_entry->list);
1298 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001299 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001300 orig_entry->orig_node = orig_node;
1301 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001302 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001303
Antonio Quartullid657e622012-07-01 14:09:12 +02001304 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001305 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001306 &tt_global->orig_list);
1307 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001308 atomic_inc(&tt_global->orig_list_count);
1309
Antonio Quartullid657e622012-07-01 14:09:12 +02001310out:
1311 if (orig_entry)
1312 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001313}
1314
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001315/**
1316 * batadv_tt_global_add - add a new TT global entry or update an existing one
1317 * @bat_priv: the bat priv with all the soft interface information
1318 * @orig_node: the originator announcing the client
1319 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001320 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001321 * @flags: TT flags that have to be set for this non-mesh client
1322 * @ttvn: the tt version number ever announcing this non-mesh client
1323 *
1324 * Add a new TT global entry for the given originator. If the entry already
1325 * exists add a new reference to the given originator (a global entry can have
1326 * references to multiple originators) and adjust the flags attribute to reflect
1327 * the function argument.
1328 * If a TT local entry exists for this non-mesh client remove it.
1329 *
1330 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001331 *
1332 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001333 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001334static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1335 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001336 const unsigned char *tt_addr,
1337 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001338 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001339{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001340 struct batadv_tt_global_entry *tt_global_entry;
1341 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001342 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001343 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001344 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001345 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001346
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001347 /* ignore global entries from backbone nodes */
1348 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1349 return true;
1350
Antonio Quartullic018ad32013-06-04 12:11:39 +02001351 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1352 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001353
1354 /* if the node already has a local client for this entry, it has to wait
1355 * for a roaming advertisement instead of manually messing up the global
1356 * table
1357 */
1358 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1359 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1360 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001361
Antonio Quartullia73105b2011-04-27 14:27:44 +02001362 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001363 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001364 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001365 goto out;
1366
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001367 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001368 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001369 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001370
Antonio Quartullid4f44692012-05-25 00:00:54 +02001371 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001372 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001373 /* node must store current time in case of roaming. This is
1374 * needed to purge this entry out on timeout (if nobody claims
1375 * it)
1376 */
1377 if (flags & BATADV_TT_CLIENT_ROAM)
1378 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001379 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001380 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001381
1382 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001383 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001384 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001385
Sven Eckelmann807736f2012-07-15 22:26:51 +02001386 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001387 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001388 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001389 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001390
1391 if (unlikely(hash_added != 0)) {
1392 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001393 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001394 goto out_remove;
1395 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001397 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001398 /* If there is already a global entry, we can use this one for
1399 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001400 * But if we are trying to add a temporary client then here are
1401 * two options at this point:
1402 * 1) the global client is not a temporary client: the global
1403 * client has to be left as it is, temporary information
1404 * should never override any already known client state
1405 * 2) the global client is a temporary client: purge the
1406 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001407 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001408 if (flags & BATADV_TT_CLIENT_TEMP) {
1409 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1410 goto out;
1411 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1412 orig_node))
1413 goto out_remove;
1414 batadv_tt_global_del_orig_list(tt_global_entry);
1415 goto add_orig_entry;
1416 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001417
1418 /* if the client was temporary added before receiving the first
1419 * OGM announcing it, we have to clear the TEMP flag
1420 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001421 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001422
Antonio Quartullie9c001362012-11-07 15:05:33 +01001423 /* the change can carry possible "attribute" flags like the
1424 * TT_CLIENT_WIFI, therefore they have to be copied in the
1425 * client entry
1426 */
1427 tt_global_entry->common.flags |= flags;
1428
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001429 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1430 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001431 * delete + roaming change for this originator.
1432 *
1433 * We should first delete the old originator before adding the
1434 * new one.
1435 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001436 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001437 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001438 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001439 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001440 }
1441 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001442add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001443 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001444 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001446 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001447 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1448 common->addr, BATADV_PRINT_VID(common->vid),
1449 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001450 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001451
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001452out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001453 /* Do not remove multicast addresses from the local hash on
1454 * global additions
1455 */
1456 if (is_multicast_ether_addr(tt_addr))
1457 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001458
Antonio Quartullia73105b2011-04-27 14:27:44 +02001459 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001460 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001461 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001462 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001463 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1464
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001465 if (!(flags & BATADV_TT_CLIENT_ROAM))
1466 /* this is a normal global add. Therefore the client is not in a
1467 * roaming state anymore.
1468 */
1469 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1470
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001471out:
1472 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001473 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001474 if (tt_local_entry)
1475 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001476 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001477}
1478
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001479/**
1480 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001481 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001482 * @tt_global_entry: global translation table entry to be analyzed
1483 *
1484 * This functon assumes the caller holds rcu_read_lock().
1485 * Returns best originator list entry or NULL on errors.
1486 */
1487static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001488batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1489 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001490{
Antonio Quartulli46274562013-09-03 11:10:24 +02001491 struct batadv_neigh_node *router, *best_router = NULL;
1492 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001493 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001494 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001495
1496 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001497 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001498 router = batadv_orig_router_get(orig_entry->orig_node,
1499 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001500 if (!router)
1501 continue;
1502
Antonio Quartulli46274562013-09-03 11:10:24 +02001503 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001504 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1505 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001506 batadv_neigh_node_free_ref(router);
1507 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001508 }
1509
Antonio Quartulli46274562013-09-03 11:10:24 +02001510 /* release the refcount for the "old" best */
1511 if (best_router)
1512 batadv_neigh_node_free_ref(best_router);
1513
1514 best_entry = orig_entry;
1515 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001516 }
1517
Antonio Quartulli46274562013-09-03 11:10:24 +02001518 if (best_router)
1519 batadv_neigh_node_free_ref(best_router);
1520
Sven Eckelmann981d8902012-10-07 13:34:15 +02001521 return best_entry;
1522}
1523
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001524/**
1525 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1526 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001527 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001528 * @tt_global_entry: global translation table entry to be printed
1529 * @seq: debugfs table seq_file struct
1530 *
1531 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001532 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001533static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001534batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1535 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001536 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001537{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001538 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001539 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001540 struct batadv_orig_node_vlan *vlan;
1541 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001542 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001543 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001544
1545 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001546 flags = tt_common_entry->flags;
1547
Antonio Quartulli46274562013-09-03 11:10:24 +02001548 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001549 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001550 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1551 tt_common_entry->vid);
1552 if (!vlan) {
1553 seq_printf(seq,
1554 " * Cannot retrieve VLAN %d for originator %pM\n",
1555 BATADV_PRINT_VID(tt_common_entry->vid),
1556 best_entry->orig_node->orig);
1557 goto print_list;
1558 }
1559
Sven Eckelmann981d8902012-10-07 13:34:15 +02001560 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001561 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001562 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001563 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001564 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001565 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001566 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001567 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1568 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1569 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1570 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001571
1572 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001573 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001574
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001575print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001576 head = &tt_global_entry->orig_list;
1577
Sasha Levinb67bfe02013-02-27 17:06:00 -08001578 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001579 if (best_entry == orig_entry)
1580 continue;
1581
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001582 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1583 tt_common_entry->vid);
1584 if (!vlan) {
1585 seq_printf(seq,
1586 " + Cannot retrieve VLAN %d for originator %pM\n",
1587 BATADV_PRINT_VID(tt_common_entry->vid),
1588 orig_entry->orig_node->orig);
1589 continue;
1590 }
1591
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001592 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001593 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001594 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001595 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001596 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001597 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001598 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001599 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1600 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1601 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1602 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001603
1604 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001605 }
1606}
1607
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001608int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001609{
1610 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001611 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001612 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001613 struct batadv_tt_common_entry *tt_common_entry;
1614 struct batadv_tt_global_entry *tt_global;
1615 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001616 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001617 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001618
Marek Lindner30da63a2012-08-03 17:15:46 +02001619 primary_if = batadv_seq_print_text_primary_if_get(seq);
1620 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001621 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001622
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001623 seq_printf(seq,
1624 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001625 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001626 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1627 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1628 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001629
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001630 for (i = 0; i < hash->size; i++) {
1631 head = &hash->table[i];
1632
Marek Lindner7aadf882011-02-18 12:28:09 +00001633 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001634 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001635 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001636 tt_global = container_of(tt_common_entry,
1637 struct batadv_tt_global_entry,
1638 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001639 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001640 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001641 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001642 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001643out:
1644 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001645 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001646 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001647}
1648
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001649/**
1650 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1651 * @tt_global_entry: the global entry to remove the orig_entry from
1652 * @orig_entry: the orig entry to remove and free
1653 *
1654 * Remove an orig_entry from its list in the given tt_global_entry and
1655 * free this orig_entry afterwards.
1656 */
1657static void
1658batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1659 struct batadv_tt_orig_list_entry *orig_entry)
1660{
1661 batadv_tt_global_size_dec(orig_entry->orig_node,
1662 tt_global_entry->common.vid);
1663 atomic_dec(&tt_global_entry->orig_list_count);
1664 hlist_del_rcu(&orig_entry->list);
1665 batadv_tt_orig_list_entry_free_ref(orig_entry);
1666}
1667
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001668/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001669static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001670batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001671{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001672 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001673 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001674 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001675
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001676 spin_lock_bh(&tt_global_entry->list_lock);
1677 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001678 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1679 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001680 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001681}
1682
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001683/**
1684 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1685 * @bat_priv: the bat priv with all the soft interface information
1686 * @tt_global_entry: the global entry to remove the orig_node from
1687 * @orig_node: the originator announcing the client
1688 * @message: message to append to the log on deletion
1689 *
1690 * Remove the given orig_node and its according orig_entry from the given
1691 * global tt entry.
1692 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001693static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001694batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1695 struct batadv_tt_global_entry *tt_global_entry,
1696 struct batadv_orig_node *orig_node,
1697 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001698{
1699 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001700 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001701 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001702 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001703
1704 spin_lock_bh(&tt_global_entry->list_lock);
1705 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001706 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001707 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001708 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001709 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001710 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001711 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001712 tt_global_entry->common.addr,
1713 BATADV_PRINT_VID(vid), message);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001714 batadv_tt_global_del_orig_entry(tt_global_entry,
1715 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001716 }
1717 }
1718 spin_unlock_bh(&tt_global_entry->list_lock);
1719}
1720
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001721/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001722 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1723 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001724 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001725static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001726batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1727 struct batadv_tt_global_entry *tt_global_entry,
1728 struct batadv_orig_node *orig_node,
1729 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001730{
1731 bool last_entry = true;
1732 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001733 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001734
1735 /* no local entry exists, case 1:
1736 * Check if this is the last one or if other entries exist.
1737 */
1738
1739 rcu_read_lock();
1740 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001741 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001742 if (orig_entry->orig_node != orig_node) {
1743 last_entry = false;
1744 break;
1745 }
1746 }
1747 rcu_read_unlock();
1748
1749 if (last_entry) {
1750 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001751 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001752 tt_global_entry->roam_at = jiffies;
1753 } else
1754 /* there is another entry, we can simply delete this
1755 * one and can still use the other one.
1756 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001757 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1758 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001759}
1760
Antonio Quartullic018ad32013-06-04 12:11:39 +02001761/**
1762 * batadv_tt_global_del - remove a client from the global table
1763 * @bat_priv: the bat priv with all the soft interface information
1764 * @orig_node: an originator serving this client
1765 * @addr: the mac address of the client
1766 * @vid: VLAN identifier
1767 * @message: a message explaining the reason for deleting the client to print
1768 * for debugging purpose
1769 * @roaming: true if the deletion has been triggered by a roaming event
1770 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001771static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1772 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001773 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001774 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001775{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001776 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001777 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001778
Antonio Quartullic018ad32013-06-04 12:11:39 +02001779 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001780 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001781 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001783 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001784 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1785 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001786
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001787 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001788 batadv_tt_global_free(bat_priv, tt_global_entry,
1789 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001790
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001791 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001792 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001793
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001794 /* if we are deleting a global entry due to a roam
1795 * event, there are two possibilities:
1796 * 1) the client roamed from node A to node B => if there
1797 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001798 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001799 * wait for node B to claim it. In case of timeout
1800 * the entry is purged.
1801 *
1802 * If there are other originators left, we directly delete
1803 * the originator.
1804 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001805 * the global entry, since it is useless now.
1806 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001807 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001808 tt_global_entry->common.addr,
1809 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001810 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001811 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001812 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001813 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001814 } else
1815 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001816 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1817 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001818
Antonio Quartullicc47f662011-04-27 14:27:57 +02001819out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001820 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001821 batadv_tt_global_entry_free_ref(tt_global_entry);
1822 if (local_entry)
1823 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001824}
1825
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001826/**
1827 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1828 * given originator matching the provided vid
1829 * @bat_priv: the bat priv with all the soft interface information
1830 * @orig_node: the originator owning the entries to remove
1831 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1832 * removed
1833 * @message: debug message to print as "reason"
1834 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001835void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1836 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001837 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001838 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001840 struct batadv_tt_global_entry *tt_global;
1841 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001842 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001843 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001844 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001846 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001847 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001848
Simon Wunderlich6e801492011-10-19 10:28:26 +02001849 if (!hash)
1850 return;
1851
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852 for (i = 0; i < hash->size; i++) {
1853 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001854 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001855
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001856 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001857 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001858 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001859 /* remove only matching entries */
1860 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1861 continue;
1862
Sven Eckelmann56303d32012-06-05 22:31:31 +02001863 tt_global = container_of(tt_common_entry,
1864 struct batadv_tt_global_entry,
1865 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001866
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001867 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1868 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001869
Sven Eckelmann56303d32012-06-05 22:31:31 +02001870 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001871 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001872 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001873 "Deleting global tt entry %pM (vid: %d): %s\n",
1874 tt_global->common.addr,
1875 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001876 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001877 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001878 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001880 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001881 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01001882 orig_node->capa_initialized &= ~BATADV_ORIG_CAPA_HAS_TT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001883}
1884
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001885static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1886 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001887{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001888 bool purge = false;
1889 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1890 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001891
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001892 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1893 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1894 purge = true;
1895 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001896 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001897
1898 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1899 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1900 purge = true;
1901 *msg = "Temporary client timeout\n";
1902 }
1903
1904 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001905}
1906
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001907static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001908{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001909 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001910 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001911 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001912 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001913 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001914 char *msg = NULL;
1915 struct batadv_tt_common_entry *tt_common;
1916 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001917
Antonio Quartullicc47f662011-04-27 14:27:57 +02001918 for (i = 0; i < hash->size; i++) {
1919 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001920 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001921
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001922 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001923 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001924 hash_entry) {
1925 tt_global = container_of(tt_common,
1926 struct batadv_tt_global_entry,
1927 common);
1928
1929 if (!batadv_tt_global_to_purge(tt_global, &msg))
1930 continue;
1931
1932 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001933 "Deleting global tt entry %pM (vid: %d): %s\n",
1934 tt_global->common.addr,
1935 BATADV_PRINT_VID(tt_global->common.vid),
1936 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001937
Sasha Levinb67bfe02013-02-27 17:06:00 -08001938 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001939
1940 batadv_tt_global_entry_free_ref(tt_global);
1941 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001942 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001943 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001944}
1945
Sven Eckelmann56303d32012-06-05 22:31:31 +02001946static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001947{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001948 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001949 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001950 struct batadv_tt_common_entry *tt_common_entry;
1951 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001952 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001953 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001954 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001955
Sven Eckelmann807736f2012-07-15 22:26:51 +02001956 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001957 return;
1958
Sven Eckelmann807736f2012-07-15 22:26:51 +02001959 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001960
1961 for (i = 0; i < hash->size; i++) {
1962 head = &hash->table[i];
1963 list_lock = &hash->list_locks[i];
1964
1965 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001966 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001967 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001968 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001969 tt_global = container_of(tt_common_entry,
1970 struct batadv_tt_global_entry,
1971 common);
1972 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001973 }
1974 spin_unlock_bh(list_lock);
1975 }
1976
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001977 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001978
Sven Eckelmann807736f2012-07-15 22:26:51 +02001979 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001980}
1981
Sven Eckelmann56303d32012-06-05 22:31:31 +02001982static bool
1983_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1984 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001985{
1986 bool ret = false;
1987
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001988 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1989 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001990 ret = true;
1991
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001992 /* check if the two clients are marked as isolated */
1993 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1994 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1995 ret = true;
1996
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001997 return ret;
1998}
1999
Antonio Quartullic018ad32013-06-04 12:11:39 +02002000/**
2001 * batadv_transtable_search - get the mesh destination for a given client
2002 * @bat_priv: the bat priv with all the soft interface information
2003 * @src: mac address of the source client
2004 * @addr: mac address of the destination client
2005 * @vid: VLAN identifier
2006 *
2007 * Returns a pointer to the originator that was selected as destination in the
2008 * mesh for contacting the client 'addr', NULL otherwise.
2009 * In case of multiple originators serving the same client, the function returns
2010 * the best one (best in terms of metric towards the destination node).
2011 *
2012 * If the two clients are AP isolated the function returns NULL.
2013 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002014struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2015 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002016 const uint8_t *addr,
2017 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002018{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002019 struct batadv_tt_local_entry *tt_local_entry = NULL;
2020 struct batadv_tt_global_entry *tt_global_entry = NULL;
2021 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002022 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002023
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002024 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002025 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002026 if (!tt_local_entry ||
2027 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002028 goto out;
2029 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002030
Antonio Quartullic018ad32013-06-04 12:11:39 +02002031 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002032 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002033 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002034
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002035 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002036 * isolation
2037 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002038 if (tt_local_entry &&
2039 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002040 goto out;
2041
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002042 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002043 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002044 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002045 if (best_entry)
2046 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002047 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2048 orig_node = NULL;
2049 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002050
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002051out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002052 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002053 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002054 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002055 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002056
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002057 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002058}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059
Antonio Quartulliced72932013-04-24 16:37:51 +02002060/**
2061 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2062 * to the given orig_node
2063 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002064 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002065 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002066 *
2067 * This function computes the checksum for the global table corresponding to a
2068 * specific originator. In particular, the checksum is computed as follows: For
2069 * each client connected to the originator the CRC32C of the MAC address and the
2070 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2071 * together.
2072 *
2073 * The idea behind is that CRC32C should be used as much as possible in order to
2074 * produce a unique hash of the table, but since the order which is used to feed
2075 * the CRC32C function affects the result and since every node in the network
2076 * probably sorts the clients differently, the hash function cannot be directly
2077 * computed over the entire table. Hence the CRC32C is used only on
2078 * the single client entry, while all the results are then xor'ed together
2079 * because the XOR operation can combine them all while trying to reduce the
2080 * noise as much as possible.
2081 *
2082 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002083 */
2084static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002085 struct batadv_orig_node *orig_node,
2086 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002088 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002089 struct batadv_tt_common_entry *tt_common;
2090 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002092 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002093 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002094 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002095
2096 for (i = 0; i < hash->size; i++) {
2097 head = &hash->table[i];
2098
2099 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002100 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002101 tt_global = container_of(tt_common,
2102 struct batadv_tt_global_entry,
2103 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002104 /* compute the CRC only for entries belonging to the
2105 * VLAN identified by the vid passed as parameter
2106 */
2107 if (tt_common->vid != vid)
2108 continue;
2109
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002110 /* Roaming clients are in the global table for
2111 * consistency only. They don't have to be
2112 * taken into account while computing the
2113 * global crc
2114 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002115 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002116 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002117 /* Temporary clients have not been announced yet, so
2118 * they have to be skipped while computing the global
2119 * crc
2120 */
2121 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2122 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002123
2124 /* find out if this global entry is announced by this
2125 * originator
2126 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002127 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002128 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002129 continue;
2130
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002131 /* use network order to read the VID: this ensures that
2132 * every node reads the bytes in the same order.
2133 */
2134 tmp_vid = htons(tt_common->vid);
2135 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002136
2137 /* compute the CRC on flags that have to be kept in sync
2138 * among nodes
2139 */
2140 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2141 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2142
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002143 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144 }
2145 rcu_read_unlock();
2146 }
2147
Antonio Quartulliced72932013-04-24 16:37:51 +02002148 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002149}
2150
Antonio Quartulliced72932013-04-24 16:37:51 +02002151/**
2152 * batadv_tt_local_crc - calculates the checksum of the local table
2153 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002154 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002155 *
2156 * For details about the computation, please refer to the documentation for
2157 * batadv_tt_global_crc().
2158 *
2159 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002160 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002161static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2162 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002163{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002164 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002165 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002167 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002168 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002169 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170
2171 for (i = 0; i < hash->size; i++) {
2172 head = &hash->table[i];
2173
2174 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002175 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002176 /* compute the CRC only for entries belonging to the
2177 * VLAN identified by vid
2178 */
2179 if (tt_common->vid != vid)
2180 continue;
2181
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002182 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002183 * account while computing the CRC
2184 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002185 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002186 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002187
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002188 /* use network order to read the VID: this ensures that
2189 * every node reads the bytes in the same order.
2190 */
2191 tmp_vid = htons(tt_common->vid);
2192 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002193
2194 /* compute the CRC on flags that have to be kept in sync
2195 * among nodes
2196 */
2197 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2198 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2199
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002200 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002201 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002202 rcu_read_unlock();
2203 }
2204
Antonio Quartulliced72932013-04-24 16:37:51 +02002205 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002206}
2207
Sven Eckelmann56303d32012-06-05 22:31:31 +02002208static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002209{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002210 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002211
Sven Eckelmann807736f2012-07-15 22:26:51 +02002212 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002213
Sven Eckelmann807736f2012-07-15 22:26:51 +02002214 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002215 list_del(&node->list);
2216 kfree(node);
2217 }
2218
Sven Eckelmann807736f2012-07-15 22:26:51 +02002219 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002220}
2221
Sven Eckelmann56303d32012-06-05 22:31:31 +02002222static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2223 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002224 const void *tt_buff,
2225 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002226{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002227 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002228 * last OGM (the OGM could carry no changes)
2229 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002230 spin_lock_bh(&orig_node->tt_buff_lock);
2231 if (tt_buff_len > 0) {
2232 kfree(orig_node->tt_buff);
2233 orig_node->tt_buff_len = 0;
2234 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2235 if (orig_node->tt_buff) {
2236 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2237 orig_node->tt_buff_len = tt_buff_len;
2238 }
2239 }
2240 spin_unlock_bh(&orig_node->tt_buff_lock);
2241}
2242
Sven Eckelmann56303d32012-06-05 22:31:31 +02002243static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002244{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002245 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002246
Sven Eckelmann807736f2012-07-15 22:26:51 +02002247 spin_lock_bh(&bat_priv->tt.req_list_lock);
2248 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002249 if (batadv_has_timed_out(node->issued_at,
2250 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002251 list_del(&node->list);
2252 kfree(node);
2253 }
2254 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002255 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002256}
2257
2258/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002259 * has already been issued for this orig_node, NULL otherwise
2260 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261static struct batadv_tt_req_node *
2262batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2263 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002264{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002265 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002266
Sven Eckelmann807736f2012-07-15 22:26:51 +02002267 spin_lock_bh(&bat_priv->tt.req_list_lock);
2268 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002269 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2270 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002271 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002272 goto unlock;
2273 }
2274
2275 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2276 if (!tt_req_node)
2277 goto unlock;
2278
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002279 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002280 tt_req_node->issued_at = jiffies;
2281
Sven Eckelmann807736f2012-07-15 22:26:51 +02002282 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002283unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002284 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002285 return tt_req_node;
2286}
2287
Marek Lindner335fbe02013-04-23 21:40:02 +08002288/**
2289 * batadv_tt_local_valid - verify that given tt entry is a valid one
2290 * @entry_ptr: to be checked local tt entry
2291 * @data_ptr: not used but definition required to satisfy the callback prototype
2292 *
2293 * Returns 1 if the entry is a valid, 0 otherwise.
2294 */
2295static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002296{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002297 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002298
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002299 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002300 return 0;
2301 return 1;
2302}
2303
Sven Eckelmanna5130882012-05-16 20:23:16 +02002304static int batadv_tt_global_valid(const void *entry_ptr,
2305 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002306{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002307 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2308 const struct batadv_tt_global_entry *tt_global_entry;
2309 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002311 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2312 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002313 return 0;
2314
Sven Eckelmann56303d32012-06-05 22:31:31 +02002315 tt_global_entry = container_of(tt_common_entry,
2316 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002317 common);
2318
Sven Eckelmanna5130882012-05-16 20:23:16 +02002319 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002320}
2321
Marek Lindner335fbe02013-04-23 21:40:02 +08002322/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002323 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2324 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002325 * @bat_priv: the bat priv with all the soft interface information
2326 * @hash: hash table containing the tt entries
2327 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002328 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002329 * @valid_cb: function to filter tt change entries
2330 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002331 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002332static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2333 struct batadv_hashtable *hash,
2334 void *tvlv_buff, uint16_t tt_len,
2335 int (*valid_cb)(const void *, const void *),
2336 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002337{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002338 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002339 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002341 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002342 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002343
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002344 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002345 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002346
2347 rcu_read_lock();
2348 for (i = 0; i < hash->size; i++) {
2349 head = &hash->table[i];
2350
Sasha Levinb67bfe02013-02-27 17:06:00 -08002351 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002352 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002353 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002354 break;
2355
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002356 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002357 continue;
2358
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002359 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002360 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002361 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002362 memset(tt_change->reserved, 0,
2363 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002364
Marek Lindner335fbe02013-04-23 21:40:02 +08002365 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002366 tt_change++;
2367 }
2368 }
2369 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002370}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002371
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002372/**
2373 * batadv_tt_global_check_crc - check if all the CRCs are correct
2374 * @orig_node: originator for which the CRCs have to be checked
2375 * @tt_vlan: pointer to the first tvlv VLAN entry
2376 * @num_vlan: number of tvlv VLAN entries
2377 * @create: if true, create VLAN objects if not found
2378 *
2379 * Return true if all the received CRCs match the locally stored ones, false
2380 * otherwise
2381 */
2382static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2383 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2384 uint16_t num_vlan)
2385{
2386 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2387 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002388 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002389 int i;
2390
2391 /* check if each received CRC matches the locally stored one */
2392 for (i = 0; i < num_vlan; i++) {
2393 tt_vlan_tmp = tt_vlan + i;
2394
2395 /* if orig_node is a backbone node for this VLAN, don't check
2396 * the CRC as we ignore all the global entries over it
2397 */
2398 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002399 orig_node->orig,
2400 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002401 continue;
2402
2403 vlan = batadv_orig_node_vlan_get(orig_node,
2404 ntohs(tt_vlan_tmp->vid));
2405 if (!vlan)
2406 return false;
2407
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002408 crc = vlan->tt.crc;
2409 batadv_orig_node_vlan_free_ref(vlan);
2410
2411 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002412 return false;
2413 }
2414
2415 return true;
2416}
2417
2418/**
2419 * batadv_tt_local_update_crc - update all the local CRCs
2420 * @bat_priv: the bat priv with all the soft interface information
2421 */
2422static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2423{
2424 struct batadv_softif_vlan *vlan;
2425
2426 /* recompute the global CRC for each VLAN */
2427 rcu_read_lock();
2428 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2429 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2430 }
2431 rcu_read_unlock();
2432}
2433
2434/**
2435 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2436 * @bat_priv: the bat priv with all the soft interface information
2437 * @orig_node: the orig_node for which the CRCs have to be updated
2438 */
2439static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2440 struct batadv_orig_node *orig_node)
2441{
2442 struct batadv_orig_node_vlan *vlan;
2443 uint32_t crc;
2444
2445 /* recompute the global CRC for each VLAN */
2446 rcu_read_lock();
2447 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2448 /* if orig_node is a backbone node for this VLAN, don't compute
2449 * the CRC as we ignore all the global entries over it
2450 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002451 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2452 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002453 continue;
2454
2455 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2456 vlan->tt.crc = crc;
2457 }
2458 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459}
2460
Antonio Quartulliced72932013-04-24 16:37:51 +02002461/**
2462 * batadv_send_tt_request - send a TT Request message to a given node
2463 * @bat_priv: the bat priv with all the soft interface information
2464 * @dst_orig_node: the destination of the message
2465 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002466 * @tt_vlan: pointer to the first tvlv VLAN object to request
2467 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002468 * @full_table: ask for the entire translation table if true, while only for the
2469 * last TT diff otherwise
2470 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002471static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2472 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002473 uint8_t ttvn,
2474 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2475 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002476{
Marek Lindner335fbe02013-04-23 21:40:02 +08002477 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002478 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002479 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2480 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002481 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002482 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002483
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002484 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002485 if (!primary_if)
2486 goto out;
2487
2488 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002489 * reply from the same orig_node yet
2490 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002491 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002492 if (!tt_req_node)
2493 goto out;
2494
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002495 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2496 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002497 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002498 goto out;
2499
Marek Lindner335fbe02013-04-23 21:40:02 +08002500 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2501 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002502 tvlv_tt_data->num_vlan = htons(num_vlan);
2503
2504 /* send all the CRCs within the request. This is needed by intermediate
2505 * nodes to ensure they have the correct table before replying
2506 */
2507 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2508 for (i = 0; i < num_vlan; i++) {
2509 tt_vlan_req->vid = tt_vlan->vid;
2510 tt_vlan_req->crc = tt_vlan->crc;
2511
2512 tt_vlan_req++;
2513 tt_vlan++;
2514 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002515
2516 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002517 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002518
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002519 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002520 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002521
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002522 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002523 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2524 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002525 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002526 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002527
2528out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002530 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002531 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002532 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002534 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002535 kfree(tt_req_node);
2536 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002537 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002538 return ret;
2539}
2540
Marek Lindner335fbe02013-04-23 21:40:02 +08002541/**
2542 * batadv_send_other_tt_response - send reply to tt request concerning another
2543 * node's translation table
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @tt_data: tt data containing the tt request information
2546 * @req_src: mac address of tt request sender
2547 * @req_dst: mac address of tt request recipient
2548 *
2549 * Returns true if tt request reply was sent, false otherwise.
2550 */
2551static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2552 struct batadv_tvlv_tt_data *tt_data,
2553 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002554{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002555 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002556 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002557 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002558 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002559 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002560 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002561 uint8_t orig_ttvn, req_ttvn;
2562 uint16_t tvlv_len;
2563 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002564
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002565 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002566 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002567 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002568 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002569
2570 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002571 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002572 if (!req_dst_orig_node)
2573 goto out;
2574
Marek Lindner335fbe02013-04-23 21:40:02 +08002575 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002576 if (!res_dst_orig_node)
2577 goto out;
2578
Antonio Quartullia73105b2011-04-27 14:27:44 +02002579 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002580 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002581
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002582 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002583 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002584 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002585 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2586 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002587 goto out;
2588
Antonio Quartulli015758d2011-07-09 17:52:13 +02002589 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002590 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002591 !req_dst_orig_node->tt_buff)
2592 full_table = true;
2593 else
2594 full_table = false;
2595
Marek Lindner335fbe02013-04-23 21:40:02 +08002596 /* TT fragmentation hasn't been implemented yet, so send as many
2597 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002598 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002599 if (!full_table) {
2600 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2601 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002602
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002603 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2604 &tvlv_tt_data,
2605 &tt_change,
2606 &tt_len);
2607 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002608 goto unlock;
2609
Antonio Quartullia73105b2011-04-27 14:27:44 +02002610 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002611 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002612 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002613 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2614 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002615 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2616 * in the initial part
2617 */
2618 tt_len = -1;
2619 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2620 &tvlv_tt_data,
2621 &tt_change,
2622 &tt_len);
2623 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002624 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002625
2626 /* fill the rest of the tvlv with the real TT entries */
2627 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2628 tt_change, tt_len,
2629 batadv_tt_global_valid,
2630 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002631 }
2632
Marek Lindnera19d3d82013-05-27 15:33:25 +08002633 /* Don't send the response, if larger than fragmented packet. */
2634 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2635 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2636 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2637 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2638 res_dst_orig_node->orig);
2639 goto out;
2640 }
2641
Marek Lindner335fbe02013-04-23 21:40:02 +08002642 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2643 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002644
2645 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002646 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002647
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002648 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002649 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2650 res_dst_orig_node->orig, req_dst_orig_node->orig,
2651 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002652
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002653 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002654
Marek Lindner335fbe02013-04-23 21:40:02 +08002655 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002656 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2657 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002658
Marek Lindner335fbe02013-04-23 21:40:02 +08002659 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002660 goto out;
2661
2662unlock:
2663 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2664
2665out:
2666 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002667 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002668 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002669 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002670 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002671 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002672}
Sven Eckelmann96412692012-06-05 22:31:30 +02002673
Marek Lindner335fbe02013-04-23 21:40:02 +08002674/**
2675 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2676 * translation table
2677 * @bat_priv: the bat priv with all the soft interface information
2678 * @tt_data: tt data containing the tt request information
2679 * @req_src: mac address of tt request sender
2680 *
2681 * Returns true if tt request reply was sent, false otherwise.
2682 */
2683static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2684 struct batadv_tvlv_tt_data *tt_data,
2685 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002686{
Marek Lindner335fbe02013-04-23 21:40:02 +08002687 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002688 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002689 struct batadv_tvlv_tt_change *tt_change;
2690 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002691 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002692 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002693 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002694 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002695
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002696 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002697 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002698 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002699 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002700
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002701 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002702
Sven Eckelmann807736f2012-07-15 22:26:51 +02002703 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002704 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705
Marek Lindner335fbe02013-04-23 21:40:02 +08002706 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002707 if (!orig_node)
2708 goto out;
2709
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002710 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002711 if (!primary_if)
2712 goto out;
2713
2714 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002715 * is too big send the whole local translation table
2716 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002717 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002718 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002719 full_table = true;
2720 else
2721 full_table = false;
2722
Marek Lindner335fbe02013-04-23 21:40:02 +08002723 /* TT fragmentation hasn't been implemented yet, so send as many
2724 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002725 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002726 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002727 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002729 tt_len = bat_priv->tt.last_changeset_len;
2730 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2731 &tvlv_tt_data,
2732 &tt_change,
2733 &tt_len);
2734 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002735 goto unlock;
2736
Marek Lindner335fbe02013-04-23 21:40:02 +08002737 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002738 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002739 bat_priv->tt.last_changeset_len);
2740 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002741 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002742 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002743
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002744 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2745 * in the initial part
2746 */
2747 tt_len = -1;
2748 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2749 &tvlv_tt_data,
2750 &tt_change,
2751 &tt_len);
2752 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002753 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002754
2755 /* fill the rest of the tvlv with the real TT entries */
2756 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2757 tt_change, tt_len,
2758 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002759 }
2760
Marek Lindner335fbe02013-04-23 21:40:02 +08002761 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2762 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002763
2764 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002765 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002766
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002767 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002768 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2769 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002770
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002771 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002772
Marek Lindner335fbe02013-04-23 21:40:02 +08002773 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002774 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2775 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002776
Antonio Quartullia73105b2011-04-27 14:27:44 +02002777 goto out;
2778
2779unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002780 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002781out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002782 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002783 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002784 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002785 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002786 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002787 kfree(tvlv_tt_data);
2788 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002789 return true;
2790}
2791
Marek Lindner335fbe02013-04-23 21:40:02 +08002792/**
2793 * batadv_send_tt_response - send reply to tt request
2794 * @bat_priv: the bat priv with all the soft interface information
2795 * @tt_data: tt data containing the tt request information
2796 * @req_src: mac address of tt request sender
2797 * @req_dst: mac address of tt request recipient
2798 *
2799 * Returns true if tt request reply was sent, false otherwise.
2800 */
2801static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2802 struct batadv_tvlv_tt_data *tt_data,
2803 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002804{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002805 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002806 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002807 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2808 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002809}
2810
Sven Eckelmann56303d32012-06-05 22:31:31 +02002811static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2812 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002813 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002814 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002815{
2816 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002817 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002818
2819 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002820 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2821 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002822 batadv_tt_global_del(bat_priv, orig_node,
2823 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002824 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002825 "tt removed by changes",
2826 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002827 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002828 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002829 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002830 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002831 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002832 /* In case of problem while storing a
2833 * global_entry, we stop the updating
2834 * procedure without committing the
2835 * ttvn change. This will avoid to send
2836 * corrupted data on tt_request
2837 */
2838 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002839 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01002841 orig_node->capa_initialized |= BATADV_ORIG_CAPA_HAS_TT;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002842}
2843
Sven Eckelmann56303d32012-06-05 22:31:31 +02002844static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002845 struct batadv_tvlv_tt_change *tt_change,
2846 uint8_t ttvn, uint8_t *resp_src,
2847 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002848{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002849 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002850
Marek Lindner335fbe02013-04-23 21:40:02 +08002851 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002852 if (!orig_node)
2853 goto out;
2854
2855 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002856 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2857 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002858
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002859 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2860 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002861
2862 spin_lock_bh(&orig_node->tt_buff_lock);
2863 kfree(orig_node->tt_buff);
2864 orig_node->tt_buff_len = 0;
2865 orig_node->tt_buff = NULL;
2866 spin_unlock_bh(&orig_node->tt_buff_lock);
2867
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002868 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002869
2870out:
2871 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002872 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002873}
2874
Sven Eckelmann56303d32012-06-05 22:31:31 +02002875static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2876 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002877 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002878 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002879{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002880 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2881 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002882
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002883 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2884 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002885 atomic_set(&orig_node->last_ttvn, ttvn);
2886}
2887
Antonio Quartullic018ad32013-06-04 12:11:39 +02002888/**
2889 * batadv_is_my_client - check if a client is served by the local node
2890 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002891 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002892 * @vid: VLAN identifier
2893 *
2894 * Returns true if the client is served by this node, false otherwise.
2895 */
2896bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2897 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002898{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002899 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002900 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002901
Antonio Quartullic018ad32013-06-04 12:11:39 +02002902 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002903 if (!tt_local_entry)
2904 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002905 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002906 * consistency purpose)
2907 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002908 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2909 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002910 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002911 ret = true;
2912out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002913 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002914 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002915 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002916}
2917
Marek Lindner335fbe02013-04-23 21:40:02 +08002918/**
2919 * batadv_handle_tt_response - process incoming tt reply
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @tt_data: tt data containing the tt request information
2922 * @resp_src: mac address of tt reply sender
2923 * @num_entries: number of tt change entries appended to the tt data
2924 */
2925static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2926 struct batadv_tvlv_tt_data *tt_data,
2927 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002928{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002929 struct batadv_tt_req_node *node, *safe;
2930 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002931 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002932 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2933 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002934
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002935 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002936 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002937 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002938 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002939
Marek Lindner335fbe02013-04-23 21:40:02 +08002940 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002941 if (!orig_node)
2942 goto out;
2943
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002944 spin_lock_bh(&orig_node->tt_lock);
2945
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002946 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2947 change_offset *= ntohs(tt_data->num_vlan);
2948 change_offset += sizeof(*tt_data);
2949 tvlv_ptr += change_offset;
2950
2951 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002952 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002953 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2954 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002955 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002956 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2957 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002958 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002959
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002960 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002961 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002962
2963 spin_unlock_bh(&orig_node->tt_lock);
2964
Antonio Quartullia73105b2011-04-27 14:27:44 +02002965 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002966 spin_lock_bh(&bat_priv->tt.req_list_lock);
2967 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002968 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002969 continue;
2970 list_del(&node->list);
2971 kfree(node);
2972 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002973
Sven Eckelmann807736f2012-07-15 22:26:51 +02002974 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002975out:
2976 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002977 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002978}
2979
Sven Eckelmann56303d32012-06-05 22:31:31 +02002980static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002981{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002982 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002983
Sven Eckelmann807736f2012-07-15 22:26:51 +02002984 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002985
Sven Eckelmann807736f2012-07-15 22:26:51 +02002986 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002987 list_del(&node->list);
2988 kfree(node);
2989 }
2990
Sven Eckelmann807736f2012-07-15 22:26:51 +02002991 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002992}
2993
Sven Eckelmann56303d32012-06-05 22:31:31 +02002994static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002995{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002996 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002997
Sven Eckelmann807736f2012-07-15 22:26:51 +02002998 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2999 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003000 if (!batadv_has_timed_out(node->first_time,
3001 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003002 continue;
3003
3004 list_del(&node->list);
3005 kfree(node);
3006 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003007 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003008}
3009
3010/* This function checks whether the client already reached the
3011 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3012 * will not be sent.
3013 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003014 * returns true if the ROAMING_ADV can be sent, false otherwise
3015 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003016static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02003017 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003018{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003019 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003020 bool ret = false;
3021
Sven Eckelmann807736f2012-07-15 22:26:51 +02003022 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003023 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003024 * reply from the same orig_node yet
3025 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003026 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003027 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003028 continue;
3029
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003030 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003031 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003032 continue;
3033
Sven Eckelmann3e348192012-05-16 20:23:22 +02003034 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003035 /* Sorry, you roamed too many times! */
3036 goto unlock;
3037 ret = true;
3038 break;
3039 }
3040
3041 if (!ret) {
3042 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3043 if (!tt_roam_node)
3044 goto unlock;
3045
3046 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003047 atomic_set(&tt_roam_node->counter,
3048 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003049 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003050
Sven Eckelmann807736f2012-07-15 22:26:51 +02003051 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003052 ret = true;
3053 }
3054
3055unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003056 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003057 return ret;
3058}
3059
Antonio Quartullic018ad32013-06-04 12:11:39 +02003060/**
3061 * batadv_send_roam_adv - send a roaming advertisement message
3062 * @bat_priv: the bat priv with all the soft interface information
3063 * @client: mac address of the roaming client
3064 * @vid: VLAN identifier
3065 * @orig_node: message destination
3066 *
3067 * Send a ROAMING_ADV message to the node which was previously serving this
3068 * client. This is done to inform the node that from now on all traffic destined
3069 * for this particular roamed client has to be forwarded to the sender of the
3070 * roaming message.
3071 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003072static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003073 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003074 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003075{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003076 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003077 struct batadv_tvlv_roam_adv tvlv_roam;
3078
3079 primary_if = batadv_primary_if_get_selected(bat_priv);
3080 if (!primary_if)
3081 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003082
3083 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003084 * already roamed to us too many times
3085 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003086 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003087 goto out;
3088
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003089 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003090 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3091 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003092
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003093 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003094
Marek Lindner122edaa2013-04-23 21:40:03 +08003095 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003096 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003097
3098 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3099 orig_node->orig, BATADV_TVLV_ROAM, 1,
3100 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003101
3102out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003103 if (primary_if)
3104 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003105}
3106
Sven Eckelmanna5130882012-05-16 20:23:16 +02003107static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003108{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003109 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003110 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003111 struct batadv_priv *bat_priv;
3112
3113 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003114 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3115 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003116
Marek Lindnera19d3d82013-05-27 15:33:25 +08003117 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003118 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003119 batadv_tt_req_purge(bat_priv);
3120 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003121
Antonio Quartulli72414442012-12-25 13:14:37 +01003122 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3123 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003124}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003125
Sven Eckelmann56303d32012-06-05 22:31:31 +02003126void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003127{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003128 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3129 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3130
Sven Eckelmann807736f2012-07-15 22:26:51 +02003131 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003132
Sven Eckelmanna5130882012-05-16 20:23:16 +02003133 batadv_tt_local_table_free(bat_priv);
3134 batadv_tt_global_table_free(bat_priv);
3135 batadv_tt_req_list_free(bat_priv);
3136 batadv_tt_changes_list_free(bat_priv);
3137 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003138
Sven Eckelmann807736f2012-07-15 22:26:51 +02003139 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003140}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003141
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003142/**
3143 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3144 * table and possibly count them in the TT size
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @flags: the flag to switch
3147 * @enable: whether to set or unset the flag
3148 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003149 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003150static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3151 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003152{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003153 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3154 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003155 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003156 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003157 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003158
3159 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003160 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003161
3162 for (i = 0; i < hash->size; i++) {
3163 head = &hash->table[i];
3164
3165 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003166 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003167 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003168 if (enable) {
3169 if ((tt_common_entry->flags & flags) == flags)
3170 continue;
3171 tt_common_entry->flags |= flags;
3172 } else {
3173 if (!(tt_common_entry->flags & flags))
3174 continue;
3175 tt_common_entry->flags &= ~flags;
3176 }
3177 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003178
3179 if (!count)
3180 continue;
3181
3182 batadv_tt_local_size_inc(bat_priv,
3183 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003184 }
3185 rcu_read_unlock();
3186 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003187}
3188
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003189/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003190static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003191{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003192 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003193 struct batadv_tt_common_entry *tt_common;
3194 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003195 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003196 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003197 struct hlist_head *head;
3198 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003199 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003200
3201 if (!hash)
3202 return;
3203
3204 for (i = 0; i < hash->size; i++) {
3205 head = &hash->table[i];
3206 list_lock = &hash->list_locks[i];
3207
3208 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003209 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003210 hash_entry) {
3211 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003212 continue;
3213
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003214 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003215 "Deleting local tt entry (%pM, vid: %d): pending\n",
3216 tt_common->addr,
3217 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003218
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003219 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003220 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003221 tt_local = container_of(tt_common,
3222 struct batadv_tt_local_entry,
3223 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003224
3225 /* decrease the reference held for this vlan */
3226 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08003227 if (vlan) {
3228 batadv_softif_vlan_free_ref(vlan);
3229 batadv_softif_vlan_free_ref(vlan);
3230 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003231
Sven Eckelmann56303d32012-06-05 22:31:31 +02003232 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003233 }
3234 spin_unlock_bh(list_lock);
3235 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003236}
3237
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003238/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003239 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3240 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003241 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003242 *
3243 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003244 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003245static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003246{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003247 /* Update multicast addresses in local translation table */
3248 batadv_mcast_mla_update(bat_priv);
3249
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003250 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3251 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3252 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003253 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003254 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003255
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003256 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003257
Sven Eckelmanna5130882012-05-16 20:23:16 +02003258 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003259 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003260
3261 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003262 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003263 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003264 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003265 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003266
3267 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003268 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003269 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003270}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003271
Marek Lindnera19d3d82013-05-27 15:33:25 +08003272/**
3273 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3274 * have been queued in the time since the last commit
3275 * @bat_priv: the bat priv with all the soft interface information
3276 */
3277void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3278{
3279 spin_lock_bh(&bat_priv->tt.commit_lock);
3280 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003281 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003282}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003283
Sven Eckelmann56303d32012-06-05 22:31:31 +02003284bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003285 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003286{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003287 struct batadv_tt_local_entry *tt_local_entry = NULL;
3288 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003289 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003290 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003291
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003292 vlan = batadv_softif_vlan_get(bat_priv, vid);
3293 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003294 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003295
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003296 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003297 if (!tt_local_entry)
3298 goto out;
3299
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003300 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003301 if (!tt_global_entry)
3302 goto out;
3303
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003304 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003305 goto out;
3306
Marek Lindner5870adc2012-06-20 17:16:05 +02003307 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003308
3309out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003310 if (vlan)
3311 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003312 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003313 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003314 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003315 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003316 return ret;
3317}
Marek Lindnera943cac2011-07-30 13:10:18 +02003318
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003319/**
3320 * batadv_tt_update_orig - update global translation table with new tt
3321 * information received via ogms
3322 * @bat_priv: the bat priv with all the soft interface information
3323 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003324 * @tt_vlan: pointer to the first tvlv VLAN entry
3325 * @tt_num_vlan: number of tvlv VLAN entries
3326 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003327 * @tt_num_changes: number of tt changes inside the tt buffer
3328 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003329 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003330 */
3331static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3332 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003333 const void *tt_buff, uint16_t tt_num_vlan,
3334 struct batadv_tvlv_tt_change *tt_change,
3335 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003336{
3337 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003338 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003339 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003340 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003341
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003342 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003343 has_tt_init = orig_node->capa_initialized & BATADV_ORIG_CAPA_HAS_TT;
3344
Antonio Quartulli17071572011-11-07 16:36:40 +01003345 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003346 * increased by one -> we can apply the attached changes
3347 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003348 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003349 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003350 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3351 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003352 * In this case send a tt request
3353 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003354 if (!tt_num_changes) {
3355 full_table = false;
3356 goto request_table;
3357 }
3358
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003359 spin_lock_bh(&orig_node->tt_lock);
3360
Sven Eckelmanna5130882012-05-16 20:23:16 +02003361 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003362 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003363
3364 /* Even if we received the precomputed crc with the OGM, we
3365 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003366 * in the global table
3367 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003368 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003369
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003370 spin_unlock_bh(&orig_node->tt_lock);
3371
Marek Lindnera943cac2011-07-30 13:10:18 +02003372 /* The ttvn alone is not enough to guarantee consistency
3373 * because a single value could represent different states
3374 * (due to the wrap around). Thus a node has to check whether
3375 * the resulting table (after applying the changes) is still
3376 * consistent or not. E.g. a node could disconnect while its
3377 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3378 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003379 * inconsistency
3380 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003381 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3382 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003383 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003384 } else {
3385 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003386 * in sync anymore -> request fresh tt data
3387 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003388 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003389 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3390 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003391request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003392 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003393 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3394 orig_node->orig, ttvn, orig_ttvn,
3395 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003396 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003397 tt_vlan, tt_num_vlan,
3398 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003399 return;
3400 }
3401 }
3402}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003403
Antonio Quartullic018ad32013-06-04 12:11:39 +02003404/**
3405 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3406 * @bat_priv: the bat priv with all the soft interface information
3407 * @addr: the mac address of the client to check
3408 * @vid: VLAN identifier
3409 *
3410 * Returns true if we know that the client has moved from its old originator
3411 * to another one. This entry is still kept for consistency purposes and will be
3412 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003413 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003414bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003415 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003416{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003417 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003418 bool ret = false;
3419
Antonio Quartullic018ad32013-06-04 12:11:39 +02003420 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003421 if (!tt_global_entry)
3422 goto out;
3423
Antonio Quartullic1d07432013-01-15 22:17:19 +10003424 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003425 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003426out:
3427 return ret;
3428}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003429
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003430/**
3431 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3432 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003433 * @addr: the mac address of the local client to query
3434 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003435 *
3436 * Returns true if the local client is known to be roaming (it is not served by
3437 * this node anymore) or not. If yes, the client is still present in the table
3438 * to keep the latter consistent with the node TTVN
3439 */
3440bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003441 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003442{
3443 struct batadv_tt_local_entry *tt_local_entry;
3444 bool ret = false;
3445
Antonio Quartullic018ad32013-06-04 12:11:39 +02003446 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003447 if (!tt_local_entry)
3448 goto out;
3449
3450 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3451 batadv_tt_local_entry_free_ref(tt_local_entry);
3452out:
3453 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003454}
3455
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003456bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3457 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003458 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003459 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003460{
3461 bool ret = false;
3462
Antonio Quartulli16052782013-06-04 12:11:41 +02003463 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003464 BATADV_TT_CLIENT_TEMP,
3465 atomic_read(&orig_node->last_ttvn)))
3466 goto out;
3467
3468 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003469 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3470 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003471 ret = true;
3472out:
3473 return ret;
3474}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003475
3476/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003477 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3478 * maximum packet size that can be transported through the mesh
3479 * @soft_iface: netdev struct of the mesh interface
3480 *
3481 * Remove entries older than 'timeout' and half timeout if more entries need
3482 * to be removed.
3483 */
3484void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3485{
3486 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3487 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3488 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3489 bool reduced = false;
3490
3491 spin_lock_bh(&bat_priv->tt.commit_lock);
3492
3493 while (true) {
3494 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3495 if (packet_size_max >= table_size)
3496 break;
3497
3498 batadv_tt_local_purge(bat_priv, timeout);
3499 batadv_tt_local_purge_pending_clients(bat_priv);
3500
3501 timeout /= 2;
3502 reduced = true;
3503 net_ratelimited_function(batadv_info, soft_iface,
3504 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3505 packet_size_max);
3506 }
3507
3508 /* commit these changes immediately, to avoid synchronization problem
3509 * with the TTVN
3510 */
3511 if (reduced)
3512 batadv_tt_local_commit_changes_nolock(bat_priv);
3513
3514 spin_unlock_bh(&bat_priv->tt.commit_lock);
3515}
3516
3517/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003518 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3519 * @bat_priv: the bat priv with all the soft interface information
3520 * @orig: the orig_node of the ogm
3521 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3522 * @tvlv_value: tvlv buffer containing the gateway data
3523 * @tvlv_value_len: tvlv buffer length
3524 */
3525static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3526 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003527 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003528 uint16_t tvlv_value_len)
3529{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003530 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3531 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003532 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003533 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003534
3535 if (tvlv_value_len < sizeof(*tt_data))
3536 return;
3537
3538 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3539 tvlv_value_len -= sizeof(*tt_data);
3540
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003541 num_vlan = ntohs(tt_data->num_vlan);
3542
3543 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3544 return;
3545
3546 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3547 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3548 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3549
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003550 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003551
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003552 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3553 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003554}
3555
3556/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003557 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3558 * container
3559 * @bat_priv: the bat priv with all the soft interface information
3560 * @src: mac address of tt tvlv sender
3561 * @dst: mac address of tt tvlv recipient
3562 * @tvlv_value: tvlv buffer containing the tt data
3563 * @tvlv_value_len: tvlv buffer length
3564 *
3565 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3566 * otherwise.
3567 */
3568static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3569 uint8_t *src, uint8_t *dst,
3570 void *tvlv_value,
3571 uint16_t tvlv_value_len)
3572{
3573 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003574 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003575 char tt_flag;
3576 bool ret;
3577
3578 if (tvlv_value_len < sizeof(*tt_data))
3579 return NET_RX_SUCCESS;
3580
3581 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3582 tvlv_value_len -= sizeof(*tt_data);
3583
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003584 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3585 tt_vlan_len *= ntohs(tt_data->num_vlan);
3586
3587 if (tvlv_value_len < tt_vlan_len)
3588 return NET_RX_SUCCESS;
3589
3590 tvlv_value_len -= tt_vlan_len;
3591 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003592
3593 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3594 case BATADV_TT_REQUEST:
3595 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3596
3597 /* If this node cannot provide a TT response the tt_request is
3598 * forwarded
3599 */
3600 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3601 if (!ret) {
3602 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3603 tt_flag = 'F';
3604 else
3605 tt_flag = '.';
3606
3607 batadv_dbg(BATADV_DBG_TT, bat_priv,
3608 "Routing TT_REQUEST to %pM [%c]\n",
3609 dst, tt_flag);
3610 /* tvlv API will re-route the packet */
3611 return NET_RX_DROP;
3612 }
3613 break;
3614 case BATADV_TT_RESPONSE:
3615 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3616
3617 if (batadv_is_my_mac(bat_priv, dst)) {
3618 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003619 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003620 return NET_RX_SUCCESS;
3621 }
3622
3623 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3624 tt_flag = 'F';
3625 else
3626 tt_flag = '.';
3627
3628 batadv_dbg(BATADV_DBG_TT, bat_priv,
3629 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3630
3631 /* tvlv API will re-route the packet */
3632 return NET_RX_DROP;
3633 }
3634
3635 return NET_RX_SUCCESS;
3636}
3637
3638/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003639 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3640 * @bat_priv: the bat priv with all the soft interface information
3641 * @src: mac address of tt tvlv sender
3642 * @dst: mac address of tt tvlv recipient
3643 * @tvlv_value: tvlv buffer containing the tt data
3644 * @tvlv_value_len: tvlv buffer length
3645 *
3646 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3647 * otherwise.
3648 */
3649static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3650 uint8_t *src, uint8_t *dst,
3651 void *tvlv_value,
3652 uint16_t tvlv_value_len)
3653{
3654 struct batadv_tvlv_roam_adv *roaming_adv;
3655 struct batadv_orig_node *orig_node = NULL;
3656
3657 /* If this node is not the intended recipient of the
3658 * roaming advertisement the packet is forwarded
3659 * (the tvlv API will re-route the packet).
3660 */
3661 if (!batadv_is_my_mac(bat_priv, dst))
3662 return NET_RX_DROP;
3663
Marek Lindner122edaa2013-04-23 21:40:03 +08003664 if (tvlv_value_len < sizeof(*roaming_adv))
3665 goto out;
3666
3667 orig_node = batadv_orig_hash_find(bat_priv, src);
3668 if (!orig_node)
3669 goto out;
3670
3671 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3672 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3673
3674 batadv_dbg(BATADV_DBG_TT, bat_priv,
3675 "Received ROAMING_ADV from %pM (client %pM)\n",
3676 src, roaming_adv->client);
3677
3678 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003679 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003680 atomic_read(&orig_node->last_ttvn) + 1);
3681
3682out:
3683 if (orig_node)
3684 batadv_orig_node_free_ref(orig_node);
3685 return NET_RX_SUCCESS;
3686}
3687
3688/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003689 * batadv_tt_init - initialise the translation table internals
3690 * @bat_priv: the bat priv with all the soft interface information
3691 *
3692 * Return 0 on success or negative error number in case of failure.
3693 */
3694int batadv_tt_init(struct batadv_priv *bat_priv)
3695{
3696 int ret;
3697
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003698 /* synchronized flags must be remote */
3699 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3700
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003701 ret = batadv_tt_local_init(bat_priv);
3702 if (ret < 0)
3703 return ret;
3704
3705 ret = batadv_tt_global_init(bat_priv);
3706 if (ret < 0)
3707 return ret;
3708
3709 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003710 batadv_tt_tvlv_unicast_handler_v1,
3711 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003712
Marek Lindner122edaa2013-04-23 21:40:03 +08003713 batadv_tvlv_handler_register(bat_priv, NULL,
3714 batadv_roam_tvlv_unicast_handler_v1,
3715 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3716
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003717 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3718 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3719 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3720
3721 return 1;
3722}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003723
3724/**
3725 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3726 * @bat_priv: the bat priv with all the soft interface information
3727 * @addr: the mac address of the client
3728 * @vid: the identifier of the VLAN where this client is connected
3729 *
3730 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3731 * otherwise
3732 */
3733bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3734 const uint8_t *addr, unsigned short vid)
3735{
3736 struct batadv_tt_global_entry *tt;
3737 bool ret;
3738
3739 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3740 if (!tt)
3741 return false;
3742
3743 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3744
3745 batadv_tt_global_entry_free_ref(tt);
3746
3747 return ret;
3748}