blob: 4c313ff8b12f363840fc36b182faa71d5d38dba7 [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;
361 continue;
362del:
363 list_del(&entry->list);
364 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000365 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200366 event_removed = true;
367 goto unlock;
368 }
369
Antonio Quartullia73105b2011-04-27 14:27:44 +0200370 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200371 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200372
373unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200374 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200375
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200376 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200377 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200378 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200379 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200380}
381
Marek Lindner335fbe02013-04-23 21:40:02 +0800382/**
383 * batadv_tt_len - compute length in bytes of given number of tt changes
384 * @changes_num: number of tt changes
385 *
386 * Returns computed length in bytes.
387 */
388static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389{
Marek Lindner335fbe02013-04-23 21:40:02 +0800390 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200391}
392
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200393/**
394 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
395 * @tt_len: available space
396 *
397 * Returns the number of entries.
398 */
399static uint16_t batadv_tt_entries(uint16_t tt_len)
400{
401 return tt_len / batadv_tt_len(1);
402}
403
Sven Eckelmann56303d32012-06-05 22:31:31 +0200404static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200406 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200407 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Sven Eckelmann807736f2012-07-15 22:26:51 +0200409 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Sven Eckelmann807736f2012-07-15 22:26:51 +0200411 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200412 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Antonio Quartullidec05072012-11-10 11:00:32 +0100414 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
415 &batadv_tt_local_hash_lock_class_key);
416
Sven Eckelmann5346c352012-05-05 13:27:28 +0200417 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200420static void batadv_tt_global_free(struct batadv_priv *bat_priv,
421 struct batadv_tt_global_entry *tt_global,
422 const char *message)
423{
424 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200425 "Deleting global tt entry %pM (vid: %d): %s\n",
426 tt_global->common.addr,
427 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200428
429 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200430 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200431 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200432}
433
Antonio Quartullic018ad32013-06-04 12:11:39 +0200434/**
435 * batadv_tt_local_add - add a new client to the local table or update an
436 * existing client
437 * @soft_iface: netdev struct of the mesh interface
438 * @addr: the mac address of the client to add
439 * @vid: VLAN identifier
440 * @ifindex: index of the interface where the client is connected to (useful to
441 * identify wireless clients)
442 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200443void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200444 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200446 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200447 struct batadv_tt_local_entry *tt_local;
448 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200449 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200450 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100451 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200452 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453
Antonio Quartullic018ad32013-06-04 12:11:39 +0200454 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
455 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Antonio Quartulli47c94652012-09-23 22:38:35 +0200457 if (tt_local) {
458 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200459 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200461 "Re-adding pending client %pM (vid: %d)\n",
462 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200463 /* whatever the reason why the PENDING flag was set,
464 * this is a client which was enqueued to be removed in
465 * this orig_interval. Since it popped up again, the
466 * flag can be reset like it was never enqueued
467 */
468 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
469 goto add_event;
470 }
471
472 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
473 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200474 "Roaming client %pM (vid: %d) came back to its original location\n",
475 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200476 /* the ROAM flag is set because this client roamed away
477 * and the node got a roaming_advertisement message. Now
478 * that the client popped up again at its original
479 * location such flag can be unset
480 */
481 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
482 roamed_back = true;
483 }
484 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 }
486
Antonio Quartulli47c94652012-09-23 22:38:35 +0200487 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
488 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200489 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200490
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200492 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
493 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200494 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Antonio Quartulli47c94652012-09-23 22:38:35 +0200496 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100497 /* The local entry has to be marked as NEW to avoid to send it in
498 * a full table response going out before the next ttvn increment
499 * (consistency check)
500 */
501 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200502 tt_local->common.vid = vid;
Sven Eckelmann95638772012-05-12 02:09:31 +0200503 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200504 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
505 atomic_set(&tt_local->common.refcount, 2);
506 tt_local->last_seen = jiffies;
507 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508
509 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200510 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200511 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
Sven Eckelmann807736f2012-07-15 22:26:51 +0200513 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200514 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200515 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100516
517 if (unlikely(hash_added != 0)) {
518 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200519 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100520 goto out;
521 }
522
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200523add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200524 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200525
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200526check_roaming:
527 /* Check whether it is a roaming, but don't do anything if the roaming
528 * process has already been handled
529 */
530 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200531 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200532 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200533 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800534 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200535 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200536 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200537 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200538 }
539 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200540 if (roamed_back) {
541 batadv_tt_global_free(bat_priv, tt_global,
542 "Roaming canceled");
543 tt_global = NULL;
544 } else {
545 /* The global entry has to be marked as ROAMING and
546 * has to be kept for consistency purpose
547 */
548 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
549 tt_global->roam_at = jiffies;
550 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200551 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200552
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200553out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200554 if (tt_local)
555 batadv_tt_local_entry_free_ref(tt_local);
556 if (tt_global)
557 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558}
559
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800560/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200561 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
562 * within a TT Response directed to another node
563 * @orig_node: originator for which the TT data has to be prepared
564 * @tt_data: uninitialised pointer to the address of the TVLV buffer
565 * @tt_change: uninitialised pointer to the address of the area where the TT
566 * changed can be stored
567 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
568 * function reserves the amount of space needed to send the entire global TT
569 * table. In case of success the value is updated with the real amount of
570 * reserved bytes
571
572 * Allocate the needed amount of memory for the entire TT TVLV and write its
573 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
574 * objects, one per active VLAN served by the originator node.
575 *
576 * Return the size of the allocated buffer or 0 in case of failure.
577 */
578static uint16_t
579batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
580 struct batadv_tvlv_tt_data **tt_data,
581 struct batadv_tvlv_tt_change **tt_change,
582 int32_t *tt_len)
583{
584 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
585 struct batadv_tvlv_tt_vlan_data *tt_vlan;
586 struct batadv_orig_node_vlan *vlan;
587 uint8_t *tt_change_ptr;
588
589 rcu_read_lock();
590 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
591 num_vlan++;
592 num_entries += atomic_read(&vlan->tt.num_entries);
593 }
594
595 change_offset = sizeof(**tt_data);
596 change_offset += num_vlan * sizeof(*tt_vlan);
597
598 /* if tt_len is negative, allocate the space needed by the full table */
599 if (*tt_len < 0)
600 *tt_len = batadv_tt_len(num_entries);
601
602 tvlv_len = *tt_len;
603 tvlv_len += change_offset;
604
605 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
606 if (!*tt_data) {
607 *tt_len = 0;
608 goto out;
609 }
610
611 (*tt_data)->flags = BATADV_NO_FLAGS;
612 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
613 (*tt_data)->num_vlan = htons(num_vlan);
614
615 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
616 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
617 tt_vlan->vid = htons(vlan->vid);
618 tt_vlan->crc = htonl(vlan->tt.crc);
619
620 tt_vlan++;
621 }
622
623 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
624 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
625
626out:
627 rcu_read_unlock();
628 return tvlv_len;
629}
630
631/**
632 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
633 * node
634 * @bat_priv: the bat priv with all the soft interface information
635 * @tt_data: uninitialised pointer to the address of the TVLV buffer
636 * @tt_change: uninitialised pointer to the address of the area where the TT
637 * changes can be stored
638 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
639 * function reserves the amount of space needed to send the entire local TT
640 * table. In case of success the value is updated with the real amount of
641 * reserved bytes
642 *
643 * Allocate the needed amount of memory for the entire TT TVLV and write its
644 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
645 * objects, one per active VLAN.
646 *
647 * Return the size of the allocated buffer or 0 in case of failure.
648 */
649static uint16_t
650batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
651 struct batadv_tvlv_tt_data **tt_data,
652 struct batadv_tvlv_tt_change **tt_change,
653 int32_t *tt_len)
654{
655 struct batadv_tvlv_tt_vlan_data *tt_vlan;
656 struct batadv_softif_vlan *vlan;
657 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
658 uint8_t *tt_change_ptr;
659 int change_offset;
660
661 rcu_read_lock();
662 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
663 num_vlan++;
664 num_entries += atomic_read(&vlan->tt.num_entries);
665 }
666
667 change_offset = sizeof(**tt_data);
668 change_offset += num_vlan * sizeof(*tt_vlan);
669
670 /* if tt_len is negative, allocate the space needed by the full table */
671 if (*tt_len < 0)
672 *tt_len = batadv_tt_len(num_entries);
673
674 tvlv_len = *tt_len;
675 tvlv_len += change_offset;
676
677 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
678 if (!*tt_data) {
679 tvlv_len = 0;
680 goto out;
681 }
682
683 (*tt_data)->flags = BATADV_NO_FLAGS;
684 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
685 (*tt_data)->num_vlan = htons(num_vlan);
686
687 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
688 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
689 tt_vlan->vid = htons(vlan->vid);
690 tt_vlan->crc = htonl(vlan->tt.crc);
691
692 tt_vlan++;
693 }
694
695 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
696 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
697
698out:
699 rcu_read_unlock();
700 return tvlv_len;
701}
702
703/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800704 * batadv_tt_tvlv_container_update - update the translation table tvlv container
705 * after local tt changes have been committed
706 * @bat_priv: the bat priv with all the soft interface information
707 */
708static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800710 struct batadv_tt_change_node *entry, *safe;
711 struct batadv_tvlv_tt_data *tt_data;
712 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200713 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800714 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200715 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200717 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
718 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800719
720 /* if we have too many changes for one packet don't send any
721 * and wait for the tt table request which will be fragmented
722 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800723 if (tt_diff_len > bat_priv->soft_iface->mtu)
724 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800725
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200726 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
727 &tt_change, &tt_diff_len);
728 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800729 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800730
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800731 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800732
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800733 if (tt_diff_len == 0)
734 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800735
Sven Eckelmann807736f2012-07-15 22:26:51 +0200736 spin_lock_bh(&bat_priv->tt.changes_list_lock);
737 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738
Sven Eckelmann807736f2012-07-15 22:26:51 +0200739 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100740 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800741 if (tt_diff_entries_count < tt_diff_entries_num) {
742 memcpy(tt_change + tt_diff_entries_count,
743 &entry->change,
744 sizeof(struct batadv_tvlv_tt_change));
745 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200747 list_del(&entry->list);
748 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200750 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751
Antonio Quartullia73105b2011-04-27 14:27:44 +0200752 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200753 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
754 kfree(bat_priv->tt.last_changeset);
755 bat_priv->tt.last_changeset_len = 0;
756 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800757 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800758 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800759 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800760 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200761 * instead of providing the diff
762 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800763 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200764 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800765 memcpy(bat_priv->tt.last_changeset,
766 tt_change, tt_change_len);
767 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200768 }
769 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200770 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800772container_register:
773 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200774 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800775 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776}
777
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200778int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779{
780 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200781 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200782 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200783 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100784 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200785 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200786 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200788 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200789 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100790 int last_seen_secs;
791 int last_seen_msecs;
792 unsigned long last_seen_jiffies;
793 bool no_purge;
794 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795
Marek Lindner30da63a2012-08-03 17:15:46 +0200796 primary_if = batadv_seq_print_text_primary_if_get(seq);
797 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200798 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100800 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200801 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
802 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
803 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
804 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806 for (i = 0; i < hash->size; i++) {
807 head = &hash->table[i];
808
Marek Lindner7aadf882011-02-18 12:28:09 +0000809 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800810 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000811 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100812 tt_local = container_of(tt_common_entry,
813 struct batadv_tt_local_entry,
814 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200815 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100816 last_seen_jiffies = jiffies - tt_local->last_seen;
817 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
818 last_seen_secs = last_seen_msecs / 1000;
819 last_seen_msecs = last_seen_msecs % 1000;
820
821 no_purge = tt_common_entry->flags & np_flag;
822
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200823 vlan = batadv_softif_vlan_get(bat_priv, vid);
824 if (!vlan) {
825 seq_printf(seq, "Cannot retrieve VLAN %d\n",
826 BATADV_PRINT_VID(vid));
827 continue;
828 }
829
830 seq_printf(seq,
831 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100832 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200833 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100834 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200835 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100836 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100837 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200838 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100839 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200840 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100841 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100842 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100843 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200844 no_purge ? 0 : last_seen_msecs,
845 vlan->tt.crc);
846
847 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000849 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200851out:
852 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200853 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200854 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855}
856
Sven Eckelmann56303d32012-06-05 22:31:31 +0200857static void
858batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
859 struct batadv_tt_local_entry *tt_local_entry,
860 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200862 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
Antonio Quartulli015758d2011-07-09 17:52:13 +0200864 /* The local client has to be marked as "pending to be removed" but has
865 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200866 * response issued before the net ttvn increment (consistency check)
867 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200868 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100869
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200870 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200871 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
872 tt_local_entry->common.addr,
873 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874}
875
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200876/**
877 * batadv_tt_local_remove - logically remove an entry from the local table
878 * @bat_priv: the bat priv with all the soft interface information
879 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200880 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200881 * @message: message to append to the log on deletion
882 * @roaming: true if the deletion is due to a roaming event
883 *
884 * Returns the flags assigned to the local entry before being deleted
885 */
886uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200887 const uint8_t *addr, unsigned short vid,
888 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200890 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200891 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
Antonio Quartullic018ad32013-06-04 12:11:39 +0200893 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200894 if (!tt_local_entry)
895 goto out;
896
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200897 curr_flags = tt_local_entry->common.flags;
898
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200899 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200900 /* if this global entry addition is due to a roaming, the node has to
901 * mark the local entry as "roamed" in order to correctly reroute
902 * packets later
903 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200904 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200905 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200906 /* mark the local client as ROAMed */
907 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
908 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200909
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200910 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
911 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
912 message);
913 goto out;
914 }
915 /* if this client has been added right now, it is possible to
916 * immediately purge it
917 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200918 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200919 hlist_del_rcu(&tt_local_entry->common.hash_entry);
920 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200921
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200922out:
923 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200924 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200925
926 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927}
928
Sven Eckelmann56303d32012-06-05 22:31:31 +0200929static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200930 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932 struct batadv_tt_local_entry *tt_local_entry;
933 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800934 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200935
Sasha Levinb67bfe02013-02-27 17:06:00 -0800936 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200937 hash_entry) {
938 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200939 struct batadv_tt_local_entry,
940 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200941 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
942 continue;
943
944 /* entry already marked for deletion */
945 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
946 continue;
947
948 if (!batadv_has_timed_out(tt_local_entry->last_seen,
949 BATADV_TT_LOCAL_TIMEOUT))
950 continue;
951
952 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
953 BATADV_TT_CLIENT_DEL, "timed out");
954 }
955}
956
Sven Eckelmann56303d32012-06-05 22:31:31 +0200957static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200958{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200959 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200961 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200962 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000964 for (i = 0; i < hash->size; i++) {
965 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200966 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000967
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200968 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200969 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200970 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000971 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972}
973
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200976 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200977 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200978 struct batadv_tt_common_entry *tt_common_entry;
979 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800980 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200981 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200982 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200983
Sven Eckelmann807736f2012-07-15 22:26:51 +0200984 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000985 return;
986
Sven Eckelmann807736f2012-07-15 22:26:51 +0200987 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
991 list_lock = &hash->list_locks[i];
992
993 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200995 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800996 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200997 tt_local = container_of(tt_common_entry,
998 struct batadv_tt_local_entry,
999 common);
1000 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001001 }
1002 spin_unlock_bh(list_lock);
1003 }
1004
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001005 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001006
Sven Eckelmann807736f2012-07-15 22:26:51 +02001007 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008}
1009
Sven Eckelmann56303d32012-06-05 22:31:31 +02001010static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001012 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001013 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Sven Eckelmann807736f2012-07-15 22:26:51 +02001015 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016
Sven Eckelmann807736f2012-07-15 22:26:51 +02001017 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001018 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019
Antonio Quartullidec05072012-11-10 11:00:32 +01001020 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1021 &batadv_tt_global_hash_lock_class_key);
1022
Sven Eckelmann5346c352012-05-05 13:27:28 +02001023 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024}
1025
Sven Eckelmann56303d32012-06-05 22:31:31 +02001026static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001027{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001028 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001029
Sven Eckelmann807736f2012-07-15 22:26:51 +02001030 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031
Sven Eckelmann807736f2012-07-15 22:26:51 +02001032 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001033 list) {
1034 list_del(&entry->list);
1035 kfree(entry);
1036 }
1037
Sven Eckelmann807736f2012-07-15 22:26:51 +02001038 atomic_set(&bat_priv->tt.local_changes, 0);
1039 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040}
1041
Antonio Quartullid657e622012-07-01 14:09:12 +02001042/* retrieves the orig_tt_list_entry belonging to orig_node from the
1043 * batadv_tt_global_entry list
1044 *
1045 * returns it with an increased refcounter, NULL if not found
1046 */
1047static struct batadv_tt_orig_list_entry *
1048batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1049 const struct batadv_orig_node *orig_node)
1050{
1051 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1052 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001053
1054 rcu_read_lock();
1055 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001056 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001057 if (tmp_orig_entry->orig_node != orig_node)
1058 continue;
1059 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1060 continue;
1061
1062 orig_entry = tmp_orig_entry;
1063 break;
1064 }
1065 rcu_read_unlock();
1066
1067 return orig_entry;
1068}
1069
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001071 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073static bool
1074batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1075 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076{
Antonio Quartullid657e622012-07-01 14:09:12 +02001077 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001078 bool found = false;
1079
Antonio Quartullid657e622012-07-01 14:09:12 +02001080 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1081 if (orig_entry) {
1082 found = true;
1083 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001084 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001085
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086 return found;
1087}
1088
Sven Eckelmanna5130882012-05-16 20:23:16 +02001089static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001090batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001091 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001092{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001093 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094
Antonio Quartullid657e622012-07-01 14:09:12 +02001095 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001096 if (orig_entry) {
1097 /* refresh the ttvn: the current value could be a bogus one that
1098 * was added during a "temporary client detection"
1099 */
1100 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001101 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001102 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001103
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001104 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1105 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001106 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001107
1108 INIT_HLIST_NODE(&orig_entry->list);
1109 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001110 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001111 orig_entry->orig_node = orig_node;
1112 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001113 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001114
Antonio Quartullid657e622012-07-01 14:09:12 +02001115 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001116 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001117 &tt_global->orig_list);
1118 spin_unlock_bh(&tt_global->list_lock);
1119out:
1120 if (orig_entry)
1121 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001122}
1123
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001124/**
1125 * batadv_tt_global_add - add a new TT global entry or update an existing one
1126 * @bat_priv: the bat priv with all the soft interface information
1127 * @orig_node: the originator announcing the client
1128 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001129 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001130 * @flags: TT flags that have to be set for this non-mesh client
1131 * @ttvn: the tt version number ever announcing this non-mesh client
1132 *
1133 * Add a new TT global entry for the given originator. If the entry already
1134 * exists add a new reference to the given originator (a global entry can have
1135 * references to multiple originators) and adjust the flags attribute to reflect
1136 * the function argument.
1137 * If a TT local entry exists for this non-mesh client remove it.
1138 *
1139 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001140 *
1141 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001142 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001143static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1144 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001145 const unsigned char *tt_addr,
1146 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001147 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001149 struct batadv_tt_global_entry *tt_global_entry;
1150 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001151 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001152 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001154 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001155
Antonio Quartullic018ad32013-06-04 12:11:39 +02001156 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1157 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001158
1159 /* if the node already has a local client for this entry, it has to wait
1160 * for a roaming advertisement instead of manually messing up the global
1161 * table
1162 */
1163 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1164 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1165 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001166
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001168 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001169 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001170 goto out;
1171
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001172 common = &tt_global_entry->common;
1173 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001174 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001175
Antonio Quartullid4f44692012-05-25 00:00:54 +02001176 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001177 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001178 /* node must store current time in case of roaming. This is
1179 * needed to purge this entry out on timeout (if nobody claims
1180 * it)
1181 */
1182 if (flags & BATADV_TT_CLIENT_ROAM)
1183 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001184 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001185 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001186
1187 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1188 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001189
Sven Eckelmann807736f2012-07-15 22:26:51 +02001190 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001191 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001192 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001193 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001194
1195 if (unlikely(hash_added != 0)) {
1196 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001197 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001198 goto out_remove;
1199 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001201 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001202 /* If there is already a global entry, we can use this one for
1203 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001204 * But if we are trying to add a temporary client then here are
1205 * two options at this point:
1206 * 1) the global client is not a temporary client: the global
1207 * client has to be left as it is, temporary information
1208 * should never override any already known client state
1209 * 2) the global client is a temporary client: purge the
1210 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001211 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001212 if (flags & BATADV_TT_CLIENT_TEMP) {
1213 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1214 goto out;
1215 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1216 orig_node))
1217 goto out_remove;
1218 batadv_tt_global_del_orig_list(tt_global_entry);
1219 goto add_orig_entry;
1220 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001221
1222 /* if the client was temporary added before receiving the first
1223 * OGM announcing it, we have to clear the TEMP flag
1224 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001225 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001226
Antonio Quartullie9c001362012-11-07 15:05:33 +01001227 /* the change can carry possible "attribute" flags like the
1228 * TT_CLIENT_WIFI, therefore they have to be copied in the
1229 * client entry
1230 */
1231 tt_global_entry->common.flags |= flags;
1232
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001233 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1234 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001235 * delete + roaming change for this originator.
1236 *
1237 * We should first delete the old originator before adding the
1238 * new one.
1239 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001240 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001241 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001242 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001243 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244 }
1245 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001246add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001247 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001248 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001249
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001250 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001251 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1252 common->addr, BATADV_PRINT_VID(common->vid),
1253 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001254 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001255
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001256out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001257
Antonio Quartullia73105b2011-04-27 14:27:44 +02001258 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001259 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001260 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001261 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001262 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1263
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001264 if (!(flags & BATADV_TT_CLIENT_ROAM))
1265 /* this is a normal global add. Therefore the client is not in a
1266 * roaming state anymore.
1267 */
1268 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1269
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001270out:
1271 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001272 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001273 if (tt_local_entry)
1274 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001275 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001276}
1277
Sven Eckelmann981d8902012-10-07 13:34:15 +02001278/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1279 * @tt_global_entry: global translation table entry to be analyzed
1280 *
1281 * This functon assumes the caller holds rcu_read_lock().
1282 * Returns best originator list entry or NULL on errors.
1283 */
1284static struct batadv_tt_orig_list_entry *
1285batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
1286{
1287 struct batadv_neigh_node *router = NULL;
1288 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001289 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1290 int best_tq = 0;
1291
1292 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001293 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001294 router = batadv_orig_node_get_router(orig_entry->orig_node);
1295 if (!router)
1296 continue;
1297
1298 if (router->tq_avg > best_tq) {
1299 best_entry = orig_entry;
1300 best_tq = router->tq_avg;
1301 }
1302
1303 batadv_neigh_node_free_ref(router);
1304 }
1305
1306 return best_entry;
1307}
1308
1309/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1310 * for this global entry
1311 * @tt_global_entry: global translation table entry to be printed
1312 * @seq: debugfs table seq_file struct
1313 *
1314 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001315 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001316static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001317batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001318 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001319{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001320 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001322 struct batadv_orig_node_vlan *vlan;
1323 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001324 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001325 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001326
1327 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001328 flags = tt_common_entry->flags;
1329
1330 best_entry = batadv_transtable_best_orig(tt_global_entry);
1331 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001332 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1333 tt_common_entry->vid);
1334 if (!vlan) {
1335 seq_printf(seq,
1336 " * Cannot retrieve VLAN %d for originator %pM\n",
1337 BATADV_PRINT_VID(tt_common_entry->vid),
1338 best_entry->orig_node->orig);
1339 goto print_list;
1340 }
1341
Sven Eckelmann981d8902012-10-07 13:34:15 +02001342 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001343 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001344 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001345 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001346 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001347 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001348 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001349 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1350 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1351 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001352
1353 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001354 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001355
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001356print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001357 head = &tt_global_entry->orig_list;
1358
Sasha Levinb67bfe02013-02-27 17:06:00 -08001359 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001360 if (best_entry == orig_entry)
1361 continue;
1362
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001363 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1364 tt_common_entry->vid);
1365 if (!vlan) {
1366 seq_printf(seq,
1367 " + Cannot retrieve VLAN %d for originator %pM\n",
1368 BATADV_PRINT_VID(tt_common_entry->vid),
1369 orig_entry->orig_node->orig);
1370 continue;
1371 }
1372
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001373 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001374 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001375 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001376 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001377 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001378 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001379 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001380 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001381 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1382 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001383
1384 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001385 }
1386}
1387
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001388int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001389{
1390 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001391 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001392 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001393 struct batadv_tt_common_entry *tt_common_entry;
1394 struct batadv_tt_global_entry *tt_global;
1395 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001396 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001397 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001398
Marek Lindner30da63a2012-08-03 17:15:46 +02001399 primary_if = batadv_seq_print_text_primary_if_get(seq);
1400 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001401 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001402
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001403 seq_printf(seq,
1404 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001405 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001406 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1407 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1408 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001409
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001410 for (i = 0; i < hash->size; i++) {
1411 head = &hash->table[i];
1412
Marek Lindner7aadf882011-02-18 12:28:09 +00001413 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001414 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001415 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001416 tt_global = container_of(tt_common_entry,
1417 struct batadv_tt_global_entry,
1418 common);
1419 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001420 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001421 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001422 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001423out:
1424 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001425 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001426 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001427}
1428
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001429/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001430static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001431batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001432{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001433 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001434 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001435 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001436
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001437 spin_lock_bh(&tt_global_entry->list_lock);
1438 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001439 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1440 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001441 batadv_tt_global_size_dec(orig_entry->orig_node,
1442 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001443 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001444 }
1445 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001446}
1447
Sven Eckelmanna5130882012-05-16 20:23:16 +02001448static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001449batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1450 struct batadv_tt_global_entry *tt_global_entry,
1451 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001452 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001453{
1454 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001455 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001456 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001457 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001458
1459 spin_lock_bh(&tt_global_entry->list_lock);
1460 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001461 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001462 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001463 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001464 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001465 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001466 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001467 tt_global_entry->common.addr,
1468 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001469 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001470 batadv_tt_global_size_dec(orig_node,
1471 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001472 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001473 }
1474 }
1475 spin_unlock_bh(&tt_global_entry->list_lock);
1476}
1477
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001478/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001479 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1480 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001481 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001482static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001483batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1484 struct batadv_tt_global_entry *tt_global_entry,
1485 struct batadv_orig_node *orig_node,
1486 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001487{
1488 bool last_entry = true;
1489 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001490 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001491
1492 /* no local entry exists, case 1:
1493 * Check if this is the last one or if other entries exist.
1494 */
1495
1496 rcu_read_lock();
1497 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001498 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001499 if (orig_entry->orig_node != orig_node) {
1500 last_entry = false;
1501 break;
1502 }
1503 }
1504 rcu_read_unlock();
1505
1506 if (last_entry) {
1507 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001508 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001509 tt_global_entry->roam_at = jiffies;
1510 } else
1511 /* there is another entry, we can simply delete this
1512 * one and can still use the other one.
1513 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001514 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1515 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001516}
1517
Antonio Quartullic018ad32013-06-04 12:11:39 +02001518/**
1519 * batadv_tt_global_del - remove a client from the global table
1520 * @bat_priv: the bat priv with all the soft interface information
1521 * @orig_node: an originator serving this client
1522 * @addr: the mac address of the client
1523 * @vid: VLAN identifier
1524 * @message: a message explaining the reason for deleting the client to print
1525 * for debugging purpose
1526 * @roaming: true if the deletion has been triggered by a roaming event
1527 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001528static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1529 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001530 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001531 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001532{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001533 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001534 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001535
Antonio Quartullic018ad32013-06-04 12:11:39 +02001536 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001537 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001538 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001540 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001541 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1542 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001543
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001544 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001545 batadv_tt_global_free(bat_priv, tt_global_entry,
1546 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001547
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001548 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001549 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001550
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001551 /* if we are deleting a global entry due to a roam
1552 * event, there are two possibilities:
1553 * 1) the client roamed from node A to node B => if there
1554 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001555 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001556 * wait for node B to claim it. In case of timeout
1557 * the entry is purged.
1558 *
1559 * If there are other originators left, we directly delete
1560 * the originator.
1561 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001562 * the global entry, since it is useless now.
1563 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001564 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001565 tt_global_entry->common.addr,
1566 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001567 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001568 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001569 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001570 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001571 } else
1572 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001573 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1574 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001575
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001576
Antonio Quartullicc47f662011-04-27 14:27:57 +02001577out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001578 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001579 batadv_tt_global_entry_free_ref(tt_global_entry);
1580 if (local_entry)
1581 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001582}
1583
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001584/**
1585 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1586 * given originator matching the provided vid
1587 * @bat_priv: the bat priv with all the soft interface information
1588 * @orig_node: the originator owning the entries to remove
1589 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1590 * removed
1591 * @message: debug message to print as "reason"
1592 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001593void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1594 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001595 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001596 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001597{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001598 struct batadv_tt_global_entry *tt_global;
1599 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001600 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001601 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001602 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001603 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001604 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001605 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606
Simon Wunderlich6e801492011-10-19 10:28:26 +02001607 if (!hash)
1608 return;
1609
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 for (i = 0; i < hash->size; i++) {
1611 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001612 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001613
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001614 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001615 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001616 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001617 /* remove only matching entries */
1618 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1619 continue;
1620
Sven Eckelmann56303d32012-06-05 22:31:31 +02001621 tt_global = container_of(tt_common_entry,
1622 struct batadv_tt_global_entry,
1623 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001624
Sven Eckelmann56303d32012-06-05 22:31:31 +02001625 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001626 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001627
Sven Eckelmann56303d32012-06-05 22:31:31 +02001628 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001629 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001630 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001631 "Deleting global tt entry %pM (vid: %d): %s\n",
1632 tt_global->common.addr,
1633 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001634 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001635 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001636 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001638 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001639 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001640 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001641}
1642
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001643static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1644 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001645{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001646 bool purge = false;
1647 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1648 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001649
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001650 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1651 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1652 purge = true;
1653 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001654 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001655
1656 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1657 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1658 purge = true;
1659 *msg = "Temporary client timeout\n";
1660 }
1661
1662 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001663}
1664
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001665static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001666{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001667 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001668 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001669 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001670 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001671 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001672 char *msg = NULL;
1673 struct batadv_tt_common_entry *tt_common;
1674 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001675
Antonio Quartullicc47f662011-04-27 14:27:57 +02001676 for (i = 0; i < hash->size; i++) {
1677 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001678 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001679
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001680 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001681 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001682 hash_entry) {
1683 tt_global = container_of(tt_common,
1684 struct batadv_tt_global_entry,
1685 common);
1686
1687 if (!batadv_tt_global_to_purge(tt_global, &msg))
1688 continue;
1689
1690 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001691 "Deleting global tt entry %pM (vid: %d): %s\n",
1692 tt_global->common.addr,
1693 BATADV_PRINT_VID(tt_global->common.vid),
1694 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001695
Sasha Levinb67bfe02013-02-27 17:06:00 -08001696 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001697
1698 batadv_tt_global_entry_free_ref(tt_global);
1699 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001700 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001701 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001702}
1703
Sven Eckelmann56303d32012-06-05 22:31:31 +02001704static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001705{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001706 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001707 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001708 struct batadv_tt_common_entry *tt_common_entry;
1709 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001710 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001711 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001712 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001713
Sven Eckelmann807736f2012-07-15 22:26:51 +02001714 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001715 return;
1716
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001718
1719 for (i = 0; i < hash->size; i++) {
1720 head = &hash->table[i];
1721 list_lock = &hash->list_locks[i];
1722
1723 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001724 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001725 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001726 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001727 tt_global = container_of(tt_common_entry,
1728 struct batadv_tt_global_entry,
1729 common);
1730 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001731 }
1732 spin_unlock_bh(list_lock);
1733 }
1734
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001735 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001736
Sven Eckelmann807736f2012-07-15 22:26:51 +02001737 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001738}
1739
Sven Eckelmann56303d32012-06-05 22:31:31 +02001740static bool
1741_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1742 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001743{
1744 bool ret = false;
1745
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001746 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1747 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001748 ret = true;
1749
1750 return ret;
1751}
1752
Antonio Quartullic018ad32013-06-04 12:11:39 +02001753/**
1754 * batadv_transtable_search - get the mesh destination for a given client
1755 * @bat_priv: the bat priv with all the soft interface information
1756 * @src: mac address of the source client
1757 * @addr: mac address of the destination client
1758 * @vid: VLAN identifier
1759 *
1760 * Returns a pointer to the originator that was selected as destination in the
1761 * mesh for contacting the client 'addr', NULL otherwise.
1762 * In case of multiple originators serving the same client, the function returns
1763 * the best one (best in terms of metric towards the destination node).
1764 *
1765 * If the two clients are AP isolated the function returns NULL.
1766 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001767struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1768 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001769 const uint8_t *addr,
1770 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001771{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001772 struct batadv_tt_local_entry *tt_local_entry = NULL;
1773 struct batadv_tt_global_entry *tt_global_entry = NULL;
1774 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001775 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001776 bool ap_isolation_enabled = false;
1777 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001778
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001779 /* if the AP isolation is requested on a VLAN, then check for its
1780 * setting in the proper VLAN private data structure
1781 */
1782 vlan = batadv_softif_vlan_get(bat_priv, vid);
1783 if (vlan) {
1784 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1785 batadv_softif_vlan_free_ref(vlan);
1786 }
1787
1788 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001789 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001790 if (!tt_local_entry ||
1791 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001792 goto out;
1793 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001794
Antonio Quartullic018ad32013-06-04 12:11:39 +02001795 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001796 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001797 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001798
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001799 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001800 * isolation
1801 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001802 if (tt_local_entry &&
1803 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001804 goto out;
1805
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001806 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001807 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001808 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001809 if (best_entry)
1810 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001811 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1812 orig_node = NULL;
1813 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001814
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001815out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001816 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001817 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001818 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001819 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001820
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001821 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001822}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Antonio Quartulliced72932013-04-24 16:37:51 +02001824/**
1825 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1826 * to the given orig_node
1827 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001828 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001829 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001830 *
1831 * This function computes the checksum for the global table corresponding to a
1832 * specific originator. In particular, the checksum is computed as follows: For
1833 * each client connected to the originator the CRC32C of the MAC address and the
1834 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1835 * together.
1836 *
1837 * The idea behind is that CRC32C should be used as much as possible in order to
1838 * produce a unique hash of the table, but since the order which is used to feed
1839 * the CRC32C function affects the result and since every node in the network
1840 * probably sorts the clients differently, the hash function cannot be directly
1841 * computed over the entire table. Hence the CRC32C is used only on
1842 * the single client entry, while all the results are then xor'ed together
1843 * because the XOR operation can combine them all while trying to reduce the
1844 * noise as much as possible.
1845 *
1846 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001847 */
1848static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001849 struct batadv_orig_node *orig_node,
1850 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001852 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001853 struct batadv_tt_common_entry *tt_common;
1854 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001856 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001857
1858 for (i = 0; i < hash->size; i++) {
1859 head = &hash->table[i];
1860
1861 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001862 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001863 tt_global = container_of(tt_common,
1864 struct batadv_tt_global_entry,
1865 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001866 /* compute the CRC only for entries belonging to the
1867 * VLAN identified by the vid passed as parameter
1868 */
1869 if (tt_common->vid != vid)
1870 continue;
1871
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001872 /* Roaming clients are in the global table for
1873 * consistency only. They don't have to be
1874 * taken into account while computing the
1875 * global crc
1876 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001877 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001878 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001879 /* Temporary clients have not been announced yet, so
1880 * they have to be skipped while computing the global
1881 * crc
1882 */
1883 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1884 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001885
1886 /* find out if this global entry is announced by this
1887 * originator
1888 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001889 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001890 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001891 continue;
1892
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001893 crc_tmp = crc32c(0, &tt_common->vid,
1894 sizeof(tt_common->vid));
1895 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001896 }
1897 rcu_read_unlock();
1898 }
1899
Antonio Quartulliced72932013-04-24 16:37:51 +02001900 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001901}
1902
Antonio Quartulliced72932013-04-24 16:37:51 +02001903/**
1904 * batadv_tt_local_crc - calculates the checksum of the local table
1905 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001906 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001907 *
1908 * For details about the computation, please refer to the documentation for
1909 * batadv_tt_global_crc().
1910 *
1911 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001912 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001913static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
1914 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001916 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001917 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001919 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920
1921 for (i = 0; i < hash->size; i++) {
1922 head = &hash->table[i];
1923
1924 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001925 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001926 /* compute the CRC only for entries belonging to the
1927 * VLAN identified by vid
1928 */
1929 if (tt_common->vid != vid)
1930 continue;
1931
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001932 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001933 * account while computing the CRC
1934 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001935 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001936 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001937
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001938 crc_tmp = crc32c(0, &tt_common->vid,
1939 sizeof(tt_common->vid));
1940 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001941 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942 rcu_read_unlock();
1943 }
1944
Antonio Quartulliced72932013-04-24 16:37:51 +02001945 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001946}
1947
Sven Eckelmann56303d32012-06-05 22:31:31 +02001948static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001949{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001950 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001951
Sven Eckelmann807736f2012-07-15 22:26:51 +02001952 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953
Sven Eckelmann807736f2012-07-15 22:26:51 +02001954 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955 list_del(&node->list);
1956 kfree(node);
1957 }
1958
Sven Eckelmann807736f2012-07-15 22:26:51 +02001959 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001960}
1961
Sven Eckelmann56303d32012-06-05 22:31:31 +02001962static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1963 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001964 const void *tt_buff,
1965 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001966{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001968 * last OGM (the OGM could carry no changes)
1969 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970 spin_lock_bh(&orig_node->tt_buff_lock);
1971 if (tt_buff_len > 0) {
1972 kfree(orig_node->tt_buff);
1973 orig_node->tt_buff_len = 0;
1974 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1975 if (orig_node->tt_buff) {
1976 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1977 orig_node->tt_buff_len = tt_buff_len;
1978 }
1979 }
1980 spin_unlock_bh(&orig_node->tt_buff_lock);
1981}
1982
Sven Eckelmann56303d32012-06-05 22:31:31 +02001983static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001984{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001985 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986
Sven Eckelmann807736f2012-07-15 22:26:51 +02001987 spin_lock_bh(&bat_priv->tt.req_list_lock);
1988 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001989 if (batadv_has_timed_out(node->issued_at,
1990 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991 list_del(&node->list);
1992 kfree(node);
1993 }
1994 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001995 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996}
1997
1998/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001999 * has already been issued for this orig_node, NULL otherwise
2000 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002001static struct batadv_tt_req_node *
2002batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2003 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002004{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002005 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002006
Sven Eckelmann807736f2012-07-15 22:26:51 +02002007 spin_lock_bh(&bat_priv->tt.req_list_lock);
2008 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002009 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2010 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002011 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002012 goto unlock;
2013 }
2014
2015 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2016 if (!tt_req_node)
2017 goto unlock;
2018
2019 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2020 tt_req_node->issued_at = jiffies;
2021
Sven Eckelmann807736f2012-07-15 22:26:51 +02002022 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002024 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025 return tt_req_node;
2026}
2027
Marek Lindner335fbe02013-04-23 21:40:02 +08002028/**
2029 * batadv_tt_local_valid - verify that given tt entry is a valid one
2030 * @entry_ptr: to be checked local tt entry
2031 * @data_ptr: not used but definition required to satisfy the callback prototype
2032 *
2033 * Returns 1 if the entry is a valid, 0 otherwise.
2034 */
2035static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002036{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002037 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002038
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002039 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002040 return 0;
2041 return 1;
2042}
2043
Sven Eckelmanna5130882012-05-16 20:23:16 +02002044static int batadv_tt_global_valid(const void *entry_ptr,
2045 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002046{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002047 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2048 const struct batadv_tt_global_entry *tt_global_entry;
2049 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002051 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2052 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002053 return 0;
2054
Sven Eckelmann56303d32012-06-05 22:31:31 +02002055 tt_global_entry = container_of(tt_common_entry,
2056 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002057 common);
2058
Sven Eckelmanna5130882012-05-16 20:23:16 +02002059 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060}
2061
Marek Lindner335fbe02013-04-23 21:40:02 +08002062/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002063 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2064 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002065 * @bat_priv: the bat priv with all the soft interface information
2066 * @hash: hash table containing the tt entries
2067 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002068 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002069 * @valid_cb: function to filter tt change entries
2070 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002071 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002072static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2073 struct batadv_hashtable *hash,
2074 void *tvlv_buff, uint16_t tt_len,
2075 int (*valid_cb)(const void *, const void *),
2076 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002077{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002078 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002079 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002081 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002082 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002084 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002085 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002086
2087 rcu_read_lock();
2088 for (i = 0; i < hash->size; i++) {
2089 head = &hash->table[i];
2090
Sasha Levinb67bfe02013-02-27 17:06:00 -08002091 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002092 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002093 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002094 break;
2095
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002096 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097 continue;
2098
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002099 memcpy(tt_change->addr, tt_common_entry->addr,
2100 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002101 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002102 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08002103 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104
Marek Lindner335fbe02013-04-23 21:40:02 +08002105 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106 tt_change++;
2107 }
2108 }
2109 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002110}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002112/**
2113 * batadv_tt_global_check_crc - check if all the CRCs are correct
2114 * @orig_node: originator for which the CRCs have to be checked
2115 * @tt_vlan: pointer to the first tvlv VLAN entry
2116 * @num_vlan: number of tvlv VLAN entries
2117 * @create: if true, create VLAN objects if not found
2118 *
2119 * Return true if all the received CRCs match the locally stored ones, false
2120 * otherwise
2121 */
2122static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2123 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2124 uint16_t num_vlan)
2125{
2126 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2127 struct batadv_orig_node_vlan *vlan;
2128 int i;
2129
2130 /* check if each received CRC matches the locally stored one */
2131 for (i = 0; i < num_vlan; i++) {
2132 tt_vlan_tmp = tt_vlan + i;
2133
2134 /* if orig_node is a backbone node for this VLAN, don't check
2135 * the CRC as we ignore all the global entries over it
2136 */
2137 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2138 orig_node->orig))
2139 continue;
2140
2141 vlan = batadv_orig_node_vlan_get(orig_node,
2142 ntohs(tt_vlan_tmp->vid));
2143 if (!vlan)
2144 return false;
2145
2146 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2147 return false;
2148 }
2149
2150 return true;
2151}
2152
2153/**
2154 * batadv_tt_local_update_crc - update all the local CRCs
2155 * @bat_priv: the bat priv with all the soft interface information
2156 */
2157static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2158{
2159 struct batadv_softif_vlan *vlan;
2160
2161 /* recompute the global CRC for each VLAN */
2162 rcu_read_lock();
2163 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2164 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2165 }
2166 rcu_read_unlock();
2167}
2168
2169/**
2170 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2171 * @bat_priv: the bat priv with all the soft interface information
2172 * @orig_node: the orig_node for which the CRCs have to be updated
2173 */
2174static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2175 struct batadv_orig_node *orig_node)
2176{
2177 struct batadv_orig_node_vlan *vlan;
2178 uint32_t crc;
2179
2180 /* recompute the global CRC for each VLAN */
2181 rcu_read_lock();
2182 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2183 /* if orig_node is a backbone node for this VLAN, don't compute
2184 * the CRC as we ignore all the global entries over it
2185 */
2186 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2187 continue;
2188
2189 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2190 vlan->tt.crc = crc;
2191 }
2192 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002193}
2194
Antonio Quartulliced72932013-04-24 16:37:51 +02002195/**
2196 * batadv_send_tt_request - send a TT Request message to a given node
2197 * @bat_priv: the bat priv with all the soft interface information
2198 * @dst_orig_node: the destination of the message
2199 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002200 * @tt_vlan: pointer to the first tvlv VLAN object to request
2201 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002202 * @full_table: ask for the entire translation table if true, while only for the
2203 * last TT diff otherwise
2204 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002205static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2206 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002207 uint8_t ttvn,
2208 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2209 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002210{
Marek Lindner335fbe02013-04-23 21:40:02 +08002211 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002212 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002213 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2214 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002215 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002216 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002217
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002218 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002219 if (!primary_if)
2220 goto out;
2221
2222 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002223 * reply from the same orig_node yet
2224 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002225 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002226 if (!tt_req_node)
2227 goto out;
2228
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002229 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2230 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002231 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232 goto out;
2233
Marek Lindner335fbe02013-04-23 21:40:02 +08002234 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2235 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002236 tvlv_tt_data->num_vlan = htons(num_vlan);
2237
2238 /* send all the CRCs within the request. This is needed by intermediate
2239 * nodes to ensure they have the correct table before replying
2240 */
2241 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2242 for (i = 0; i < num_vlan; i++) {
2243 tt_vlan_req->vid = tt_vlan->vid;
2244 tt_vlan_req->crc = tt_vlan->crc;
2245
2246 tt_vlan_req++;
2247 tt_vlan++;
2248 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002249
2250 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002251 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002252
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002253 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002254 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002255
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002256 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002257 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2258 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002259 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002260 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002261
2262out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002263 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002264 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002265 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002266 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002268 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002269 kfree(tt_req_node);
2270 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002271 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002272 return ret;
2273}
2274
Marek Lindner335fbe02013-04-23 21:40:02 +08002275/**
2276 * batadv_send_other_tt_response - send reply to tt request concerning another
2277 * node's translation table
2278 * @bat_priv: the bat priv with all the soft interface information
2279 * @tt_data: tt data containing the tt request information
2280 * @req_src: mac address of tt request sender
2281 * @req_dst: mac address of tt request recipient
2282 *
2283 * Returns true if tt request reply was sent, false otherwise.
2284 */
2285static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2286 struct batadv_tvlv_tt_data *tt_data,
2287 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002288{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002289 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002290 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002291 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002292 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002293 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002294 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002295 uint8_t orig_ttvn, req_ttvn;
2296 uint16_t tvlv_len;
2297 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002298
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002299 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002300 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002301 req_src, tt_data->ttvn, req_dst,
2302 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002303
2304 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002305 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002306 if (!req_dst_orig_node)
2307 goto out;
2308
Marek Lindner335fbe02013-04-23 21:40:02 +08002309 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310 if (!res_dst_orig_node)
2311 goto out;
2312
Antonio Quartullia73105b2011-04-27 14:27:44 +02002313 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002314 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002315
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002316 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002317 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002318 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002319 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2320 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002321 goto out;
2322
Antonio Quartulli015758d2011-07-09 17:52:13 +02002323 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002324 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002325 !req_dst_orig_node->tt_buff)
2326 full_table = true;
2327 else
2328 full_table = false;
2329
Marek Lindner335fbe02013-04-23 21:40:02 +08002330 /* TT fragmentation hasn't been implemented yet, so send as many
2331 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002332 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002333 if (!full_table) {
2334 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2335 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002336
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002337 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2338 &tvlv_tt_data,
2339 &tt_change,
2340 &tt_len);
2341 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002342 goto unlock;
2343
Antonio Quartullia73105b2011-04-27 14:27:44 +02002344 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002345 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002346 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002347 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2348 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002349 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2350 * in the initial part
2351 */
2352 tt_len = -1;
2353 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2354 &tvlv_tt_data,
2355 &tt_change,
2356 &tt_len);
2357 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002358 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002359
2360 /* fill the rest of the tvlv with the real TT entries */
2361 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2362 tt_change, tt_len,
2363 batadv_tt_global_valid,
2364 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002365 }
2366
Marek Lindner335fbe02013-04-23 21:40:02 +08002367 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2368 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002369
2370 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002371 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002372
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002373 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002374 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2375 res_dst_orig_node->orig, req_dst_orig_node->orig,
2376 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002377
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002378 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002379
Marek Lindner335fbe02013-04-23 21:40:02 +08002380 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002381 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2382 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002383
Marek Lindner335fbe02013-04-23 21:40:02 +08002384 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002385 goto out;
2386
2387unlock:
2388 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2389
2390out:
2391 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002392 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002393 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002394 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002395 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002396 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002397}
Sven Eckelmann96412692012-06-05 22:31:30 +02002398
Marek Lindner335fbe02013-04-23 21:40:02 +08002399/**
2400 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2401 * translation table
2402 * @bat_priv: the bat priv with all the soft interface information
2403 * @tt_data: tt data containing the tt request information
2404 * @req_src: mac address of tt request sender
2405 *
2406 * Returns true if tt request reply was sent, false otherwise.
2407 */
2408static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2409 struct batadv_tvlv_tt_data *tt_data,
2410 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002411{
Marek Lindner335fbe02013-04-23 21:40:02 +08002412 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002413 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002414 struct batadv_tvlv_tt_change *tt_change;
2415 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002416 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002417 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002418 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002419 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002420
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002421 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002422 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002423 req_src, tt_data->ttvn,
2424 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002425
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002426 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002427
Sven Eckelmann807736f2012-07-15 22:26:51 +02002428 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002429 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002430
Marek Lindner335fbe02013-04-23 21:40:02 +08002431 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002432 if (!orig_node)
2433 goto out;
2434
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002435 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002436 if (!primary_if)
2437 goto out;
2438
2439 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002440 * is too big send the whole local translation table
2441 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002442 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002443 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002444 full_table = true;
2445 else
2446 full_table = false;
2447
Marek Lindner335fbe02013-04-23 21:40:02 +08002448 /* TT fragmentation hasn't been implemented yet, so send as many
2449 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002450 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002451 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002452 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002453
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002454 tt_len = bat_priv->tt.last_changeset_len;
2455 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2456 &tvlv_tt_data,
2457 &tt_change,
2458 &tt_len);
2459 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002460 goto unlock;
2461
Marek Lindner335fbe02013-04-23 21:40:02 +08002462 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002463 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002464 bat_priv->tt.last_changeset_len);
2465 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002466 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002467 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002468
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002469 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2470 * in the initial part
2471 */
2472 tt_len = -1;
2473 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2474 &tvlv_tt_data,
2475 &tt_change,
2476 &tt_len);
2477 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002478 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002479
2480 /* fill the rest of the tvlv with the real TT entries */
2481 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2482 tt_change, tt_len,
2483 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002484 }
2485
Marek Lindner335fbe02013-04-23 21:40:02 +08002486 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2487 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002488
2489 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002490 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002491
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002492 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002493 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2494 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002495
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002496 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002497
Marek Lindner335fbe02013-04-23 21:40:02 +08002498 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002499 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2500 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002501
Antonio Quartullia73105b2011-04-27 14:27:44 +02002502 goto out;
2503
2504unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002505 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002506out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002507 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002508 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002509 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002510 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002511 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002512 kfree(tvlv_tt_data);
2513 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002514 return true;
2515}
2516
Marek Lindner335fbe02013-04-23 21:40:02 +08002517/**
2518 * batadv_send_tt_response - send reply to tt request
2519 * @bat_priv: the bat priv with all the soft interface information
2520 * @tt_data: tt data containing the tt request information
2521 * @req_src: mac address of tt request sender
2522 * @req_dst: mac address of tt request recipient
2523 *
2524 * Returns true if tt request reply was sent, false otherwise.
2525 */
2526static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2527 struct batadv_tvlv_tt_data *tt_data,
2528 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002529{
Marek Lindner335fbe02013-04-23 21:40:02 +08002530 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002531 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08002532 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002533 return true;
2534
Marek Lindner335fbe02013-04-23 21:40:02 +08002535 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002536 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002537 return batadv_send_other_tt_response(bat_priv, tt_data,
2538 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002539 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002540}
2541
Sven Eckelmann56303d32012-06-05 22:31:31 +02002542static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2543 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002544 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002545 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002546{
2547 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002548 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002549
2550 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002551 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2552 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002553 batadv_tt_global_del(bat_priv, orig_node,
2554 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002555 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002556 "tt removed by changes",
2557 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002558 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002559 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002560 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002561 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002562 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002563 /* In case of problem while storing a
2564 * global_entry, we stop the updating
2565 * procedure without committing the
2566 * ttvn change. This will avoid to send
2567 * corrupted data on tt_request
2568 */
2569 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002570 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002571 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002572 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002573}
2574
Sven Eckelmann56303d32012-06-05 22:31:31 +02002575static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002576 struct batadv_tvlv_tt_change *tt_change,
2577 uint8_t ttvn, uint8_t *resp_src,
2578 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002579{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002580 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002581
Marek Lindner335fbe02013-04-23 21:40:02 +08002582 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002583 if (!orig_node)
2584 goto out;
2585
2586 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002587 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2588 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002589
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002590 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2591 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002592
2593 spin_lock_bh(&orig_node->tt_buff_lock);
2594 kfree(orig_node->tt_buff);
2595 orig_node->tt_buff_len = 0;
2596 orig_node->tt_buff = NULL;
2597 spin_unlock_bh(&orig_node->tt_buff_lock);
2598
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002599 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002600
2601out:
2602 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002603 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002604}
2605
Sven Eckelmann56303d32012-06-05 22:31:31 +02002606static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2607 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002608 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002609 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002610{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002611 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2612 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002613
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002614 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2615 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616 atomic_set(&orig_node->last_ttvn, ttvn);
2617}
2618
Antonio Quartullic018ad32013-06-04 12:11:39 +02002619/**
2620 * batadv_is_my_client - check if a client is served by the local node
2621 * @bat_priv: the bat priv with all the soft interface information
2622 * @addr: the mac adress of the client to check
2623 * @vid: VLAN identifier
2624 *
2625 * Returns true if the client is served by this node, false otherwise.
2626 */
2627bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2628 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002629{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002630 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002631 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002632
Antonio Quartullic018ad32013-06-04 12:11:39 +02002633 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002634 if (!tt_local_entry)
2635 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002636 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002637 * consistency purpose)
2638 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002639 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2640 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002641 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002642 ret = true;
2643out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002644 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002645 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002646 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002647}
2648
Marek Lindner335fbe02013-04-23 21:40:02 +08002649/**
2650 * batadv_handle_tt_response - process incoming tt reply
2651 * @bat_priv: the bat priv with all the soft interface information
2652 * @tt_data: tt data containing the tt request information
2653 * @resp_src: mac address of tt reply sender
2654 * @num_entries: number of tt change entries appended to the tt data
2655 */
2656static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2657 struct batadv_tvlv_tt_data *tt_data,
2658 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002659{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002660 struct batadv_tt_req_node *node, *safe;
2661 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002662 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002663 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2664 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002666 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002667 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002668 resp_src, tt_data->ttvn, num_entries,
2669 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002670
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002671 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002672 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002673 goto out;
2674
Marek Lindner335fbe02013-04-23 21:40:02 +08002675 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676 if (!orig_node)
2677 goto out;
2678
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002679 spin_lock_bh(&orig_node->tt_lock);
2680
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002681 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2682 change_offset *= ntohs(tt_data->num_vlan);
2683 change_offset += sizeof(*tt_data);
2684 tvlv_ptr += change_offset;
2685
2686 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002687 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002688 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2689 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002690 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002691 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2692 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002693 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002694
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002695 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002696 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002697
2698 spin_unlock_bh(&orig_node->tt_lock);
2699
Antonio Quartullia73105b2011-04-27 14:27:44 +02002700 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002701 spin_lock_bh(&bat_priv->tt.req_list_lock);
2702 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002703 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002704 continue;
2705 list_del(&node->list);
2706 kfree(node);
2707 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002708
Sven Eckelmann807736f2012-07-15 22:26:51 +02002709 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710out:
2711 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002712 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002713}
2714
Sven Eckelmann56303d32012-06-05 22:31:31 +02002715static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002717 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002718
Sven Eckelmann807736f2012-07-15 22:26:51 +02002719 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002720
Sven Eckelmann807736f2012-07-15 22:26:51 +02002721 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002722 list_del(&node->list);
2723 kfree(node);
2724 }
2725
Sven Eckelmann807736f2012-07-15 22:26:51 +02002726 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002727}
2728
Sven Eckelmann56303d32012-06-05 22:31:31 +02002729static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002730{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002731 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002732
Sven Eckelmann807736f2012-07-15 22:26:51 +02002733 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2734 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002735 if (!batadv_has_timed_out(node->first_time,
2736 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002737 continue;
2738
2739 list_del(&node->list);
2740 kfree(node);
2741 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002742 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002743}
2744
2745/* This function checks whether the client already reached the
2746 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2747 * will not be sent.
2748 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002749 * returns true if the ROAMING_ADV can be sent, false otherwise
2750 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002751static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002752 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002753{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002754 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002755 bool ret = false;
2756
Sven Eckelmann807736f2012-07-15 22:26:51 +02002757 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002758 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002759 * reply from the same orig_node yet
2760 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002761 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002762 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002763 continue;
2764
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002765 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002766 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002767 continue;
2768
Sven Eckelmann3e348192012-05-16 20:23:22 +02002769 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002770 /* Sorry, you roamed too many times! */
2771 goto unlock;
2772 ret = true;
2773 break;
2774 }
2775
2776 if (!ret) {
2777 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2778 if (!tt_roam_node)
2779 goto unlock;
2780
2781 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002782 atomic_set(&tt_roam_node->counter,
2783 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002784 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2785
Sven Eckelmann807736f2012-07-15 22:26:51 +02002786 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002787 ret = true;
2788 }
2789
2790unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002791 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002792 return ret;
2793}
2794
Antonio Quartullic018ad32013-06-04 12:11:39 +02002795/**
2796 * batadv_send_roam_adv - send a roaming advertisement message
2797 * @bat_priv: the bat priv with all the soft interface information
2798 * @client: mac address of the roaming client
2799 * @vid: VLAN identifier
2800 * @orig_node: message destination
2801 *
2802 * Send a ROAMING_ADV message to the node which was previously serving this
2803 * client. This is done to inform the node that from now on all traffic destined
2804 * for this particular roamed client has to be forwarded to the sender of the
2805 * roaming message.
2806 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002807static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002808 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002809 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002810{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002811 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002812 struct batadv_tvlv_roam_adv tvlv_roam;
2813
2814 primary_if = batadv_primary_if_get_selected(bat_priv);
2815 if (!primary_if)
2816 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002817
2818 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002819 * already roamed to us too many times
2820 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002821 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002822 goto out;
2823
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002824 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002825 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2826 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002827
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002828 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002829
Marek Lindner122edaa2013-04-23 21:40:03 +08002830 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002831 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002832
2833 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2834 orig_node->orig, BATADV_TVLV_ROAM, 1,
2835 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002836
2837out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002838 if (primary_if)
2839 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840}
2841
Sven Eckelmanna5130882012-05-16 20:23:16 +02002842static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002844 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002845 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002846 struct batadv_priv *bat_priv;
2847
2848 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002849 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2850 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002851
Sven Eckelmanna5130882012-05-16 20:23:16 +02002852 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002853 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002854 batadv_tt_req_purge(bat_priv);
2855 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002856
Antonio Quartulli72414442012-12-25 13:14:37 +01002857 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2858 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002859}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002860
Sven Eckelmann56303d32012-06-05 22:31:31 +02002861void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002862{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002863 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2864 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2865
Sven Eckelmann807736f2012-07-15 22:26:51 +02002866 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002867
Sven Eckelmanna5130882012-05-16 20:23:16 +02002868 batadv_tt_local_table_free(bat_priv);
2869 batadv_tt_global_table_free(bat_priv);
2870 batadv_tt_req_list_free(bat_priv);
2871 batadv_tt_changes_list_free(bat_priv);
2872 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002873
Sven Eckelmann807736f2012-07-15 22:26:51 +02002874 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002875}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002876
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002877/**
2878 * batadv_tt_local_set_flags - set or unset the specified flags on the local
2879 * table and possibly count them in the TT size
2880 * @bat_priv: the bat priv with all the soft interface information
2881 * @flags: the flag to switch
2882 * @enable: whether to set or unset the flag
2883 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002884 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002885static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2886 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002887{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002888 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2889 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002890 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002891 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002892 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002893
2894 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002895 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002896
2897 for (i = 0; i < hash->size; i++) {
2898 head = &hash->table[i];
2899
2900 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002901 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002902 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002903 if (enable) {
2904 if ((tt_common_entry->flags & flags) == flags)
2905 continue;
2906 tt_common_entry->flags |= flags;
2907 } else {
2908 if (!(tt_common_entry->flags & flags))
2909 continue;
2910 tt_common_entry->flags &= ~flags;
2911 }
2912 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002913
2914 if (!count)
2915 continue;
2916
2917 batadv_tt_local_size_inc(bat_priv,
2918 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002919 }
2920 rcu_read_unlock();
2921 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002922}
2923
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002924/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002925static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002926{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002927 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002928 struct batadv_tt_common_entry *tt_common;
2929 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002930 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002931 struct hlist_head *head;
2932 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002933 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002934
2935 if (!hash)
2936 return;
2937
2938 for (i = 0; i < hash->size; i++) {
2939 head = &hash->table[i];
2940 list_lock = &hash->list_locks[i];
2941
2942 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002943 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002944 hash_entry) {
2945 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002946 continue;
2947
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002948 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002949 "Deleting local tt entry (%pM, vid: %d): pending\n",
2950 tt_common->addr,
2951 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002952
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002953 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002954 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002955 tt_local = container_of(tt_common,
2956 struct batadv_tt_local_entry,
2957 common);
2958 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002959 }
2960 spin_unlock_bh(list_lock);
2961 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002962}
2963
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002964/**
2965 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2966 * have been queued in the time since the last commit
2967 * @bat_priv: the bat priv with all the soft interface information
2968 */
2969void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002970{
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002971 spin_lock_bh(&bat_priv->tt.commit_lock);
2972
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002973 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2974 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2975 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002976 goto out;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002977 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002978
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002979 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002980
Sven Eckelmanna5130882012-05-16 20:23:16 +02002981 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002982 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002983
2984 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002985 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002986 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002987 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002988 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002989
2990 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002991 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002992 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002993
2994out:
2995 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002996}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002997
Sven Eckelmann56303d32012-06-05 22:31:31 +02002998bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002999 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003000{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003001 struct batadv_tt_local_entry *tt_local_entry = NULL;
3002 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003003 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003004 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003005
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003006 vlan = batadv_softif_vlan_get(bat_priv, vid);
3007 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003008 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003009
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003010 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003011 if (!tt_local_entry)
3012 goto out;
3013
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003014 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003015 if (!tt_global_entry)
3016 goto out;
3017
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003018 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003019 goto out;
3020
Marek Lindner5870adc2012-06-20 17:16:05 +02003021 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003022
3023out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003024 if (vlan)
3025 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003026 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003027 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003028 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003029 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003030 return ret;
3031}
Marek Lindnera943cac2011-07-30 13:10:18 +02003032
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003033/**
3034 * batadv_tt_update_orig - update global translation table with new tt
3035 * information received via ogms
3036 * @bat_priv: the bat priv with all the soft interface information
3037 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003038 * @tt_vlan: pointer to the first tvlv VLAN entry
3039 * @tt_num_vlan: number of tvlv VLAN entries
3040 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003041 * @tt_num_changes: number of tt changes inside the tt buffer
3042 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003043 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003044 */
3045static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3046 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003047 const void *tt_buff, uint16_t tt_num_vlan,
3048 struct batadv_tvlv_tt_change *tt_change,
3049 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003050{
3051 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003052 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003053 bool full_table = true;
3054
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01003055 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02003056 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01003057 return;
3058
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003059 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003060 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003061 * increased by one -> we can apply the attached changes
3062 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003063 if ((!orig_node->tt_initialised && ttvn == 1) ||
3064 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003065 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003066 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3067 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003068 * In this case send a tt request
3069 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003070 if (!tt_num_changes) {
3071 full_table = false;
3072 goto request_table;
3073 }
3074
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003075 spin_lock_bh(&orig_node->tt_lock);
3076
Marek Lindner335fbe02013-04-23 21:40:02 +08003077 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003078 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003079 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003080
3081 /* Even if we received the precomputed crc with the OGM, we
3082 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003083 * in the global table
3084 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003085 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003086
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003087 spin_unlock_bh(&orig_node->tt_lock);
3088
Marek Lindnera943cac2011-07-30 13:10:18 +02003089 /* The ttvn alone is not enough to guarantee consistency
3090 * because a single value could represent different states
3091 * (due to the wrap around). Thus a node has to check whether
3092 * the resulting table (after applying the changes) is still
3093 * consistent or not. E.g. a node could disconnect while its
3094 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3095 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003096 * inconsistency
3097 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003098 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3099 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003100 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003101 } else {
3102 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003103 * in sync anymore -> request fresh tt data
3104 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003105 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003106 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3107 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003108request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003109 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003110 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3111 orig_node->orig, ttvn, orig_ttvn,
3112 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003113 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003114 tt_vlan, tt_num_vlan,
3115 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003116 return;
3117 }
3118 }
3119}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003120
Antonio Quartullic018ad32013-06-04 12:11:39 +02003121/**
3122 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3123 * @bat_priv: the bat priv with all the soft interface information
3124 * @addr: the mac address of the client to check
3125 * @vid: VLAN identifier
3126 *
3127 * Returns true if we know that the client has moved from its old originator
3128 * to another one. This entry is still kept for consistency purposes and will be
3129 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003130 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003131bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003132 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003133{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003134 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003135 bool ret = false;
3136
Antonio Quartullic018ad32013-06-04 12:11:39 +02003137 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003138 if (!tt_global_entry)
3139 goto out;
3140
Antonio Quartullic1d07432013-01-15 22:17:19 +10003141 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003142 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003143out:
3144 return ret;
3145}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003146
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003147/**
3148 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3149 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003150 * @addr: the mac address of the local client to query
3151 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003152 *
3153 * Returns true if the local client is known to be roaming (it is not served by
3154 * this node anymore) or not. If yes, the client is still present in the table
3155 * to keep the latter consistent with the node TTVN
3156 */
3157bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003158 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003159{
3160 struct batadv_tt_local_entry *tt_local_entry;
3161 bool ret = false;
3162
Antonio Quartullic018ad32013-06-04 12:11:39 +02003163 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003164 if (!tt_local_entry)
3165 goto out;
3166
3167 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3168 batadv_tt_local_entry_free_ref(tt_local_entry);
3169out:
3170 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003171}
3172
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003173bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3174 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003175 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003176 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003177{
3178 bool ret = false;
3179
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01003180 /* if the originator is a backbone node (meaning it belongs to the same
3181 * LAN of this node) the temporary client must not be added because to
3182 * reach such destination the node must use the LAN instead of the mesh
3183 */
3184 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
3185 goto out;
3186
Antonio Quartulli16052782013-06-04 12:11:41 +02003187 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003188 BATADV_TT_CLIENT_TEMP,
3189 atomic_read(&orig_node->last_ttvn)))
3190 goto out;
3191
3192 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003193 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3194 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003195 ret = true;
3196out:
3197 return ret;
3198}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003199
3200/**
3201 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3202 * @bat_priv: the bat priv with all the soft interface information
3203 * @orig: the orig_node of the ogm
3204 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3205 * @tvlv_value: tvlv buffer containing the gateway data
3206 * @tvlv_value_len: tvlv buffer length
3207 */
3208static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3209 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003210 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003211 uint16_t tvlv_value_len)
3212{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003213 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3214 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003215 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003216 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003217
3218 if (tvlv_value_len < sizeof(*tt_data))
3219 return;
3220
3221 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3222 tvlv_value_len -= sizeof(*tt_data);
3223
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003224 num_vlan = ntohs(tt_data->num_vlan);
3225
3226 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3227 return;
3228
3229 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3230 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3231 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3232
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003233 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003234
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003235 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3236 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003237}
3238
3239/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003240 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3241 * container
3242 * @bat_priv: the bat priv with all the soft interface information
3243 * @src: mac address of tt tvlv sender
3244 * @dst: mac address of tt tvlv recipient
3245 * @tvlv_value: tvlv buffer containing the tt data
3246 * @tvlv_value_len: tvlv buffer length
3247 *
3248 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3249 * otherwise.
3250 */
3251static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3252 uint8_t *src, uint8_t *dst,
3253 void *tvlv_value,
3254 uint16_t tvlv_value_len)
3255{
3256 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003257 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003258 char tt_flag;
3259 bool ret;
3260
3261 if (tvlv_value_len < sizeof(*tt_data))
3262 return NET_RX_SUCCESS;
3263
3264 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3265 tvlv_value_len -= sizeof(*tt_data);
3266
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003267 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3268 tt_vlan_len *= ntohs(tt_data->num_vlan);
3269
3270 if (tvlv_value_len < tt_vlan_len)
3271 return NET_RX_SUCCESS;
3272
3273 tvlv_value_len -= tt_vlan_len;
3274 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003275
3276 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3277 case BATADV_TT_REQUEST:
3278 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3279
3280 /* If this node cannot provide a TT response the tt_request is
3281 * forwarded
3282 */
3283 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3284 if (!ret) {
3285 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3286 tt_flag = 'F';
3287 else
3288 tt_flag = '.';
3289
3290 batadv_dbg(BATADV_DBG_TT, bat_priv,
3291 "Routing TT_REQUEST to %pM [%c]\n",
3292 dst, tt_flag);
3293 /* tvlv API will re-route the packet */
3294 return NET_RX_DROP;
3295 }
3296 break;
3297 case BATADV_TT_RESPONSE:
3298 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3299
3300 if (batadv_is_my_mac(bat_priv, dst)) {
3301 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003302 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003303 return NET_RX_SUCCESS;
3304 }
3305
3306 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3307 tt_flag = 'F';
3308 else
3309 tt_flag = '.';
3310
3311 batadv_dbg(BATADV_DBG_TT, bat_priv,
3312 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3313
3314 /* tvlv API will re-route the packet */
3315 return NET_RX_DROP;
3316 }
3317
3318 return NET_RX_SUCCESS;
3319}
3320
3321/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003322 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3323 * @bat_priv: the bat priv with all the soft interface information
3324 * @src: mac address of tt tvlv sender
3325 * @dst: mac address of tt tvlv recipient
3326 * @tvlv_value: tvlv buffer containing the tt data
3327 * @tvlv_value_len: tvlv buffer length
3328 *
3329 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3330 * otherwise.
3331 */
3332static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3333 uint8_t *src, uint8_t *dst,
3334 void *tvlv_value,
3335 uint16_t tvlv_value_len)
3336{
3337 struct batadv_tvlv_roam_adv *roaming_adv;
3338 struct batadv_orig_node *orig_node = NULL;
3339
3340 /* If this node is not the intended recipient of the
3341 * roaming advertisement the packet is forwarded
3342 * (the tvlv API will re-route the packet).
3343 */
3344 if (!batadv_is_my_mac(bat_priv, dst))
3345 return NET_RX_DROP;
3346
3347 /* check if it is a backbone gateway. we don't accept
3348 * roaming advertisement from it, as it has the same
3349 * entries as we have.
3350 */
3351 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
3352 goto out;
3353
3354 if (tvlv_value_len < sizeof(*roaming_adv))
3355 goto out;
3356
3357 orig_node = batadv_orig_hash_find(bat_priv, src);
3358 if (!orig_node)
3359 goto out;
3360
3361 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3362 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3363
3364 batadv_dbg(BATADV_DBG_TT, bat_priv,
3365 "Received ROAMING_ADV from %pM (client %pM)\n",
3366 src, roaming_adv->client);
3367
3368 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003369 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003370 atomic_read(&orig_node->last_ttvn) + 1);
3371
3372out:
3373 if (orig_node)
3374 batadv_orig_node_free_ref(orig_node);
3375 return NET_RX_SUCCESS;
3376}
3377
3378/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003379 * batadv_tt_init - initialise the translation table internals
3380 * @bat_priv: the bat priv with all the soft interface information
3381 *
3382 * Return 0 on success or negative error number in case of failure.
3383 */
3384int batadv_tt_init(struct batadv_priv *bat_priv)
3385{
3386 int ret;
3387
3388 ret = batadv_tt_local_init(bat_priv);
3389 if (ret < 0)
3390 return ret;
3391
3392 ret = batadv_tt_global_init(bat_priv);
3393 if (ret < 0)
3394 return ret;
3395
3396 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003397 batadv_tt_tvlv_unicast_handler_v1,
3398 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003399
Marek Lindner122edaa2013-04-23 21:40:03 +08003400 batadv_tvlv_handler_register(bat_priv, NULL,
3401 batadv_roam_tvlv_unicast_handler_v1,
3402 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3403
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003404 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3405 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3406 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3407
3408 return 1;
3409}