blob: 7731eaed737d1f3cbb716d9f4b65cf2bd391fb85 [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 Quartullicfd4f752013-08-07 18:28:56 +02001156 /* ignore global entries from backbone nodes */
1157 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1158 return true;
1159
Antonio Quartullic018ad32013-06-04 12:11:39 +02001160 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1161 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001162
1163 /* if the node already has a local client for this entry, it has to wait
1164 * for a roaming advertisement instead of manually messing up the global
1165 * table
1166 */
1167 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1168 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1169 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001170
Antonio Quartullia73105b2011-04-27 14:27:44 +02001171 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001172 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001174 goto out;
1175
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001176 common = &tt_global_entry->common;
1177 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001178 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001179
Antonio Quartullid4f44692012-05-25 00:00:54 +02001180 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001181 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001182 /* node must store current time in case of roaming. This is
1183 * needed to purge this entry out on timeout (if nobody claims
1184 * it)
1185 */
1186 if (flags & BATADV_TT_CLIENT_ROAM)
1187 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001188 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001189 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001190
1191 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1192 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001193
Sven Eckelmann807736f2012-07-15 22:26:51 +02001194 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001195 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001196 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001197 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001198
1199 if (unlikely(hash_added != 0)) {
1200 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001201 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001202 goto out_remove;
1203 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001204 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001205 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001206 /* If there is already a global entry, we can use this one for
1207 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001208 * But if we are trying to add a temporary client then here are
1209 * two options at this point:
1210 * 1) the global client is not a temporary client: the global
1211 * client has to be left as it is, temporary information
1212 * should never override any already known client state
1213 * 2) the global client is a temporary client: purge the
1214 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001215 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001216 if (flags & BATADV_TT_CLIENT_TEMP) {
1217 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1218 goto out;
1219 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1220 orig_node))
1221 goto out_remove;
1222 batadv_tt_global_del_orig_list(tt_global_entry);
1223 goto add_orig_entry;
1224 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001225
1226 /* if the client was temporary added before receiving the first
1227 * OGM announcing it, we have to clear the TEMP flag
1228 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001229 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001230
Antonio Quartullie9c001362012-11-07 15:05:33 +01001231 /* the change can carry possible "attribute" flags like the
1232 * TT_CLIENT_WIFI, therefore they have to be copied in the
1233 * client entry
1234 */
1235 tt_global_entry->common.flags |= flags;
1236
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001237 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1238 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001239 * delete + roaming change for this originator.
1240 *
1241 * We should first delete the old originator before adding the
1242 * new one.
1243 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001244 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001245 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001246 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001247 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001248 }
1249 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001250add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001251 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001252 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001253
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001254 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001255 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1256 common->addr, BATADV_PRINT_VID(common->vid),
1257 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001258 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001260out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001261
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001263 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001264 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001265 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001266 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1267
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001268 if (!(flags & BATADV_TT_CLIENT_ROAM))
1269 /* this is a normal global add. Therefore the client is not in a
1270 * roaming state anymore.
1271 */
1272 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1273
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001274out:
1275 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001276 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001277 if (tt_local_entry)
1278 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001279 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001280}
1281
Sven Eckelmann981d8902012-10-07 13:34:15 +02001282/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1283 * @tt_global_entry: global translation table entry to be analyzed
1284 *
1285 * This functon assumes the caller holds rcu_read_lock().
1286 * Returns best originator list entry or NULL on errors.
1287 */
1288static struct batadv_tt_orig_list_entry *
1289batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
1290{
1291 struct batadv_neigh_node *router = NULL;
1292 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001293 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1294 int best_tq = 0;
1295
1296 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001297 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001298 router = batadv_orig_node_get_router(orig_entry->orig_node);
1299 if (!router)
1300 continue;
1301
1302 if (router->tq_avg > best_tq) {
1303 best_entry = orig_entry;
1304 best_tq = router->tq_avg;
1305 }
1306
1307 batadv_neigh_node_free_ref(router);
1308 }
1309
1310 return best_entry;
1311}
1312
1313/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1314 * for this global entry
1315 * @tt_global_entry: global translation table entry to be printed
1316 * @seq: debugfs table seq_file struct
1317 *
1318 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001319 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001320static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001322 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001323{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001324 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001325 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001326 struct batadv_orig_node_vlan *vlan;
1327 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001328 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001329 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001330
1331 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001332 flags = tt_common_entry->flags;
1333
1334 best_entry = batadv_transtable_best_orig(tt_global_entry);
1335 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001336 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1337 tt_common_entry->vid);
1338 if (!vlan) {
1339 seq_printf(seq,
1340 " * Cannot retrieve VLAN %d for originator %pM\n",
1341 BATADV_PRINT_VID(tt_common_entry->vid),
1342 best_entry->orig_node->orig);
1343 goto print_list;
1344 }
1345
Sven Eckelmann981d8902012-10-07 13:34:15 +02001346 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001347 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001348 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001349 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001350 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001351 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001352 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001353 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1354 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1355 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001356
1357 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001358 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001359
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001360print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001361 head = &tt_global_entry->orig_list;
1362
Sasha Levinb67bfe02013-02-27 17:06:00 -08001363 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001364 if (best_entry == orig_entry)
1365 continue;
1366
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001367 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1368 tt_common_entry->vid);
1369 if (!vlan) {
1370 seq_printf(seq,
1371 " + Cannot retrieve VLAN %d for originator %pM\n",
1372 BATADV_PRINT_VID(tt_common_entry->vid),
1373 orig_entry->orig_node->orig);
1374 continue;
1375 }
1376
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001377 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001378 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001379 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001380 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001381 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001382 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001383 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001384 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001385 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1386 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001387
1388 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001389 }
1390}
1391
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001392int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001393{
1394 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001395 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001396 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001397 struct batadv_tt_common_entry *tt_common_entry;
1398 struct batadv_tt_global_entry *tt_global;
1399 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001400 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001401 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001402
Marek Lindner30da63a2012-08-03 17:15:46 +02001403 primary_if = batadv_seq_print_text_primary_if_get(seq);
1404 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001405 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001406
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001407 seq_printf(seq,
1408 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001409 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001410 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1411 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1412 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001413
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001414 for (i = 0; i < hash->size; i++) {
1415 head = &hash->table[i];
1416
Marek Lindner7aadf882011-02-18 12:28:09 +00001417 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001418 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001419 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001420 tt_global = container_of(tt_common_entry,
1421 struct batadv_tt_global_entry,
1422 common);
1423 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001424 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001425 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001426 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001427out:
1428 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001429 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001430 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001431}
1432
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001433/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001434static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001435batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001436{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001437 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001438 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001439 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001440
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001441 spin_lock_bh(&tt_global_entry->list_lock);
1442 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001443 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1444 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001445 batadv_tt_global_size_dec(orig_entry->orig_node,
1446 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001447 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001448 }
1449 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001450}
1451
Sven Eckelmanna5130882012-05-16 20:23:16 +02001452static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001453batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1454 struct batadv_tt_global_entry *tt_global_entry,
1455 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001456 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001457{
1458 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001459 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001460 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001461 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001462
1463 spin_lock_bh(&tt_global_entry->list_lock);
1464 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001465 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001466 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001467 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001468 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001469 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001470 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001471 tt_global_entry->common.addr,
1472 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001473 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001474 batadv_tt_global_size_dec(orig_node,
1475 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001476 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001477 }
1478 }
1479 spin_unlock_bh(&tt_global_entry->list_lock);
1480}
1481
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001482/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001483 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1484 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001485 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001486static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001487batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1488 struct batadv_tt_global_entry *tt_global_entry,
1489 struct batadv_orig_node *orig_node,
1490 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001491{
1492 bool last_entry = true;
1493 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001494 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001495
1496 /* no local entry exists, case 1:
1497 * Check if this is the last one or if other entries exist.
1498 */
1499
1500 rcu_read_lock();
1501 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001502 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001503 if (orig_entry->orig_node != orig_node) {
1504 last_entry = false;
1505 break;
1506 }
1507 }
1508 rcu_read_unlock();
1509
1510 if (last_entry) {
1511 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001512 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001513 tt_global_entry->roam_at = jiffies;
1514 } else
1515 /* there is another entry, we can simply delete this
1516 * one and can still use the other one.
1517 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001518 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1519 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001520}
1521
Antonio Quartullic018ad32013-06-04 12:11:39 +02001522/**
1523 * batadv_tt_global_del - remove a client from the global table
1524 * @bat_priv: the bat priv with all the soft interface information
1525 * @orig_node: an originator serving this client
1526 * @addr: the mac address of the client
1527 * @vid: VLAN identifier
1528 * @message: a message explaining the reason for deleting the client to print
1529 * for debugging purpose
1530 * @roaming: true if the deletion has been triggered by a roaming event
1531 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001532static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1533 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001534 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001535 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001536{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001537 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001538 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001539
Antonio Quartullic018ad32013-06-04 12:11:39 +02001540 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001541 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001542 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001543
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001544 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001545 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1546 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001547
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001548 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001549 batadv_tt_global_free(bat_priv, tt_global_entry,
1550 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001551
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001552 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001554
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001555 /* if we are deleting a global entry due to a roam
1556 * event, there are two possibilities:
1557 * 1) the client roamed from node A to node B => if there
1558 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001559 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001560 * wait for node B to claim it. In case of timeout
1561 * the entry is purged.
1562 *
1563 * If there are other originators left, we directly delete
1564 * the originator.
1565 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001566 * the global entry, since it is useless now.
1567 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001568 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001569 tt_global_entry->common.addr,
1570 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001571 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001572 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001573 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001574 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001575 } else
1576 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001577 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1578 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001579
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001580
Antonio Quartullicc47f662011-04-27 14:27:57 +02001581out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001582 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001583 batadv_tt_global_entry_free_ref(tt_global_entry);
1584 if (local_entry)
1585 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586}
1587
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001588/**
1589 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1590 * given originator matching the provided vid
1591 * @bat_priv: the bat priv with all the soft interface information
1592 * @orig_node: the originator owning the entries to remove
1593 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1594 * removed
1595 * @message: debug message to print as "reason"
1596 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001597void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1598 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001599 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001600 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001601{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001602 struct batadv_tt_global_entry *tt_global;
1603 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001604 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001605 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001606 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001608 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001609 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610
Simon Wunderlich6e801492011-10-19 10:28:26 +02001611 if (!hash)
1612 return;
1613
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614 for (i = 0; i < hash->size; i++) {
1615 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001616 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001617
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001618 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001619 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001620 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001621 /* remove only matching entries */
1622 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1623 continue;
1624
Sven Eckelmann56303d32012-06-05 22:31:31 +02001625 tt_global = container_of(tt_common_entry,
1626 struct batadv_tt_global_entry,
1627 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001628
Sven Eckelmann56303d32012-06-05 22:31:31 +02001629 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001630 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001631
Sven Eckelmann56303d32012-06-05 22:31:31 +02001632 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001633 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001634 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001635 "Deleting global tt entry %pM (vid: %d): %s\n",
1636 tt_global->common.addr,
1637 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001638 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001639 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001640 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001642 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001643 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001644 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001645}
1646
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001647static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1648 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001649{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001650 bool purge = false;
1651 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1652 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001653
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001654 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1655 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1656 purge = true;
1657 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001658 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001659
1660 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1661 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1662 purge = true;
1663 *msg = "Temporary client timeout\n";
1664 }
1665
1666 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001667}
1668
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001669static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001670{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001671 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001672 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001673 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001674 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001675 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001676 char *msg = NULL;
1677 struct batadv_tt_common_entry *tt_common;
1678 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001679
Antonio Quartullicc47f662011-04-27 14:27:57 +02001680 for (i = 0; i < hash->size; i++) {
1681 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001682 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001683
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001684 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001685 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001686 hash_entry) {
1687 tt_global = container_of(tt_common,
1688 struct batadv_tt_global_entry,
1689 common);
1690
1691 if (!batadv_tt_global_to_purge(tt_global, &msg))
1692 continue;
1693
1694 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001695 "Deleting global tt entry %pM (vid: %d): %s\n",
1696 tt_global->common.addr,
1697 BATADV_PRINT_VID(tt_global->common.vid),
1698 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001699
Sasha Levinb67bfe02013-02-27 17:06:00 -08001700 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001701
1702 batadv_tt_global_entry_free_ref(tt_global);
1703 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001704 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001705 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001706}
1707
Sven Eckelmann56303d32012-06-05 22:31:31 +02001708static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001709{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001710 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001711 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001712 struct batadv_tt_common_entry *tt_common_entry;
1713 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001714 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001715 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001716 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001717
Sven Eckelmann807736f2012-07-15 22:26:51 +02001718 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001719 return;
1720
Sven Eckelmann807736f2012-07-15 22:26:51 +02001721 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001722
1723 for (i = 0; i < hash->size; i++) {
1724 head = &hash->table[i];
1725 list_lock = &hash->list_locks[i];
1726
1727 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001728 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001729 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001730 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001731 tt_global = container_of(tt_common_entry,
1732 struct batadv_tt_global_entry,
1733 common);
1734 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001735 }
1736 spin_unlock_bh(list_lock);
1737 }
1738
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001739 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001740
Sven Eckelmann807736f2012-07-15 22:26:51 +02001741 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001742}
1743
Sven Eckelmann56303d32012-06-05 22:31:31 +02001744static bool
1745_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1746 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001747{
1748 bool ret = false;
1749
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001750 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1751 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001752 ret = true;
1753
1754 return ret;
1755}
1756
Antonio Quartullic018ad32013-06-04 12:11:39 +02001757/**
1758 * batadv_transtable_search - get the mesh destination for a given client
1759 * @bat_priv: the bat priv with all the soft interface information
1760 * @src: mac address of the source client
1761 * @addr: mac address of the destination client
1762 * @vid: VLAN identifier
1763 *
1764 * Returns a pointer to the originator that was selected as destination in the
1765 * mesh for contacting the client 'addr', NULL otherwise.
1766 * In case of multiple originators serving the same client, the function returns
1767 * the best one (best in terms of metric towards the destination node).
1768 *
1769 * If the two clients are AP isolated the function returns NULL.
1770 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001771struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1772 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001773 const uint8_t *addr,
1774 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001775{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001776 struct batadv_tt_local_entry *tt_local_entry = NULL;
1777 struct batadv_tt_global_entry *tt_global_entry = NULL;
1778 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001779 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001780 bool ap_isolation_enabled = false;
1781 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001782
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001783 /* if the AP isolation is requested on a VLAN, then check for its
1784 * setting in the proper VLAN private data structure
1785 */
1786 vlan = batadv_softif_vlan_get(bat_priv, vid);
1787 if (vlan) {
1788 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1789 batadv_softif_vlan_free_ref(vlan);
1790 }
1791
1792 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001793 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001794 if (!tt_local_entry ||
1795 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001796 goto out;
1797 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001798
Antonio Quartullic018ad32013-06-04 12:11:39 +02001799 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001800 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001801 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001802
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001803 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001804 * isolation
1805 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001806 if (tt_local_entry &&
1807 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001808 goto out;
1809
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001810 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001811 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001812 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001813 if (best_entry)
1814 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001815 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1816 orig_node = NULL;
1817 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001818
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001819out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001820 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001821 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001822 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001823 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001824
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001825 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001826}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827
Antonio Quartulliced72932013-04-24 16:37:51 +02001828/**
1829 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1830 * to the given orig_node
1831 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001832 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001833 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001834 *
1835 * This function computes the checksum for the global table corresponding to a
1836 * specific originator. In particular, the checksum is computed as follows: For
1837 * each client connected to the originator the CRC32C of the MAC address and the
1838 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1839 * together.
1840 *
1841 * The idea behind is that CRC32C should be used as much as possible in order to
1842 * produce a unique hash of the table, but since the order which is used to feed
1843 * the CRC32C function affects the result and since every node in the network
1844 * probably sorts the clients differently, the hash function cannot be directly
1845 * computed over the entire table. Hence the CRC32C is used only on
1846 * the single client entry, while all the results are then xor'ed together
1847 * because the XOR operation can combine them all while trying to reduce the
1848 * noise as much as possible.
1849 *
1850 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001851 */
1852static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001853 struct batadv_orig_node *orig_node,
1854 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001856 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001857 struct batadv_tt_common_entry *tt_common;
1858 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001860 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861
1862 for (i = 0; i < hash->size; i++) {
1863 head = &hash->table[i];
1864
1865 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001866 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001867 tt_global = container_of(tt_common,
1868 struct batadv_tt_global_entry,
1869 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001870 /* compute the CRC only for entries belonging to the
1871 * VLAN identified by the vid passed as parameter
1872 */
1873 if (tt_common->vid != vid)
1874 continue;
1875
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001876 /* Roaming clients are in the global table for
1877 * consistency only. They don't have to be
1878 * taken into account while computing the
1879 * global crc
1880 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001881 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001882 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001883 /* Temporary clients have not been announced yet, so
1884 * they have to be skipped while computing the global
1885 * crc
1886 */
1887 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1888 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001889
1890 /* find out if this global entry is announced by this
1891 * originator
1892 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001893 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001894 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001895 continue;
1896
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001897 crc_tmp = crc32c(0, &tt_common->vid,
1898 sizeof(tt_common->vid));
1899 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001900 }
1901 rcu_read_unlock();
1902 }
1903
Antonio Quartulliced72932013-04-24 16:37:51 +02001904 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905}
1906
Antonio Quartulliced72932013-04-24 16:37:51 +02001907/**
1908 * batadv_tt_local_crc - calculates the checksum of the local table
1909 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001910 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001911 *
1912 * For details about the computation, please refer to the documentation for
1913 * batadv_tt_global_crc().
1914 *
1915 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001916 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001917static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
1918 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001919{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001920 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001921 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001923 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001924
1925 for (i = 0; i < hash->size; i++) {
1926 head = &hash->table[i];
1927
1928 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001929 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001930 /* compute the CRC only for entries belonging to the
1931 * VLAN identified by vid
1932 */
1933 if (tt_common->vid != vid)
1934 continue;
1935
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001936 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001937 * account while computing the CRC
1938 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001939 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001940 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001941
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001942 crc_tmp = crc32c(0, &tt_common->vid,
1943 sizeof(tt_common->vid));
1944 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001945 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001946 rcu_read_unlock();
1947 }
1948
Antonio Quartulliced72932013-04-24 16:37:51 +02001949 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001950}
1951
Sven Eckelmann56303d32012-06-05 22:31:31 +02001952static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001954 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955
Sven Eckelmann807736f2012-07-15 22:26:51 +02001956 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001957
Sven Eckelmann807736f2012-07-15 22:26:51 +02001958 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959 list_del(&node->list);
1960 kfree(node);
1961 }
1962
Sven Eckelmann807736f2012-07-15 22:26:51 +02001963 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964}
1965
Sven Eckelmann56303d32012-06-05 22:31:31 +02001966static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1967 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001968 const void *tt_buff,
1969 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001971 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001972 * last OGM (the OGM could carry no changes)
1973 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001974 spin_lock_bh(&orig_node->tt_buff_lock);
1975 if (tt_buff_len > 0) {
1976 kfree(orig_node->tt_buff);
1977 orig_node->tt_buff_len = 0;
1978 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1979 if (orig_node->tt_buff) {
1980 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1981 orig_node->tt_buff_len = tt_buff_len;
1982 }
1983 }
1984 spin_unlock_bh(&orig_node->tt_buff_lock);
1985}
1986
Sven Eckelmann56303d32012-06-05 22:31:31 +02001987static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001988{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001989 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001990
Sven Eckelmann807736f2012-07-15 22:26:51 +02001991 spin_lock_bh(&bat_priv->tt.req_list_lock);
1992 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001993 if (batadv_has_timed_out(node->issued_at,
1994 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001995 list_del(&node->list);
1996 kfree(node);
1997 }
1998 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001999 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002000}
2001
2002/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002003 * has already been issued for this orig_node, NULL otherwise
2004 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002005static struct batadv_tt_req_node *
2006batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2007 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002008{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002009 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002010
Sven Eckelmann807736f2012-07-15 22:26:51 +02002011 spin_lock_bh(&bat_priv->tt.req_list_lock);
2012 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002013 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2014 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002015 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002016 goto unlock;
2017 }
2018
2019 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2020 if (!tt_req_node)
2021 goto unlock;
2022
2023 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2024 tt_req_node->issued_at = jiffies;
2025
Sven Eckelmann807736f2012-07-15 22:26:51 +02002026 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002027unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002028 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002029 return tt_req_node;
2030}
2031
Marek Lindner335fbe02013-04-23 21:40:02 +08002032/**
2033 * batadv_tt_local_valid - verify that given tt entry is a valid one
2034 * @entry_ptr: to be checked local tt entry
2035 * @data_ptr: not used but definition required to satisfy the callback prototype
2036 *
2037 * Returns 1 if the entry is a valid, 0 otherwise.
2038 */
2039static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002040{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002041 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002042
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002043 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002044 return 0;
2045 return 1;
2046}
2047
Sven Eckelmanna5130882012-05-16 20:23:16 +02002048static int batadv_tt_global_valid(const void *entry_ptr,
2049 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002051 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2052 const struct batadv_tt_global_entry *tt_global_entry;
2053 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002054
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002055 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2056 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002057 return 0;
2058
Sven Eckelmann56303d32012-06-05 22:31:31 +02002059 tt_global_entry = container_of(tt_common_entry,
2060 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002061 common);
2062
Sven Eckelmanna5130882012-05-16 20:23:16 +02002063 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002064}
2065
Marek Lindner335fbe02013-04-23 21:40:02 +08002066/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002067 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2068 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002069 * @bat_priv: the bat priv with all the soft interface information
2070 * @hash: hash table containing the tt entries
2071 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002072 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002073 * @valid_cb: function to filter tt change entries
2074 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002075 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002076static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2077 struct batadv_hashtable *hash,
2078 void *tvlv_buff, uint16_t tt_len,
2079 int (*valid_cb)(const void *, const void *),
2080 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002081{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002082 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002083 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002084 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002085 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002086 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002088 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002089 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002090
2091 rcu_read_lock();
2092 for (i = 0; i < hash->size; i++) {
2093 head = &hash->table[i];
2094
Sasha Levinb67bfe02013-02-27 17:06:00 -08002095 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002097 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002098 break;
2099
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002100 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101 continue;
2102
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002103 memcpy(tt_change->addr, tt_common_entry->addr,
2104 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002105 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002106 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08002107 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002108
Marek Lindner335fbe02013-04-23 21:40:02 +08002109 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002110 tt_change++;
2111 }
2112 }
2113 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002114}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002115
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002116/**
2117 * batadv_tt_global_check_crc - check if all the CRCs are correct
2118 * @orig_node: originator for which the CRCs have to be checked
2119 * @tt_vlan: pointer to the first tvlv VLAN entry
2120 * @num_vlan: number of tvlv VLAN entries
2121 * @create: if true, create VLAN objects if not found
2122 *
2123 * Return true if all the received CRCs match the locally stored ones, false
2124 * otherwise
2125 */
2126static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2127 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2128 uint16_t num_vlan)
2129{
2130 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2131 struct batadv_orig_node_vlan *vlan;
2132 int i;
2133
2134 /* check if each received CRC matches the locally stored one */
2135 for (i = 0; i < num_vlan; i++) {
2136 tt_vlan_tmp = tt_vlan + i;
2137
2138 /* if orig_node is a backbone node for this VLAN, don't check
2139 * the CRC as we ignore all the global entries over it
2140 */
2141 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002142 orig_node->orig,
2143 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002144 continue;
2145
2146 vlan = batadv_orig_node_vlan_get(orig_node,
2147 ntohs(tt_vlan_tmp->vid));
2148 if (!vlan)
2149 return false;
2150
2151 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2152 return false;
2153 }
2154
2155 return true;
2156}
2157
2158/**
2159 * batadv_tt_local_update_crc - update all the local CRCs
2160 * @bat_priv: the bat priv with all the soft interface information
2161 */
2162static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2163{
2164 struct batadv_softif_vlan *vlan;
2165
2166 /* recompute the global CRC for each VLAN */
2167 rcu_read_lock();
2168 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2169 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2170 }
2171 rcu_read_unlock();
2172}
2173
2174/**
2175 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2176 * @bat_priv: the bat priv with all the soft interface information
2177 * @orig_node: the orig_node for which the CRCs have to be updated
2178 */
2179static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2180 struct batadv_orig_node *orig_node)
2181{
2182 struct batadv_orig_node_vlan *vlan;
2183 uint32_t crc;
2184
2185 /* recompute the global CRC for each VLAN */
2186 rcu_read_lock();
2187 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2188 /* if orig_node is a backbone node for this VLAN, don't compute
2189 * the CRC as we ignore all the global entries over it
2190 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002191 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2192 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002193 continue;
2194
2195 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2196 vlan->tt.crc = crc;
2197 }
2198 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002199}
2200
Antonio Quartulliced72932013-04-24 16:37:51 +02002201/**
2202 * batadv_send_tt_request - send a TT Request message to a given node
2203 * @bat_priv: the bat priv with all the soft interface information
2204 * @dst_orig_node: the destination of the message
2205 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002206 * @tt_vlan: pointer to the first tvlv VLAN object to request
2207 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002208 * @full_table: ask for the entire translation table if true, while only for the
2209 * last TT diff otherwise
2210 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002211static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2212 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002213 uint8_t ttvn,
2214 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2215 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002216{
Marek Lindner335fbe02013-04-23 21:40:02 +08002217 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002218 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002219 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2220 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002221 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002222 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002223
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002224 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002225 if (!primary_if)
2226 goto out;
2227
2228 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002229 * reply from the same orig_node yet
2230 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002231 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232 if (!tt_req_node)
2233 goto out;
2234
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002235 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2236 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002237 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002238 goto out;
2239
Marek Lindner335fbe02013-04-23 21:40:02 +08002240 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2241 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002242 tvlv_tt_data->num_vlan = htons(num_vlan);
2243
2244 /* send all the CRCs within the request. This is needed by intermediate
2245 * nodes to ensure they have the correct table before replying
2246 */
2247 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2248 for (i = 0; i < num_vlan; i++) {
2249 tt_vlan_req->vid = tt_vlan->vid;
2250 tt_vlan_req->crc = tt_vlan->crc;
2251
2252 tt_vlan_req++;
2253 tt_vlan++;
2254 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002255
2256 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002257 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002258
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002259 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002260 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002261
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002262 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002263 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2264 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002265 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002266 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267
2268out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002269 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002270 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002271 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002272 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002273 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002274 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002275 kfree(tt_req_node);
2276 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002277 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002278 return ret;
2279}
2280
Marek Lindner335fbe02013-04-23 21:40:02 +08002281/**
2282 * batadv_send_other_tt_response - send reply to tt request concerning another
2283 * node's translation table
2284 * @bat_priv: the bat priv with all the soft interface information
2285 * @tt_data: tt data containing the tt request information
2286 * @req_src: mac address of tt request sender
2287 * @req_dst: mac address of tt request recipient
2288 *
2289 * Returns true if tt request reply was sent, false otherwise.
2290 */
2291static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2292 struct batadv_tvlv_tt_data *tt_data,
2293 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002294{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002295 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002296 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002297 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002298 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002299 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002300 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002301 uint8_t orig_ttvn, req_ttvn;
2302 uint16_t tvlv_len;
2303 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002304
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002305 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002306 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002307 req_src, tt_data->ttvn, req_dst,
2308 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002309
2310 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002311 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002312 if (!req_dst_orig_node)
2313 goto out;
2314
Marek Lindner335fbe02013-04-23 21:40:02 +08002315 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002316 if (!res_dst_orig_node)
2317 goto out;
2318
Antonio Quartullia73105b2011-04-27 14:27:44 +02002319 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002320 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002321
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002322 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002323 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002324 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002325 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2326 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002327 goto out;
2328
Antonio Quartulli015758d2011-07-09 17:52:13 +02002329 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002330 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002331 !req_dst_orig_node->tt_buff)
2332 full_table = true;
2333 else
2334 full_table = false;
2335
Marek Lindner335fbe02013-04-23 21:40:02 +08002336 /* TT fragmentation hasn't been implemented yet, so send as many
2337 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002338 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002339 if (!full_table) {
2340 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2341 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002342
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002343 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2344 &tvlv_tt_data,
2345 &tt_change,
2346 &tt_len);
2347 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002348 goto unlock;
2349
Antonio Quartullia73105b2011-04-27 14:27:44 +02002350 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002351 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002352 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002353 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2354 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002355 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2356 * in the initial part
2357 */
2358 tt_len = -1;
2359 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2360 &tvlv_tt_data,
2361 &tt_change,
2362 &tt_len);
2363 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002364 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002365
2366 /* fill the rest of the tvlv with the real TT entries */
2367 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2368 tt_change, tt_len,
2369 batadv_tt_global_valid,
2370 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002371 }
2372
Marek Lindner335fbe02013-04-23 21:40:02 +08002373 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2374 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002375
2376 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002377 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002378
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002379 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002380 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2381 res_dst_orig_node->orig, req_dst_orig_node->orig,
2382 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002383
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002384 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002385
Marek Lindner335fbe02013-04-23 21:40:02 +08002386 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002387 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2388 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002389
Marek Lindner335fbe02013-04-23 21:40:02 +08002390 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002391 goto out;
2392
2393unlock:
2394 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2395
2396out:
2397 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002398 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002399 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002400 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002401 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002402 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002403}
Sven Eckelmann96412692012-06-05 22:31:30 +02002404
Marek Lindner335fbe02013-04-23 21:40:02 +08002405/**
2406 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2407 * translation table
2408 * @bat_priv: the bat priv with all the soft interface information
2409 * @tt_data: tt data containing the tt request information
2410 * @req_src: mac address of tt request sender
2411 *
2412 * Returns true if tt request reply was sent, false otherwise.
2413 */
2414static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2415 struct batadv_tvlv_tt_data *tt_data,
2416 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002417{
Marek Lindner335fbe02013-04-23 21:40:02 +08002418 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002419 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002420 struct batadv_tvlv_tt_change *tt_change;
2421 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002422 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002423 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002424 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002425 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002426
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002427 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002428 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002429 req_src, tt_data->ttvn,
2430 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002431
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002432 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002433
Sven Eckelmann807736f2012-07-15 22:26:51 +02002434 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002435 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002436
Marek Lindner335fbe02013-04-23 21:40:02 +08002437 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002438 if (!orig_node)
2439 goto out;
2440
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002441 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002442 if (!primary_if)
2443 goto out;
2444
2445 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002446 * is too big send the whole local translation table
2447 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002448 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002449 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002450 full_table = true;
2451 else
2452 full_table = false;
2453
Marek Lindner335fbe02013-04-23 21:40:02 +08002454 /* TT fragmentation hasn't been implemented yet, so send as many
2455 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002456 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002457 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002458 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002460 tt_len = bat_priv->tt.last_changeset_len;
2461 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2462 &tvlv_tt_data,
2463 &tt_change,
2464 &tt_len);
2465 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002466 goto unlock;
2467
Marek Lindner335fbe02013-04-23 21:40:02 +08002468 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002469 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002470 bat_priv->tt.last_changeset_len);
2471 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002472 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002473 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002474
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002475 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2476 * in the initial part
2477 */
2478 tt_len = -1;
2479 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2480 &tvlv_tt_data,
2481 &tt_change,
2482 &tt_len);
2483 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002484 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002485
2486 /* fill the rest of the tvlv with the real TT entries */
2487 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2488 tt_change, tt_len,
2489 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002490 }
2491
Marek Lindner335fbe02013-04-23 21:40:02 +08002492 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2493 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002494
2495 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002496 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002497
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002498 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002499 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2500 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002501
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002502 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002503
Marek Lindner335fbe02013-04-23 21:40:02 +08002504 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002505 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2506 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002507
Antonio Quartullia73105b2011-04-27 14:27:44 +02002508 goto out;
2509
2510unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002511 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002512out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002513 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002514 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002515 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002516 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002517 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002518 kfree(tvlv_tt_data);
2519 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002520 return true;
2521}
2522
Marek Lindner335fbe02013-04-23 21:40:02 +08002523/**
2524 * batadv_send_tt_response - send reply to tt request
2525 * @bat_priv: the bat priv with all the soft interface information
2526 * @tt_data: tt data containing the tt request information
2527 * @req_src: mac address of tt request sender
2528 * @req_dst: mac address of tt request recipient
2529 *
2530 * Returns true if tt request reply was sent, false otherwise.
2531 */
2532static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2533 struct batadv_tvlv_tt_data *tt_data,
2534 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002535{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002536 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002537 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002538 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002539 return batadv_send_other_tt_response(bat_priv, tt_data,
2540 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002541}
2542
Sven Eckelmann56303d32012-06-05 22:31:31 +02002543static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2544 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002545 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002546 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002547{
2548 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002549 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002550
2551 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002552 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2553 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002554 batadv_tt_global_del(bat_priv, orig_node,
2555 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002556 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002557 "tt removed by changes",
2558 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002559 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002560 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002561 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002562 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002563 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002564 /* In case of problem while storing a
2565 * global_entry, we stop the updating
2566 * procedure without committing the
2567 * ttvn change. This will avoid to send
2568 * corrupted data on tt_request
2569 */
2570 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002571 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002572 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002573 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002574}
2575
Sven Eckelmann56303d32012-06-05 22:31:31 +02002576static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002577 struct batadv_tvlv_tt_change *tt_change,
2578 uint8_t ttvn, uint8_t *resp_src,
2579 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002580{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002581 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002582
Marek Lindner335fbe02013-04-23 21:40:02 +08002583 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002584 if (!orig_node)
2585 goto out;
2586
2587 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002588 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2589 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002590
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002591 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2592 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002593
2594 spin_lock_bh(&orig_node->tt_buff_lock);
2595 kfree(orig_node->tt_buff);
2596 orig_node->tt_buff_len = 0;
2597 orig_node->tt_buff = NULL;
2598 spin_unlock_bh(&orig_node->tt_buff_lock);
2599
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002600 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002601
2602out:
2603 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002604 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002605}
2606
Sven Eckelmann56303d32012-06-05 22:31:31 +02002607static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2608 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002609 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002610 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002611{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002612 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2613 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002614
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002615 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2616 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002617 atomic_set(&orig_node->last_ttvn, ttvn);
2618}
2619
Antonio Quartullic018ad32013-06-04 12:11:39 +02002620/**
2621 * batadv_is_my_client - check if a client is served by the local node
2622 * @bat_priv: the bat priv with all the soft interface information
2623 * @addr: the mac adress of the client to check
2624 * @vid: VLAN identifier
2625 *
2626 * Returns true if the client is served by this node, false otherwise.
2627 */
2628bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2629 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002630{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002631 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002632 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002633
Antonio Quartullic018ad32013-06-04 12:11:39 +02002634 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002635 if (!tt_local_entry)
2636 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002637 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002638 * consistency purpose)
2639 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002640 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2641 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002642 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002643 ret = true;
2644out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002645 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002646 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002647 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002648}
2649
Marek Lindner335fbe02013-04-23 21:40:02 +08002650/**
2651 * batadv_handle_tt_response - process incoming tt reply
2652 * @bat_priv: the bat priv with all the soft interface information
2653 * @tt_data: tt data containing the tt request information
2654 * @resp_src: mac address of tt reply sender
2655 * @num_entries: number of tt change entries appended to the tt data
2656 */
2657static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2658 struct batadv_tvlv_tt_data *tt_data,
2659 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002660{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002661 struct batadv_tt_req_node *node, *safe;
2662 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002664 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2665 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002666
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002667 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002668 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002669 resp_src, tt_data->ttvn, num_entries,
2670 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002671
Marek Lindner335fbe02013-04-23 21:40:02 +08002672 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673 if (!orig_node)
2674 goto out;
2675
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002676 spin_lock_bh(&orig_node->tt_lock);
2677
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002678 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2679 change_offset *= ntohs(tt_data->num_vlan);
2680 change_offset += sizeof(*tt_data);
2681 tvlv_ptr += change_offset;
2682
2683 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002684 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002685 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2686 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002687 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002688 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2689 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002690 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002691
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002692 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002693 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002694
2695 spin_unlock_bh(&orig_node->tt_lock);
2696
Antonio Quartullia73105b2011-04-27 14:27:44 +02002697 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002698 spin_lock_bh(&bat_priv->tt.req_list_lock);
2699 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002700 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002701 continue;
2702 list_del(&node->list);
2703 kfree(node);
2704 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002705
Sven Eckelmann807736f2012-07-15 22:26:51 +02002706 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002707out:
2708 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002709 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710}
2711
Sven Eckelmann56303d32012-06-05 22:31:31 +02002712static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002713{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002714 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002715
Sven Eckelmann807736f2012-07-15 22:26:51 +02002716 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002717
Sven Eckelmann807736f2012-07-15 22:26:51 +02002718 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002719 list_del(&node->list);
2720 kfree(node);
2721 }
2722
Sven Eckelmann807736f2012-07-15 22:26:51 +02002723 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002724}
2725
Sven Eckelmann56303d32012-06-05 22:31:31 +02002726static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002727{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002728 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002729
Sven Eckelmann807736f2012-07-15 22:26:51 +02002730 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2731 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002732 if (!batadv_has_timed_out(node->first_time,
2733 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002734 continue;
2735
2736 list_del(&node->list);
2737 kfree(node);
2738 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002739 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002740}
2741
2742/* This function checks whether the client already reached the
2743 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2744 * will not be sent.
2745 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002746 * returns true if the ROAMING_ADV can be sent, false otherwise
2747 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002748static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002749 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002750{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002751 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002752 bool ret = false;
2753
Sven Eckelmann807736f2012-07-15 22:26:51 +02002754 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002755 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002756 * reply from the same orig_node yet
2757 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002758 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002759 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002760 continue;
2761
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002762 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002763 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002764 continue;
2765
Sven Eckelmann3e348192012-05-16 20:23:22 +02002766 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002767 /* Sorry, you roamed too many times! */
2768 goto unlock;
2769 ret = true;
2770 break;
2771 }
2772
2773 if (!ret) {
2774 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2775 if (!tt_roam_node)
2776 goto unlock;
2777
2778 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002779 atomic_set(&tt_roam_node->counter,
2780 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002781 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2782
Sven Eckelmann807736f2012-07-15 22:26:51 +02002783 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002784 ret = true;
2785 }
2786
2787unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002788 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002789 return ret;
2790}
2791
Antonio Quartullic018ad32013-06-04 12:11:39 +02002792/**
2793 * batadv_send_roam_adv - send a roaming advertisement message
2794 * @bat_priv: the bat priv with all the soft interface information
2795 * @client: mac address of the roaming client
2796 * @vid: VLAN identifier
2797 * @orig_node: message destination
2798 *
2799 * Send a ROAMING_ADV message to the node which was previously serving this
2800 * client. This is done to inform the node that from now on all traffic destined
2801 * for this particular roamed client has to be forwarded to the sender of the
2802 * roaming message.
2803 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002804static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002805 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002806 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002807{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002808 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002809 struct batadv_tvlv_roam_adv tvlv_roam;
2810
2811 primary_if = batadv_primary_if_get_selected(bat_priv);
2812 if (!primary_if)
2813 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002814
2815 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002816 * already roamed to us too many times
2817 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002818 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002819 goto out;
2820
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002821 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002822 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2823 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002824
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002825 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002826
Marek Lindner122edaa2013-04-23 21:40:03 +08002827 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002828 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002829
2830 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2831 orig_node->orig, BATADV_TVLV_ROAM, 1,
2832 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002833
2834out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002835 if (primary_if)
2836 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837}
2838
Sven Eckelmanna5130882012-05-16 20:23:16 +02002839static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002841 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002842 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002843 struct batadv_priv *bat_priv;
2844
2845 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002846 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2847 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002848
Sven Eckelmanna5130882012-05-16 20:23:16 +02002849 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002850 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002851 batadv_tt_req_purge(bat_priv);
2852 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002853
Antonio Quartulli72414442012-12-25 13:14:37 +01002854 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2855 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002856}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002857
Sven Eckelmann56303d32012-06-05 22:31:31 +02002858void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002859{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002860 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2861 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2862
Sven Eckelmann807736f2012-07-15 22:26:51 +02002863 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002864
Sven Eckelmanna5130882012-05-16 20:23:16 +02002865 batadv_tt_local_table_free(bat_priv);
2866 batadv_tt_global_table_free(bat_priv);
2867 batadv_tt_req_list_free(bat_priv);
2868 batadv_tt_changes_list_free(bat_priv);
2869 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002870
Sven Eckelmann807736f2012-07-15 22:26:51 +02002871 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002872}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002873
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002874/**
2875 * batadv_tt_local_set_flags - set or unset the specified flags on the local
2876 * table and possibly count them in the TT size
2877 * @bat_priv: the bat priv with all the soft interface information
2878 * @flags: the flag to switch
2879 * @enable: whether to set or unset the flag
2880 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002881 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002882static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2883 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002884{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002885 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2886 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002887 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002888 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002889 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002890
2891 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002892 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002893
2894 for (i = 0; i < hash->size; i++) {
2895 head = &hash->table[i];
2896
2897 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002898 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002899 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002900 if (enable) {
2901 if ((tt_common_entry->flags & flags) == flags)
2902 continue;
2903 tt_common_entry->flags |= flags;
2904 } else {
2905 if (!(tt_common_entry->flags & flags))
2906 continue;
2907 tt_common_entry->flags &= ~flags;
2908 }
2909 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002910
2911 if (!count)
2912 continue;
2913
2914 batadv_tt_local_size_inc(bat_priv,
2915 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002916 }
2917 rcu_read_unlock();
2918 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002919}
2920
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002921/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002922static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002923{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002924 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002925 struct batadv_tt_common_entry *tt_common;
2926 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002927 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002928 struct hlist_head *head;
2929 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002930 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002931
2932 if (!hash)
2933 return;
2934
2935 for (i = 0; i < hash->size; i++) {
2936 head = &hash->table[i];
2937 list_lock = &hash->list_locks[i];
2938
2939 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002940 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002941 hash_entry) {
2942 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002943 continue;
2944
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002945 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002946 "Deleting local tt entry (%pM, vid: %d): pending\n",
2947 tt_common->addr,
2948 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002949
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002950 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002951 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002952 tt_local = container_of(tt_common,
2953 struct batadv_tt_local_entry,
2954 common);
2955 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002956 }
2957 spin_unlock_bh(list_lock);
2958 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002959}
2960
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002961/**
2962 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2963 * have been queued in the time since the last commit
2964 * @bat_priv: the bat priv with all the soft interface information
2965 */
2966void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002967{
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002968 spin_lock_bh(&bat_priv->tt.commit_lock);
2969
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002970 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2971 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2972 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002973 goto out;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002974 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002975
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002976 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002977
Sven Eckelmanna5130882012-05-16 20:23:16 +02002978 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002979 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002980
2981 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002982 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002983 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002984 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002985 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002986
2987 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002988 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002989 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002990
2991out:
2992 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002993}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002994
Sven Eckelmann56303d32012-06-05 22:31:31 +02002995bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002996 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002997{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002998 struct batadv_tt_local_entry *tt_local_entry = NULL;
2999 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003000 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003001 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003002
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003003 vlan = batadv_softif_vlan_get(bat_priv, vid);
3004 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003005 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003006
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003007 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003008 if (!tt_local_entry)
3009 goto out;
3010
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003011 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003012 if (!tt_global_entry)
3013 goto out;
3014
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003015 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003016 goto out;
3017
Marek Lindner5870adc2012-06-20 17:16:05 +02003018 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003019
3020out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003021 if (vlan)
3022 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003023 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003024 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003025 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003026 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003027 return ret;
3028}
Marek Lindnera943cac2011-07-30 13:10:18 +02003029
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003030/**
3031 * batadv_tt_update_orig - update global translation table with new tt
3032 * information received via ogms
3033 * @bat_priv: the bat priv with all the soft interface information
3034 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003035 * @tt_vlan: pointer to the first tvlv VLAN entry
3036 * @tt_num_vlan: number of tvlv VLAN entries
3037 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003038 * @tt_num_changes: number of tt changes inside the tt buffer
3039 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003040 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003041 */
3042static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3043 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003044 const void *tt_buff, uint16_t tt_num_vlan,
3045 struct batadv_tvlv_tt_change *tt_change,
3046 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003047{
3048 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003049 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003050 bool full_table = true;
3051
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003052 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003053 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003054 * increased by one -> we can apply the attached changes
3055 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003056 if ((!orig_node->tt_initialised && ttvn == 1) ||
3057 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003058 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003059 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3060 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003061 * In this case send a tt request
3062 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003063 if (!tt_num_changes) {
3064 full_table = false;
3065 goto request_table;
3066 }
3067
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003068 spin_lock_bh(&orig_node->tt_lock);
3069
Marek Lindner335fbe02013-04-23 21:40:02 +08003070 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003071 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003072 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003073
3074 /* Even if we received the precomputed crc with the OGM, we
3075 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003076 * in the global table
3077 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003078 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003079
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003080 spin_unlock_bh(&orig_node->tt_lock);
3081
Marek Lindnera943cac2011-07-30 13:10:18 +02003082 /* The ttvn alone is not enough to guarantee consistency
3083 * because a single value could represent different states
3084 * (due to the wrap around). Thus a node has to check whether
3085 * the resulting table (after applying the changes) is still
3086 * consistent or not. E.g. a node could disconnect while its
3087 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3088 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003089 * inconsistency
3090 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003091 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3092 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003093 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003094 } else {
3095 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003096 * in sync anymore -> request fresh tt data
3097 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003098 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003099 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3100 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003101request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003102 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003103 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3104 orig_node->orig, ttvn, orig_ttvn,
3105 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003106 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003107 tt_vlan, tt_num_vlan,
3108 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003109 return;
3110 }
3111 }
3112}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003113
Antonio Quartullic018ad32013-06-04 12:11:39 +02003114/**
3115 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3116 * @bat_priv: the bat priv with all the soft interface information
3117 * @addr: the mac address of the client to check
3118 * @vid: VLAN identifier
3119 *
3120 * Returns true if we know that the client has moved from its old originator
3121 * to another one. This entry is still kept for consistency purposes and will be
3122 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003123 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003124bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003125 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003126{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003127 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003128 bool ret = false;
3129
Antonio Quartullic018ad32013-06-04 12:11:39 +02003130 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003131 if (!tt_global_entry)
3132 goto out;
3133
Antonio Quartullic1d07432013-01-15 22:17:19 +10003134 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003135 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003136out:
3137 return ret;
3138}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003139
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003140/**
3141 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3142 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003143 * @addr: the mac address of the local client to query
3144 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003145 *
3146 * Returns true if the local client is known to be roaming (it is not served by
3147 * this node anymore) or not. If yes, the client is still present in the table
3148 * to keep the latter consistent with the node TTVN
3149 */
3150bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003151 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003152{
3153 struct batadv_tt_local_entry *tt_local_entry;
3154 bool ret = false;
3155
Antonio Quartullic018ad32013-06-04 12:11:39 +02003156 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003157 if (!tt_local_entry)
3158 goto out;
3159
3160 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3161 batadv_tt_local_entry_free_ref(tt_local_entry);
3162out:
3163 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003164}
3165
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003166bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3167 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003168 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003169 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003170{
3171 bool ret = false;
3172
Antonio Quartulli16052782013-06-04 12:11:41 +02003173 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003174 BATADV_TT_CLIENT_TEMP,
3175 atomic_read(&orig_node->last_ttvn)))
3176 goto out;
3177
3178 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003179 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3180 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003181 ret = true;
3182out:
3183 return ret;
3184}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003185
3186/**
3187 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3188 * @bat_priv: the bat priv with all the soft interface information
3189 * @orig: the orig_node of the ogm
3190 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3191 * @tvlv_value: tvlv buffer containing the gateway data
3192 * @tvlv_value_len: tvlv buffer length
3193 */
3194static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3195 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003196 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003197 uint16_t tvlv_value_len)
3198{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003199 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3200 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003201 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003202 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003203
3204 if (tvlv_value_len < sizeof(*tt_data))
3205 return;
3206
3207 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3208 tvlv_value_len -= sizeof(*tt_data);
3209
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003210 num_vlan = ntohs(tt_data->num_vlan);
3211
3212 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3213 return;
3214
3215 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3216 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3217 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3218
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003219 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003220
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003221 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3222 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003223}
3224
3225/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003226 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3227 * container
3228 * @bat_priv: the bat priv with all the soft interface information
3229 * @src: mac address of tt tvlv sender
3230 * @dst: mac address of tt tvlv recipient
3231 * @tvlv_value: tvlv buffer containing the tt data
3232 * @tvlv_value_len: tvlv buffer length
3233 *
3234 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3235 * otherwise.
3236 */
3237static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3238 uint8_t *src, uint8_t *dst,
3239 void *tvlv_value,
3240 uint16_t tvlv_value_len)
3241{
3242 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003243 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003244 char tt_flag;
3245 bool ret;
3246
3247 if (tvlv_value_len < sizeof(*tt_data))
3248 return NET_RX_SUCCESS;
3249
3250 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3251 tvlv_value_len -= sizeof(*tt_data);
3252
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003253 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3254 tt_vlan_len *= ntohs(tt_data->num_vlan);
3255
3256 if (tvlv_value_len < tt_vlan_len)
3257 return NET_RX_SUCCESS;
3258
3259 tvlv_value_len -= tt_vlan_len;
3260 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003261
3262 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3263 case BATADV_TT_REQUEST:
3264 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3265
3266 /* If this node cannot provide a TT response the tt_request is
3267 * forwarded
3268 */
3269 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3270 if (!ret) {
3271 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3272 tt_flag = 'F';
3273 else
3274 tt_flag = '.';
3275
3276 batadv_dbg(BATADV_DBG_TT, bat_priv,
3277 "Routing TT_REQUEST to %pM [%c]\n",
3278 dst, tt_flag);
3279 /* tvlv API will re-route the packet */
3280 return NET_RX_DROP;
3281 }
3282 break;
3283 case BATADV_TT_RESPONSE:
3284 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3285
3286 if (batadv_is_my_mac(bat_priv, dst)) {
3287 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003288 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003289 return NET_RX_SUCCESS;
3290 }
3291
3292 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3293 tt_flag = 'F';
3294 else
3295 tt_flag = '.';
3296
3297 batadv_dbg(BATADV_DBG_TT, bat_priv,
3298 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3299
3300 /* tvlv API will re-route the packet */
3301 return NET_RX_DROP;
3302 }
3303
3304 return NET_RX_SUCCESS;
3305}
3306
3307/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003308 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3309 * @bat_priv: the bat priv with all the soft interface information
3310 * @src: mac address of tt tvlv sender
3311 * @dst: mac address of tt tvlv recipient
3312 * @tvlv_value: tvlv buffer containing the tt data
3313 * @tvlv_value_len: tvlv buffer length
3314 *
3315 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3316 * otherwise.
3317 */
3318static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3319 uint8_t *src, uint8_t *dst,
3320 void *tvlv_value,
3321 uint16_t tvlv_value_len)
3322{
3323 struct batadv_tvlv_roam_adv *roaming_adv;
3324 struct batadv_orig_node *orig_node = NULL;
3325
3326 /* If this node is not the intended recipient of the
3327 * roaming advertisement the packet is forwarded
3328 * (the tvlv API will re-route the packet).
3329 */
3330 if (!batadv_is_my_mac(bat_priv, dst))
3331 return NET_RX_DROP;
3332
Marek Lindner122edaa2013-04-23 21:40:03 +08003333 if (tvlv_value_len < sizeof(*roaming_adv))
3334 goto out;
3335
3336 orig_node = batadv_orig_hash_find(bat_priv, src);
3337 if (!orig_node)
3338 goto out;
3339
3340 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3341 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3342
3343 batadv_dbg(BATADV_DBG_TT, bat_priv,
3344 "Received ROAMING_ADV from %pM (client %pM)\n",
3345 src, roaming_adv->client);
3346
3347 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003348 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003349 atomic_read(&orig_node->last_ttvn) + 1);
3350
3351out:
3352 if (orig_node)
3353 batadv_orig_node_free_ref(orig_node);
3354 return NET_RX_SUCCESS;
3355}
3356
3357/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003358 * batadv_tt_init - initialise the translation table internals
3359 * @bat_priv: the bat priv with all the soft interface information
3360 *
3361 * Return 0 on success or negative error number in case of failure.
3362 */
3363int batadv_tt_init(struct batadv_priv *bat_priv)
3364{
3365 int ret;
3366
3367 ret = batadv_tt_local_init(bat_priv);
3368 if (ret < 0)
3369 return ret;
3370
3371 ret = batadv_tt_global_init(bat_priv);
3372 if (ret < 0)
3373 return ret;
3374
3375 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003376 batadv_tt_tvlv_unicast_handler_v1,
3377 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003378
Marek Lindner122edaa2013-04-23 21:40:03 +08003379 batadv_tvlv_handler_register(bat_priv, NULL,
3380 batadv_roam_tvlv_unicast_handler_v1,
3381 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3382
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003383 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3384 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3385 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3386
3387 return 1;
3388}