blob: 7d8fc90c9e3886f28469eaf9a8aa26fc95ce3493 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "translation-table.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
Linus Lüssingac4eebd2015-06-16 17:10:24 +020022#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/bug.h>
24#include <linux/byteorder/generic.h>
25#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020026#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/string.h>
44#include <linux/workqueue.h>
45#include <net/net_namespace.h>
46
47#include "bridge_loop_avoidance.h"
48#include "hard-interface.h"
49#include "hash.h"
50#include "multicast.h"
51#include "originator.h"
52#include "packet.h"
53#include "soft-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020054
Antonio Quartullidec05072012-11-10 11:00:32 +010055/* hash class keys */
56static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57static struct lock_class_key batadv_tt_global_hash_lock_class_key;
58
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020059static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020060 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020061 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020062static void batadv_tt_purge(struct work_struct *work);
63static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020064batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020065static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020068 unsigned short vid, const char *message,
69 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070
Marek Lindner4c718952015-08-06 10:38:54 +020071/* returns 1 if they are the same mac addr and vid */
Sven Eckelmanna5130882012-05-16 20:23:16 +020072static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000073{
Sven Eckelmann56303d32012-06-05 22:31:31 +020074 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020075 hash_entry);
Marek Lindner4c718952015-08-06 10:38:54 +020076 const struct batadv_tt_common_entry *tt1 = data1;
77 const struct batadv_tt_common_entry *tt2 = data2;
Marek Lindner7aadf882011-02-18 12:28:09 +000078
Marek Lindner4c718952015-08-06 10:38:54 +020079 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000080}
81
Antonio Quartullic018ad32013-06-04 12:11:39 +020082/**
83 * batadv_choose_tt - return the index of the tt entry in the hash table
84 * @data: pointer to the tt_common_entry object to map
85 * @size: the size of the hash table
86 *
87 * Returns the hash index where the object represented by 'data' should be
88 * stored at.
89 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020090static inline u32 batadv_choose_tt(const void *data, u32 size)
Antonio Quartullic018ad32013-06-04 12:11:39 +020091{
92 struct batadv_tt_common_entry *tt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020093 u32 hash = 0;
Antonio Quartullic018ad32013-06-04 12:11:39 +020094
95 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010096 hash = jhash(&tt->addr, ETH_ALEN, hash);
97 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +020098
99 return hash % size;
100}
101
102/**
103 * batadv_tt_hash_find - look for a client in the given hash table
104 * @hash: the hash table to search
105 * @addr: the mac address of the client to look for
106 * @vid: VLAN identifier
107 *
108 * Returns a pointer to the tt_common struct belonging to the searched client if
109 * found, NULL otherwise.
110 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200111static struct batadv_tt_common_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200112batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200113 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000114{
Marek Lindner7aadf882011-02-18 12:28:09 +0000115 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200116 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200117 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000118
119 if (!hash)
120 return NULL;
121
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100122 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200123 to_search.vid = vid;
124
125 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000126 head = &hash->table[index];
127
128 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200129 hlist_for_each_entry_rcu(tt, head, hash_entry) {
130 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000131 continue;
132
Antonio Quartullic018ad32013-06-04 12:11:39 +0200133 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134 continue;
135
Antonio Quartullic018ad32013-06-04 12:11:39 +0200136 if (!atomic_inc_not_zero(&tt->refcount))
137 continue;
138
139 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000140 break;
141 }
142 rcu_read_unlock();
143
Antonio Quartullic018ad32013-06-04 12:11:39 +0200144 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100145}
146
Antonio Quartullic018ad32013-06-04 12:11:39 +0200147/**
148 * batadv_tt_local_hash_find - search the local table for a given client
149 * @bat_priv: the bat priv with all the soft interface information
150 * @addr: the mac address of the client to look for
151 * @vid: VLAN identifier
152 *
153 * Returns a pointer to the corresponding tt_local_entry struct if the client is
154 * found, NULL otherwise.
155 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200156static struct batadv_tt_local_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200157batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200158 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100159{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160 struct batadv_tt_common_entry *tt_common_entry;
161 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100162
Antonio Quartullic018ad32013-06-04 12:11:39 +0200163 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
164 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100165 if (tt_common_entry)
166 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200167 struct batadv_tt_local_entry,
168 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100169 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000170}
171
Antonio Quartullic018ad32013-06-04 12:11:39 +0200172/**
173 * batadv_tt_global_hash_find - search the global table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
177 *
178 * Returns a pointer to the corresponding tt_global_entry struct if the client
179 * is found, NULL otherwise.
180 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200181static struct batadv_tt_global_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200182batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200183 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000184{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000187
Antonio Quartullic018ad32013-06-04 12:11:39 +0200188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
189 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100190 if (tt_common_entry)
191 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200192 struct batadv_tt_global_entry,
193 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100194 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000195}
196
Sven Eckelmanna5130882012-05-16 20:23:16 +0200197static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200198batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100200 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
201 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200202}
203
Antonio Quartulli21026052013-05-07 00:29:22 +0200204/**
205 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
206 * tt_global_entry and possibly free it
207 * @tt_global_entry: the object to free
208 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200209static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200210batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200211{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200212 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200213 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200214 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200215 }
216}
217
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100218/**
219 * batadv_tt_global_hash_count - count the number of orig entries
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100220 * @addr: the mac address of the client to count entries for
221 * @vid: VLAN identifier
222 *
223 * Return the number of originators advertising the given address/data
224 * (excluding ourself).
225 */
226int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200227 const u8 *addr, unsigned short vid)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100228{
229 struct batadv_tt_global_entry *tt_global_entry;
230 int count;
231
232 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
233 if (!tt_global_entry)
234 return 0;
235
236 count = atomic_read(&tt_global_entry->orig_list_count);
237 batadv_tt_global_entry_free_ref(tt_global_entry);
238
239 return count;
240}
241
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200242/**
243 * batadv_tt_local_size_mod - change the size by v of the local table identified
244 * by vid
245 * @bat_priv: the bat priv with all the soft interface information
246 * @vid: the VLAN identifier of the sub-table to change
247 * @v: the amount to sum to the local table size
248 */
249static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
250 unsigned short vid, int v)
251{
252 struct batadv_softif_vlan *vlan;
253
254 vlan = batadv_softif_vlan_get(bat_priv, vid);
255 if (!vlan)
256 return;
257
258 atomic_add(v, &vlan->tt.num_entries);
259
260 batadv_softif_vlan_free_ref(vlan);
261}
262
263/**
264 * batadv_tt_local_size_inc - increase by one the local table size for the given
265 * vid
266 * @bat_priv: the bat priv with all the soft interface information
267 * @vid: the VLAN identifier
268 */
269static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
270 unsigned short vid)
271{
272 batadv_tt_local_size_mod(bat_priv, vid, 1);
273}
274
275/**
276 * batadv_tt_local_size_dec - decrease 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_dec(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_global_size_mod - change the size by v of the local table
289 * identified by vid
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200290 * @vid: the VLAN identifier
291 * @v: the amount to sum to the global table size
292 */
293static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
294 unsigned short vid, int v)
295{
296 struct batadv_orig_node_vlan *vlan;
297
298 vlan = batadv_orig_node_vlan_new(orig_node, vid);
299 if (!vlan)
300 return;
301
302 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
303 spin_lock_bh(&orig_node->vlan_list_lock);
Marek Lindnera1210482015-06-22 00:30:23 +0800304 hlist_del_init_rcu(&vlan->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200305 spin_unlock_bh(&orig_node->vlan_list_lock);
306 batadv_orig_node_vlan_free_ref(vlan);
307 }
308
309 batadv_orig_node_vlan_free_ref(vlan);
310}
311
312/**
313 * batadv_tt_global_size_inc - increase by one the global table size for the
314 * given vid
315 * @orig_node: the originator which global table size has to be decreased
316 * @vid: the vlan identifier
317 */
318static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
319 unsigned short vid)
320{
321 batadv_tt_global_size_mod(orig_node, vid, 1);
322}
323
324/**
325 * batadv_tt_global_size_dec - decrease by one the global table size for the
326 * given vid
327 * @orig_node: the originator which global table size has to be decreased
328 * @vid: the vlan identifier
329 */
330static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
331 unsigned short vid)
332{
333 batadv_tt_global_size_mod(orig_node, vid, -1);
334}
335
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100336/**
337 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
338 * queue for free after rcu grace period
339 * @orig_entry: tt orig entry to be free'd
340 */
341static void
342batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
343{
344 batadv_orig_node_free_ref(orig_entry->orig_node);
345 kfree_rcu(orig_entry, rcu);
346}
347
Sven Eckelmanna5130882012-05-16 20:23:16 +0200348static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200350{
Antonio Quartullid657e622012-07-01 14:09:12 +0200351 if (!atomic_dec_and_test(&orig_entry->refcount))
352 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200353
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100354 batadv_tt_orig_list_entry_release(orig_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200355}
356
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200357/**
358 * batadv_tt_local_event - store a local TT event (ADD/DEL)
359 * @bat_priv: the bat priv with all the soft interface information
360 * @tt_local_entry: the TT entry involved in the event
361 * @event_flags: flags to store in the event structure
362 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200363static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200364 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200365 u8 event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200366{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200367 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200368 struct batadv_tt_common_entry *common = &tt_local_entry->common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200369 u8 flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200370 bool event_removed = false;
371 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200372
373 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200374 if (!tt_change_node)
375 return;
376
Antonio Quartulliff66c972011-06-30 01:14:00 +0200377 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100378 memset(tt_change_node->change.reserved, 0,
379 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100380 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200381 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200382
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200383 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200384
385 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200386 spin_lock_bh(&bat_priv->tt.changes_list_lock);
387 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200388 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200389 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200390 continue;
391
392 /* DEL+ADD in the same orig interval have no effect and can be
393 * removed to avoid silly behaviour on the receiver side. The
394 * other way around (ADD+DEL) can happen in case of roaming of
395 * a client still in the NEW state. Roaming of NEW clients is
396 * now possible due to automatically recognition of "temporary"
397 * clients
398 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200399 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200400 if (!del_op_requested && del_op_entry)
401 goto del;
402 if (del_op_requested && !del_op_entry)
403 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200404
405 /* this is a second add in the same originator interval. It
406 * means that flags have been changed: update them!
407 */
408 if (!del_op_requested && !del_op_entry)
409 entry->change.flags = flags;
410
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200411 continue;
412del:
413 list_del(&entry->list);
414 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000415 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200416 event_removed = true;
417 goto unlock;
418 }
419
Antonio Quartullia73105b2011-04-27 14:27:44 +0200420 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200421 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200422
423unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200424 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200425
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200426 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200427 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200428 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200430}
431
Marek Lindner335fbe02013-04-23 21:40:02 +0800432/**
433 * batadv_tt_len - compute length in bytes of given number of tt changes
434 * @changes_num: number of tt changes
435 *
436 * Returns computed length in bytes.
437 */
438static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200439{
Marek Lindner335fbe02013-04-23 21:40:02 +0800440 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441}
442
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200443/**
444 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
445 * @tt_len: available space
446 *
447 * Returns the number of entries.
448 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200449static u16 batadv_tt_entries(u16 tt_len)
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200450{
451 return tt_len / batadv_tt_len(1);
452}
453
Marek Lindnera19d3d82013-05-27 15:33:25 +0800454/**
455 * batadv_tt_local_table_transmit_size - calculates the local translation table
456 * size when transmitted over the air
457 * @bat_priv: the bat priv with all the soft interface information
458 *
459 * Returns local translation table size in bytes.
460 */
461static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
462{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200463 u16 num_vlan = 0;
464 u16 tt_local_entries = 0;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800465 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 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200527bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
528 unsigned short vid, int ifindex, u32 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;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200538 bool ret = false;
539 bool roamed_back = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200540 u8 remote_flags;
541 u32 match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200543 if (ifindex != BATADV_NULL_IFINDEX)
544 in_dev = dev_get_by_index(&init_net, ifindex);
545
Antonio Quartullic018ad32013-06-04 12:11:39 +0200546 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100547
548 if (!is_multicast_ether_addr(addr))
549 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Antonio Quartulli47c94652012-09-23 22:38:35 +0200551 if (tt_local) {
552 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200553 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
554 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200555 "Re-adding pending client %pM (vid: %d)\n",
556 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200557 /* whatever the reason why the PENDING flag was set,
558 * this is a client which was enqueued to be removed in
559 * this orig_interval. Since it popped up again, the
560 * flag can be reset like it was never enqueued
561 */
562 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
563 goto add_event;
564 }
565
566 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
567 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200568 "Roaming client %pM (vid: %d) came back to its original location\n",
569 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200570 /* the ROAM flag is set because this client roamed away
571 * and the node got a roaming_advertisement message. Now
572 * that the client popped up again at its original
573 * location such flag can be unset
574 */
575 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
576 roamed_back = true;
577 }
578 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 }
580
Marek Lindnera19d3d82013-05-27 15:33:25 +0800581 /* Ignore the client if we cannot send it in a full table response. */
582 table_size = batadv_tt_local_table_transmit_size(bat_priv);
583 table_size += batadv_tt_len(1);
584 packet_size_max = atomic_read(&bat_priv->packet_size_max);
585 if (table_size > packet_size_max) {
586 net_ratelimited_function(batadv_info, soft_iface,
587 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
588 table_size, packet_size_max, addr);
589 goto out;
590 }
591
Antonio Quartulli47c94652012-09-23 22:38:35 +0200592 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
593 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200594 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200595
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200596 /* increase the refcounter of the related vlan */
597 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +0800598 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200599 addr, BATADV_PRINT_VID(vid))) {
600 kfree(tt_local);
601 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800602 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200603 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200604
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200605 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200606 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
607 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200608 (u8)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100610 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100611 /* The local entry has to be marked as NEW to avoid to send it in
612 * a full table response going out before the next ttvn increment
613 * (consistency check)
614 */
615 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200616 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200617 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200618 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
619 atomic_set(&tt_local->common.refcount, 2);
620 tt_local->last_seen = jiffies;
621 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100623 /* the batman interface mac and multicast addresses should never be
624 * purged
625 */
626 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
627 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200628 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629
Sven Eckelmann807736f2012-07-15 22:26:51 +0200630 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200631 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200632 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100633
634 if (unlikely(hash_added != 0)) {
635 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200636 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200637 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100638 goto out;
639 }
640
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200641add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200642 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200643
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200644check_roaming:
645 /* Check whether it is a roaming, but don't do anything if the roaming
646 * process has already been handled
647 */
648 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200649 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200650 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200651 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800652 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200653 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200654 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200655 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200656 }
657 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200658 if (roamed_back) {
659 batadv_tt_global_free(bat_priv, tt_global,
660 "Roaming canceled");
661 tt_global = NULL;
662 } else {
663 /* The global entry has to be marked as ROAMING and
664 * has to be kept for consistency purpose
665 */
666 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
667 tt_global->roam_at = jiffies;
668 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200669 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200670
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200671 /* store the current remote flags before altering them. This helps
672 * understanding is flags are changing or not
673 */
674 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800675
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200676 if (batadv_is_wifi_netdev(in_dev))
677 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
678 else
679 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
680
Antonio Quartulli9464d072013-11-16 12:03:48 +0100681 /* check the mark in the skb: if it's equal to the configured
682 * isolation_mark, it means the packet is coming from an isolated
683 * non-mesh client
684 */
685 match_mark = (mark & bat_priv->isolation_mark_mask);
686 if (bat_priv->isolation_mark_mask &&
687 match_mark == bat_priv->isolation_mark)
688 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
689 else
690 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
691
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200692 /* if any "dynamic" flag has been modified, resend an ADD event for this
693 * entry so that all the nodes can get the new flags
694 */
695 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
696 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
697
698 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200699out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200700 if (in_dev)
701 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200702 if (tt_local)
703 batadv_tt_local_entry_free_ref(tt_local);
704 if (tt_global)
705 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800706 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707}
708
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800709/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200710 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
711 * within a TT Response directed to another node
712 * @orig_node: originator for which the TT data has to be prepared
713 * @tt_data: uninitialised pointer to the address of the TVLV buffer
714 * @tt_change: uninitialised pointer to the address of the area where the TT
715 * changed can be stored
716 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
717 * function reserves the amount of space needed to send the entire global TT
718 * table. In case of success the value is updated with the real amount of
719 * reserved bytes
720
721 * Allocate the needed amount of memory for the entire TT TVLV and write its
722 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
723 * objects, one per active VLAN served by the originator node.
724 *
725 * Return the size of the allocated buffer or 0 in case of failure.
726 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200727static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200728batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
729 struct batadv_tvlv_tt_data **tt_data,
730 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200731 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200732{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200733 u16 num_vlan = 0;
734 u16 num_entries = 0;
735 u16 change_offset;
736 u16 tvlv_len;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200737 struct batadv_tvlv_tt_vlan_data *tt_vlan;
738 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200739 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200740
741 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800742 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200743 num_vlan++;
744 num_entries += atomic_read(&vlan->tt.num_entries);
745 }
746
747 change_offset = sizeof(**tt_data);
748 change_offset += num_vlan * sizeof(*tt_vlan);
749
750 /* if tt_len is negative, allocate the space needed by the full table */
751 if (*tt_len < 0)
752 *tt_len = batadv_tt_len(num_entries);
753
754 tvlv_len = *tt_len;
755 tvlv_len += change_offset;
756
757 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
758 if (!*tt_data) {
759 *tt_len = 0;
760 goto out;
761 }
762
763 (*tt_data)->flags = BATADV_NO_FLAGS;
764 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
765 (*tt_data)->num_vlan = htons(num_vlan);
766
767 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800768 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200769 tt_vlan->vid = htons(vlan->vid);
770 tt_vlan->crc = htonl(vlan->tt.crc);
771
772 tt_vlan++;
773 }
774
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200775 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200776 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
777
778out:
779 rcu_read_unlock();
780 return tvlv_len;
781}
782
783/**
784 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
785 * node
786 * @bat_priv: the bat priv with all the soft interface information
787 * @tt_data: uninitialised pointer to the address of the TVLV buffer
788 * @tt_change: uninitialised pointer to the address of the area where the TT
789 * changes can be stored
790 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
791 * function reserves the amount of space needed to send the entire local TT
792 * table. In case of success the value is updated with the real amount of
793 * reserved bytes
794 *
795 * Allocate the needed amount of memory for the entire TT TVLV and write its
796 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
797 * objects, one per active VLAN.
798 *
799 * Return the size of the allocated buffer or 0 in case of failure.
800 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200801static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200802batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
803 struct batadv_tvlv_tt_data **tt_data,
804 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200805 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200806{
807 struct batadv_tvlv_tt_vlan_data *tt_vlan;
808 struct batadv_softif_vlan *vlan;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200809 u16 num_vlan = 0;
810 u16 num_entries = 0;
811 u16 tvlv_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200812 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200813 int change_offset;
814
815 rcu_read_lock();
816 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
817 num_vlan++;
818 num_entries += atomic_read(&vlan->tt.num_entries);
819 }
820
821 change_offset = sizeof(**tt_data);
822 change_offset += num_vlan * sizeof(*tt_vlan);
823
824 /* if tt_len is negative, allocate the space needed by the full table */
825 if (*tt_len < 0)
826 *tt_len = batadv_tt_len(num_entries);
827
828 tvlv_len = *tt_len;
829 tvlv_len += change_offset;
830
831 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
832 if (!*tt_data) {
833 tvlv_len = 0;
834 goto out;
835 }
836
837 (*tt_data)->flags = BATADV_NO_FLAGS;
838 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
839 (*tt_data)->num_vlan = htons(num_vlan);
840
841 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
842 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
843 tt_vlan->vid = htons(vlan->vid);
844 tt_vlan->crc = htonl(vlan->tt.crc);
845
846 tt_vlan++;
847 }
848
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200849 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200850 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
851
852out:
853 rcu_read_unlock();
854 return tvlv_len;
855}
856
857/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800858 * batadv_tt_tvlv_container_update - update the translation table tvlv container
859 * after local tt changes have been committed
860 * @bat_priv: the bat priv with all the soft interface information
861 */
862static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800864 struct batadv_tt_change_node *entry, *safe;
865 struct batadv_tvlv_tt_data *tt_data;
866 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200867 int tt_diff_len, tt_change_len = 0;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200868 int tt_diff_entries_num = 0;
869 int tt_diff_entries_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200870 u16 tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200872 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
873 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800874
875 /* if we have too many changes for one packet don't send any
876 * and wait for the tt table request which will be fragmented
877 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800878 if (tt_diff_len > bat_priv->soft_iface->mtu)
879 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800880
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200881 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
882 &tt_change, &tt_diff_len);
883 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800884 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800885
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800886 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800887
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800888 if (tt_diff_len == 0)
889 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800890
Sven Eckelmann807736f2012-07-15 22:26:51 +0200891 spin_lock_bh(&bat_priv->tt.changes_list_lock);
892 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893
Sven Eckelmann807736f2012-07-15 22:26:51 +0200894 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100895 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800896 if (tt_diff_entries_count < tt_diff_entries_num) {
897 memcpy(tt_change + tt_diff_entries_count,
898 &entry->change,
899 sizeof(struct batadv_tvlv_tt_change));
900 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000901 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200902 list_del(&entry->list);
903 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200905 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200908 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
909 kfree(bat_priv->tt.last_changeset);
910 bat_priv->tt.last_changeset_len = 0;
911 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800912 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800913 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800914 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800915 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200916 * instead of providing the diff
917 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800918 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200919 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800920 memcpy(bat_priv->tt.last_changeset,
921 tt_change, tt_change_len);
922 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200923 }
924 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200925 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800927container_register:
928 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200929 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800930 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931}
932
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200933int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934{
935 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200936 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200937 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200938 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100939 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200940 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200941 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200943 unsigned short vid;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200944 u32 i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100945 int last_seen_secs;
946 int last_seen_msecs;
947 unsigned long last_seen_jiffies;
948 bool no_purge;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200949 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Marek Lindner30da63a2012-08-03 17:15:46 +0200951 primary_if = batadv_seq_print_text_primary_if_get(seq);
952 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200953 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000954
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100955 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200956 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200957 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100958 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200959 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961 for (i = 0; i < hash->size; i++) {
962 head = &hash->table[i];
963
Marek Lindner7aadf882011-02-18 12:28:09 +0000964 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800965 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000966 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100967 tt_local = container_of(tt_common_entry,
968 struct batadv_tt_local_entry,
969 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200970 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100971 last_seen_jiffies = jiffies - tt_local->last_seen;
972 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
973 last_seen_secs = last_seen_msecs / 1000;
974 last_seen_msecs = last_seen_msecs % 1000;
975
976 no_purge = tt_common_entry->flags & np_flag;
977
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200978 vlan = batadv_softif_vlan_get(bat_priv, vid);
979 if (!vlan) {
980 seq_printf(seq, "Cannot retrieve VLAN %d\n",
981 BATADV_PRINT_VID(vid));
982 continue;
983 }
984
985 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100986 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100987 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200988 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200989 ((tt_common_entry->flags &
990 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100991 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200992 ((tt_common_entry->flags &
993 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
994 ((tt_common_entry->flags &
995 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
996 ((tt_common_entry->flags &
997 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
998 ((tt_common_entry->flags &
999 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +01001000 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001001 no_purge ? 0 : last_seen_msecs,
1002 vlan->tt.crc);
1003
1004 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001005 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001006 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001007 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001008out:
1009 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001010 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001011 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012}
1013
Sven Eckelmann56303d32012-06-05 22:31:31 +02001014static void
1015batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1016 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001017 u16 flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001019 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020
Antonio Quartulli015758d2011-07-09 17:52:13 +02001021 /* The local client has to be marked as "pending to be removed" but has
1022 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001023 * response issued before the net ttvn increment (consistency check)
1024 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001025 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001026
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001027 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001028 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1029 tt_local_entry->common.addr,
1030 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031}
1032
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001033/**
1034 * batadv_tt_local_remove - logically remove an entry from the local table
1035 * @bat_priv: the bat priv with all the soft interface information
1036 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001037 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001038 * @message: message to append to the log on deletion
1039 * @roaming: true if the deletion is due to a roaming event
1040 *
1041 * Returns the flags assigned to the local entry before being deleted
1042 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001043u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1044 unsigned short vid, const char *message,
1045 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001047 struct batadv_tt_local_entry *tt_local_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001048 u16 flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001049 struct batadv_softif_vlan *vlan;
Marek Lindneref727062015-06-17 20:01:36 +08001050 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051
Antonio Quartullic018ad32013-06-04 12:11:39 +02001052 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001053 if (!tt_local_entry)
1054 goto out;
1055
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001056 curr_flags = tt_local_entry->common.flags;
1057
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001058 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001059 /* if this global entry addition is due to a roaming, the node has to
1060 * mark the local entry as "roamed" in order to correctly reroute
1061 * packets later
1062 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001063 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001064 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001065 /* mark the local client as ROAMed */
1066 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1067 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001068
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001069 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1070 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1071 message);
1072 goto out;
1073 }
1074 /* if this client has been added right now, it is possible to
1075 * immediately purge it
1076 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001077 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001078
1079 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1080 batadv_compare_tt,
1081 batadv_choose_tt,
1082 &tt_local_entry->common);
1083 if (!tt_entry_exists)
1084 goto out;
1085
1086 /* extra call to free the local tt entry */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001087 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001088
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001089 /* decrease the reference held for this vlan */
1090 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001091 if (!vlan)
1092 goto out;
1093
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001094 batadv_softif_vlan_free_ref(vlan);
1095 batadv_softif_vlan_free_ref(vlan);
1096
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001097out:
1098 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001099 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001100
1101 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102}
1103
Marek Lindnera19d3d82013-05-27 15:33:25 +08001104/**
1105 * batadv_tt_local_purge_list - purge inactive tt local entries
1106 * @bat_priv: the bat priv with all the soft interface information
1107 * @head: pointer to the list containing the local tt entries
1108 * @timeout: parameter deciding whether a given tt local entry is considered
1109 * inactive or not
1110 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001112 struct hlist_head *head,
1113 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001114{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001115 struct batadv_tt_local_entry *tt_local_entry;
1116 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001117 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001118
Sasha Levinb67bfe02013-02-27 17:06:00 -08001119 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001120 hash_entry) {
1121 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001122 struct batadv_tt_local_entry,
1123 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001124 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1125 continue;
1126
1127 /* entry already marked for deletion */
1128 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1129 continue;
1130
Marek Lindnera19d3d82013-05-27 15:33:25 +08001131 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001132 continue;
1133
1134 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1135 BATADV_TT_CLIENT_DEL, "timed out");
1136 }
1137}
1138
Marek Lindnera19d3d82013-05-27 15:33:25 +08001139/**
1140 * batadv_tt_local_purge - purge inactive tt local entries
1141 * @bat_priv: the bat priv with all the soft interface information
1142 * @timeout: parameter deciding whether a given tt local entry is considered
1143 * inactive or not
1144 */
1145static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1146 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001147{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001148 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001150 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001151 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001152
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001153 for (i = 0; i < hash->size; i++) {
1154 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001155 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001157 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001158 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001159 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001160 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001161}
1162
Sven Eckelmann56303d32012-06-05 22:31:31 +02001163static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001165 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001166 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001167 struct batadv_tt_common_entry *tt_common_entry;
1168 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001169 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001170 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001171 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001172 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173
Sven Eckelmann807736f2012-07-15 22:26:51 +02001174 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001175 return;
1176
Sven Eckelmann807736f2012-07-15 22:26:51 +02001177 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001178
1179 for (i = 0; i < hash->size; i++) {
1180 head = &hash->table[i];
1181 list_lock = &hash->list_locks[i];
1182
1183 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001184 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001185 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001187 tt_local = container_of(tt_common_entry,
1188 struct batadv_tt_local_entry,
1189 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001190
1191 /* decrease the reference held for this vlan */
1192 vlan = batadv_softif_vlan_get(bat_priv,
1193 tt_common_entry->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001194 if (vlan) {
1195 batadv_softif_vlan_free_ref(vlan);
1196 batadv_softif_vlan_free_ref(vlan);
1197 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001198
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200 }
1201 spin_unlock_bh(list_lock);
1202 }
1203
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001204 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205
Sven Eckelmann807736f2012-07-15 22:26:51 +02001206 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207}
1208
Sven Eckelmann56303d32012-06-05 22:31:31 +02001209static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001210{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001211 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001212 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001213
Sven Eckelmann807736f2012-07-15 22:26:51 +02001214 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215
Sven Eckelmann807736f2012-07-15 22:26:51 +02001216 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001217 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001218
Antonio Quartullidec05072012-11-10 11:00:32 +01001219 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1220 &batadv_tt_global_hash_lock_class_key);
1221
Sven Eckelmann5346c352012-05-05 13:27:28 +02001222 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001223}
1224
Sven Eckelmann56303d32012-06-05 22:31:31 +02001225static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001227 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001228
Sven Eckelmann807736f2012-07-15 22:26:51 +02001229 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230
Sven Eckelmann807736f2012-07-15 22:26:51 +02001231 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 list) {
1233 list_del(&entry->list);
1234 kfree(entry);
1235 }
1236
Sven Eckelmann807736f2012-07-15 22:26:51 +02001237 atomic_set(&bat_priv->tt.local_changes, 0);
1238 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001239}
1240
Antonio Quartullid657e622012-07-01 14:09:12 +02001241/* retrieves the orig_tt_list_entry belonging to orig_node from the
1242 * batadv_tt_global_entry list
1243 *
1244 * returns it with an increased refcounter, NULL if not found
1245 */
1246static struct batadv_tt_orig_list_entry *
1247batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1248 const struct batadv_orig_node *orig_node)
1249{
1250 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1251 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001252
1253 rcu_read_lock();
1254 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001255 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001256 if (tmp_orig_entry->orig_node != orig_node)
1257 continue;
1258 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1259 continue;
1260
1261 orig_entry = tmp_orig_entry;
1262 break;
1263 }
1264 rcu_read_unlock();
1265
1266 return orig_entry;
1267}
1268
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001269/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001270 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001271 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001272static bool
1273batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1274 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001275{
Antonio Quartullid657e622012-07-01 14:09:12 +02001276 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277 bool found = false;
1278
Antonio Quartullid657e622012-07-01 14:09:12 +02001279 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1280 if (orig_entry) {
1281 found = true;
1282 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001283 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001284
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001285 return found;
1286}
1287
Sven Eckelmanna5130882012-05-16 20:23:16 +02001288static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001289batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001290 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001291{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001292 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001293
Antonio Quartullid657e622012-07-01 14:09:12 +02001294 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001295 if (orig_entry) {
1296 /* refresh the ttvn: the current value could be a bogus one that
1297 * was added during a "temporary client detection"
1298 */
1299 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001300 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001301 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001302
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001303 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1304 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001305 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001306
1307 INIT_HLIST_NODE(&orig_entry->list);
1308 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001309 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001310 orig_entry->orig_node = orig_node;
1311 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001312 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001313
Antonio Quartullid657e622012-07-01 14:09:12 +02001314 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001315 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001316 &tt_global->orig_list);
1317 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001318 atomic_inc(&tt_global->orig_list_count);
1319
Antonio Quartullid657e622012-07-01 14:09:12 +02001320out:
1321 if (orig_entry)
1322 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001323}
1324
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001325/**
1326 * batadv_tt_global_add - add a new TT global entry or update an existing one
1327 * @bat_priv: the bat priv with all the soft interface information
1328 * @orig_node: the originator announcing the client
1329 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001330 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001331 * @flags: TT flags that have to be set for this non-mesh client
1332 * @ttvn: the tt version number ever announcing this non-mesh client
1333 *
1334 * Add a new TT global entry for the given originator. If the entry already
1335 * exists add a new reference to the given originator (a global entry can have
1336 * references to multiple originators) and adjust the flags attribute to reflect
1337 * the function argument.
1338 * If a TT local entry exists for this non-mesh client remove it.
1339 *
1340 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001341 *
1342 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001343 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001344static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1345 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001346 const unsigned char *tt_addr,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001347 unsigned short vid, u16 flags, u8 ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001348{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001349 struct batadv_tt_global_entry *tt_global_entry;
1350 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001351 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001352 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001353 struct batadv_tt_common_entry *common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001354 u16 local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001356 /* ignore global entries from backbone nodes */
1357 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1358 return true;
1359
Antonio Quartullic018ad32013-06-04 12:11:39 +02001360 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1361 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001362
1363 /* if the node already has a local client for this entry, it has to wait
1364 * for a roaming advertisement instead of manually messing up the global
1365 * table
1366 */
1367 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1368 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1369 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001370
Antonio Quartullia73105b2011-04-27 14:27:44 +02001371 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001372 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001373 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001374 goto out;
1375
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001376 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001377 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001378 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001379
Antonio Quartullid4f44692012-05-25 00:00:54 +02001380 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001381 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001382 /* node must store current time in case of roaming. This is
1383 * needed to purge this entry out on timeout (if nobody claims
1384 * it)
1385 */
1386 if (flags & BATADV_TT_CLIENT_ROAM)
1387 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001388 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001389 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001390
1391 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001392 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001393 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001394
Sven Eckelmann807736f2012-07-15 22:26:51 +02001395 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001396 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001397 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001398 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001399
1400 if (unlikely(hash_added != 0)) {
1401 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001402 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001403 goto out_remove;
1404 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001405 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001406 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001407 /* If there is already a global entry, we can use this one for
1408 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001409 * But if we are trying to add a temporary client then here are
1410 * two options at this point:
1411 * 1) the global client is not a temporary client: the global
1412 * client has to be left as it is, temporary information
1413 * should never override any already known client state
1414 * 2) the global client is a temporary client: purge the
1415 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001416 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001417 if (flags & BATADV_TT_CLIENT_TEMP) {
1418 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1419 goto out;
1420 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1421 orig_node))
1422 goto out_remove;
1423 batadv_tt_global_del_orig_list(tt_global_entry);
1424 goto add_orig_entry;
1425 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001426
1427 /* if the client was temporary added before receiving the first
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001428 * OGM announcing it, we have to clear the TEMP flag. Also,
1429 * remove the previous temporary orig node and re-add it
1430 * if required. If the orig entry changed, the new one which
1431 * is a non-temporary entry is preferred.
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001432 */
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001433 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1434 batadv_tt_global_del_orig_list(tt_global_entry);
1435 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1436 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001437
Antonio Quartullie9c001362012-11-07 15:05:33 +01001438 /* the change can carry possible "attribute" flags like the
1439 * TT_CLIENT_WIFI, therefore they have to be copied in the
1440 * client entry
1441 */
Simon Wunderlichad7e2c42015-08-26 16:33:34 +02001442 common->flags |= flags;
Antonio Quartullie9c001362012-11-07 15:05:33 +01001443
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001444 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1445 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001446 * delete + roaming change for this originator.
1447 *
1448 * We should first delete the old originator before adding the
1449 * new one.
1450 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001451 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001452 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001453 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001454 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001455 }
1456 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001457add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001458 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001459 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001460
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001461 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001462 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1463 common->addr, BATADV_PRINT_VID(common->vid),
1464 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001465 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001466
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001467out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001468 /* Do not remove multicast addresses from the local hash on
1469 * global additions
1470 */
1471 if (is_multicast_ether_addr(tt_addr))
1472 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001473
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001475 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001476 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001477 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001478 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1479
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001480 if (!(flags & BATADV_TT_CLIENT_ROAM))
1481 /* this is a normal global add. Therefore the client is not in a
1482 * roaming state anymore.
1483 */
1484 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1485
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001486out:
1487 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001488 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001489 if (tt_local_entry)
1490 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001491 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001492}
1493
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001494/**
1495 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001496 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001497 * @tt_global_entry: global translation table entry to be analyzed
1498 *
1499 * This functon assumes the caller holds rcu_read_lock().
1500 * Returns best originator list entry or NULL on errors.
1501 */
1502static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001503batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1504 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001505{
Antonio Quartulli46274562013-09-03 11:10:24 +02001506 struct batadv_neigh_node *router, *best_router = NULL;
1507 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001508 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001509 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001510
1511 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001512 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001513 router = batadv_orig_router_get(orig_entry->orig_node,
1514 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001515 if (!router)
1516 continue;
1517
Antonio Quartulli46274562013-09-03 11:10:24 +02001518 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001519 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1520 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001521 batadv_neigh_node_free_ref(router);
1522 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001523 }
1524
Antonio Quartulli46274562013-09-03 11:10:24 +02001525 /* release the refcount for the "old" best */
1526 if (best_router)
1527 batadv_neigh_node_free_ref(best_router);
1528
1529 best_entry = orig_entry;
1530 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001531 }
1532
Antonio Quartulli46274562013-09-03 11:10:24 +02001533 if (best_router)
1534 batadv_neigh_node_free_ref(best_router);
1535
Sven Eckelmann981d8902012-10-07 13:34:15 +02001536 return best_entry;
1537}
1538
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001539/**
1540 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1541 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001542 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001543 * @tt_global_entry: global translation table entry to be printed
1544 * @seq: debugfs table seq_file struct
1545 *
1546 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001547 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001548static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001549batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1550 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001551 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001552{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001553 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001554 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001555 struct batadv_orig_node_vlan *vlan;
1556 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001557 u8 last_ttvn;
1558 u16 flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001559
1560 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001561 flags = tt_common_entry->flags;
1562
Antonio Quartulli46274562013-09-03 11:10:24 +02001563 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001564 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001565 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1566 tt_common_entry->vid);
1567 if (!vlan) {
1568 seq_printf(seq,
1569 " * Cannot retrieve VLAN %d for originator %pM\n",
1570 BATADV_PRINT_VID(tt_common_entry->vid),
1571 best_entry->orig_node->orig);
1572 goto print_list;
1573 }
1574
Sven Eckelmann981d8902012-10-07 13:34:15 +02001575 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001576 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001577 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001578 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001579 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001580 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001581 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001582 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1583 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1584 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1585 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001586
1587 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001588 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001589
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001590print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001591 head = &tt_global_entry->orig_list;
1592
Sasha Levinb67bfe02013-02-27 17:06:00 -08001593 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001594 if (best_entry == orig_entry)
1595 continue;
1596
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001597 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1598 tt_common_entry->vid);
1599 if (!vlan) {
1600 seq_printf(seq,
1601 " + Cannot retrieve VLAN %d for originator %pM\n",
1602 BATADV_PRINT_VID(tt_common_entry->vid),
1603 orig_entry->orig_node->orig);
1604 continue;
1605 }
1606
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001607 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001608 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001609 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001610 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001611 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001612 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001613 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001614 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1615 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1616 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1617 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001618
1619 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001620 }
1621}
1622
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001623int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001624{
1625 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001626 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001627 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001628 struct batadv_tt_common_entry *tt_common_entry;
1629 struct batadv_tt_global_entry *tt_global;
1630 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001631 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001632 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001633
Marek Lindner30da63a2012-08-03 17:15:46 +02001634 primary_if = batadv_seq_print_text_primary_if_get(seq);
1635 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001636 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001637
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001638 seq_printf(seq,
1639 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001640 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001641 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1642 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1643 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001644
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001645 for (i = 0; i < hash->size; i++) {
1646 head = &hash->table[i];
1647
Marek Lindner7aadf882011-02-18 12:28:09 +00001648 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001649 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001650 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001651 tt_global = container_of(tt_common_entry,
1652 struct batadv_tt_global_entry,
1653 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001654 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001655 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001656 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001657 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001658out:
1659 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001660 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001661 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001662}
1663
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001664/**
Marek Lindner433ff982015-06-18 16:11:07 +08001665 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001666 * @tt_global_entry: the global entry to remove the orig_entry from
1667 * @orig_entry: the orig entry to remove and free
1668 *
1669 * Remove an orig_entry from its list in the given tt_global_entry and
1670 * free this orig_entry afterwards.
Marek Lindner433ff982015-06-18 16:11:07 +08001671 *
1672 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1673 * part of a list.
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001674 */
1675static void
Marek Lindner433ff982015-06-18 16:11:07 +08001676_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1677 struct batadv_tt_orig_list_entry *orig_entry)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001678{
Sven Eckelmann2c72d652015-06-21 14:45:14 +02001679 lockdep_assert_held(&tt_global_entry->list_lock);
1680
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001681 batadv_tt_global_size_dec(orig_entry->orig_node,
1682 tt_global_entry->common.vid);
1683 atomic_dec(&tt_global_entry->orig_list_count);
Marek Lindner433ff982015-06-18 16:11:07 +08001684 /* requires holding tt_global_entry->list_lock and orig_entry->list
1685 * being part of a list
1686 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001687 hlist_del_rcu(&orig_entry->list);
1688 batadv_tt_orig_list_entry_free_ref(orig_entry);
1689}
1690
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001691/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001692static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001693batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001694{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001695 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001696 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001697 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001699 spin_lock_bh(&tt_global_entry->list_lock);
1700 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001701 hlist_for_each_entry_safe(orig_entry, safe, head, list)
Marek Lindner433ff982015-06-18 16:11:07 +08001702 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001703 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001704}
1705
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001706/**
1707 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1708 * @bat_priv: the bat priv with all the soft interface information
1709 * @tt_global_entry: the global entry to remove the orig_node from
1710 * @orig_node: the originator announcing the client
1711 * @message: message to append to the log on deletion
1712 *
1713 * Remove the given orig_node and its according orig_entry from the given
1714 * global tt entry.
1715 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001716static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001717batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1718 struct batadv_tt_global_entry *tt_global_entry,
1719 struct batadv_orig_node *orig_node,
1720 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001721{
1722 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001723 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001724 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001725 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001726
1727 spin_lock_bh(&tt_global_entry->list_lock);
1728 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001729 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001730 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001731 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001732 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001733 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001734 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001735 tt_global_entry->common.addr,
1736 BATADV_PRINT_VID(vid), message);
Marek Lindner433ff982015-06-18 16:11:07 +08001737 _batadv_tt_global_del_orig_entry(tt_global_entry,
1738 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001739 }
1740 }
1741 spin_unlock_bh(&tt_global_entry->list_lock);
1742}
1743
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001744/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001745 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1746 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001747 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001748static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001749batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1750 struct batadv_tt_global_entry *tt_global_entry,
1751 struct batadv_orig_node *orig_node,
1752 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001753{
1754 bool last_entry = true;
1755 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001756 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001757
1758 /* no local entry exists, case 1:
1759 * Check if this is the last one or if other entries exist.
1760 */
1761
1762 rcu_read_lock();
1763 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001764 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001765 if (orig_entry->orig_node != orig_node) {
1766 last_entry = false;
1767 break;
1768 }
1769 }
1770 rcu_read_unlock();
1771
1772 if (last_entry) {
1773 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001774 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001775 tt_global_entry->roam_at = jiffies;
1776 } else
1777 /* there is another entry, we can simply delete this
1778 * one and can still use the other one.
1779 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001780 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1781 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001782}
1783
Antonio Quartullic018ad32013-06-04 12:11:39 +02001784/**
1785 * batadv_tt_global_del - remove a client from the global table
1786 * @bat_priv: the bat priv with all the soft interface information
1787 * @orig_node: an originator serving this client
1788 * @addr: the mac address of the client
1789 * @vid: VLAN identifier
1790 * @message: a message explaining the reason for deleting the client to print
1791 * for debugging purpose
1792 * @roaming: true if the deletion has been triggered by a roaming event
1793 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001794static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1795 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001796 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001797 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001798{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001799 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001800 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001801
Antonio Quartullic018ad32013-06-04 12:11:39 +02001802 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001803 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001804 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001806 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001807 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1808 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001809
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001810 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001811 batadv_tt_global_free(bat_priv, tt_global_entry,
1812 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001813
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001814 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001816
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001817 /* if we are deleting a global entry due to a roam
1818 * event, there are two possibilities:
1819 * 1) the client roamed from node A to node B => if there
1820 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001821 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001822 * wait for node B to claim it. In case of timeout
1823 * the entry is purged.
1824 *
1825 * If there are other originators left, we directly delete
1826 * the originator.
1827 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001828 * the global entry, since it is useless now.
1829 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001830 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001831 tt_global_entry->common.addr,
1832 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001833 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001834 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001835 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001836 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001837 } else
1838 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001839 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1840 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001841
Antonio Quartullicc47f662011-04-27 14:27:57 +02001842out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001843 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001844 batadv_tt_global_entry_free_ref(tt_global_entry);
1845 if (local_entry)
1846 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001847}
1848
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001849/**
1850 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1851 * given originator matching the provided vid
1852 * @bat_priv: the bat priv with all the soft interface information
1853 * @orig_node: the originator owning the entries to remove
1854 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1855 * removed
1856 * @message: debug message to print as "reason"
1857 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001858void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1859 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001860 s32 match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001861 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001862{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001863 struct batadv_tt_global_entry *tt_global;
1864 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001865 u32 i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001866 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001867 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001869 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001870 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001871
Simon Wunderlich6e801492011-10-19 10:28:26 +02001872 if (!hash)
1873 return;
1874
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875 for (i = 0; i < hash->size; i++) {
1876 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001877 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001878
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001879 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001880 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001881 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001882 /* remove only matching entries */
1883 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1884 continue;
1885
Sven Eckelmann56303d32012-06-05 22:31:31 +02001886 tt_global = container_of(tt_common_entry,
1887 struct batadv_tt_global_entry,
1888 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001889
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001890 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1891 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001892
Sven Eckelmann56303d32012-06-05 22:31:31 +02001893 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001894 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001895 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001896 "Deleting global tt entry %pM (vid: %d): %s\n",
1897 tt_global->common.addr,
1898 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001899 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001900 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001901 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001903 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001904 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02001905 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001906}
1907
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001908static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1909 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001910{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001911 bool purge = false;
1912 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1913 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001914
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001915 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1916 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1917 purge = true;
1918 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001919 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001920
1921 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1922 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1923 purge = true;
1924 *msg = "Temporary client timeout\n";
1925 }
1926
1927 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001928}
1929
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001930static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001931{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001932 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001933 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001934 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001935 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001936 u32 i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001937 char *msg = NULL;
1938 struct batadv_tt_common_entry *tt_common;
1939 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001940
Antonio Quartullicc47f662011-04-27 14:27:57 +02001941 for (i = 0; i < hash->size; i++) {
1942 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001943 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001944
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001945 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001946 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001947 hash_entry) {
1948 tt_global = container_of(tt_common,
1949 struct batadv_tt_global_entry,
1950 common);
1951
1952 if (!batadv_tt_global_to_purge(tt_global, &msg))
1953 continue;
1954
1955 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001956 "Deleting global tt entry %pM (vid: %d): %s\n",
1957 tt_global->common.addr,
1958 BATADV_PRINT_VID(tt_global->common.vid),
1959 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001960
Sasha Levinb67bfe02013-02-27 17:06:00 -08001961 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001962
1963 batadv_tt_global_entry_free_ref(tt_global);
1964 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001965 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001966 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001967}
1968
Sven Eckelmann56303d32012-06-05 22:31:31 +02001969static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001970{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001971 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001972 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001973 struct batadv_tt_common_entry *tt_common_entry;
1974 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001975 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001976 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001977 u32 i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001978
Sven Eckelmann807736f2012-07-15 22:26:51 +02001979 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001980 return;
1981
Sven Eckelmann807736f2012-07-15 22:26:51 +02001982 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001983
1984 for (i = 0; i < hash->size; i++) {
1985 head = &hash->table[i];
1986 list_lock = &hash->list_locks[i];
1987
1988 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001989 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001990 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001991 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001992 tt_global = container_of(tt_common_entry,
1993 struct batadv_tt_global_entry,
1994 common);
1995 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001996 }
1997 spin_unlock_bh(list_lock);
1998 }
1999
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02002000 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002001
Sven Eckelmann807736f2012-07-15 22:26:51 +02002002 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002003}
2004
Sven Eckelmann56303d32012-06-05 22:31:31 +02002005static bool
2006_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2007 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002008{
2009 bool ret = false;
2010
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002011 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2012 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002013 ret = true;
2014
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002015 /* check if the two clients are marked as isolated */
2016 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2017 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2018 ret = true;
2019
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002020 return ret;
2021}
2022
Antonio Quartullic018ad32013-06-04 12:11:39 +02002023/**
2024 * batadv_transtable_search - get the mesh destination for a given client
2025 * @bat_priv: the bat priv with all the soft interface information
2026 * @src: mac address of the source client
2027 * @addr: mac address of the destination client
2028 * @vid: VLAN identifier
2029 *
2030 * Returns a pointer to the originator that was selected as destination in the
2031 * mesh for contacting the client 'addr', NULL otherwise.
2032 * In case of multiple originators serving the same client, the function returns
2033 * the best one (best in terms of metric towards the destination node).
2034 *
2035 * If the two clients are AP isolated the function returns NULL.
2036 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002037struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002038 const u8 *src,
2039 const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002040 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002041{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002042 struct batadv_tt_local_entry *tt_local_entry = NULL;
2043 struct batadv_tt_global_entry *tt_global_entry = NULL;
2044 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002045 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002046
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002047 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002048 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002049 if (!tt_local_entry ||
2050 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002051 goto out;
2052 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002053
Antonio Quartullic018ad32013-06-04 12:11:39 +02002054 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002055 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002056 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002057
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002058 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002059 * isolation
2060 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002061 if (tt_local_entry &&
2062 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002063 goto out;
2064
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002065 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002066 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002067 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002068 if (best_entry)
2069 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002070 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2071 orig_node = NULL;
2072 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002073
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002074out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002075 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002076 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002077 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002078 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002079
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002080 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002081}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002082
Antonio Quartulliced72932013-04-24 16:37:51 +02002083/**
2084 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2085 * to the given orig_node
2086 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002087 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002088 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002089 *
2090 * This function computes the checksum for the global table corresponding to a
2091 * specific originator. In particular, the checksum is computed as follows: For
2092 * each client connected to the originator the CRC32C of the MAC address and the
2093 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2094 * together.
2095 *
2096 * The idea behind is that CRC32C should be used as much as possible in order to
2097 * produce a unique hash of the table, but since the order which is used to feed
2098 * the CRC32C function affects the result and since every node in the network
2099 * probably sorts the clients differently, the hash function cannot be directly
2100 * computed over the entire table. Hence the CRC32C is used only on
2101 * the single client entry, while all the results are then xor'ed together
2102 * because the XOR operation can combine them all while trying to reduce the
2103 * noise as much as possible.
2104 *
2105 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002106 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002107static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2108 struct batadv_orig_node *orig_node,
2109 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002110{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002111 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002112 struct batadv_tt_common_entry *tt_common;
2113 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002114 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002115 u32 i, crc_tmp, crc = 0;
2116 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002117 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002118
2119 for (i = 0; i < hash->size; i++) {
2120 head = &hash->table[i];
2121
2122 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002123 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002124 tt_global = container_of(tt_common,
2125 struct batadv_tt_global_entry,
2126 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002127 /* compute the CRC only for entries belonging to the
2128 * VLAN identified by the vid passed as parameter
2129 */
2130 if (tt_common->vid != vid)
2131 continue;
2132
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002133 /* Roaming clients are in the global table for
2134 * consistency only. They don't have to be
2135 * taken into account while computing the
2136 * global crc
2137 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002138 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002139 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002140 /* Temporary clients have not been announced yet, so
2141 * they have to be skipped while computing the global
2142 * crc
2143 */
2144 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2145 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002146
2147 /* find out if this global entry is announced by this
2148 * originator
2149 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002150 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002151 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002152 continue;
2153
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002154 /* use network order to read the VID: this ensures that
2155 * every node reads the bytes in the same order.
2156 */
2157 tmp_vid = htons(tt_common->vid);
2158 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002159
2160 /* compute the CRC on flags that have to be kept in sync
2161 * among nodes
2162 */
2163 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2164 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2165
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002166 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167 }
2168 rcu_read_unlock();
2169 }
2170
Antonio Quartulliced72932013-04-24 16:37:51 +02002171 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002172}
2173
Antonio Quartulliced72932013-04-24 16:37:51 +02002174/**
2175 * batadv_tt_local_crc - calculates the checksum of the local table
2176 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002177 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002178 *
2179 * For details about the computation, please refer to the documentation for
2180 * batadv_tt_global_crc().
2181 *
2182 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002183 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002184static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2185 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002186{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002187 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002188 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002189 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002190 u32 i, crc_tmp, crc = 0;
2191 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002192 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002193
2194 for (i = 0; i < hash->size; i++) {
2195 head = &hash->table[i];
2196
2197 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002198 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002199 /* compute the CRC only for entries belonging to the
2200 * VLAN identified by vid
2201 */
2202 if (tt_common->vid != vid)
2203 continue;
2204
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002205 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002206 * account while computing the CRC
2207 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002208 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002209 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002210
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002211 /* use network order to read the VID: this ensures that
2212 * every node reads the bytes in the same order.
2213 */
2214 tmp_vid = htons(tt_common->vid);
2215 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002216
2217 /* compute the CRC on flags that have to be kept in sync
2218 * among nodes
2219 */
2220 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2221 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2222
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002223 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002224 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002225 rcu_read_unlock();
2226 }
2227
Antonio Quartulliced72932013-04-24 16:37:51 +02002228 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002229}
2230
Sven Eckelmann56303d32012-06-05 22:31:31 +02002231static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232{
Marek Lindner7c26a532015-06-28 22:16:06 +08002233 struct batadv_tt_req_node *node;
2234 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002235
Sven Eckelmann807736f2012-07-15 22:26:51 +02002236 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002237
Marek Lindner7c26a532015-06-28 22:16:06 +08002238 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2239 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002240 kfree(node);
2241 }
2242
Sven Eckelmann807736f2012-07-15 22:26:51 +02002243 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002244}
2245
Sven Eckelmann56303d32012-06-05 22:31:31 +02002246static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2247 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002248 const void *tt_buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002249 u16 tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002250{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002251 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002252 * last OGM (the OGM could carry no changes)
2253 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002254 spin_lock_bh(&orig_node->tt_buff_lock);
2255 if (tt_buff_len > 0) {
2256 kfree(orig_node->tt_buff);
2257 orig_node->tt_buff_len = 0;
2258 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2259 if (orig_node->tt_buff) {
2260 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2261 orig_node->tt_buff_len = tt_buff_len;
2262 }
2263 }
2264 spin_unlock_bh(&orig_node->tt_buff_lock);
2265}
2266
Sven Eckelmann56303d32012-06-05 22:31:31 +02002267static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002268{
Marek Lindner7c26a532015-06-28 22:16:06 +08002269 struct batadv_tt_req_node *node;
2270 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002271
Sven Eckelmann807736f2012-07-15 22:26:51 +02002272 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002273 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002274 if (batadv_has_timed_out(node->issued_at,
2275 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner7c26a532015-06-28 22:16:06 +08002276 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002277 kfree(node);
2278 }
2279 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002280 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002281}
2282
Marek Lindner383b8632015-06-18 16:24:24 +08002283/**
2284 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2285 * @bat_priv: the bat priv with all the soft interface information
2286 * @orig_node: orig node this request is being issued for
2287 *
2288 * Returns the pointer to the new tt_req_node struct if no request
2289 * has already been issued for this orig_node, NULL otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002290 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002291static struct batadv_tt_req_node *
Marek Lindner383b8632015-06-18 16:24:24 +08002292batadv_tt_req_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002293 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002294{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002295 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002296
Sven Eckelmann807736f2012-07-15 22:26:51 +02002297 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002298 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002299 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2300 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002301 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002302 goto unlock;
2303 }
2304
2305 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2306 if (!tt_req_node)
2307 goto unlock;
2308
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002309 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310 tt_req_node->issued_at = jiffies;
2311
Marek Lindner7c26a532015-06-28 22:16:06 +08002312 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002313unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002314 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002315 return tt_req_node;
2316}
2317
Marek Lindner335fbe02013-04-23 21:40:02 +08002318/**
2319 * batadv_tt_local_valid - verify that given tt entry is a valid one
2320 * @entry_ptr: to be checked local tt entry
2321 * @data_ptr: not used but definition required to satisfy the callback prototype
2322 *
2323 * Returns 1 if the entry is a valid, 0 otherwise.
2324 */
2325static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002326{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002327 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002328
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002329 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002330 return 0;
2331 return 1;
2332}
2333
Sven Eckelmanna5130882012-05-16 20:23:16 +02002334static int batadv_tt_global_valid(const void *entry_ptr,
2335 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002336{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002337 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2338 const struct batadv_tt_global_entry *tt_global_entry;
2339 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002341 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2342 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002343 return 0;
2344
Sven Eckelmann56303d32012-06-05 22:31:31 +02002345 tt_global_entry = container_of(tt_common_entry,
2346 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002347 common);
2348
Sven Eckelmanna5130882012-05-16 20:23:16 +02002349 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002350}
2351
Marek Lindner335fbe02013-04-23 21:40:02 +08002352/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002353 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2354 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002355 * @bat_priv: the bat priv with all the soft interface information
2356 * @hash: hash table containing the tt entries
2357 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002358 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002359 * @valid_cb: function to filter tt change entries
2360 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002361 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002362static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2363 struct batadv_hashtable *hash,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002364 void *tvlv_buff, u16 tt_len,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002365 int (*valid_cb)(const void *, const void *),
2366 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002367{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002368 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002369 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002370 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002371 u16 tt_tot, tt_num_entries = 0;
2372 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002373
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002374 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002375 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002376
2377 rcu_read_lock();
2378 for (i = 0; i < hash->size; i++) {
2379 head = &hash->table[i];
2380
Sasha Levinb67bfe02013-02-27 17:06:00 -08002381 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002382 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002383 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384 break;
2385
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002386 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002387 continue;
2388
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002389 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002390 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002391 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002392 memset(tt_change->reserved, 0,
2393 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002394
Marek Lindner335fbe02013-04-23 21:40:02 +08002395 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002396 tt_change++;
2397 }
2398 }
2399 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002400}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002401
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002402/**
2403 * batadv_tt_global_check_crc - check if all the CRCs are correct
2404 * @orig_node: originator for which the CRCs have to be checked
2405 * @tt_vlan: pointer to the first tvlv VLAN entry
2406 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002407 *
2408 * Return true if all the received CRCs match the locally stored ones, false
2409 * otherwise
2410 */
2411static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2412 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002413 u16 num_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002414{
2415 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2416 struct batadv_orig_node_vlan *vlan;
Simon Wunderlichc169c592015-09-02 20:09:56 +02002417 int i, orig_num_vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002418 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002419
2420 /* check if each received CRC matches the locally stored one */
2421 for (i = 0; i < num_vlan; i++) {
2422 tt_vlan_tmp = tt_vlan + i;
2423
2424 /* if orig_node is a backbone node for this VLAN, don't check
2425 * the CRC as we ignore all the global entries over it
2426 */
2427 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002428 orig_node->orig,
2429 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002430 continue;
2431
2432 vlan = batadv_orig_node_vlan_get(orig_node,
2433 ntohs(tt_vlan_tmp->vid));
2434 if (!vlan)
2435 return false;
2436
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002437 crc = vlan->tt.crc;
2438 batadv_orig_node_vlan_free_ref(vlan);
2439
2440 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002441 return false;
2442 }
2443
Simon Wunderlichc169c592015-09-02 20:09:56 +02002444 /* check if any excess VLANs exist locally for the originator
2445 * which are not mentioned in the TVLV from the originator.
2446 */
2447 rcu_read_lock();
2448 orig_num_vlan = 0;
2449 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2450 orig_num_vlan++;
2451 rcu_read_unlock();
2452
2453 if (orig_num_vlan > num_vlan)
2454 return false;
2455
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002456 return true;
2457}
2458
2459/**
2460 * batadv_tt_local_update_crc - update all the local CRCs
2461 * @bat_priv: the bat priv with all the soft interface information
2462 */
2463static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2464{
2465 struct batadv_softif_vlan *vlan;
2466
2467 /* recompute the global CRC for each VLAN */
2468 rcu_read_lock();
2469 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2470 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2471 }
2472 rcu_read_unlock();
2473}
2474
2475/**
2476 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2477 * @bat_priv: the bat priv with all the soft interface information
2478 * @orig_node: the orig_node for which the CRCs have to be updated
2479 */
2480static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2481 struct batadv_orig_node *orig_node)
2482{
2483 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002484 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002485
2486 /* recompute the global CRC for each VLAN */
2487 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08002488 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002489 /* if orig_node is a backbone node for this VLAN, don't compute
2490 * the CRC as we ignore all the global entries over it
2491 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002492 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2493 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002494 continue;
2495
2496 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2497 vlan->tt.crc = crc;
2498 }
2499 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002500}
2501
Antonio Quartulliced72932013-04-24 16:37:51 +02002502/**
2503 * batadv_send_tt_request - send a TT Request message to a given node
2504 * @bat_priv: the bat priv with all the soft interface information
2505 * @dst_orig_node: the destination of the message
2506 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002507 * @tt_vlan: pointer to the first tvlv VLAN object to request
2508 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002509 * @full_table: ask for the entire translation table if true, while only for the
2510 * last TT diff otherwise
2511 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002512static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2513 struct batadv_orig_node *dst_orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002514 u8 ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002515 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002516 u16 num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002517{
Marek Lindner335fbe02013-04-23 21:40:02 +08002518 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002519 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002520 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2521 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002522 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002523 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002524
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002525 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002526 if (!primary_if)
2527 goto out;
2528
2529 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002530 * reply from the same orig_node yet
2531 */
Marek Lindner383b8632015-06-18 16:24:24 +08002532 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533 if (!tt_req_node)
2534 goto out;
2535
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002536 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2537 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002538 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002539 goto out;
2540
Marek Lindner335fbe02013-04-23 21:40:02 +08002541 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2542 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002543 tvlv_tt_data->num_vlan = htons(num_vlan);
2544
2545 /* send all the CRCs within the request. This is needed by intermediate
2546 * nodes to ensure they have the correct table before replying
2547 */
2548 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2549 for (i = 0; i < num_vlan; i++) {
2550 tt_vlan_req->vid = tt_vlan->vid;
2551 tt_vlan_req->crc = tt_vlan->crc;
2552
2553 tt_vlan_req++;
2554 tt_vlan++;
2555 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002556
2557 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002558 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002559
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002560 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002561 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002562
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002563 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002564 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2565 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002566 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002567 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002568
2569out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002570 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002571 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002572 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002573 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002574 /* hlist_del_init() verifies tt_req_node still is in the list */
2575 hlist_del_init(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002576 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002577 kfree(tt_req_node);
2578 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002579 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002580 return ret;
2581}
2582
Marek Lindner335fbe02013-04-23 21:40:02 +08002583/**
2584 * batadv_send_other_tt_response - send reply to tt request concerning another
2585 * node's translation table
2586 * @bat_priv: the bat priv with all the soft interface information
2587 * @tt_data: tt data containing the tt request information
2588 * @req_src: mac address of tt request sender
2589 * @req_dst: mac address of tt request recipient
2590 *
2591 * Returns true if tt request reply was sent, false otherwise.
2592 */
2593static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2594 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002595 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002596{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002597 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002598 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002599 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002600 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002601 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002602 bool ret = false, full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002603 u8 orig_ttvn, req_ttvn;
2604 u16 tvlv_len;
2605 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002606
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002607 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002608 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002609 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002610 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002611
2612 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002613 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002614 if (!req_dst_orig_node)
2615 goto out;
2616
Marek Lindner335fbe02013-04-23 21:40:02 +08002617 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002618 if (!res_dst_orig_node)
2619 goto out;
2620
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002621 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002622 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002623
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002624 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002625 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002626 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002627 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2628 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002629 goto out;
2630
Antonio Quartulli015758d2011-07-09 17:52:13 +02002631 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002632 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002633 !req_dst_orig_node->tt_buff)
2634 full_table = true;
2635 else
2636 full_table = false;
2637
Marek Lindner335fbe02013-04-23 21:40:02 +08002638 /* TT fragmentation hasn't been implemented yet, so send as many
2639 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002640 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002641 if (!full_table) {
2642 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2643 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002644
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002645 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2646 &tvlv_tt_data,
2647 &tt_change,
2648 &tt_len);
2649 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002650 goto unlock;
2651
Antonio Quartullia73105b2011-04-27 14:27:44 +02002652 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002653 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002654 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002655 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2656 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002657 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2658 * in the initial part
2659 */
2660 tt_len = -1;
2661 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2662 &tvlv_tt_data,
2663 &tt_change,
2664 &tt_len);
2665 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002666 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002667
2668 /* fill the rest of the tvlv with the real TT entries */
2669 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2670 tt_change, tt_len,
2671 batadv_tt_global_valid,
2672 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673 }
2674
Marek Lindnera19d3d82013-05-27 15:33:25 +08002675 /* Don't send the response, if larger than fragmented packet. */
2676 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2677 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2678 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2679 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2680 res_dst_orig_node->orig);
2681 goto out;
2682 }
2683
Marek Lindner335fbe02013-04-23 21:40:02 +08002684 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2685 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002686
2687 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002688 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002689
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002690 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002691 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2692 res_dst_orig_node->orig, req_dst_orig_node->orig,
2693 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002694
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002695 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002696
Marek Lindner335fbe02013-04-23 21:40:02 +08002697 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002698 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2699 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002700
Marek Lindner335fbe02013-04-23 21:40:02 +08002701 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002702 goto out;
2703
2704unlock:
2705 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2706
2707out:
2708 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002709 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002711 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002712 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002713 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002714}
Sven Eckelmann96412692012-06-05 22:31:30 +02002715
Marek Lindner335fbe02013-04-23 21:40:02 +08002716/**
2717 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2718 * translation table
2719 * @bat_priv: the bat priv with all the soft interface information
2720 * @tt_data: tt data containing the tt request information
2721 * @req_src: mac address of tt request sender
2722 *
2723 * Returns true if tt request reply was sent, false otherwise.
2724 */
2725static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2726 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002727 u8 *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728{
Marek Lindner335fbe02013-04-23 21:40:02 +08002729 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002730 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002731 struct batadv_tvlv_tt_change *tt_change;
2732 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002733 u8 my_ttvn, req_ttvn;
2734 u16 tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002735 bool full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002736 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002737
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002738 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002739 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002740 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002741 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002742
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002743 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002744
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002745 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002746 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002747
Marek Lindner335fbe02013-04-23 21:40:02 +08002748 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002749 if (!orig_node)
2750 goto out;
2751
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002752 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002753 if (!primary_if)
2754 goto out;
2755
2756 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002757 * is too big send the whole local translation table
2758 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002759 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002760 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002761 full_table = true;
2762 else
2763 full_table = false;
2764
Marek Lindner335fbe02013-04-23 21:40:02 +08002765 /* TT fragmentation hasn't been implemented yet, so send as many
2766 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002767 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002768 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002769 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002770
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002771 tt_len = bat_priv->tt.last_changeset_len;
2772 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2773 &tvlv_tt_data,
2774 &tt_change,
2775 &tt_len);
2776 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002777 goto unlock;
2778
Marek Lindner335fbe02013-04-23 21:40:02 +08002779 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002780 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002781 bat_priv->tt.last_changeset_len);
2782 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002783 } else {
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002784 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002785
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002786 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2787 * in the initial part
2788 */
2789 tt_len = -1;
2790 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2791 &tvlv_tt_data,
2792 &tt_change,
2793 &tt_len);
2794 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002795 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002796
2797 /* fill the rest of the tvlv with the real TT entries */
2798 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2799 tt_change, tt_len,
2800 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002801 }
2802
Marek Lindner335fbe02013-04-23 21:40:02 +08002803 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2804 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002805
2806 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002807 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002808
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002809 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002810 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2811 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002812
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002813 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002814
Marek Lindner335fbe02013-04-23 21:40:02 +08002815 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002816 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2817 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002818
Antonio Quartullia73105b2011-04-27 14:27:44 +02002819 goto out;
2820
2821unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002822 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002823out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002824 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002825 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002826 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002828 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002829 kfree(tvlv_tt_data);
2830 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002831 return true;
2832}
2833
Marek Lindner335fbe02013-04-23 21:40:02 +08002834/**
2835 * batadv_send_tt_response - send reply to tt request
2836 * @bat_priv: the bat priv with all the soft interface information
2837 * @tt_data: tt data containing the tt request information
2838 * @req_src: mac address of tt request sender
2839 * @req_dst: mac address of tt request recipient
2840 *
2841 * Returns true if tt request reply was sent, false otherwise.
2842 */
2843static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2844 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002845 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002846{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002847 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002848 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002849 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2850 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002851}
2852
Sven Eckelmann56303d32012-06-05 22:31:31 +02002853static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2854 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002855 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002856 u16 tt_num_changes, u8 ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002857{
2858 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002859 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002860
2861 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002862 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2863 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002864 batadv_tt_global_del(bat_priv, orig_node,
2865 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002866 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002867 "tt removed by changes",
2868 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002869 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002870 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002871 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002872 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002873 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002874 /* In case of problem while storing a
2875 * global_entry, we stop the updating
2876 * procedure without committing the
2877 * ttvn change. This will avoid to send
2878 * corrupted data on tt_request
2879 */
2880 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002881 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002882 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002883 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002884}
2885
Sven Eckelmann56303d32012-06-05 22:31:31 +02002886static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002887 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002888 u8 ttvn, u8 *resp_src,
2889 u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002890{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002891 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002892
Marek Lindner335fbe02013-04-23 21:40:02 +08002893 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002894 if (!orig_node)
2895 goto out;
2896
2897 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002898 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2899 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002900
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002901 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2902 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002903
2904 spin_lock_bh(&orig_node->tt_buff_lock);
2905 kfree(orig_node->tt_buff);
2906 orig_node->tt_buff_len = 0;
2907 orig_node->tt_buff = NULL;
2908 spin_unlock_bh(&orig_node->tt_buff_lock);
2909
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002910 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002911
2912out:
2913 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002914 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002915}
2916
Sven Eckelmann56303d32012-06-05 22:31:31 +02002917static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2918 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002919 u16 tt_num_changes, u8 ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002920 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002921{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002922 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2923 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002924
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002925 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2926 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002927 atomic_set(&orig_node->last_ttvn, ttvn);
2928}
2929
Antonio Quartullic018ad32013-06-04 12:11:39 +02002930/**
2931 * batadv_is_my_client - check if a client is served by the local node
2932 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002933 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002934 * @vid: VLAN identifier
2935 *
2936 * Returns true if the client is served by this node, false otherwise.
2937 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002938bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002939 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002940{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002941 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002942 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002943
Antonio Quartullic018ad32013-06-04 12:11:39 +02002944 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002945 if (!tt_local_entry)
2946 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002947 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002948 * consistency purpose)
2949 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002950 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2951 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002952 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002953 ret = true;
2954out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002955 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002956 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002957 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002958}
2959
Marek Lindner335fbe02013-04-23 21:40:02 +08002960/**
2961 * batadv_handle_tt_response - process incoming tt reply
2962 * @bat_priv: the bat priv with all the soft interface information
2963 * @tt_data: tt data containing the tt request information
2964 * @resp_src: mac address of tt reply sender
2965 * @num_entries: number of tt change entries appended to the tt data
2966 */
2967static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2968 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002969 u8 *resp_src, u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002970{
Marek Lindner7c26a532015-06-28 22:16:06 +08002971 struct batadv_tt_req_node *node;
2972 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002973 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002974 struct batadv_tvlv_tt_change *tt_change;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002975 u8 *tvlv_ptr = (u8 *)tt_data;
2976 u16 change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002977
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002978 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002979 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002980 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002981 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002982
Marek Lindner335fbe02013-04-23 21:40:02 +08002983 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002984 if (!orig_node)
2985 goto out;
2986
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002987 spin_lock_bh(&orig_node->tt_lock);
2988
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002989 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2990 change_offset *= ntohs(tt_data->num_vlan);
2991 change_offset += sizeof(*tt_data);
2992 tvlv_ptr += change_offset;
2993
2994 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002995 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002996 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2997 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002998 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002999 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3000 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02003001 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003002
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003003 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003004 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003005
3006 spin_unlock_bh(&orig_node->tt_lock);
3007
Antonio Quartullia73105b2011-04-27 14:27:44 +02003008 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003009 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08003010 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08003011 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003012 continue;
Marek Lindner7c26a532015-06-28 22:16:06 +08003013 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003014 kfree(node);
3015 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003016
Sven Eckelmann807736f2012-07-15 22:26:51 +02003017 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003018out:
3019 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02003020 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003021}
3022
Sven Eckelmann56303d32012-06-05 22:31:31 +02003023static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003024{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003025 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003026
Sven Eckelmann807736f2012-07-15 22:26:51 +02003027 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003028
Sven Eckelmann807736f2012-07-15 22:26:51 +02003029 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02003030 list_del(&node->list);
3031 kfree(node);
3032 }
3033
Sven Eckelmann807736f2012-07-15 22:26:51 +02003034 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003035}
3036
Sven Eckelmann56303d32012-06-05 22:31:31 +02003037static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003038{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003039 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003040
Sven Eckelmann807736f2012-07-15 22:26:51 +02003041 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3042 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003043 if (!batadv_has_timed_out(node->first_time,
3044 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003045 continue;
3046
3047 list_del(&node->list);
3048 kfree(node);
3049 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003050 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003051}
3052
3053/* This function checks whether the client already reached the
3054 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3055 * will not be sent.
3056 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003057 * returns true if the ROAMING_ADV can be sent, false otherwise
3058 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003059static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003060{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003061 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003062 bool ret = false;
3063
Sven Eckelmann807736f2012-07-15 22:26:51 +02003064 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003065 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003066 * reply from the same orig_node yet
3067 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003068 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003069 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003070 continue;
3071
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003072 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003073 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003074 continue;
3075
Sven Eckelmann3e348192012-05-16 20:23:22 +02003076 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003077 /* Sorry, you roamed too many times! */
3078 goto unlock;
3079 ret = true;
3080 break;
3081 }
3082
3083 if (!ret) {
3084 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3085 if (!tt_roam_node)
3086 goto unlock;
3087
3088 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003089 atomic_set(&tt_roam_node->counter,
3090 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003091 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003092
Sven Eckelmann807736f2012-07-15 22:26:51 +02003093 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003094 ret = true;
3095 }
3096
3097unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003098 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003099 return ret;
3100}
3101
Antonio Quartullic018ad32013-06-04 12:11:39 +02003102/**
3103 * batadv_send_roam_adv - send a roaming advertisement message
3104 * @bat_priv: the bat priv with all the soft interface information
3105 * @client: mac address of the roaming client
3106 * @vid: VLAN identifier
3107 * @orig_node: message destination
3108 *
3109 * Send a ROAMING_ADV message to the node which was previously serving this
3110 * client. This is done to inform the node that from now on all traffic destined
3111 * for this particular roamed client has to be forwarded to the sender of the
3112 * roaming message.
3113 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003114static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003115 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003116 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003117{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003118 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003119 struct batadv_tvlv_roam_adv tvlv_roam;
3120
3121 primary_if = batadv_primary_if_get_selected(bat_priv);
3122 if (!primary_if)
3123 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003124
3125 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003126 * already roamed to us too many times
3127 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003128 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003129 goto out;
3130
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003131 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003132 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3133 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003134
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003135 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003136
Marek Lindner122edaa2013-04-23 21:40:03 +08003137 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003138 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003139
3140 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3141 orig_node->orig, BATADV_TVLV_ROAM, 1,
3142 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003143
3144out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003145 if (primary_if)
3146 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003147}
3148
Sven Eckelmanna5130882012-05-16 20:23:16 +02003149static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003150{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003151 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003152 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003153 struct batadv_priv *bat_priv;
3154
3155 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003156 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3157 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003158
Marek Lindnera19d3d82013-05-27 15:33:25 +08003159 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003160 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003161 batadv_tt_req_purge(bat_priv);
3162 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003163
Antonio Quartulli72414442012-12-25 13:14:37 +01003164 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3165 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003166}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003167
Sven Eckelmann56303d32012-06-05 22:31:31 +02003168void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003169{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003170 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3171 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3172
Sven Eckelmann807736f2012-07-15 22:26:51 +02003173 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003174
Sven Eckelmanna5130882012-05-16 20:23:16 +02003175 batadv_tt_local_table_free(bat_priv);
3176 batadv_tt_global_table_free(bat_priv);
3177 batadv_tt_req_list_free(bat_priv);
3178 batadv_tt_changes_list_free(bat_priv);
3179 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003180
Sven Eckelmann807736f2012-07-15 22:26:51 +02003181 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003182}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003183
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003184/**
3185 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3186 * table and possibly count them in the TT size
3187 * @bat_priv: the bat priv with all the soft interface information
3188 * @flags: the flag to switch
3189 * @enable: whether to set or unset the flag
3190 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003191 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003192static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3193 bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003194{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003195 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3196 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003197 u16 changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003198 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003199 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003200
3201 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003202 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003203
3204 for (i = 0; i < hash->size; i++) {
3205 head = &hash->table[i];
3206
3207 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003208 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003209 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003210 if (enable) {
3211 if ((tt_common_entry->flags & flags) == flags)
3212 continue;
3213 tt_common_entry->flags |= flags;
3214 } else {
3215 if (!(tt_common_entry->flags & flags))
3216 continue;
3217 tt_common_entry->flags &= ~flags;
3218 }
3219 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003220
3221 if (!count)
3222 continue;
3223
3224 batadv_tt_local_size_inc(bat_priv,
3225 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003226 }
3227 rcu_read_unlock();
3228 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003229}
3230
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003231/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003232static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003233{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003234 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003235 struct batadv_tt_common_entry *tt_common;
3236 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003237 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003238 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003239 struct hlist_head *head;
3240 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003241 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003242
3243 if (!hash)
3244 return;
3245
3246 for (i = 0; i < hash->size; i++) {
3247 head = &hash->table[i];
3248 list_lock = &hash->list_locks[i];
3249
3250 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003251 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003252 hash_entry) {
3253 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003254 continue;
3255
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003256 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003257 "Deleting local tt entry (%pM, vid: %d): pending\n",
3258 tt_common->addr,
3259 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003260
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003261 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003262 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003263 tt_local = container_of(tt_common,
3264 struct batadv_tt_local_entry,
3265 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003266
3267 /* decrease the reference held for this vlan */
3268 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08003269 if (vlan) {
3270 batadv_softif_vlan_free_ref(vlan);
3271 batadv_softif_vlan_free_ref(vlan);
3272 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003273
Sven Eckelmann56303d32012-06-05 22:31:31 +02003274 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003275 }
3276 spin_unlock_bh(list_lock);
3277 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003278}
3279
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003280/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003281 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3282 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003283 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003284 *
3285 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003286 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003287static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003288{
Sven Eckelmann5274cd62015-06-21 14:45:15 +02003289 lockdep_assert_held(&bat_priv->tt.commit_lock);
3290
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003291 /* Update multicast addresses in local translation table */
3292 batadv_mcast_mla_update(bat_priv);
3293
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003294 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3295 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3296 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003297 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003298 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003299
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003300 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003301
Sven Eckelmanna5130882012-05-16 20:23:16 +02003302 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003303 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003304
3305 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003306 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003307 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003308 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003309 (u8)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003310
3311 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003312 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003313 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003314}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003315
Marek Lindnera19d3d82013-05-27 15:33:25 +08003316/**
3317 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3318 * have been queued in the time since the last commit
3319 * @bat_priv: the bat priv with all the soft interface information
3320 */
3321void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3322{
3323 spin_lock_bh(&bat_priv->tt.commit_lock);
3324 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003325 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003326}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003327
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003328bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3329 unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003330{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003331 struct batadv_tt_local_entry *tt_local_entry = NULL;
3332 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003333 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003334 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003335
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003336 vlan = batadv_softif_vlan_get(bat_priv, vid);
Markus Elfringe087f342015-11-03 19:20:34 +01003337 if (!vlan)
3338 return false;
3339
3340 if (!atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003341 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003342
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003343 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003344 if (!tt_local_entry)
3345 goto out;
3346
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003347 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003348 if (!tt_global_entry)
3349 goto out;
3350
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003351 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003352 goto out;
3353
Marek Lindner5870adc2012-06-20 17:16:05 +02003354 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003355
3356out:
Markus Elfringf75a33a2015-11-03 19:20:34 +01003357 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003358 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003359 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003360 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003361 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003362 return ret;
3363}
Marek Lindnera943cac2011-07-30 13:10:18 +02003364
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003365/**
3366 * batadv_tt_update_orig - update global translation table with new tt
3367 * information received via ogms
3368 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmanne51f0392015-09-06 21:38:51 +02003369 * @orig_node: the orig_node of the ogm
3370 * @tt_buff: pointer to the first tvlv VLAN entry
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003371 * @tt_num_vlan: number of tvlv VLAN entries
3372 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003373 * @tt_num_changes: number of tt changes inside the tt buffer
3374 * @ttvn: translation table version number of this changeset
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003375 */
3376static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3377 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003378 const void *tt_buff, u16 tt_num_vlan,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003379 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003380 u16 tt_num_changes, u8 ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003381{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003382 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003383 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003384 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003385 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003386
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003387 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003388 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3389 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003390
Antonio Quartulli17071572011-11-07 16:36:40 +01003391 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003392 * increased by one -> we can apply the attached changes
3393 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003394 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003395 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003396 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3397 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003398 * In this case send a tt request
3399 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003400 if (!tt_num_changes) {
3401 full_table = false;
3402 goto request_table;
3403 }
3404
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003405 spin_lock_bh(&orig_node->tt_lock);
3406
Sven Eckelmanna5130882012-05-16 20:23:16 +02003407 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003408 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003409
3410 /* Even if we received the precomputed crc with the OGM, we
3411 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003412 * in the global table
3413 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003414 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003415
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003416 spin_unlock_bh(&orig_node->tt_lock);
3417
Marek Lindnera943cac2011-07-30 13:10:18 +02003418 /* The ttvn alone is not enough to guarantee consistency
3419 * because a single value could represent different states
3420 * (due to the wrap around). Thus a node has to check whether
3421 * the resulting table (after applying the changes) is still
3422 * consistent or not. E.g. a node could disconnect while its
3423 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3424 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003425 * inconsistency
3426 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003427 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3428 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003429 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003430 } else {
3431 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003432 * in sync anymore -> request fresh tt data
3433 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003434 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003435 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3436 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003437request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003438 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003439 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3440 orig_node->orig, ttvn, orig_ttvn,
3441 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003442 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003443 tt_vlan, tt_num_vlan,
3444 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003445 return;
3446 }
3447 }
3448}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003449
Antonio Quartullic018ad32013-06-04 12:11:39 +02003450/**
3451 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3452 * @bat_priv: the bat priv with all the soft interface information
3453 * @addr: the mac address of the client to check
3454 * @vid: VLAN identifier
3455 *
3456 * Returns true if we know that the client has moved from its old originator
3457 * to another one. This entry is still kept for consistency purposes and will be
3458 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003459 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003460bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003461 u8 *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003462{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003463 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003464 bool ret = false;
3465
Antonio Quartullic018ad32013-06-04 12:11:39 +02003466 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003467 if (!tt_global_entry)
3468 goto out;
3469
Antonio Quartullic1d07432013-01-15 22:17:19 +10003470 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003471 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003472out:
3473 return ret;
3474}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003475
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003476/**
3477 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3478 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003479 * @addr: the mac address of the local client to query
3480 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003481 *
3482 * Returns true if the local client is known to be roaming (it is not served by
3483 * this node anymore) or not. If yes, the client is still present in the table
3484 * to keep the latter consistent with the node TTVN
3485 */
3486bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003487 u8 *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003488{
3489 struct batadv_tt_local_entry *tt_local_entry;
3490 bool ret = false;
3491
Antonio Quartullic018ad32013-06-04 12:11:39 +02003492 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003493 if (!tt_local_entry)
3494 goto out;
3495
3496 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3497 batadv_tt_local_entry_free_ref(tt_local_entry);
3498out:
3499 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003500}
3501
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003502bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3503 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003504 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003505 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003506{
3507 bool ret = false;
3508
Antonio Quartulli16052782013-06-04 12:11:41 +02003509 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003510 BATADV_TT_CLIENT_TEMP,
3511 atomic_read(&orig_node->last_ttvn)))
3512 goto out;
3513
3514 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003515 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3516 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003517 ret = true;
3518out:
3519 return ret;
3520}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003521
3522/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003523 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3524 * maximum packet size that can be transported through the mesh
3525 * @soft_iface: netdev struct of the mesh interface
3526 *
3527 * Remove entries older than 'timeout' and half timeout if more entries need
3528 * to be removed.
3529 */
3530void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3531{
3532 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3533 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3534 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3535 bool reduced = false;
3536
3537 spin_lock_bh(&bat_priv->tt.commit_lock);
3538
3539 while (true) {
3540 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3541 if (packet_size_max >= table_size)
3542 break;
3543
3544 batadv_tt_local_purge(bat_priv, timeout);
3545 batadv_tt_local_purge_pending_clients(bat_priv);
3546
3547 timeout /= 2;
3548 reduced = true;
3549 net_ratelimited_function(batadv_info, soft_iface,
3550 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3551 packet_size_max);
3552 }
3553
3554 /* commit these changes immediately, to avoid synchronization problem
3555 * with the TTVN
3556 */
3557 if (reduced)
3558 batadv_tt_local_commit_changes_nolock(bat_priv);
3559
3560 spin_unlock_bh(&bat_priv->tt.commit_lock);
3561}
3562
3563/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003564 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3565 * @bat_priv: the bat priv with all the soft interface information
3566 * @orig: the orig_node of the ogm
3567 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3568 * @tvlv_value: tvlv buffer containing the gateway data
3569 * @tvlv_value_len: tvlv buffer length
3570 */
3571static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3572 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003573 u8 flags, void *tvlv_value,
3574 u16 tvlv_value_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003575{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003576 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3577 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003578 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003579 u16 num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003580
3581 if (tvlv_value_len < sizeof(*tt_data))
3582 return;
3583
3584 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3585 tvlv_value_len -= sizeof(*tt_data);
3586
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003587 num_vlan = ntohs(tt_data->num_vlan);
3588
3589 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3590 return;
3591
3592 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3593 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3594 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3595
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003596 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003597
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003598 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3599 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003600}
3601
3602/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003603 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3604 * container
3605 * @bat_priv: the bat priv with all the soft interface information
3606 * @src: mac address of tt tvlv sender
3607 * @dst: mac address of tt tvlv recipient
3608 * @tvlv_value: tvlv buffer containing the tt data
3609 * @tvlv_value_len: tvlv buffer length
3610 *
3611 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3612 * otherwise.
3613 */
3614static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003615 u8 *src, u8 *dst,
Marek Lindner335fbe02013-04-23 21:40:02 +08003616 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003617 u16 tvlv_value_len)
Marek Lindner335fbe02013-04-23 21:40:02 +08003618{
3619 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003620 u16 tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003621 char tt_flag;
3622 bool ret;
3623
3624 if (tvlv_value_len < sizeof(*tt_data))
3625 return NET_RX_SUCCESS;
3626
3627 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3628 tvlv_value_len -= sizeof(*tt_data);
3629
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003630 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3631 tt_vlan_len *= ntohs(tt_data->num_vlan);
3632
3633 if (tvlv_value_len < tt_vlan_len)
3634 return NET_RX_SUCCESS;
3635
3636 tvlv_value_len -= tt_vlan_len;
3637 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003638
3639 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3640 case BATADV_TT_REQUEST:
3641 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3642
3643 /* If this node cannot provide a TT response the tt_request is
3644 * forwarded
3645 */
3646 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3647 if (!ret) {
3648 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3649 tt_flag = 'F';
3650 else
3651 tt_flag = '.';
3652
3653 batadv_dbg(BATADV_DBG_TT, bat_priv,
3654 "Routing TT_REQUEST to %pM [%c]\n",
3655 dst, tt_flag);
3656 /* tvlv API will re-route the packet */
3657 return NET_RX_DROP;
3658 }
3659 break;
3660 case BATADV_TT_RESPONSE:
3661 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3662
3663 if (batadv_is_my_mac(bat_priv, dst)) {
3664 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003665 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003666 return NET_RX_SUCCESS;
3667 }
3668
3669 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3670 tt_flag = 'F';
3671 else
3672 tt_flag = '.';
3673
3674 batadv_dbg(BATADV_DBG_TT, bat_priv,
3675 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3676
3677 /* tvlv API will re-route the packet */
3678 return NET_RX_DROP;
3679 }
3680
3681 return NET_RX_SUCCESS;
3682}
3683
3684/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003685 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3686 * @bat_priv: the bat priv with all the soft interface information
3687 * @src: mac address of tt tvlv sender
3688 * @dst: mac address of tt tvlv recipient
3689 * @tvlv_value: tvlv buffer containing the tt data
3690 * @tvlv_value_len: tvlv buffer length
3691 *
3692 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3693 * otherwise.
3694 */
3695static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003696 u8 *src, u8 *dst,
Marek Lindner122edaa2013-04-23 21:40:03 +08003697 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003698 u16 tvlv_value_len)
Marek Lindner122edaa2013-04-23 21:40:03 +08003699{
3700 struct batadv_tvlv_roam_adv *roaming_adv;
3701 struct batadv_orig_node *orig_node = NULL;
3702
3703 /* If this node is not the intended recipient of the
3704 * roaming advertisement the packet is forwarded
3705 * (the tvlv API will re-route the packet).
3706 */
3707 if (!batadv_is_my_mac(bat_priv, dst))
3708 return NET_RX_DROP;
3709
Marek Lindner122edaa2013-04-23 21:40:03 +08003710 if (tvlv_value_len < sizeof(*roaming_adv))
3711 goto out;
3712
3713 orig_node = batadv_orig_hash_find(bat_priv, src);
3714 if (!orig_node)
3715 goto out;
3716
3717 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3718 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3719
3720 batadv_dbg(BATADV_DBG_TT, bat_priv,
3721 "Received ROAMING_ADV from %pM (client %pM)\n",
3722 src, roaming_adv->client);
3723
3724 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003725 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003726 atomic_read(&orig_node->last_ttvn) + 1);
3727
3728out:
3729 if (orig_node)
3730 batadv_orig_node_free_ref(orig_node);
3731 return NET_RX_SUCCESS;
3732}
3733
3734/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003735 * batadv_tt_init - initialise the translation table internals
3736 * @bat_priv: the bat priv with all the soft interface information
3737 *
3738 * Return 0 on success or negative error number in case of failure.
3739 */
3740int batadv_tt_init(struct batadv_priv *bat_priv)
3741{
3742 int ret;
3743
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003744 /* synchronized flags must be remote */
3745 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3746
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003747 ret = batadv_tt_local_init(bat_priv);
3748 if (ret < 0)
3749 return ret;
3750
3751 ret = batadv_tt_global_init(bat_priv);
3752 if (ret < 0)
3753 return ret;
3754
3755 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003756 batadv_tt_tvlv_unicast_handler_v1,
3757 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003758
Marek Lindner122edaa2013-04-23 21:40:03 +08003759 batadv_tvlv_handler_register(bat_priv, NULL,
3760 batadv_roam_tvlv_unicast_handler_v1,
3761 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3762
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003763 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3764 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3765 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3766
3767 return 1;
3768}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003769
3770/**
3771 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3772 * @bat_priv: the bat priv with all the soft interface information
3773 * @addr: the mac address of the client
3774 * @vid: the identifier of the VLAN where this client is connected
3775 *
3776 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3777 * otherwise
3778 */
3779bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003780 const u8 *addr, unsigned short vid)
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003781{
3782 struct batadv_tt_global_entry *tt;
3783 bool ret;
3784
3785 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3786 if (!tt)
3787 return false;
3788
3789 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3790
3791 batadv_tt_global_entry_free_ref(tt);
3792
3793 return ret;
3794}