blob: 4add57d4857f11e5ea9edfa13aae1f46dab3e4f9 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartulliced72932013-04-24 16:37:51 +020030#include <linux/crc32c.h>
Antonio Quartullia73105b2011-04-27 14:27:44 +020031
Antonio Quartullidec05072012-11-10 11:00:32 +010032/* hash class keys */
33static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34static struct lock_class_key batadv_tt_global_hash_lock_class_key;
35
Sven Eckelmann56303d32012-06-05 22:31:31 +020036static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020037 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020038 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020039static void batadv_tt_purge(struct work_struct *work);
40static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020041batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020042static void batadv_tt_global_del(struct batadv_priv *bat_priv,
43 struct batadv_orig_node *orig_node,
44 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020045 unsigned short vid, const char *message,
46 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Marek Lindner7aadf882011-02-18 12:28:09 +000048/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020049static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000050{
Sven Eckelmann56303d32012-06-05 22:31:31 +020051 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020052 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000053
54 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
55}
56
Antonio Quartullic018ad32013-06-04 12:11:39 +020057/**
58 * batadv_choose_tt - return the index of the tt entry in the hash table
59 * @data: pointer to the tt_common_entry object to map
60 * @size: the size of the hash table
61 *
62 * Returns the hash index where the object represented by 'data' should be
63 * stored at.
64 */
65static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
66{
67 struct batadv_tt_common_entry *tt;
68 uint32_t hash = 0;
69
70 tt = (struct batadv_tt_common_entry *)data;
71 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
72 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
73
74 hash += (hash << 3);
75 hash ^= (hash >> 11);
76 hash += (hash << 15);
77
78 return hash % size;
79}
80
81/**
82 * batadv_tt_hash_find - look for a client in the given hash table
83 * @hash: the hash table to search
84 * @addr: the mac address of the client to look for
85 * @vid: VLAN identifier
86 *
87 * Returns a pointer to the tt_common struct belonging to the searched client if
88 * found, NULL otherwise.
89 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020090static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +020091batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
92 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +000093{
Marek Lindner7aadf882011-02-18 12:28:09 +000094 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +020095 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020096 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000097
98 if (!hash)
99 return NULL;
100
Antonio Quartullic018ad32013-06-04 12:11:39 +0200101 memcpy(to_search.addr, addr, ETH_ALEN);
102 to_search.vid = vid;
103
104 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000105 head = &hash->table[index];
106
107 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200108 hlist_for_each_entry_rcu(tt, head, hash_entry) {
109 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000110 continue;
111
Antonio Quartullic018ad32013-06-04 12:11:39 +0200112 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200113 continue;
114
Antonio Quartullic018ad32013-06-04 12:11:39 +0200115 if (!atomic_inc_not_zero(&tt->refcount))
116 continue;
117
118 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000119 break;
120 }
121 rcu_read_unlock();
122
Antonio Quartullic018ad32013-06-04 12:11:39 +0200123 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100124}
125
Antonio Quartullic018ad32013-06-04 12:11:39 +0200126/**
127 * batadv_tt_local_hash_find - search the local table for a given client
128 * @bat_priv: the bat priv with all the soft interface information
129 * @addr: the mac address of the client to look for
130 * @vid: VLAN identifier
131 *
132 * Returns a pointer to the corresponding tt_local_entry struct if the client is
133 * found, NULL otherwise.
134 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200135static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200136batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
137 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100138{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200139 struct batadv_tt_common_entry *tt_common_entry;
140 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100141
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
143 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100144 if (tt_common_entry)
145 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200146 struct batadv_tt_local_entry,
147 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100148 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000149}
150
Antonio Quartullic018ad32013-06-04 12:11:39 +0200151/**
152 * batadv_tt_global_hash_find - search the global table for a given client
153 * @bat_priv: the bat priv with all the soft interface information
154 * @addr: the mac address of the client to look for
155 * @vid: VLAN identifier
156 *
157 * Returns a pointer to the corresponding tt_global_entry struct if the client
158 * is found, NULL otherwise.
159 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200161batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
162 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000163{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164 struct batadv_tt_common_entry *tt_common_entry;
165 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000166
Antonio Quartullic018ad32013-06-04 12:11:39 +0200167 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
168 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100169 if (tt_common_entry)
170 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200171 struct batadv_tt_global_entry,
172 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100173 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000174}
175
Sven Eckelmanna5130882012-05-16 20:23:16 +0200176static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200177batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200178{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100179 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
180 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200181}
182
Antonio Quartulli21026052013-05-07 00:29:22 +0200183/**
184 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
185 * tt_global_entry and possibly free it
186 * @tt_global_entry: the object to free
187 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200188static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200189batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200190{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200191 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200192 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200193 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200194 }
195}
196
Sven Eckelmanna5130882012-05-16 20:23:16 +0200197static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200198{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200200
Sven Eckelmann56303d32012-06-05 22:31:31 +0200201 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800202
203 /* We are in an rcu callback here, therefore we cannot use
204 * batadv_orig_node_free_ref() and its call_rcu():
205 * An rcu_barrier() wouldn't wait for that to finish
206 */
207 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200208 kfree(orig_entry);
209}
210
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200211/**
212 * batadv_tt_local_size_mod - change the size by v of the local table identified
213 * by vid
214 * @bat_priv: the bat priv with all the soft interface information
215 * @vid: the VLAN identifier of the sub-table to change
216 * @v: the amount to sum to the local table size
217 */
218static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
219 unsigned short vid, int v)
220{
221 struct batadv_softif_vlan *vlan;
222
223 vlan = batadv_softif_vlan_get(bat_priv, vid);
224 if (!vlan)
225 return;
226
227 atomic_add(v, &vlan->tt.num_entries);
228
229 batadv_softif_vlan_free_ref(vlan);
230}
231
232/**
233 * batadv_tt_local_size_inc - increase by one the local table size for the given
234 * vid
235 * @bat_priv: the bat priv with all the soft interface information
236 * @vid: the VLAN identifier
237 */
238static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
239 unsigned short vid)
240{
241 batadv_tt_local_size_mod(bat_priv, vid, 1);
242}
243
244/**
245 * batadv_tt_local_size_dec - decrease by one the local table size for the given
246 * vid
247 * @bat_priv: the bat priv with all the soft interface information
248 * @vid: the VLAN identifier
249 */
250static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
251 unsigned short vid)
252{
253 batadv_tt_local_size_mod(bat_priv, vid, -1);
254}
255
256/**
257 * batadv_tt_global_size_mod - change the size by v of the local table
258 * identified by vid
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 * @v: the amount to sum to the global table size
262 */
263static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
264 unsigned short vid, int v)
265{
266 struct batadv_orig_node_vlan *vlan;
267
268 vlan = batadv_orig_node_vlan_new(orig_node, vid);
269 if (!vlan)
270 return;
271
272 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
273 spin_lock_bh(&orig_node->vlan_list_lock);
274 list_del_rcu(&vlan->list);
275 spin_unlock_bh(&orig_node->vlan_list_lock);
276 batadv_orig_node_vlan_free_ref(vlan);
277 }
278
279 batadv_orig_node_vlan_free_ref(vlan);
280}
281
282/**
283 * batadv_tt_global_size_inc - increase by one the global table size for the
284 * given vid
285 * @orig_node: the originator which global table size has to be decreased
286 * @vid: the vlan identifier
287 */
288static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
289 unsigned short vid)
290{
291 batadv_tt_global_size_mod(orig_node, vid, 1);
292}
293
294/**
295 * batadv_tt_global_size_dec - decrease by one the global table size for the
296 * given vid
297 * @orig_node: the originator which global table size has to be decreased
298 * @vid: the vlan identifier
299 */
300static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
301 unsigned short vid)
302{
303 batadv_tt_global_size_mod(orig_node, vid, -1);
304}
305
Sven Eckelmanna5130882012-05-16 20:23:16 +0200306static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200307batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200308{
Antonio Quartullid657e622012-07-01 14:09:12 +0200309 if (!atomic_dec_and_test(&orig_entry->refcount))
310 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200311
Sven Eckelmanna5130882012-05-16 20:23:16 +0200312 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200313}
314
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200315/**
316 * batadv_tt_local_event - store a local TT event (ADD/DEL)
317 * @bat_priv: the bat priv with all the soft interface information
318 * @tt_local_entry: the TT entry involved in the event
319 * @event_flags: flags to store in the event structure
320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200321static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200322 struct batadv_tt_local_entry *tt_local_entry,
323 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200324{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200325 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200326 struct batadv_tt_common_entry *common = &tt_local_entry->common;
327 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200328 bool event_removed = false;
329 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200330
331 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200332 if (!tt_change_node)
333 return;
334
Antonio Quartulliff66c972011-06-30 01:14:00 +0200335 tt_change_node->change.flags = flags;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800336 tt_change_node->change.reserved = 0;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200337 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200338 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200339
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200341
342 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200343 spin_lock_bh(&bat_priv->tt.changes_list_lock);
344 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200345 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200346 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200347 continue;
348
349 /* DEL+ADD in the same orig interval have no effect and can be
350 * removed to avoid silly behaviour on the receiver side. The
351 * other way around (ADD+DEL) can happen in case of roaming of
352 * a client still in the NEW state. Roaming of NEW clients is
353 * now possible due to automatically recognition of "temporary"
354 * clients
355 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200356 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200357 if (!del_op_requested && del_op_entry)
358 goto del;
359 if (del_op_requested && !del_op_entry)
360 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200361
362 /* this is a second add in the same originator interval. It
363 * means that flags have been changed: update them!
364 */
365 if (!del_op_requested && !del_op_entry)
366 entry->change.flags = flags;
367
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200368 continue;
369del:
370 list_del(&entry->list);
371 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000372 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200373 event_removed = true;
374 goto unlock;
375 }
376
Antonio Quartullia73105b2011-04-27 14:27:44 +0200377 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200378 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200379
380unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200381 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200382
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200383 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200384 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200385 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200386 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200387}
388
Marek Lindner335fbe02013-04-23 21:40:02 +0800389/**
390 * batadv_tt_len - compute length in bytes of given number of tt changes
391 * @changes_num: number of tt changes
392 *
393 * Returns computed length in bytes.
394 */
395static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396{
Marek Lindner335fbe02013-04-23 21:40:02 +0800397 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398}
399
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200400/**
401 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
402 * @tt_len: available space
403 *
404 * Returns the number of entries.
405 */
406static uint16_t batadv_tt_entries(uint16_t tt_len)
407{
408 return tt_len / batadv_tt_len(1);
409}
410
Marek Lindnera19d3d82013-05-27 15:33:25 +0800411/**
412 * batadv_tt_local_table_transmit_size - calculates the local translation table
413 * size when transmitted over the air
414 * @bat_priv: the bat priv with all the soft interface information
415 *
416 * Returns local translation table size in bytes.
417 */
418static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
419{
420 uint16_t num_vlan = 0, tt_local_entries = 0;
421 struct batadv_softif_vlan *vlan;
422 int hdr_size;
423
424 rcu_read_lock();
425 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
426 num_vlan++;
427 tt_local_entries += atomic_read(&vlan->tt.num_entries);
428 }
429 rcu_read_unlock();
430
431 /* header size of tvlv encapsulated tt response payload */
432 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
433 hdr_size += sizeof(struct batadv_tvlv_hdr);
434 hdr_size += sizeof(struct batadv_tvlv_tt_data);
435 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
436
437 return hdr_size + batadv_tt_len(tt_local_entries);
438}
439
Sven Eckelmann56303d32012-06-05 22:31:31 +0200440static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200442 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200443 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Sven Eckelmann807736f2012-07-15 22:26:51 +0200445 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Sven Eckelmann807736f2012-07-15 22:26:51 +0200447 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200448 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Antonio Quartullidec05072012-11-10 11:00:32 +0100450 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
451 &batadv_tt_local_hash_lock_class_key);
452
Sven Eckelmann5346c352012-05-05 13:27:28 +0200453 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454}
455
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200456static void batadv_tt_global_free(struct batadv_priv *bat_priv,
457 struct batadv_tt_global_entry *tt_global,
458 const char *message)
459{
460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200461 "Deleting global tt entry %pM (vid: %d): %s\n",
462 tt_global->common.addr,
463 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200464
465 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200466 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200467 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200468}
469
Antonio Quartullic018ad32013-06-04 12:11:39 +0200470/**
471 * batadv_tt_local_add - add a new client to the local table or update an
472 * existing client
473 * @soft_iface: netdev struct of the mesh interface
474 * @addr: the mac address of the client to add
475 * @vid: VLAN identifier
476 * @ifindex: index of the interface where the client is connected to (useful to
477 * identify wireless clients)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800478 *
479 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200480 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800481bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200482 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200484 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200485 struct batadv_tt_local_entry *tt_local;
486 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200487 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200488 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200489 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800490 int hash_added, table_size, packet_size_max;
491 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200492 uint8_t remote_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200494 if (ifindex != BATADV_NULL_IFINDEX)
495 in_dev = dev_get_by_index(&init_net, ifindex);
496
Antonio Quartullic018ad32013-06-04 12:11:39 +0200497 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
498 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
Antonio Quartulli47c94652012-09-23 22:38:35 +0200500 if (tt_local) {
501 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200502 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
503 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200504 "Re-adding pending client %pM (vid: %d)\n",
505 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200506 /* whatever the reason why the PENDING flag was set,
507 * this is a client which was enqueued to be removed in
508 * this orig_interval. Since it popped up again, the
509 * flag can be reset like it was never enqueued
510 */
511 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
512 goto add_event;
513 }
514
515 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
516 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200517 "Roaming client %pM (vid: %d) came back to its original location\n",
518 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200519 /* the ROAM flag is set because this client roamed away
520 * and the node got a roaming_advertisement message. Now
521 * that the client popped up again at its original
522 * location such flag can be unset
523 */
524 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
525 roamed_back = true;
526 }
527 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528 }
529
Marek Lindnera19d3d82013-05-27 15:33:25 +0800530 /* Ignore the client if we cannot send it in a full table response. */
531 table_size = batadv_tt_local_table_transmit_size(bat_priv);
532 table_size += batadv_tt_len(1);
533 packet_size_max = atomic_read(&bat_priv->packet_size_max);
534 if (table_size > packet_size_max) {
535 net_ratelimited_function(batadv_info, soft_iface,
536 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
537 table_size, packet_size_max, addr);
538 goto out;
539 }
540
Antonio Quartulli47c94652012-09-23 22:38:35 +0200541 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
542 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200543 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200544
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200545 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200546 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
547 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200548 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
Antonio Quartulli47c94652012-09-23 22:38:35 +0200550 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100551 /* The local entry has to be marked as NEW to avoid to send it in
552 * a full table response going out before the next ttvn increment
553 * (consistency check)
554 */
555 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200556 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200557 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200558 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
559 atomic_set(&tt_local->common.refcount, 2);
560 tt_local->last_seen = jiffies;
561 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
563 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200564 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200565 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
Sven Eckelmann807736f2012-07-15 22:26:51 +0200567 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200568 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200569 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100570
571 if (unlikely(hash_added != 0)) {
572 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200573 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100574 goto out;
575 }
576
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200577add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200578 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200579
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200580check_roaming:
581 /* Check whether it is a roaming, but don't do anything if the roaming
582 * process has already been handled
583 */
584 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200585 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200586 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200587 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800588 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200589 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200590 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200591 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200592 }
593 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200594 if (roamed_back) {
595 batadv_tt_global_free(bat_priv, tt_global,
596 "Roaming canceled");
597 tt_global = NULL;
598 } else {
599 /* The global entry has to be marked as ROAMING and
600 * has to be kept for consistency purpose
601 */
602 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
603 tt_global->roam_at = jiffies;
604 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200605 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200606
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200607 /* store the current remote flags before altering them. This helps
608 * understanding is flags are changing or not
609 */
610 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800611
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200612 if (batadv_is_wifi_netdev(in_dev))
613 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
614 else
615 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
616
617 /* if any "dynamic" flag has been modified, resend an ADD event for this
618 * entry so that all the nodes can get the new flags
619 */
620 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
621 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
622
623 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200624out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200625 if (in_dev)
626 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200627 if (tt_local)
628 batadv_tt_local_entry_free_ref(tt_local);
629 if (tt_global)
630 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800631 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632}
633
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800634/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200635 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
636 * within a TT Response directed to another node
637 * @orig_node: originator for which the TT data has to be prepared
638 * @tt_data: uninitialised pointer to the address of the TVLV buffer
639 * @tt_change: uninitialised pointer to the address of the area where the TT
640 * changed can be stored
641 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
642 * function reserves the amount of space needed to send the entire global TT
643 * table. In case of success the value is updated with the real amount of
644 * reserved bytes
645
646 * Allocate the needed amount of memory for the entire TT TVLV and write its
647 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
648 * objects, one per active VLAN served by the originator node.
649 *
650 * Return the size of the allocated buffer or 0 in case of failure.
651 */
652static uint16_t
653batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
654 struct batadv_tvlv_tt_data **tt_data,
655 struct batadv_tvlv_tt_change **tt_change,
656 int32_t *tt_len)
657{
658 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
659 struct batadv_tvlv_tt_vlan_data *tt_vlan;
660 struct batadv_orig_node_vlan *vlan;
661 uint8_t *tt_change_ptr;
662
663 rcu_read_lock();
664 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
665 num_vlan++;
666 num_entries += atomic_read(&vlan->tt.num_entries);
667 }
668
669 change_offset = sizeof(**tt_data);
670 change_offset += num_vlan * sizeof(*tt_vlan);
671
672 /* if tt_len is negative, allocate the space needed by the full table */
673 if (*tt_len < 0)
674 *tt_len = batadv_tt_len(num_entries);
675
676 tvlv_len = *tt_len;
677 tvlv_len += change_offset;
678
679 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
680 if (!*tt_data) {
681 *tt_len = 0;
682 goto out;
683 }
684
685 (*tt_data)->flags = BATADV_NO_FLAGS;
686 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
687 (*tt_data)->num_vlan = htons(num_vlan);
688
689 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
690 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
691 tt_vlan->vid = htons(vlan->vid);
692 tt_vlan->crc = htonl(vlan->tt.crc);
693
694 tt_vlan++;
695 }
696
697 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
698 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
699
700out:
701 rcu_read_unlock();
702 return tvlv_len;
703}
704
705/**
706 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
707 * node
708 * @bat_priv: the bat priv with all the soft interface information
709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changes can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire local TT
714 * table. In case of success the value is updated with the real amount of
715 * reserved bytes
716 *
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN.
720 *
721 * Return the size of the allocated buffer or 0 in case of failure.
722 */
723static uint16_t
724batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
725 struct batadv_tvlv_tt_data **tt_data,
726 struct batadv_tvlv_tt_change **tt_change,
727 int32_t *tt_len)
728{
729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
730 struct batadv_softif_vlan *vlan;
731 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
732 uint8_t *tt_change_ptr;
733 int change_offset;
734
735 rcu_read_lock();
736 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
737 num_vlan++;
738 num_entries += atomic_read(&vlan->tt.num_entries);
739 }
740
741 change_offset = sizeof(**tt_data);
742 change_offset += num_vlan * sizeof(*tt_vlan);
743
744 /* if tt_len is negative, allocate the space needed by the full table */
745 if (*tt_len < 0)
746 *tt_len = batadv_tt_len(num_entries);
747
748 tvlv_len = *tt_len;
749 tvlv_len += change_offset;
750
751 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
752 if (!*tt_data) {
753 tvlv_len = 0;
754 goto out;
755 }
756
757 (*tt_data)->flags = BATADV_NO_FLAGS;
758 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
759 (*tt_data)->num_vlan = htons(num_vlan);
760
761 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
762 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
763 tt_vlan->vid = htons(vlan->vid);
764 tt_vlan->crc = htonl(vlan->tt.crc);
765
766 tt_vlan++;
767 }
768
769 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
770 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
771
772out:
773 rcu_read_unlock();
774 return tvlv_len;
775}
776
777/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800778 * batadv_tt_tvlv_container_update - update the translation table tvlv container
779 * after local tt changes have been committed
780 * @bat_priv: the bat priv with all the soft interface information
781 */
782static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800784 struct batadv_tt_change_node *entry, *safe;
785 struct batadv_tvlv_tt_data *tt_data;
786 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200787 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800788 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200789 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200791 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
792 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800793
794 /* if we have too many changes for one packet don't send any
795 * and wait for the tt table request which will be fragmented
796 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800797 if (tt_diff_len > bat_priv->soft_iface->mtu)
798 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800799
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200800 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
801 &tt_change, &tt_diff_len);
802 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800803 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800804
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800805 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800806
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800807 if (tt_diff_len == 0)
808 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800809
Sven Eckelmann807736f2012-07-15 22:26:51 +0200810 spin_lock_bh(&bat_priv->tt.changes_list_lock);
811 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812
Sven Eckelmann807736f2012-07-15 22:26:51 +0200813 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100814 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800815 if (tt_diff_entries_count < tt_diff_entries_num) {
816 memcpy(tt_change + tt_diff_entries_count,
817 &entry->change,
818 sizeof(struct batadv_tvlv_tt_change));
819 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200821 list_del(&entry->list);
822 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200824 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825
Antonio Quartullia73105b2011-04-27 14:27:44 +0200826 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200827 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
828 kfree(bat_priv->tt.last_changeset);
829 bat_priv->tt.last_changeset_len = 0;
830 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800831 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800832 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800833 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800834 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200835 * instead of providing the diff
836 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800837 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200838 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800839 memcpy(bat_priv->tt.last_changeset,
840 tt_change, tt_change_len);
841 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200842 }
843 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200844 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800846container_register:
847 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200848 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800849 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850}
851
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200852int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853{
854 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200855 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200856 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200857 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100858 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200859 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200860 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200862 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200863 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100864 int last_seen_secs;
865 int last_seen_msecs;
866 unsigned long last_seen_jiffies;
867 bool no_purge;
868 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
Marek Lindner30da63a2012-08-03 17:15:46 +0200870 primary_if = batadv_seq_print_text_primary_if_get(seq);
871 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200872 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100874 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200875 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
876 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
877 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
878 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880 for (i = 0; i < hash->size; i++) {
881 head = &hash->table[i];
882
Marek Lindner7aadf882011-02-18 12:28:09 +0000883 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800884 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000885 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100886 tt_local = container_of(tt_common_entry,
887 struct batadv_tt_local_entry,
888 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200889 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100890 last_seen_jiffies = jiffies - tt_local->last_seen;
891 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
892 last_seen_secs = last_seen_msecs / 1000;
893 last_seen_msecs = last_seen_msecs % 1000;
894
895 no_purge = tt_common_entry->flags & np_flag;
896
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200897 vlan = batadv_softif_vlan_get(bat_priv, vid);
898 if (!vlan) {
899 seq_printf(seq, "Cannot retrieve VLAN %d\n",
900 BATADV_PRINT_VID(vid));
901 continue;
902 }
903
904 seq_printf(seq,
905 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100906 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200907 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100908 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200909 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100910 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100911 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200912 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100913 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200914 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100915 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100916 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100917 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200918 no_purge ? 0 : last_seen_msecs,
919 vlan->tt.crc);
920
921 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000923 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200925out:
926 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200927 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200928 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929}
930
Sven Eckelmann56303d32012-06-05 22:31:31 +0200931static void
932batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
933 struct batadv_tt_local_entry *tt_local_entry,
934 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000935{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200936 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000937
Antonio Quartulli015758d2011-07-09 17:52:13 +0200938 /* The local client has to be marked as "pending to be removed" but has
939 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200940 * response issued before the net ttvn increment (consistency check)
941 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200942 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100943
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200944 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200945 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
946 tt_local_entry->common.addr,
947 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000948}
949
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200950/**
951 * batadv_tt_local_remove - logically remove an entry from the local table
952 * @bat_priv: the bat priv with all the soft interface information
953 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200954 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200955 * @message: message to append to the log on deletion
956 * @roaming: true if the deletion is due to a roaming event
957 *
958 * Returns the flags assigned to the local entry before being deleted
959 */
960uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200961 const uint8_t *addr, unsigned short vid,
962 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200964 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200965 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966
Antonio Quartullic018ad32013-06-04 12:11:39 +0200967 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200968 if (!tt_local_entry)
969 goto out;
970
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200971 curr_flags = tt_local_entry->common.flags;
972
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200973 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200974 /* if this global entry addition is due to a roaming, the node has to
975 * mark the local entry as "roamed" in order to correctly reroute
976 * packets later
977 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200978 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200979 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200980 /* mark the local client as ROAMed */
981 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
982 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200983
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200984 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
985 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
986 message);
987 goto out;
988 }
989 /* if this client has been added right now, it is possible to
990 * immediately purge it
991 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200992 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200993 hlist_del_rcu(&tt_local_entry->common.hash_entry);
994 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200995
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200996out:
997 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200998 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200999
1000 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001}
1002
Marek Lindnera19d3d82013-05-27 15:33:25 +08001003/**
1004 * batadv_tt_local_purge_list - purge inactive tt local entries
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @head: pointer to the list containing the local tt entries
1007 * @timeout: parameter deciding whether a given tt local entry is considered
1008 * inactive or not
1009 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001010static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001011 struct hlist_head *head,
1012 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001014 struct batadv_tt_local_entry *tt_local_entry;
1015 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001016 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001017
Sasha Levinb67bfe02013-02-27 17:06:00 -08001018 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001019 hash_entry) {
1020 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001021 struct batadv_tt_local_entry,
1022 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001023 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1024 continue;
1025
1026 /* entry already marked for deletion */
1027 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1028 continue;
1029
Marek Lindnera19d3d82013-05-27 15:33:25 +08001030 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001031 continue;
1032
1033 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1034 BATADV_TT_CLIENT_DEL, "timed out");
1035 }
1036}
1037
Marek Lindnera19d3d82013-05-27 15:33:25 +08001038/**
1039 * batadv_tt_local_purge - purge inactive tt local entries
1040 * @bat_priv: the bat priv with all the soft interface information
1041 * @timeout: parameter deciding whether a given tt local entry is considered
1042 * inactive or not
1043 */
1044static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1045 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001046{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001049 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001050 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052 for (i = 0; i < hash->size; i++) {
1053 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001054 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001055
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001056 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001057 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001058 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060}
1061
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001064 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001065 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001066 struct batadv_tt_common_entry *tt_common_entry;
1067 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001068 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001069 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001070 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001071
Sven Eckelmann807736f2012-07-15 22:26:51 +02001072 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073 return;
1074
Sven Eckelmann807736f2012-07-15 22:26:51 +02001075 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001076
1077 for (i = 0; i < hash->size; i++) {
1078 head = &hash->table[i];
1079 list_lock = &hash->list_locks[i];
1080
1081 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001082 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001083 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001084 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001085 tt_local = container_of(tt_common_entry,
1086 struct batadv_tt_local_entry,
1087 common);
1088 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001089 }
1090 spin_unlock_bh(list_lock);
1091 }
1092
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001093 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001094
Sven Eckelmann807736f2012-07-15 22:26:51 +02001095 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001096}
1097
Sven Eckelmann56303d32012-06-05 22:31:31 +02001098static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001099{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001100 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001101 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102
Sven Eckelmann807736f2012-07-15 22:26:51 +02001103 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104
Sven Eckelmann807736f2012-07-15 22:26:51 +02001105 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001106 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001107
Antonio Quartullidec05072012-11-10 11:00:32 +01001108 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1109 &batadv_tt_global_hash_lock_class_key);
1110
Sven Eckelmann5346c352012-05-05 13:27:28 +02001111 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001112}
1113
Sven Eckelmann56303d32012-06-05 22:31:31 +02001114static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001115{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001116 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001117
Sven Eckelmann807736f2012-07-15 22:26:51 +02001118 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001119
Sven Eckelmann807736f2012-07-15 22:26:51 +02001120 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001121 list) {
1122 list_del(&entry->list);
1123 kfree(entry);
1124 }
1125
Sven Eckelmann807736f2012-07-15 22:26:51 +02001126 atomic_set(&bat_priv->tt.local_changes, 0);
1127 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001128}
1129
Antonio Quartullid657e622012-07-01 14:09:12 +02001130/* retrieves the orig_tt_list_entry belonging to orig_node from the
1131 * batadv_tt_global_entry list
1132 *
1133 * returns it with an increased refcounter, NULL if not found
1134 */
1135static struct batadv_tt_orig_list_entry *
1136batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1137 const struct batadv_orig_node *orig_node)
1138{
1139 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1140 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001141
1142 rcu_read_lock();
1143 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001144 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001145 if (tmp_orig_entry->orig_node != orig_node)
1146 continue;
1147 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1148 continue;
1149
1150 orig_entry = tmp_orig_entry;
1151 break;
1152 }
1153 rcu_read_unlock();
1154
1155 return orig_entry;
1156}
1157
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001158/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001159 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001160 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001161static bool
1162batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1163 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164{
Antonio Quartullid657e622012-07-01 14:09:12 +02001165 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001166 bool found = false;
1167
Antonio Quartullid657e622012-07-01 14:09:12 +02001168 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1169 if (orig_entry) {
1170 found = true;
1171 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001172 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001173
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001174 return found;
1175}
1176
Sven Eckelmanna5130882012-05-16 20:23:16 +02001177static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001178batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001179 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001180{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001182
Antonio Quartullid657e622012-07-01 14:09:12 +02001183 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001184 if (orig_entry) {
1185 /* refresh the ttvn: the current value could be a bogus one that
1186 * was added during a "temporary client detection"
1187 */
1188 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001189 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001190 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001191
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001192 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1193 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001194 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001195
1196 INIT_HLIST_NODE(&orig_entry->list);
1197 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001198 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001199 orig_entry->orig_node = orig_node;
1200 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001201 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001202
Antonio Quartullid657e622012-07-01 14:09:12 +02001203 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001204 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001205 &tt_global->orig_list);
1206 spin_unlock_bh(&tt_global->list_lock);
1207out:
1208 if (orig_entry)
1209 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001210}
1211
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001212/**
1213 * batadv_tt_global_add - add a new TT global entry or update an existing one
1214 * @bat_priv: the bat priv with all the soft interface information
1215 * @orig_node: the originator announcing the client
1216 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001217 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001218 * @flags: TT flags that have to be set for this non-mesh client
1219 * @ttvn: the tt version number ever announcing this non-mesh client
1220 *
1221 * Add a new TT global entry for the given originator. If the entry already
1222 * exists add a new reference to the given originator (a global entry can have
1223 * references to multiple originators) and adjust the flags attribute to reflect
1224 * the function argument.
1225 * If a TT local entry exists for this non-mesh client remove it.
1226 *
1227 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001228 *
1229 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001230 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001231static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1232 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001233 const unsigned char *tt_addr,
1234 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001235 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001236{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001237 struct batadv_tt_global_entry *tt_global_entry;
1238 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001239 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001240 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001241 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001242 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001243
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001244 /* ignore global entries from backbone nodes */
1245 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1246 return true;
1247
Antonio Quartullic018ad32013-06-04 12:11:39 +02001248 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1249 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001250
1251 /* if the node already has a local client for this entry, it has to wait
1252 * for a roaming advertisement instead of manually messing up the global
1253 * table
1254 */
1255 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1256 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1257 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001258
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001260 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001261 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001262 goto out;
1263
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001264 common = &tt_global_entry->common;
1265 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001266 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267
Antonio Quartullid4f44692012-05-25 00:00:54 +02001268 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001269 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001270 /* node must store current time in case of roaming. This is
1271 * needed to purge this entry out on timeout (if nobody claims
1272 * it)
1273 */
1274 if (flags & BATADV_TT_CLIENT_ROAM)
1275 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001276 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001277 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001278
1279 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1280 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001281
Sven Eckelmann807736f2012-07-15 22:26:51 +02001282 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001283 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001284 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001285 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001286
1287 if (unlikely(hash_added != 0)) {
1288 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001289 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001290 goto out_remove;
1291 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001292 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001293 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001294 /* If there is already a global entry, we can use this one for
1295 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001296 * But if we are trying to add a temporary client then here are
1297 * two options at this point:
1298 * 1) the global client is not a temporary client: the global
1299 * client has to be left as it is, temporary information
1300 * should never override any already known client state
1301 * 2) the global client is a temporary client: purge the
1302 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001303 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001304 if (flags & BATADV_TT_CLIENT_TEMP) {
1305 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1306 goto out;
1307 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1308 orig_node))
1309 goto out_remove;
1310 batadv_tt_global_del_orig_list(tt_global_entry);
1311 goto add_orig_entry;
1312 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001313
1314 /* if the client was temporary added before receiving the first
1315 * OGM announcing it, we have to clear the TEMP flag
1316 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001317 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001318
Antonio Quartullie9c001362012-11-07 15:05:33 +01001319 /* the change can carry possible "attribute" flags like the
1320 * TT_CLIENT_WIFI, therefore they have to be copied in the
1321 * client entry
1322 */
1323 tt_global_entry->common.flags |= flags;
1324
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001325 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1326 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001327 * delete + roaming change for this originator.
1328 *
1329 * We should first delete the old originator before adding the
1330 * new one.
1331 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001332 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001333 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001334 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001335 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001336 }
1337 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001338add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001339 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001340 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001342 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001343 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1344 common->addr, BATADV_PRINT_VID(common->vid),
1345 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001346 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001347
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001348out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001349
Antonio Quartullia73105b2011-04-27 14:27:44 +02001350 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001351 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001352 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001353 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001354 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1355
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001356 if (!(flags & BATADV_TT_CLIENT_ROAM))
1357 /* this is a normal global add. Therefore the client is not in a
1358 * roaming state anymore.
1359 */
1360 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1361
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001362out:
1363 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001364 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001365 if (tt_local_entry)
1366 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001367 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001368}
1369
Sven Eckelmann981d8902012-10-07 13:34:15 +02001370/* batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001371 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001372 * @tt_global_entry: global translation table entry to be analyzed
1373 *
1374 * This functon assumes the caller holds rcu_read_lock().
1375 * Returns best originator list entry or NULL on errors.
1376 */
1377static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001378batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1379 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001380{
Antonio Quartulli46274562013-09-03 11:10:24 +02001381 struct batadv_neigh_node *router, *best_router = NULL;
1382 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001383 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001384 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001385
1386 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001387 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001388 router = batadv_orig_node_get_router(orig_entry->orig_node);
1389 if (!router)
1390 continue;
1391
Antonio Quartulli46274562013-09-03 11:10:24 +02001392 if (best_router &&
1393 bao->bat_neigh_cmp(router, best_router) <= 0) {
1394 batadv_neigh_node_free_ref(router);
1395 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001396 }
1397
Antonio Quartulli46274562013-09-03 11:10:24 +02001398 /* release the refcount for the "old" best */
1399 if (best_router)
1400 batadv_neigh_node_free_ref(best_router);
1401
1402 best_entry = orig_entry;
1403 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001404 }
1405
Antonio Quartulli46274562013-09-03 11:10:24 +02001406 if (best_router)
1407 batadv_neigh_node_free_ref(best_router);
1408
Sven Eckelmann981d8902012-10-07 13:34:15 +02001409 return best_entry;
1410}
1411
1412/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1413 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001414 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001415 * @tt_global_entry: global translation table entry to be printed
1416 * @seq: debugfs table seq_file struct
1417 *
1418 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001420static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001421batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1422 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001423 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001424{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001425 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001426 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001427 struct batadv_orig_node_vlan *vlan;
1428 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001429 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001430 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001431
1432 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001433 flags = tt_common_entry->flags;
1434
Antonio Quartulli46274562013-09-03 11:10:24 +02001435 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001436 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001437 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1438 tt_common_entry->vid);
1439 if (!vlan) {
1440 seq_printf(seq,
1441 " * Cannot retrieve VLAN %d for originator %pM\n",
1442 BATADV_PRINT_VID(tt_common_entry->vid),
1443 best_entry->orig_node->orig);
1444 goto print_list;
1445 }
1446
Sven Eckelmann981d8902012-10-07 13:34:15 +02001447 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001448 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001449 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001450 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001451 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001452 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001453 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001454 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1455 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1456 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001457
1458 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001459 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001460
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001461print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001462 head = &tt_global_entry->orig_list;
1463
Sasha Levinb67bfe02013-02-27 17:06:00 -08001464 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001465 if (best_entry == orig_entry)
1466 continue;
1467
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001468 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1469 tt_common_entry->vid);
1470 if (!vlan) {
1471 seq_printf(seq,
1472 " + Cannot retrieve VLAN %d for originator %pM\n",
1473 BATADV_PRINT_VID(tt_common_entry->vid),
1474 orig_entry->orig_node->orig);
1475 continue;
1476 }
1477
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001478 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001479 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001480 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001481 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001482 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001483 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001484 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001485 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001486 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1487 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001488
1489 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001490 }
1491}
1492
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001493int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001494{
1495 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001496 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001497 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001498 struct batadv_tt_common_entry *tt_common_entry;
1499 struct batadv_tt_global_entry *tt_global;
1500 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001501 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001502 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001503
Marek Lindner30da63a2012-08-03 17:15:46 +02001504 primary_if = batadv_seq_print_text_primary_if_get(seq);
1505 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001506 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001507
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001508 seq_printf(seq,
1509 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001510 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001511 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1512 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1513 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001514
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001515 for (i = 0; i < hash->size; i++) {
1516 head = &hash->table[i];
1517
Marek Lindner7aadf882011-02-18 12:28:09 +00001518 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001519 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001520 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001521 tt_global = container_of(tt_common_entry,
1522 struct batadv_tt_global_entry,
1523 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001524 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001525 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001526 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001527 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001528out:
1529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001530 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001531 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001532}
1533
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001534/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001535static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001536batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001537{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001538 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001539 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001540 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001541
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001542 spin_lock_bh(&tt_global_entry->list_lock);
1543 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001544 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1545 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001546 batadv_tt_global_size_dec(orig_entry->orig_node,
1547 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001548 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001549 }
1550 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001551}
1552
Sven Eckelmanna5130882012-05-16 20:23:16 +02001553static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001554batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1555 struct batadv_tt_global_entry *tt_global_entry,
1556 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001557 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001558{
1559 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001560 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001561 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001562 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001563
1564 spin_lock_bh(&tt_global_entry->list_lock);
1565 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001566 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001567 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001568 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001569 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001570 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001571 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001572 tt_global_entry->common.addr,
1573 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001574 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001575 batadv_tt_global_size_dec(orig_node,
1576 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001577 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001578 }
1579 }
1580 spin_unlock_bh(&tt_global_entry->list_lock);
1581}
1582
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001583/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001584 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1585 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001586 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001587static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001588batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1589 struct batadv_tt_global_entry *tt_global_entry,
1590 struct batadv_orig_node *orig_node,
1591 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001592{
1593 bool last_entry = true;
1594 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001595 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001596
1597 /* no local entry exists, case 1:
1598 * Check if this is the last one or if other entries exist.
1599 */
1600
1601 rcu_read_lock();
1602 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001603 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001604 if (orig_entry->orig_node != orig_node) {
1605 last_entry = false;
1606 break;
1607 }
1608 }
1609 rcu_read_unlock();
1610
1611 if (last_entry) {
1612 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001613 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001614 tt_global_entry->roam_at = jiffies;
1615 } else
1616 /* there is another entry, we can simply delete this
1617 * one and can still use the other one.
1618 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001619 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1620 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001621}
1622
Antonio Quartullic018ad32013-06-04 12:11:39 +02001623/**
1624 * batadv_tt_global_del - remove a client from the global table
1625 * @bat_priv: the bat priv with all the soft interface information
1626 * @orig_node: an originator serving this client
1627 * @addr: the mac address of the client
1628 * @vid: VLAN identifier
1629 * @message: a message explaining the reason for deleting the client to print
1630 * for debugging purpose
1631 * @roaming: true if the deletion has been triggered by a roaming event
1632 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001633static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1634 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001635 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001636 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001637{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001638 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001639 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001640
Antonio Quartullic018ad32013-06-04 12:11:39 +02001641 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001642 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001643 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001645 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001646 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1647 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001648
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001649 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001650 batadv_tt_global_free(bat_priv, tt_global_entry,
1651 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001652
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001653 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001654 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001655
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001656 /* if we are deleting a global entry due to a roam
1657 * event, there are two possibilities:
1658 * 1) the client roamed from node A to node B => if there
1659 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001660 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001661 * wait for node B to claim it. In case of timeout
1662 * the entry is purged.
1663 *
1664 * If there are other originators left, we directly delete
1665 * the originator.
1666 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001667 * the global entry, since it is useless now.
1668 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001669 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001670 tt_global_entry->common.addr,
1671 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001672 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001673 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001674 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001675 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001676 } else
1677 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001678 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1679 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001680
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001681
Antonio Quartullicc47f662011-04-27 14:27:57 +02001682out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001683 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001684 batadv_tt_global_entry_free_ref(tt_global_entry);
1685 if (local_entry)
1686 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001687}
1688
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001689/**
1690 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1691 * given originator matching the provided vid
1692 * @bat_priv: the bat priv with all the soft interface information
1693 * @orig_node: the originator owning the entries to remove
1694 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1695 * removed
1696 * @message: debug message to print as "reason"
1697 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001698void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1699 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001700 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001701 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001703 struct batadv_tt_global_entry *tt_global;
1704 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001705 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001706 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001707 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001709 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001710 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001711
Simon Wunderlich6e801492011-10-19 10:28:26 +02001712 if (!hash)
1713 return;
1714
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 for (i = 0; i < hash->size; i++) {
1716 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001717 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001718
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001719 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001720 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001721 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001722 /* remove only matching entries */
1723 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1724 continue;
1725
Sven Eckelmann56303d32012-06-05 22:31:31 +02001726 tt_global = container_of(tt_common_entry,
1727 struct batadv_tt_global_entry,
1728 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001729
Sven Eckelmann56303d32012-06-05 22:31:31 +02001730 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001731 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001732
Sven Eckelmann56303d32012-06-05 22:31:31 +02001733 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001734 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001735 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001736 "Deleting global tt entry %pM (vid: %d): %s\n",
1737 tt_global->common.addr,
1738 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001739 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001740 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001741 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001743 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001744 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001745 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001746}
1747
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001748static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1749 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001750{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001751 bool purge = false;
1752 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1753 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001754
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001755 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1756 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1757 purge = true;
1758 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001759 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001760
1761 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1762 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1763 purge = true;
1764 *msg = "Temporary client timeout\n";
1765 }
1766
1767 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001768}
1769
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001770static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001771{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001772 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001773 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001774 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001775 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001776 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001777 char *msg = NULL;
1778 struct batadv_tt_common_entry *tt_common;
1779 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001780
Antonio Quartullicc47f662011-04-27 14:27:57 +02001781 for (i = 0; i < hash->size; i++) {
1782 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001783 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001784
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001785 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001786 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001787 hash_entry) {
1788 tt_global = container_of(tt_common,
1789 struct batadv_tt_global_entry,
1790 common);
1791
1792 if (!batadv_tt_global_to_purge(tt_global, &msg))
1793 continue;
1794
1795 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001796 "Deleting global tt entry %pM (vid: %d): %s\n",
1797 tt_global->common.addr,
1798 BATADV_PRINT_VID(tt_global->common.vid),
1799 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001800
Sasha Levinb67bfe02013-02-27 17:06:00 -08001801 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001802
1803 batadv_tt_global_entry_free_ref(tt_global);
1804 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001805 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001806 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001807}
1808
Sven Eckelmann56303d32012-06-05 22:31:31 +02001809static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001810{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001811 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001812 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001813 struct batadv_tt_common_entry *tt_common_entry;
1814 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001815 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001816 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001817 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001818
Sven Eckelmann807736f2012-07-15 22:26:51 +02001819 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001820 return;
1821
Sven Eckelmann807736f2012-07-15 22:26:51 +02001822 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001823
1824 for (i = 0; i < hash->size; i++) {
1825 head = &hash->table[i];
1826 list_lock = &hash->list_locks[i];
1827
1828 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001829 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001830 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001831 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001832 tt_global = container_of(tt_common_entry,
1833 struct batadv_tt_global_entry,
1834 common);
1835 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001836 }
1837 spin_unlock_bh(list_lock);
1838 }
1839
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001840 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001841
Sven Eckelmann807736f2012-07-15 22:26:51 +02001842 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001843}
1844
Sven Eckelmann56303d32012-06-05 22:31:31 +02001845static bool
1846_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1847 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001848{
1849 bool ret = false;
1850
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001851 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1852 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001853 ret = true;
1854
1855 return ret;
1856}
1857
Antonio Quartullic018ad32013-06-04 12:11:39 +02001858/**
1859 * batadv_transtable_search - get the mesh destination for a given client
1860 * @bat_priv: the bat priv with all the soft interface information
1861 * @src: mac address of the source client
1862 * @addr: mac address of the destination client
1863 * @vid: VLAN identifier
1864 *
1865 * Returns a pointer to the originator that was selected as destination in the
1866 * mesh for contacting the client 'addr', NULL otherwise.
1867 * In case of multiple originators serving the same client, the function returns
1868 * the best one (best in terms of metric towards the destination node).
1869 *
1870 * If the two clients are AP isolated the function returns NULL.
1871 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001872struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1873 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001874 const uint8_t *addr,
1875 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001876{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001877 struct batadv_tt_local_entry *tt_local_entry = NULL;
1878 struct batadv_tt_global_entry *tt_global_entry = NULL;
1879 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001880 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001881 bool ap_isolation_enabled = false;
1882 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001883
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001884 /* if the AP isolation is requested on a VLAN, then check for its
1885 * setting in the proper VLAN private data structure
1886 */
1887 vlan = batadv_softif_vlan_get(bat_priv, vid);
1888 if (vlan) {
1889 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1890 batadv_softif_vlan_free_ref(vlan);
1891 }
1892
1893 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001894 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001895 if (!tt_local_entry ||
1896 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001897 goto out;
1898 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001899
Antonio Quartullic018ad32013-06-04 12:11:39 +02001900 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001901 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001902 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001903
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001904 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001905 * isolation
1906 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001907 if (tt_local_entry &&
1908 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001909 goto out;
1910
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001911 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001912 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001913 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001914 if (best_entry)
1915 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001916 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1917 orig_node = NULL;
1918 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001919
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001920out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001921 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001922 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001923 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001924 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001925
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001926 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001927}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928
Antonio Quartulliced72932013-04-24 16:37:51 +02001929/**
1930 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1931 * to the given orig_node
1932 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001933 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001934 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001935 *
1936 * This function computes the checksum for the global table corresponding to a
1937 * specific originator. In particular, the checksum is computed as follows: For
1938 * each client connected to the originator the CRC32C of the MAC address and the
1939 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1940 * together.
1941 *
1942 * The idea behind is that CRC32C should be used as much as possible in order to
1943 * produce a unique hash of the table, but since the order which is used to feed
1944 * the CRC32C function affects the result and since every node in the network
1945 * probably sorts the clients differently, the hash function cannot be directly
1946 * computed over the entire table. Hence the CRC32C is used only on
1947 * the single client entry, while all the results are then xor'ed together
1948 * because the XOR operation can combine them all while trying to reduce the
1949 * noise as much as possible.
1950 *
1951 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001952 */
1953static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001954 struct batadv_orig_node *orig_node,
1955 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001957 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001958 struct batadv_tt_common_entry *tt_common;
1959 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001960 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001961 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02001962 uint8_t flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963
1964 for (i = 0; i < hash->size; i++) {
1965 head = &hash->table[i];
1966
1967 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001968 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001969 tt_global = container_of(tt_common,
1970 struct batadv_tt_global_entry,
1971 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001972 /* compute the CRC only for entries belonging to the
1973 * VLAN identified by the vid passed as parameter
1974 */
1975 if (tt_common->vid != vid)
1976 continue;
1977
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001978 /* Roaming clients are in the global table for
1979 * consistency only. They don't have to be
1980 * taken into account while computing the
1981 * global crc
1982 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001983 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001984 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001985 /* Temporary clients have not been announced yet, so
1986 * they have to be skipped while computing the global
1987 * crc
1988 */
1989 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1990 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001991
1992 /* find out if this global entry is announced by this
1993 * originator
1994 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001995 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001996 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001997 continue;
1998
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001999 crc_tmp = crc32c(0, &tt_common->vid,
2000 sizeof(tt_common->vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002001
2002 /* compute the CRC on flags that have to be kept in sync
2003 * among nodes
2004 */
2005 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2006 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2007
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002008 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009 }
2010 rcu_read_unlock();
2011 }
2012
Antonio Quartulliced72932013-04-24 16:37:51 +02002013 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014}
2015
Antonio Quartulliced72932013-04-24 16:37:51 +02002016/**
2017 * batadv_tt_local_crc - calculates the checksum of the local table
2018 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002019 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002020 *
2021 * For details about the computation, please refer to the documentation for
2022 * batadv_tt_global_crc().
2023 *
2024 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002025 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002026static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2027 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002029 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002030 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002032 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002033 uint8_t flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002034
2035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
2037
2038 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002039 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002040 /* compute the CRC only for entries belonging to the
2041 * VLAN identified by vid
2042 */
2043 if (tt_common->vid != vid)
2044 continue;
2045
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002046 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002047 * account while computing the CRC
2048 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002049 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002050 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002051
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002052 crc_tmp = crc32c(0, &tt_common->vid,
2053 sizeof(tt_common->vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002054
2055 /* compute the CRC on flags that have to be kept in sync
2056 * among nodes
2057 */
2058 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2059 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2060
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002061 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002062 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002063 rcu_read_unlock();
2064 }
2065
Antonio Quartulliced72932013-04-24 16:37:51 +02002066 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002067}
2068
Sven Eckelmann56303d32012-06-05 22:31:31 +02002069static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002070{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002071 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072
Sven Eckelmann807736f2012-07-15 22:26:51 +02002073 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002074
Sven Eckelmann807736f2012-07-15 22:26:51 +02002075 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002076 list_del(&node->list);
2077 kfree(node);
2078 }
2079
Sven Eckelmann807736f2012-07-15 22:26:51 +02002080 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002081}
2082
Sven Eckelmann56303d32012-06-05 22:31:31 +02002083static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2084 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002085 const void *tt_buff,
2086 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002088 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002089 * last OGM (the OGM could carry no changes)
2090 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091 spin_lock_bh(&orig_node->tt_buff_lock);
2092 if (tt_buff_len > 0) {
2093 kfree(orig_node->tt_buff);
2094 orig_node->tt_buff_len = 0;
2095 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2096 if (orig_node->tt_buff) {
2097 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2098 orig_node->tt_buff_len = tt_buff_len;
2099 }
2100 }
2101 spin_unlock_bh(&orig_node->tt_buff_lock);
2102}
2103
Sven Eckelmann56303d32012-06-05 22:31:31 +02002104static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002105{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002106 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002107
Sven Eckelmann807736f2012-07-15 22:26:51 +02002108 spin_lock_bh(&bat_priv->tt.req_list_lock);
2109 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002110 if (batadv_has_timed_out(node->issued_at,
2111 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112 list_del(&node->list);
2113 kfree(node);
2114 }
2115 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002116 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002117}
2118
2119/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002120 * has already been issued for this orig_node, NULL otherwise
2121 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002122static struct batadv_tt_req_node *
2123batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2124 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002125{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002126 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002127
Sven Eckelmann807736f2012-07-15 22:26:51 +02002128 spin_lock_bh(&bat_priv->tt.req_list_lock);
2129 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002130 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2131 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002132 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002133 goto unlock;
2134 }
2135
2136 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2137 if (!tt_req_node)
2138 goto unlock;
2139
2140 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2141 tt_req_node->issued_at = jiffies;
2142
Sven Eckelmann807736f2012-07-15 22:26:51 +02002143 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002145 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002146 return tt_req_node;
2147}
2148
Marek Lindner335fbe02013-04-23 21:40:02 +08002149/**
2150 * batadv_tt_local_valid - verify that given tt entry is a valid one
2151 * @entry_ptr: to be checked local tt entry
2152 * @data_ptr: not used but definition required to satisfy the callback prototype
2153 *
2154 * Returns 1 if the entry is a valid, 0 otherwise.
2155 */
2156static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002157{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002158 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002159
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002160 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002161 return 0;
2162 return 1;
2163}
2164
Sven Eckelmanna5130882012-05-16 20:23:16 +02002165static int batadv_tt_global_valid(const void *entry_ptr,
2166 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002168 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2169 const struct batadv_tt_global_entry *tt_global_entry;
2170 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002171
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002172 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2173 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002174 return 0;
2175
Sven Eckelmann56303d32012-06-05 22:31:31 +02002176 tt_global_entry = container_of(tt_common_entry,
2177 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002178 common);
2179
Sven Eckelmanna5130882012-05-16 20:23:16 +02002180 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002181}
2182
Marek Lindner335fbe02013-04-23 21:40:02 +08002183/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002184 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2185 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002186 * @bat_priv: the bat priv with all the soft interface information
2187 * @hash: hash table containing the tt entries
2188 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002189 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002190 * @valid_cb: function to filter tt change entries
2191 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002192 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002193static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2194 struct batadv_hashtable *hash,
2195 void *tvlv_buff, uint16_t tt_len,
2196 int (*valid_cb)(const void *, const void *),
2197 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002198{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002199 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002200 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002201 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002202 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002203 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002204
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002205 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002206 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002207
2208 rcu_read_lock();
2209 for (i = 0; i < hash->size; i++) {
2210 head = &hash->table[i];
2211
Sasha Levinb67bfe02013-02-27 17:06:00 -08002212 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002213 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002214 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002215 break;
2216
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002217 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002218 continue;
2219
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002220 memcpy(tt_change->addr, tt_common_entry->addr,
2221 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002222 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002223 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08002224 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002225
Marek Lindner335fbe02013-04-23 21:40:02 +08002226 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002227 tt_change++;
2228 }
2229 }
2230 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002231}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002233/**
2234 * batadv_tt_global_check_crc - check if all the CRCs are correct
2235 * @orig_node: originator for which the CRCs have to be checked
2236 * @tt_vlan: pointer to the first tvlv VLAN entry
2237 * @num_vlan: number of tvlv VLAN entries
2238 * @create: if true, create VLAN objects if not found
2239 *
2240 * Return true if all the received CRCs match the locally stored ones, false
2241 * otherwise
2242 */
2243static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2244 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2245 uint16_t num_vlan)
2246{
2247 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2248 struct batadv_orig_node_vlan *vlan;
2249 int i;
2250
2251 /* check if each received CRC matches the locally stored one */
2252 for (i = 0; i < num_vlan; i++) {
2253 tt_vlan_tmp = tt_vlan + i;
2254
2255 /* if orig_node is a backbone node for this VLAN, don't check
2256 * the CRC as we ignore all the global entries over it
2257 */
2258 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002259 orig_node->orig,
2260 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002261 continue;
2262
2263 vlan = batadv_orig_node_vlan_get(orig_node,
2264 ntohs(tt_vlan_tmp->vid));
2265 if (!vlan)
2266 return false;
2267
2268 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2269 return false;
2270 }
2271
2272 return true;
2273}
2274
2275/**
2276 * batadv_tt_local_update_crc - update all the local CRCs
2277 * @bat_priv: the bat priv with all the soft interface information
2278 */
2279static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2280{
2281 struct batadv_softif_vlan *vlan;
2282
2283 /* recompute the global CRC for each VLAN */
2284 rcu_read_lock();
2285 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2286 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2287 }
2288 rcu_read_unlock();
2289}
2290
2291/**
2292 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2293 * @bat_priv: the bat priv with all the soft interface information
2294 * @orig_node: the orig_node for which the CRCs have to be updated
2295 */
2296static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2297 struct batadv_orig_node *orig_node)
2298{
2299 struct batadv_orig_node_vlan *vlan;
2300 uint32_t crc;
2301
2302 /* recompute the global CRC for each VLAN */
2303 rcu_read_lock();
2304 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2305 /* if orig_node is a backbone node for this VLAN, don't compute
2306 * the CRC as we ignore all the global entries over it
2307 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002308 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2309 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002310 continue;
2311
2312 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2313 vlan->tt.crc = crc;
2314 }
2315 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002316}
2317
Antonio Quartulliced72932013-04-24 16:37:51 +02002318/**
2319 * batadv_send_tt_request - send a TT Request message to a given node
2320 * @bat_priv: the bat priv with all the soft interface information
2321 * @dst_orig_node: the destination of the message
2322 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002323 * @tt_vlan: pointer to the first tvlv VLAN object to request
2324 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002325 * @full_table: ask for the entire translation table if true, while only for the
2326 * last TT diff otherwise
2327 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002328static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2329 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002330 uint8_t ttvn,
2331 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2332 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002333{
Marek Lindner335fbe02013-04-23 21:40:02 +08002334 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002335 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002336 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2337 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002338 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002339 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002341 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002342 if (!primary_if)
2343 goto out;
2344
2345 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002346 * reply from the same orig_node yet
2347 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002348 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002349 if (!tt_req_node)
2350 goto out;
2351
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002352 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2353 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002354 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002355 goto out;
2356
Marek Lindner335fbe02013-04-23 21:40:02 +08002357 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2358 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002359 tvlv_tt_data->num_vlan = htons(num_vlan);
2360
2361 /* send all the CRCs within the request. This is needed by intermediate
2362 * nodes to ensure they have the correct table before replying
2363 */
2364 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2365 for (i = 0; i < num_vlan; i++) {
2366 tt_vlan_req->vid = tt_vlan->vid;
2367 tt_vlan_req->crc = tt_vlan->crc;
2368
2369 tt_vlan_req++;
2370 tt_vlan++;
2371 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002372
2373 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002374 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002375
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002376 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002377 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002378
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002379 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002380 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2381 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002382 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002383 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384
2385out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002386 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002387 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002388 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002389 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002390 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002391 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002392 kfree(tt_req_node);
2393 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002394 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002395 return ret;
2396}
2397
Marek Lindner335fbe02013-04-23 21:40:02 +08002398/**
2399 * batadv_send_other_tt_response - send reply to tt request concerning another
2400 * node's translation table
2401 * @bat_priv: the bat priv with all the soft interface information
2402 * @tt_data: tt data containing the tt request information
2403 * @req_src: mac address of tt request sender
2404 * @req_dst: mac address of tt request recipient
2405 *
2406 * Returns true if tt request reply was sent, false otherwise.
2407 */
2408static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2409 struct batadv_tvlv_tt_data *tt_data,
2410 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002411{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002412 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002413 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002414 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002415 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002416 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002417 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002418 uint8_t orig_ttvn, req_ttvn;
2419 uint16_t tvlv_len;
2420 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002421
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002422 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002423 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002424 req_src, tt_data->ttvn, req_dst,
2425 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002426
2427 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002428 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002429 if (!req_dst_orig_node)
2430 goto out;
2431
Marek Lindner335fbe02013-04-23 21:40:02 +08002432 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002433 if (!res_dst_orig_node)
2434 goto out;
2435
Antonio Quartullia73105b2011-04-27 14:27:44 +02002436 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002437 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002438
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002439 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002440 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002441 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002442 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2443 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002444 goto out;
2445
Antonio Quartulli015758d2011-07-09 17:52:13 +02002446 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002447 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002448 !req_dst_orig_node->tt_buff)
2449 full_table = true;
2450 else
2451 full_table = false;
2452
Marek Lindner335fbe02013-04-23 21:40:02 +08002453 /* TT fragmentation hasn't been implemented yet, so send as many
2454 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002455 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002456 if (!full_table) {
2457 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2458 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002460 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2461 &tvlv_tt_data,
2462 &tt_change,
2463 &tt_len);
2464 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002465 goto unlock;
2466
Antonio Quartullia73105b2011-04-27 14:27:44 +02002467 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002468 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002469 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002470 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2471 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002472 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2473 * in the initial part
2474 */
2475 tt_len = -1;
2476 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2477 &tvlv_tt_data,
2478 &tt_change,
2479 &tt_len);
2480 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002481 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002482
2483 /* fill the rest of the tvlv with the real TT entries */
2484 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2485 tt_change, tt_len,
2486 batadv_tt_global_valid,
2487 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002488 }
2489
Marek Lindnera19d3d82013-05-27 15:33:25 +08002490 /* Don't send the response, if larger than fragmented packet. */
2491 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2492 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2493 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2494 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2495 res_dst_orig_node->orig);
2496 goto out;
2497 }
2498
Marek Lindner335fbe02013-04-23 21:40:02 +08002499 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2500 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002501
2502 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002503 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002504
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002505 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002506 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2507 res_dst_orig_node->orig, req_dst_orig_node->orig,
2508 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002509
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002510 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002511
Marek Lindner335fbe02013-04-23 21:40:02 +08002512 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002513 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2514 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002515
Marek Lindner335fbe02013-04-23 21:40:02 +08002516 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002517 goto out;
2518
2519unlock:
2520 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2521
2522out:
2523 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002524 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002525 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002526 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002527 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002528 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002529}
Sven Eckelmann96412692012-06-05 22:31:30 +02002530
Marek Lindner335fbe02013-04-23 21:40:02 +08002531/**
2532 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2533 * translation table
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @tt_data: tt data containing the tt request information
2536 * @req_src: mac address of tt request sender
2537 *
2538 * Returns true if tt request reply was sent, false otherwise.
2539 */
2540static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2541 struct batadv_tvlv_tt_data *tt_data,
2542 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002543{
Marek Lindner335fbe02013-04-23 21:40:02 +08002544 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002545 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002546 struct batadv_tvlv_tt_change *tt_change;
2547 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002548 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002549 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002550 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002551 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002552
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002553 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002554 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002555 req_src, tt_data->ttvn,
2556 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002557
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002558 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002559
Sven Eckelmann807736f2012-07-15 22:26:51 +02002560 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002561 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002562
Marek Lindner335fbe02013-04-23 21:40:02 +08002563 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002564 if (!orig_node)
2565 goto out;
2566
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002567 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002568 if (!primary_if)
2569 goto out;
2570
2571 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002572 * is too big send the whole local translation table
2573 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002574 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002575 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002576 full_table = true;
2577 else
2578 full_table = false;
2579
Marek Lindner335fbe02013-04-23 21:40:02 +08002580 /* TT fragmentation hasn't been implemented yet, so send as many
2581 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002582 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002583 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002584 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002585
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002586 tt_len = bat_priv->tt.last_changeset_len;
2587 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2588 &tvlv_tt_data,
2589 &tt_change,
2590 &tt_len);
2591 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002592 goto unlock;
2593
Marek Lindner335fbe02013-04-23 21:40:02 +08002594 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002595 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002596 bat_priv->tt.last_changeset_len);
2597 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002598 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002599 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002600
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002601 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2602 * in the initial part
2603 */
2604 tt_len = -1;
2605 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2606 &tvlv_tt_data,
2607 &tt_change,
2608 &tt_len);
2609 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002610 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002611
2612 /* fill the rest of the tvlv with the real TT entries */
2613 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2614 tt_change, tt_len,
2615 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616 }
2617
Marek Lindner335fbe02013-04-23 21:40:02 +08002618 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2619 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002620
2621 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002622 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002623
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002624 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002625 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2626 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002627
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002629
Marek Lindner335fbe02013-04-23 21:40:02 +08002630 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002631 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2632 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002633
Antonio Quartullia73105b2011-04-27 14:27:44 +02002634 goto out;
2635
2636unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002637 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002638out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002639 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002640 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002641 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002642 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002643 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002644 kfree(tvlv_tt_data);
2645 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646 return true;
2647}
2648
Marek Lindner335fbe02013-04-23 21:40:02 +08002649/**
2650 * batadv_send_tt_response - send reply to tt request
2651 * @bat_priv: the bat priv with all the soft interface information
2652 * @tt_data: tt data containing the tt request information
2653 * @req_src: mac address of tt request sender
2654 * @req_dst: mac address of tt request recipient
2655 *
2656 * Returns true if tt request reply was sent, false otherwise.
2657 */
2658static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2659 struct batadv_tvlv_tt_data *tt_data,
2660 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002662 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002664 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002665 return batadv_send_other_tt_response(bat_priv, tt_data,
2666 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002667}
2668
Sven Eckelmann56303d32012-06-05 22:31:31 +02002669static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2670 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002671 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002672 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673{
2674 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002675 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676
2677 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002678 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2679 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002680 batadv_tt_global_del(bat_priv, orig_node,
2681 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002682 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002683 "tt removed by changes",
2684 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002685 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002686 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002687 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002688 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002689 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690 /* In case of problem while storing a
2691 * global_entry, we stop the updating
2692 * procedure without committing the
2693 * ttvn change. This will avoid to send
2694 * corrupted data on tt_request
2695 */
2696 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002697 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002698 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002699 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002700}
2701
Sven Eckelmann56303d32012-06-05 22:31:31 +02002702static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002703 struct batadv_tvlv_tt_change *tt_change,
2704 uint8_t ttvn, uint8_t *resp_src,
2705 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002706{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002707 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002708
Marek Lindner335fbe02013-04-23 21:40:02 +08002709 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710 if (!orig_node)
2711 goto out;
2712
2713 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002714 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2715 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002717 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2718 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002719
2720 spin_lock_bh(&orig_node->tt_buff_lock);
2721 kfree(orig_node->tt_buff);
2722 orig_node->tt_buff_len = 0;
2723 orig_node->tt_buff = NULL;
2724 spin_unlock_bh(&orig_node->tt_buff_lock);
2725
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002726 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002727
2728out:
2729 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002730 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002731}
2732
Sven Eckelmann56303d32012-06-05 22:31:31 +02002733static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2734 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002735 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002736 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002737{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002738 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2739 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002740
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002741 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2742 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002743 atomic_set(&orig_node->last_ttvn, ttvn);
2744}
2745
Antonio Quartullic018ad32013-06-04 12:11:39 +02002746/**
2747 * batadv_is_my_client - check if a client is served by the local node
2748 * @bat_priv: the bat priv with all the soft interface information
2749 * @addr: the mac adress of the client to check
2750 * @vid: VLAN identifier
2751 *
2752 * Returns true if the client is served by this node, false otherwise.
2753 */
2754bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2755 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002756{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002757 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002758 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002759
Antonio Quartullic018ad32013-06-04 12:11:39 +02002760 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002761 if (!tt_local_entry)
2762 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002763 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002764 * consistency purpose)
2765 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002766 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2767 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002768 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002769 ret = true;
2770out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002771 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002772 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002773 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002774}
2775
Marek Lindner335fbe02013-04-23 21:40:02 +08002776/**
2777 * batadv_handle_tt_response - process incoming tt reply
2778 * @bat_priv: the bat priv with all the soft interface information
2779 * @tt_data: tt data containing the tt request information
2780 * @resp_src: mac address of tt reply sender
2781 * @num_entries: number of tt change entries appended to the tt data
2782 */
2783static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2784 struct batadv_tvlv_tt_data *tt_data,
2785 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002786{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002787 struct batadv_tt_req_node *node, *safe;
2788 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002789 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002790 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2791 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002792
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002793 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002794 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002795 resp_src, tt_data->ttvn, num_entries,
2796 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797
Marek Lindner335fbe02013-04-23 21:40:02 +08002798 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002799 if (!orig_node)
2800 goto out;
2801
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002802 spin_lock_bh(&orig_node->tt_lock);
2803
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002804 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2805 change_offset *= ntohs(tt_data->num_vlan);
2806 change_offset += sizeof(*tt_data);
2807 tvlv_ptr += change_offset;
2808
2809 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002810 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002811 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2812 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002813 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002814 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2815 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002816 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002818 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002819 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002820
2821 spin_unlock_bh(&orig_node->tt_lock);
2822
Antonio Quartullia73105b2011-04-27 14:27:44 +02002823 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002824 spin_lock_bh(&bat_priv->tt.req_list_lock);
2825 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002826 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827 continue;
2828 list_del(&node->list);
2829 kfree(node);
2830 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002831
Sven Eckelmann807736f2012-07-15 22:26:51 +02002832 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833out:
2834 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002835 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002836}
2837
Sven Eckelmann56303d32012-06-05 22:31:31 +02002838static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002839{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002840 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002841
Sven Eckelmann807736f2012-07-15 22:26:51 +02002842 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843
Sven Eckelmann807736f2012-07-15 22:26:51 +02002844 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002845 list_del(&node->list);
2846 kfree(node);
2847 }
2848
Sven Eckelmann807736f2012-07-15 22:26:51 +02002849 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002850}
2851
Sven Eckelmann56303d32012-06-05 22:31:31 +02002852static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002853{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002854 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002855
Sven Eckelmann807736f2012-07-15 22:26:51 +02002856 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2857 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002858 if (!batadv_has_timed_out(node->first_time,
2859 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002860 continue;
2861
2862 list_del(&node->list);
2863 kfree(node);
2864 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002865 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002866}
2867
2868/* This function checks whether the client already reached the
2869 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2870 * will not be sent.
2871 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002872 * returns true if the ROAMING_ADV can be sent, false otherwise
2873 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002874static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002875 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002876{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002877 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002878 bool ret = false;
2879
Sven Eckelmann807736f2012-07-15 22:26:51 +02002880 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002881 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002882 * reply from the same orig_node yet
2883 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002884 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002885 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002886 continue;
2887
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002888 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002889 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002890 continue;
2891
Sven Eckelmann3e348192012-05-16 20:23:22 +02002892 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002893 /* Sorry, you roamed too many times! */
2894 goto unlock;
2895 ret = true;
2896 break;
2897 }
2898
2899 if (!ret) {
2900 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2901 if (!tt_roam_node)
2902 goto unlock;
2903
2904 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002905 atomic_set(&tt_roam_node->counter,
2906 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002907 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2908
Sven Eckelmann807736f2012-07-15 22:26:51 +02002909 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002910 ret = true;
2911 }
2912
2913unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002914 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002915 return ret;
2916}
2917
Antonio Quartullic018ad32013-06-04 12:11:39 +02002918/**
2919 * batadv_send_roam_adv - send a roaming advertisement message
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @client: mac address of the roaming client
2922 * @vid: VLAN identifier
2923 * @orig_node: message destination
2924 *
2925 * Send a ROAMING_ADV message to the node which was previously serving this
2926 * client. This is done to inform the node that from now on all traffic destined
2927 * for this particular roamed client has to be forwarded to the sender of the
2928 * roaming message.
2929 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002930static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002931 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002932 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002933{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002934 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002935 struct batadv_tvlv_roam_adv tvlv_roam;
2936
2937 primary_if = batadv_primary_if_get_selected(bat_priv);
2938 if (!primary_if)
2939 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002940
2941 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002942 * already roamed to us too many times
2943 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002944 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002945 goto out;
2946
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002947 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002948 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2949 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002950
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002951 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002952
Marek Lindner122edaa2013-04-23 21:40:03 +08002953 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002954 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002955
2956 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2957 orig_node->orig, BATADV_TVLV_ROAM, 1,
2958 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002959
2960out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002961 if (primary_if)
2962 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002963}
2964
Sven Eckelmanna5130882012-05-16 20:23:16 +02002965static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002966{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002967 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002968 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002969 struct batadv_priv *bat_priv;
2970
2971 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002972 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2973 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002974
Marek Lindnera19d3d82013-05-27 15:33:25 +08002975 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002976 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002977 batadv_tt_req_purge(bat_priv);
2978 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002979
Antonio Quartulli72414442012-12-25 13:14:37 +01002980 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2981 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002982}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002983
Sven Eckelmann56303d32012-06-05 22:31:31 +02002984void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002985{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002986 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2987 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2988
Sven Eckelmann807736f2012-07-15 22:26:51 +02002989 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002990
Sven Eckelmanna5130882012-05-16 20:23:16 +02002991 batadv_tt_local_table_free(bat_priv);
2992 batadv_tt_global_table_free(bat_priv);
2993 batadv_tt_req_list_free(bat_priv);
2994 batadv_tt_changes_list_free(bat_priv);
2995 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002996
Sven Eckelmann807736f2012-07-15 22:26:51 +02002997 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002998}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002999
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003000/**
3001 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3002 * table and possibly count them in the TT size
3003 * @bat_priv: the bat priv with all the soft interface information
3004 * @flags: the flag to switch
3005 * @enable: whether to set or unset the flag
3006 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003007 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003008static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3009 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003010{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003011 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3012 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003013 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003014 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003015 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003016
3017 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003018 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003019
3020 for (i = 0; i < hash->size; i++) {
3021 head = &hash->table[i];
3022
3023 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003024 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003025 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003026 if (enable) {
3027 if ((tt_common_entry->flags & flags) == flags)
3028 continue;
3029 tt_common_entry->flags |= flags;
3030 } else {
3031 if (!(tt_common_entry->flags & flags))
3032 continue;
3033 tt_common_entry->flags &= ~flags;
3034 }
3035 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003036
3037 if (!count)
3038 continue;
3039
3040 batadv_tt_local_size_inc(bat_priv,
3041 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003042 }
3043 rcu_read_unlock();
3044 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003045}
3046
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003047/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003048static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003049{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003050 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003051 struct batadv_tt_common_entry *tt_common;
3052 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003053 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003054 struct hlist_head *head;
3055 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003056 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003057
3058 if (!hash)
3059 return;
3060
3061 for (i = 0; i < hash->size; i++) {
3062 head = &hash->table[i];
3063 list_lock = &hash->list_locks[i];
3064
3065 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003066 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003067 hash_entry) {
3068 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003069 continue;
3070
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003071 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003072 "Deleting local tt entry (%pM, vid: %d): pending\n",
3073 tt_common->addr,
3074 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003075
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003076 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003077 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003078 tt_local = container_of(tt_common,
3079 struct batadv_tt_local_entry,
3080 common);
3081 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003082 }
3083 spin_unlock_bh(list_lock);
3084 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003085}
3086
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003087/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003088 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3089 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003090 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003091 *
3092 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003093 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003094static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003095{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003096 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3097 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3098 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003099 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003100 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003101
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003102 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003103
Sven Eckelmanna5130882012-05-16 20:23:16 +02003104 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003105 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003106
3107 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003108 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003109 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003110 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003111 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003112
3113 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003114 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003115 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003116}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003117
Marek Lindnera19d3d82013-05-27 15:33:25 +08003118/**
3119 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3120 * have been queued in the time since the last commit
3121 * @bat_priv: the bat priv with all the soft interface information
3122 */
3123void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3124{
3125 spin_lock_bh(&bat_priv->tt.commit_lock);
3126 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003127 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003128}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003129
Sven Eckelmann56303d32012-06-05 22:31:31 +02003130bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003131 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003132{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003133 struct batadv_tt_local_entry *tt_local_entry = NULL;
3134 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003135 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003136 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003137
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003138 vlan = batadv_softif_vlan_get(bat_priv, vid);
3139 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003140 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003141
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003143 if (!tt_local_entry)
3144 goto out;
3145
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003146 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003147 if (!tt_global_entry)
3148 goto out;
3149
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003150 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003151 goto out;
3152
Marek Lindner5870adc2012-06-20 17:16:05 +02003153 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003154
3155out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003156 if (vlan)
3157 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003158 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003159 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003160 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003161 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003162 return ret;
3163}
Marek Lindnera943cac2011-07-30 13:10:18 +02003164
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003165/**
3166 * batadv_tt_update_orig - update global translation table with new tt
3167 * information received via ogms
3168 * @bat_priv: the bat priv with all the soft interface information
3169 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003170 * @tt_vlan: pointer to the first tvlv VLAN entry
3171 * @tt_num_vlan: number of tvlv VLAN entries
3172 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003173 * @tt_num_changes: number of tt changes inside the tt buffer
3174 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003175 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003176 */
3177static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3178 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003179 const void *tt_buff, uint16_t tt_num_vlan,
3180 struct batadv_tvlv_tt_change *tt_change,
3181 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003182{
3183 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003184 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003185 bool full_table = true;
3186
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003187 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003188 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003189 * increased by one -> we can apply the attached changes
3190 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003191 if ((!orig_node->tt_initialised && ttvn == 1) ||
3192 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003193 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003194 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3195 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003196 * In this case send a tt request
3197 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003198 if (!tt_num_changes) {
3199 full_table = false;
3200 goto request_table;
3201 }
3202
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003203 spin_lock_bh(&orig_node->tt_lock);
3204
Marek Lindner335fbe02013-04-23 21:40:02 +08003205 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003206 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003207 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003208
3209 /* Even if we received the precomputed crc with the OGM, we
3210 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003211 * in the global table
3212 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003213 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003214
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003215 spin_unlock_bh(&orig_node->tt_lock);
3216
Marek Lindnera943cac2011-07-30 13:10:18 +02003217 /* The ttvn alone is not enough to guarantee consistency
3218 * because a single value could represent different states
3219 * (due to the wrap around). Thus a node has to check whether
3220 * the resulting table (after applying the changes) is still
3221 * consistent or not. E.g. a node could disconnect while its
3222 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3223 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003224 * inconsistency
3225 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003226 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3227 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003228 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003229 } else {
3230 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003231 * in sync anymore -> request fresh tt data
3232 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003233 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003234 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3235 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003236request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003237 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003238 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3239 orig_node->orig, ttvn, orig_ttvn,
3240 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003241 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003242 tt_vlan, tt_num_vlan,
3243 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003244 return;
3245 }
3246 }
3247}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003248
Antonio Quartullic018ad32013-06-04 12:11:39 +02003249/**
3250 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3251 * @bat_priv: the bat priv with all the soft interface information
3252 * @addr: the mac address of the client to check
3253 * @vid: VLAN identifier
3254 *
3255 * Returns true if we know that the client has moved from its old originator
3256 * to another one. This entry is still kept for consistency purposes and will be
3257 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003258 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003259bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003260 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003261{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003262 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003263 bool ret = false;
3264
Antonio Quartullic018ad32013-06-04 12:11:39 +02003265 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003266 if (!tt_global_entry)
3267 goto out;
3268
Antonio Quartullic1d07432013-01-15 22:17:19 +10003269 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003270 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003271out:
3272 return ret;
3273}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003274
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003275/**
3276 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3277 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003278 * @addr: the mac address of the local client to query
3279 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003280 *
3281 * Returns true if the local client is known to be roaming (it is not served by
3282 * this node anymore) or not. If yes, the client is still present in the table
3283 * to keep the latter consistent with the node TTVN
3284 */
3285bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003286 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003287{
3288 struct batadv_tt_local_entry *tt_local_entry;
3289 bool ret = false;
3290
Antonio Quartullic018ad32013-06-04 12:11:39 +02003291 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003292 if (!tt_local_entry)
3293 goto out;
3294
3295 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3296 batadv_tt_local_entry_free_ref(tt_local_entry);
3297out:
3298 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003299}
3300
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003301bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3302 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003303 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003304 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003305{
3306 bool ret = false;
3307
Antonio Quartulli16052782013-06-04 12:11:41 +02003308 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003309 BATADV_TT_CLIENT_TEMP,
3310 atomic_read(&orig_node->last_ttvn)))
3311 goto out;
3312
3313 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003314 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3315 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003316 ret = true;
3317out:
3318 return ret;
3319}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003320
3321/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003322 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3323 * maximum packet size that can be transported through the mesh
3324 * @soft_iface: netdev struct of the mesh interface
3325 *
3326 * Remove entries older than 'timeout' and half timeout if more entries need
3327 * to be removed.
3328 */
3329void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3330{
3331 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3332 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3333 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3334 bool reduced = false;
3335
3336 spin_lock_bh(&bat_priv->tt.commit_lock);
3337
3338 while (true) {
3339 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3340 if (packet_size_max >= table_size)
3341 break;
3342
3343 batadv_tt_local_purge(bat_priv, timeout);
3344 batadv_tt_local_purge_pending_clients(bat_priv);
3345
3346 timeout /= 2;
3347 reduced = true;
3348 net_ratelimited_function(batadv_info, soft_iface,
3349 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3350 packet_size_max);
3351 }
3352
3353 /* commit these changes immediately, to avoid synchronization problem
3354 * with the TTVN
3355 */
3356 if (reduced)
3357 batadv_tt_local_commit_changes_nolock(bat_priv);
3358
3359 spin_unlock_bh(&bat_priv->tt.commit_lock);
3360}
3361
3362/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003363 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3364 * @bat_priv: the bat priv with all the soft interface information
3365 * @orig: the orig_node of the ogm
3366 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3367 * @tvlv_value: tvlv buffer containing the gateway data
3368 * @tvlv_value_len: tvlv buffer length
3369 */
3370static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3371 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003372 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003373 uint16_t tvlv_value_len)
3374{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003375 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3376 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003377 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003378 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003379
3380 if (tvlv_value_len < sizeof(*tt_data))
3381 return;
3382
3383 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3384 tvlv_value_len -= sizeof(*tt_data);
3385
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003386 num_vlan = ntohs(tt_data->num_vlan);
3387
3388 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3389 return;
3390
3391 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3392 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3393 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3394
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003395 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003396
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003397 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3398 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003399}
3400
3401/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003402 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3403 * container
3404 * @bat_priv: the bat priv with all the soft interface information
3405 * @src: mac address of tt tvlv sender
3406 * @dst: mac address of tt tvlv recipient
3407 * @tvlv_value: tvlv buffer containing the tt data
3408 * @tvlv_value_len: tvlv buffer length
3409 *
3410 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3411 * otherwise.
3412 */
3413static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3414 uint8_t *src, uint8_t *dst,
3415 void *tvlv_value,
3416 uint16_t tvlv_value_len)
3417{
3418 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003419 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003420 char tt_flag;
3421 bool ret;
3422
3423 if (tvlv_value_len < sizeof(*tt_data))
3424 return NET_RX_SUCCESS;
3425
3426 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3427 tvlv_value_len -= sizeof(*tt_data);
3428
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003429 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3430 tt_vlan_len *= ntohs(tt_data->num_vlan);
3431
3432 if (tvlv_value_len < tt_vlan_len)
3433 return NET_RX_SUCCESS;
3434
3435 tvlv_value_len -= tt_vlan_len;
3436 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003437
3438 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3439 case BATADV_TT_REQUEST:
3440 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3441
3442 /* If this node cannot provide a TT response the tt_request is
3443 * forwarded
3444 */
3445 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3446 if (!ret) {
3447 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3448 tt_flag = 'F';
3449 else
3450 tt_flag = '.';
3451
3452 batadv_dbg(BATADV_DBG_TT, bat_priv,
3453 "Routing TT_REQUEST to %pM [%c]\n",
3454 dst, tt_flag);
3455 /* tvlv API will re-route the packet */
3456 return NET_RX_DROP;
3457 }
3458 break;
3459 case BATADV_TT_RESPONSE:
3460 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3461
3462 if (batadv_is_my_mac(bat_priv, dst)) {
3463 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003464 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003465 return NET_RX_SUCCESS;
3466 }
3467
3468 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3469 tt_flag = 'F';
3470 else
3471 tt_flag = '.';
3472
3473 batadv_dbg(BATADV_DBG_TT, bat_priv,
3474 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3475
3476 /* tvlv API will re-route the packet */
3477 return NET_RX_DROP;
3478 }
3479
3480 return NET_RX_SUCCESS;
3481}
3482
3483/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003484 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3485 * @bat_priv: the bat priv with all the soft interface information
3486 * @src: mac address of tt tvlv sender
3487 * @dst: mac address of tt tvlv recipient
3488 * @tvlv_value: tvlv buffer containing the tt data
3489 * @tvlv_value_len: tvlv buffer length
3490 *
3491 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3492 * otherwise.
3493 */
3494static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3495 uint8_t *src, uint8_t *dst,
3496 void *tvlv_value,
3497 uint16_t tvlv_value_len)
3498{
3499 struct batadv_tvlv_roam_adv *roaming_adv;
3500 struct batadv_orig_node *orig_node = NULL;
3501
3502 /* If this node is not the intended recipient of the
3503 * roaming advertisement the packet is forwarded
3504 * (the tvlv API will re-route the packet).
3505 */
3506 if (!batadv_is_my_mac(bat_priv, dst))
3507 return NET_RX_DROP;
3508
Marek Lindner122edaa2013-04-23 21:40:03 +08003509 if (tvlv_value_len < sizeof(*roaming_adv))
3510 goto out;
3511
3512 orig_node = batadv_orig_hash_find(bat_priv, src);
3513 if (!orig_node)
3514 goto out;
3515
3516 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3517 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3518
3519 batadv_dbg(BATADV_DBG_TT, bat_priv,
3520 "Received ROAMING_ADV from %pM (client %pM)\n",
3521 src, roaming_adv->client);
3522
3523 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003524 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003525 atomic_read(&orig_node->last_ttvn) + 1);
3526
3527out:
3528 if (orig_node)
3529 batadv_orig_node_free_ref(orig_node);
3530 return NET_RX_SUCCESS;
3531}
3532
3533/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003534 * batadv_tt_init - initialise the translation table internals
3535 * @bat_priv: the bat priv with all the soft interface information
3536 *
3537 * Return 0 on success or negative error number in case of failure.
3538 */
3539int batadv_tt_init(struct batadv_priv *bat_priv)
3540{
3541 int ret;
3542
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003543 /* synchronized flags must be remote */
3544 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3545
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003546 ret = batadv_tt_local_init(bat_priv);
3547 if (ret < 0)
3548 return ret;
3549
3550 ret = batadv_tt_global_init(bat_priv);
3551 if (ret < 0)
3552 return ret;
3553
3554 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003555 batadv_tt_tvlv_unicast_handler_v1,
3556 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003557
Marek Lindner122edaa2013-04-23 21:40:03 +08003558 batadv_tvlv_handler_register(bat_priv, NULL,
3559 batadv_roam_tvlv_unicast_handler_v1,
3560 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3561
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003562 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3563 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3564 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3565
3566 return 1;
3567}