blob: a3c965dd1d96f4d56856aa9426e43a72623e6584 [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
Marek Lindnera19d3d82013-05-27 15:33:25 +0800404/**
405 * batadv_tt_local_table_transmit_size - calculates the local translation table
406 * size when transmitted over the air
407 * @bat_priv: the bat priv with all the soft interface information
408 *
409 * Returns local translation table size in bytes.
410 */
411static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
412{
413 uint16_t num_vlan = 0, tt_local_entries = 0;
414 struct batadv_softif_vlan *vlan;
415 int hdr_size;
416
417 rcu_read_lock();
418 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
419 num_vlan++;
420 tt_local_entries += atomic_read(&vlan->tt.num_entries);
421 }
422 rcu_read_unlock();
423
424 /* header size of tvlv encapsulated tt response payload */
425 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
426 hdr_size += sizeof(struct batadv_tvlv_hdr);
427 hdr_size += sizeof(struct batadv_tvlv_tt_data);
428 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
429
430 return hdr_size + batadv_tt_len(tt_local_entries);
431}
432
Sven Eckelmann56303d32012-06-05 22:31:31 +0200433static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200435 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200436 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Sven Eckelmann807736f2012-07-15 22:26:51 +0200438 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Sven Eckelmann807736f2012-07-15 22:26:51 +0200440 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200441 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442
Antonio Quartullidec05072012-11-10 11:00:32 +0100443 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
444 &batadv_tt_local_hash_lock_class_key);
445
Sven Eckelmann5346c352012-05-05 13:27:28 +0200446 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447}
448
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200449static void batadv_tt_global_free(struct batadv_priv *bat_priv,
450 struct batadv_tt_global_entry *tt_global,
451 const char *message)
452{
453 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200454 "Deleting global tt entry %pM (vid: %d): %s\n",
455 tt_global->common.addr,
456 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200457
458 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200459 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200460 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200461}
462
Antonio Quartullic018ad32013-06-04 12:11:39 +0200463/**
464 * batadv_tt_local_add - add a new client to the local table or update an
465 * existing client
466 * @soft_iface: netdev struct of the mesh interface
467 * @addr: the mac address of the client to add
468 * @vid: VLAN identifier
469 * @ifindex: index of the interface where the client is connected to (useful to
470 * identify wireless clients)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800471 *
472 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200473 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800474bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200475 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200477 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200478 struct batadv_tt_local_entry *tt_local;
479 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200480 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800482 int hash_added, table_size, packet_size_max;
483 bool ret = false, roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Antonio Quartullic018ad32013-06-04 12:11:39 +0200485 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
486 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487
Antonio Quartulli47c94652012-09-23 22:38:35 +0200488 if (tt_local) {
489 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200490 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200492 "Re-adding pending client %pM (vid: %d)\n",
493 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200494 /* whatever the reason why the PENDING flag was set,
495 * this is a client which was enqueued to be removed in
496 * this orig_interval. Since it popped up again, the
497 * flag can be reset like it was never enqueued
498 */
499 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
500 goto add_event;
501 }
502
503 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
504 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200505 "Roaming client %pM (vid: %d) came back to its original location\n",
506 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200507 /* the ROAM flag is set because this client roamed away
508 * and the node got a roaming_advertisement message. Now
509 * that the client popped up again at its original
510 * location such flag can be unset
511 */
512 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
513 roamed_back = true;
514 }
515 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 }
517
Marek Lindnera19d3d82013-05-27 15:33:25 +0800518 /* Ignore the client if we cannot send it in a full table response. */
519 table_size = batadv_tt_local_table_transmit_size(bat_priv);
520 table_size += batadv_tt_len(1);
521 packet_size_max = atomic_read(&bat_priv->packet_size_max);
522 if (table_size > packet_size_max) {
523 net_ratelimited_function(batadv_info, soft_iface,
524 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
525 table_size, packet_size_max, addr);
526 goto out;
527 }
528
Antonio Quartulli47c94652012-09-23 22:38:35 +0200529 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
530 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200531 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200532
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200533 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200534 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
535 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200536 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537
Antonio Quartulli47c94652012-09-23 22:38:35 +0200538 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100539 /* The local entry has to be marked as NEW to avoid to send it in
540 * a full table response going out before the next ttvn increment
541 * (consistency check)
542 */
543 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200544 tt_local->common.vid = vid;
Sven Eckelmann95638772012-05-12 02:09:31 +0200545 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200546 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
547 atomic_set(&tt_local->common.refcount, 2);
548 tt_local->last_seen = jiffies;
549 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
551 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200552 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200553 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Sven Eckelmann807736f2012-07-15 22:26:51 +0200555 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200556 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200557 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100558
559 if (unlikely(hash_added != 0)) {
560 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200561 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100562 goto out;
563 }
564
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200565add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200566 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200567
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200568check_roaming:
569 /* Check whether it is a roaming, but don't do anything if the roaming
570 * process has already been handled
571 */
572 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200573 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200574 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200575 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800576 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200577 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200578 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200579 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200580 }
581 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200582 if (roamed_back) {
583 batadv_tt_global_free(bat_priv, tt_global,
584 "Roaming canceled");
585 tt_global = NULL;
586 } else {
587 /* The global entry has to be marked as ROAMING and
588 * has to be kept for consistency purpose
589 */
590 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
591 tt_global->roam_at = jiffies;
592 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200593 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200594
Marek Lindnera19d3d82013-05-27 15:33:25 +0800595 ret = true;
596
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200597out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200598 if (tt_local)
599 batadv_tt_local_entry_free_ref(tt_local);
600 if (tt_global)
601 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800602 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603}
604
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800605/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200606 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
607 * within a TT Response directed to another node
608 * @orig_node: originator for which the TT data has to be prepared
609 * @tt_data: uninitialised pointer to the address of the TVLV buffer
610 * @tt_change: uninitialised pointer to the address of the area where the TT
611 * changed can be stored
612 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
613 * function reserves the amount of space needed to send the entire global TT
614 * table. In case of success the value is updated with the real amount of
615 * reserved bytes
616
617 * Allocate the needed amount of memory for the entire TT TVLV and write its
618 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
619 * objects, one per active VLAN served by the originator node.
620 *
621 * Return the size of the allocated buffer or 0 in case of failure.
622 */
623static uint16_t
624batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
625 struct batadv_tvlv_tt_data **tt_data,
626 struct batadv_tvlv_tt_change **tt_change,
627 int32_t *tt_len)
628{
629 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
630 struct batadv_tvlv_tt_vlan_data *tt_vlan;
631 struct batadv_orig_node_vlan *vlan;
632 uint8_t *tt_change_ptr;
633
634 rcu_read_lock();
635 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
636 num_vlan++;
637 num_entries += atomic_read(&vlan->tt.num_entries);
638 }
639
640 change_offset = sizeof(**tt_data);
641 change_offset += num_vlan * sizeof(*tt_vlan);
642
643 /* if tt_len is negative, allocate the space needed by the full table */
644 if (*tt_len < 0)
645 *tt_len = batadv_tt_len(num_entries);
646
647 tvlv_len = *tt_len;
648 tvlv_len += change_offset;
649
650 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
651 if (!*tt_data) {
652 *tt_len = 0;
653 goto out;
654 }
655
656 (*tt_data)->flags = BATADV_NO_FLAGS;
657 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
658 (*tt_data)->num_vlan = htons(num_vlan);
659
660 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
661 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
662 tt_vlan->vid = htons(vlan->vid);
663 tt_vlan->crc = htonl(vlan->tt.crc);
664
665 tt_vlan++;
666 }
667
668 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
669 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
670
671out:
672 rcu_read_unlock();
673 return tvlv_len;
674}
675
676/**
677 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
678 * node
679 * @bat_priv: the bat priv with all the soft interface information
680 * @tt_data: uninitialised pointer to the address of the TVLV buffer
681 * @tt_change: uninitialised pointer to the address of the area where the TT
682 * changes can be stored
683 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
684 * function reserves the amount of space needed to send the entire local TT
685 * table. In case of success the value is updated with the real amount of
686 * reserved bytes
687 *
688 * Allocate the needed amount of memory for the entire TT TVLV and write its
689 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
690 * objects, one per active VLAN.
691 *
692 * Return the size of the allocated buffer or 0 in case of failure.
693 */
694static uint16_t
695batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
696 struct batadv_tvlv_tt_data **tt_data,
697 struct batadv_tvlv_tt_change **tt_change,
698 int32_t *tt_len)
699{
700 struct batadv_tvlv_tt_vlan_data *tt_vlan;
701 struct batadv_softif_vlan *vlan;
702 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
703 uint8_t *tt_change_ptr;
704 int change_offset;
705
706 rcu_read_lock();
707 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
708 num_vlan++;
709 num_entries += atomic_read(&vlan->tt.num_entries);
710 }
711
712 change_offset = sizeof(**tt_data);
713 change_offset += num_vlan * sizeof(*tt_vlan);
714
715 /* if tt_len is negative, allocate the space needed by the full table */
716 if (*tt_len < 0)
717 *tt_len = batadv_tt_len(num_entries);
718
719 tvlv_len = *tt_len;
720 tvlv_len += change_offset;
721
722 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
723 if (!*tt_data) {
724 tvlv_len = 0;
725 goto out;
726 }
727
728 (*tt_data)->flags = BATADV_NO_FLAGS;
729 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
730 (*tt_data)->num_vlan = htons(num_vlan);
731
732 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
733 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
734 tt_vlan->vid = htons(vlan->vid);
735 tt_vlan->crc = htonl(vlan->tt.crc);
736
737 tt_vlan++;
738 }
739
740 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
741 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
742
743out:
744 rcu_read_unlock();
745 return tvlv_len;
746}
747
748/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800749 * batadv_tt_tvlv_container_update - update the translation table tvlv container
750 * after local tt changes have been committed
751 * @bat_priv: the bat priv with all the soft interface information
752 */
753static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800755 struct batadv_tt_change_node *entry, *safe;
756 struct batadv_tvlv_tt_data *tt_data;
757 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200758 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800759 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200760 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200762 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
763 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800764
765 /* if we have too many changes for one packet don't send any
766 * and wait for the tt table request which will be fragmented
767 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800768 if (tt_diff_len > bat_priv->soft_iface->mtu)
769 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800770
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200771 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
772 &tt_change, &tt_diff_len);
773 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800774 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800775
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800776 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800777
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800778 if (tt_diff_len == 0)
779 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800780
Sven Eckelmann807736f2012-07-15 22:26:51 +0200781 spin_lock_bh(&bat_priv->tt.changes_list_lock);
782 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783
Sven Eckelmann807736f2012-07-15 22:26:51 +0200784 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100785 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800786 if (tt_diff_entries_count < tt_diff_entries_num) {
787 memcpy(tt_change + tt_diff_entries_count,
788 &entry->change,
789 sizeof(struct batadv_tvlv_tt_change));
790 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200792 list_del(&entry->list);
793 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200795 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796
Antonio Quartullia73105b2011-04-27 14:27:44 +0200797 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200798 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
799 kfree(bat_priv->tt.last_changeset);
800 bat_priv->tt.last_changeset_len = 0;
801 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800802 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800803 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800804 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800805 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200806 * instead of providing the diff
807 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800808 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200809 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800810 memcpy(bat_priv->tt.last_changeset,
811 tt_change, tt_change_len);
812 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200813 }
814 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200815 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800817container_register:
818 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200819 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800820 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821}
822
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200823int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824{
825 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200826 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200827 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200828 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100829 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200830 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200831 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200833 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200834 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100835 int last_seen_secs;
836 int last_seen_msecs;
837 unsigned long last_seen_jiffies;
838 bool no_purge;
839 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840
Marek Lindner30da63a2012-08-03 17:15:46 +0200841 primary_if = batadv_seq_print_text_primary_if_get(seq);
842 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200843 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100845 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200846 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
847 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
848 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
849 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851 for (i = 0; i < hash->size; i++) {
852 head = &hash->table[i];
853
Marek Lindner7aadf882011-02-18 12:28:09 +0000854 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800855 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000856 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100857 tt_local = container_of(tt_common_entry,
858 struct batadv_tt_local_entry,
859 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200860 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100861 last_seen_jiffies = jiffies - tt_local->last_seen;
862 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
863 last_seen_secs = last_seen_msecs / 1000;
864 last_seen_msecs = last_seen_msecs % 1000;
865
866 no_purge = tt_common_entry->flags & np_flag;
867
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200868 vlan = batadv_softif_vlan_get(bat_priv, vid);
869 if (!vlan) {
870 seq_printf(seq, "Cannot retrieve VLAN %d\n",
871 BATADV_PRINT_VID(vid));
872 continue;
873 }
874
875 seq_printf(seq,
876 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100877 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200878 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100879 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200880 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100881 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100882 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200883 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100884 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200885 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100886 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100887 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100888 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200889 no_purge ? 0 : last_seen_msecs,
890 vlan->tt.crc);
891
892 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000894 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200896out:
897 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200898 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200899 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900}
901
Sven Eckelmann56303d32012-06-05 22:31:31 +0200902static void
903batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
904 struct batadv_tt_local_entry *tt_local_entry,
905 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200907 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
Antonio Quartulli015758d2011-07-09 17:52:13 +0200909 /* The local client has to be marked as "pending to be removed" but has
910 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200911 * response issued before the net ttvn increment (consistency check)
912 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200913 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100914
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200915 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200916 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
917 tt_local_entry->common.addr,
918 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919}
920
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200921/**
922 * batadv_tt_local_remove - logically remove an entry from the local table
923 * @bat_priv: the bat priv with all the soft interface information
924 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200925 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200926 * @message: message to append to the log on deletion
927 * @roaming: true if the deletion is due to a roaming event
928 *
929 * Returns the flags assigned to the local entry before being deleted
930 */
931uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200932 const uint8_t *addr, unsigned short vid,
933 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200935 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200936 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000937
Antonio Quartullic018ad32013-06-04 12:11:39 +0200938 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200939 if (!tt_local_entry)
940 goto out;
941
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200942 curr_flags = tt_local_entry->common.flags;
943
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200944 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200945 /* if this global entry addition is due to a roaming, the node has to
946 * mark the local entry as "roamed" in order to correctly reroute
947 * packets later
948 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200949 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200950 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200951 /* mark the local client as ROAMed */
952 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
953 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200954
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200955 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
956 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
957 message);
958 goto out;
959 }
960 /* if this client has been added right now, it is possible to
961 * immediately purge it
962 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200963 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200964 hlist_del_rcu(&tt_local_entry->common.hash_entry);
965 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200966
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200967out:
968 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200969 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200970
971 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972}
973
Marek Lindnera19d3d82013-05-27 15:33:25 +0800974/**
975 * batadv_tt_local_purge_list - purge inactive tt local entries
976 * @bat_priv: the bat priv with all the soft interface information
977 * @head: pointer to the list containing the local tt entries
978 * @timeout: parameter deciding whether a given tt local entry is considered
979 * inactive or not
980 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200981static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +0800982 struct hlist_head *head,
983 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000984{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200985 struct batadv_tt_local_entry *tt_local_entry;
986 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800987 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200988
Sasha Levinb67bfe02013-02-27 17:06:00 -0800989 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200990 hash_entry) {
991 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200992 struct batadv_tt_local_entry,
993 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200994 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
995 continue;
996
997 /* entry already marked for deletion */
998 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
999 continue;
1000
Marek Lindnera19d3d82013-05-27 15:33:25 +08001001 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001002 continue;
1003
1004 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1005 BATADV_TT_CLIENT_DEL, "timed out");
1006 }
1007}
1008
Marek Lindnera19d3d82013-05-27 15:33:25 +08001009/**
1010 * batadv_tt_local_purge - purge inactive tt local entries
1011 * @bat_priv: the bat priv with all the soft interface information
1012 * @timeout: parameter deciding whether a given tt local entry is considered
1013 * inactive or not
1014 */
1015static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1016 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001017{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001018 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001020 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001021 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023 for (i = 0; i < hash->size; i++) {
1024 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001025 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001027 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001028 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001029 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031}
1032
Sven Eckelmann56303d32012-06-05 22:31:31 +02001033static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001035 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001036 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001037 struct batadv_tt_common_entry *tt_common_entry;
1038 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001039 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001040 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001041 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001042
Sven Eckelmann807736f2012-07-15 22:26:51 +02001043 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044 return;
1045
Sven Eckelmann807736f2012-07-15 22:26:51 +02001046 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001047
1048 for (i = 0; i < hash->size; i++) {
1049 head = &hash->table[i];
1050 list_lock = &hash->list_locks[i];
1051
1052 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001053 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001054 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001055 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001056 tt_local = container_of(tt_common_entry,
1057 struct batadv_tt_local_entry,
1058 common);
1059 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001060 }
1061 spin_unlock_bh(list_lock);
1062 }
1063
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001064 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001065
Sven Eckelmann807736f2012-07-15 22:26:51 +02001066 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067}
1068
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001070{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001071 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001072 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073
Sven Eckelmann807736f2012-07-15 22:26:51 +02001074 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001075
Sven Eckelmann807736f2012-07-15 22:26:51 +02001076 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001077 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001078
Antonio Quartullidec05072012-11-10 11:00:32 +01001079 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1080 &batadv_tt_global_hash_lock_class_key);
1081
Sven Eckelmann5346c352012-05-05 13:27:28 +02001082 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083}
1084
Sven Eckelmann56303d32012-06-05 22:31:31 +02001085static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001086{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001087 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001088
Sven Eckelmann807736f2012-07-15 22:26:51 +02001089 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001090
Sven Eckelmann807736f2012-07-15 22:26:51 +02001091 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001092 list) {
1093 list_del(&entry->list);
1094 kfree(entry);
1095 }
1096
Sven Eckelmann807736f2012-07-15 22:26:51 +02001097 atomic_set(&bat_priv->tt.local_changes, 0);
1098 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001099}
1100
Antonio Quartullid657e622012-07-01 14:09:12 +02001101/* retrieves the orig_tt_list_entry belonging to orig_node from the
1102 * batadv_tt_global_entry list
1103 *
1104 * returns it with an increased refcounter, NULL if not found
1105 */
1106static struct batadv_tt_orig_list_entry *
1107batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1108 const struct batadv_orig_node *orig_node)
1109{
1110 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1111 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001112
1113 rcu_read_lock();
1114 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001115 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001116 if (tmp_orig_entry->orig_node != orig_node)
1117 continue;
1118 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1119 continue;
1120
1121 orig_entry = tmp_orig_entry;
1122 break;
1123 }
1124 rcu_read_unlock();
1125
1126 return orig_entry;
1127}
1128
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001129/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001130 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001131 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001132static bool
1133batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1134 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001135{
Antonio Quartullid657e622012-07-01 14:09:12 +02001136 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001137 bool found = false;
1138
Antonio Quartullid657e622012-07-01 14:09:12 +02001139 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1140 if (orig_entry) {
1141 found = true;
1142 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001143 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001144
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001145 return found;
1146}
1147
Sven Eckelmanna5130882012-05-16 20:23:16 +02001148static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001149batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001150 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001152 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001153
Antonio Quartullid657e622012-07-01 14:09:12 +02001154 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001155 if (orig_entry) {
1156 /* refresh the ttvn: the current value could be a bogus one that
1157 * was added during a "temporary client detection"
1158 */
1159 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001160 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001161 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001162
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001163 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1164 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001165 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001166
1167 INIT_HLIST_NODE(&orig_entry->list);
1168 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001169 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 orig_entry->orig_node = orig_node;
1171 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001172 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001173
Antonio Quartullid657e622012-07-01 14:09:12 +02001174 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001175 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001176 &tt_global->orig_list);
1177 spin_unlock_bh(&tt_global->list_lock);
1178out:
1179 if (orig_entry)
1180 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181}
1182
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001183/**
1184 * batadv_tt_global_add - add a new TT global entry or update an existing one
1185 * @bat_priv: the bat priv with all the soft interface information
1186 * @orig_node: the originator announcing the client
1187 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001188 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001189 * @flags: TT flags that have to be set for this non-mesh client
1190 * @ttvn: the tt version number ever announcing this non-mesh client
1191 *
1192 * Add a new TT global entry for the given originator. If the entry already
1193 * exists add a new reference to the given originator (a global entry can have
1194 * references to multiple originators) and adjust the flags attribute to reflect
1195 * the function argument.
1196 * If a TT local entry exists for this non-mesh client remove it.
1197 *
1198 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001199 *
1200 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001201 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001202static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1203 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001204 const unsigned char *tt_addr,
1205 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001206 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001208 struct batadv_tt_global_entry *tt_global_entry;
1209 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001210 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001211 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001212 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001213 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001214
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001215 /* ignore global entries from backbone nodes */
1216 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1217 return true;
1218
Antonio Quartullic018ad32013-06-04 12:11:39 +02001219 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1220 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001221
1222 /* if the node already has a local client for this entry, it has to wait
1223 * for a roaming advertisement instead of manually messing up the global
1224 * table
1225 */
1226 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1227 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1228 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001231 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001233 goto out;
1234
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001235 common = &tt_global_entry->common;
1236 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001237 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001238
Antonio Quartullid4f44692012-05-25 00:00:54 +02001239 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001240 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001241 /* node must store current time in case of roaming. This is
1242 * needed to purge this entry out on timeout (if nobody claims
1243 * it)
1244 */
1245 if (flags & BATADV_TT_CLIENT_ROAM)
1246 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001247 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001248 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001249
1250 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1251 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001252
Sven Eckelmann807736f2012-07-15 22:26:51 +02001253 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001254 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001255 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001256 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001257
1258 if (unlikely(hash_added != 0)) {
1259 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001260 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001261 goto out_remove;
1262 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001263 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001264 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001265 /* If there is already a global entry, we can use this one for
1266 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001267 * But if we are trying to add a temporary client then here are
1268 * two options at this point:
1269 * 1) the global client is not a temporary client: the global
1270 * client has to be left as it is, temporary information
1271 * should never override any already known client state
1272 * 2) the global client is a temporary client: purge the
1273 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001274 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001275 if (flags & BATADV_TT_CLIENT_TEMP) {
1276 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1277 goto out;
1278 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1279 orig_node))
1280 goto out_remove;
1281 batadv_tt_global_del_orig_list(tt_global_entry);
1282 goto add_orig_entry;
1283 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001284
1285 /* if the client was temporary added before receiving the first
1286 * OGM announcing it, we have to clear the TEMP flag
1287 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001288 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001289
Antonio Quartullie9c001362012-11-07 15:05:33 +01001290 /* the change can carry possible "attribute" flags like the
1291 * TT_CLIENT_WIFI, therefore they have to be copied in the
1292 * client entry
1293 */
1294 tt_global_entry->common.flags |= flags;
1295
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001296 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1297 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001298 * delete + roaming change for this originator.
1299 *
1300 * We should first delete the old originator before adding the
1301 * new one.
1302 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001303 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001304 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001305 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001306 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001307 }
1308 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001309add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001310 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001311 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001312
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001313 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001314 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1315 common->addr, BATADV_PRINT_VID(common->vid),
1316 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001317 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001318
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001319out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001320
Antonio Quartullia73105b2011-04-27 14:27:44 +02001321 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001322 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001323 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001324 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001325 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1326
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001327 if (!(flags & BATADV_TT_CLIENT_ROAM))
1328 /* this is a normal global add. Therefore the client is not in a
1329 * roaming state anymore.
1330 */
1331 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1332
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001333out:
1334 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001335 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001336 if (tt_local_entry)
1337 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001338 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001339}
1340
Sven Eckelmann981d8902012-10-07 13:34:15 +02001341/* batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001342 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001343 * @tt_global_entry: global translation table entry to be analyzed
1344 *
1345 * This functon assumes the caller holds rcu_read_lock().
1346 * Returns best originator list entry or NULL on errors.
1347 */
1348static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001349batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1350 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001351{
Antonio Quartulli46274562013-09-03 11:10:24 +02001352 struct batadv_neigh_node *router, *best_router = NULL;
1353 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001354 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001355 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001356
1357 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001358 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001359 router = batadv_orig_node_get_router(orig_entry->orig_node);
1360 if (!router)
1361 continue;
1362
Antonio Quartulli46274562013-09-03 11:10:24 +02001363 if (best_router &&
1364 bao->bat_neigh_cmp(router, best_router) <= 0) {
1365 batadv_neigh_node_free_ref(router);
1366 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001367 }
1368
Antonio Quartulli46274562013-09-03 11:10:24 +02001369 /* release the refcount for the "old" best */
1370 if (best_router)
1371 batadv_neigh_node_free_ref(best_router);
1372
1373 best_entry = orig_entry;
1374 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001375 }
1376
Antonio Quartulli46274562013-09-03 11:10:24 +02001377 if (best_router)
1378 batadv_neigh_node_free_ref(best_router);
1379
Sven Eckelmann981d8902012-10-07 13:34:15 +02001380 return best_entry;
1381}
1382
1383/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1384 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001385 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001386 * @tt_global_entry: global translation table entry to be printed
1387 * @seq: debugfs table seq_file struct
1388 *
1389 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001390 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001391static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001392batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1393 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001394 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001395{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001396 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001397 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001398 struct batadv_orig_node_vlan *vlan;
1399 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001400 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001401 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001402
1403 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001404 flags = tt_common_entry->flags;
1405
Antonio Quartulli46274562013-09-03 11:10:24 +02001406 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001407 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001408 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1409 tt_common_entry->vid);
1410 if (!vlan) {
1411 seq_printf(seq,
1412 " * Cannot retrieve VLAN %d for originator %pM\n",
1413 BATADV_PRINT_VID(tt_common_entry->vid),
1414 best_entry->orig_node->orig);
1415 goto print_list;
1416 }
1417
Sven Eckelmann981d8902012-10-07 13:34:15 +02001418 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001419 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001420 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001421 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001422 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001423 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001424 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001425 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1426 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1427 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001428
1429 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001430 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001431
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001432print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001433 head = &tt_global_entry->orig_list;
1434
Sasha Levinb67bfe02013-02-27 17:06:00 -08001435 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001436 if (best_entry == orig_entry)
1437 continue;
1438
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001439 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1440 tt_common_entry->vid);
1441 if (!vlan) {
1442 seq_printf(seq,
1443 " + Cannot retrieve VLAN %d for originator %pM\n",
1444 BATADV_PRINT_VID(tt_common_entry->vid),
1445 orig_entry->orig_node->orig);
1446 continue;
1447 }
1448
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001449 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001450 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001451 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001452 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001453 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001454 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001455 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001456 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001457 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1458 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001459
1460 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001461 }
1462}
1463
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001464int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001465{
1466 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001467 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001468 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001469 struct batadv_tt_common_entry *tt_common_entry;
1470 struct batadv_tt_global_entry *tt_global;
1471 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001472 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001473 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001474
Marek Lindner30da63a2012-08-03 17:15:46 +02001475 primary_if = batadv_seq_print_text_primary_if_get(seq);
1476 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001477 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001478
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001479 seq_printf(seq,
1480 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001481 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001482 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1483 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1484 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001485
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001486 for (i = 0; i < hash->size; i++) {
1487 head = &hash->table[i];
1488
Marek Lindner7aadf882011-02-18 12:28:09 +00001489 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001490 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001491 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001492 tt_global = container_of(tt_common_entry,
1493 struct batadv_tt_global_entry,
1494 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001495 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001496 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001497 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001498 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001499out:
1500 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001501 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001502 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001503}
1504
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001505/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001506static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001507batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001508{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001509 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001510 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001511 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001512
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001513 spin_lock_bh(&tt_global_entry->list_lock);
1514 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001515 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1516 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001517 batadv_tt_global_size_dec(orig_entry->orig_node,
1518 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001519 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001520 }
1521 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001522}
1523
Sven Eckelmanna5130882012-05-16 20:23:16 +02001524static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001525batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1526 struct batadv_tt_global_entry *tt_global_entry,
1527 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001528 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001529{
1530 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001531 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001532 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001533 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001534
1535 spin_lock_bh(&tt_global_entry->list_lock);
1536 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001537 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001538 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001539 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001540 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001541 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001542 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001543 tt_global_entry->common.addr,
1544 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001545 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001546 batadv_tt_global_size_dec(orig_node,
1547 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001548 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001549 }
1550 }
1551 spin_unlock_bh(&tt_global_entry->list_lock);
1552}
1553
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001554/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001555 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1556 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001557 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001558static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001559batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1560 struct batadv_tt_global_entry *tt_global_entry,
1561 struct batadv_orig_node *orig_node,
1562 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001563{
1564 bool last_entry = true;
1565 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001566 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001567
1568 /* no local entry exists, case 1:
1569 * Check if this is the last one or if other entries exist.
1570 */
1571
1572 rcu_read_lock();
1573 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001574 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001575 if (orig_entry->orig_node != orig_node) {
1576 last_entry = false;
1577 break;
1578 }
1579 }
1580 rcu_read_unlock();
1581
1582 if (last_entry) {
1583 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001584 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001585 tt_global_entry->roam_at = jiffies;
1586 } else
1587 /* there is another entry, we can simply delete this
1588 * one and can still use the other one.
1589 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001590 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1591 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001592}
1593
Antonio Quartullic018ad32013-06-04 12:11:39 +02001594/**
1595 * batadv_tt_global_del - remove a client from the global table
1596 * @bat_priv: the bat priv with all the soft interface information
1597 * @orig_node: an originator serving this client
1598 * @addr: the mac address of the client
1599 * @vid: VLAN identifier
1600 * @message: a message explaining the reason for deleting the client to print
1601 * for debugging purpose
1602 * @roaming: true if the deletion has been triggered by a roaming event
1603 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001604static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1605 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001606 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001607 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001608{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001609 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001610 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001611
Antonio Quartullic018ad32013-06-04 12:11:39 +02001612 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001613 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001614 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001616 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001617 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1618 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001619
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001620 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001621 batadv_tt_global_free(bat_priv, tt_global_entry,
1622 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001623
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001624 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001625 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001626
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001627 /* if we are deleting a global entry due to a roam
1628 * event, there are two possibilities:
1629 * 1) the client roamed from node A to node B => if there
1630 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001631 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001632 * wait for node B to claim it. In case of timeout
1633 * the entry is purged.
1634 *
1635 * If there are other originators left, we directly delete
1636 * the originator.
1637 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001638 * the global entry, since it is useless now.
1639 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001640 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001641 tt_global_entry->common.addr,
1642 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001643 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001644 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001645 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001646 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001647 } else
1648 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001649 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1650 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001651
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001652
Antonio Quartullicc47f662011-04-27 14:27:57 +02001653out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001654 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001655 batadv_tt_global_entry_free_ref(tt_global_entry);
1656 if (local_entry)
1657 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001658}
1659
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001660/**
1661 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1662 * given originator matching the provided vid
1663 * @bat_priv: the bat priv with all the soft interface information
1664 * @orig_node: the originator owning the entries to remove
1665 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1666 * removed
1667 * @message: debug message to print as "reason"
1668 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001669void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1670 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001671 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001672 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001674 struct batadv_tt_global_entry *tt_global;
1675 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001676 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001677 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001678 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001680 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001681 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682
Simon Wunderlich6e801492011-10-19 10:28:26 +02001683 if (!hash)
1684 return;
1685
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 for (i = 0; i < hash->size; i++) {
1687 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001688 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001689
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001690 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001691 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001692 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001693 /* remove only matching entries */
1694 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1695 continue;
1696
Sven Eckelmann56303d32012-06-05 22:31:31 +02001697 tt_global = container_of(tt_common_entry,
1698 struct batadv_tt_global_entry,
1699 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001700
Sven Eckelmann56303d32012-06-05 22:31:31 +02001701 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001702 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001703
Sven Eckelmann56303d32012-06-05 22:31:31 +02001704 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001705 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001706 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001707 "Deleting global tt entry %pM (vid: %d): %s\n",
1708 tt_global->common.addr,
1709 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001710 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001711 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001712 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001713 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001714 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001715 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001716 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001717}
1718
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001719static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1720 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001721{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001722 bool purge = false;
1723 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1724 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001725
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001726 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1727 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1728 purge = true;
1729 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001730 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001731
1732 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1733 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1734 purge = true;
1735 *msg = "Temporary client timeout\n";
1736 }
1737
1738 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001739}
1740
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001741static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001742{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001743 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001744 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001745 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001746 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001747 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001748 char *msg = NULL;
1749 struct batadv_tt_common_entry *tt_common;
1750 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001751
Antonio Quartullicc47f662011-04-27 14:27:57 +02001752 for (i = 0; i < hash->size; i++) {
1753 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001754 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001755
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001756 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001757 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001758 hash_entry) {
1759 tt_global = container_of(tt_common,
1760 struct batadv_tt_global_entry,
1761 common);
1762
1763 if (!batadv_tt_global_to_purge(tt_global, &msg))
1764 continue;
1765
1766 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001767 "Deleting global tt entry %pM (vid: %d): %s\n",
1768 tt_global->common.addr,
1769 BATADV_PRINT_VID(tt_global->common.vid),
1770 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001771
Sasha Levinb67bfe02013-02-27 17:06:00 -08001772 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001773
1774 batadv_tt_global_entry_free_ref(tt_global);
1775 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001776 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001777 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001778}
1779
Sven Eckelmann56303d32012-06-05 22:31:31 +02001780static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001781{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001782 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001783 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001784 struct batadv_tt_common_entry *tt_common_entry;
1785 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001786 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001787 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001788 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001789
Sven Eckelmann807736f2012-07-15 22:26:51 +02001790 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001791 return;
1792
Sven Eckelmann807736f2012-07-15 22:26:51 +02001793 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001794
1795 for (i = 0; i < hash->size; i++) {
1796 head = &hash->table[i];
1797 list_lock = &hash->list_locks[i];
1798
1799 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001800 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001801 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001802 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001803 tt_global = container_of(tt_common_entry,
1804 struct batadv_tt_global_entry,
1805 common);
1806 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001807 }
1808 spin_unlock_bh(list_lock);
1809 }
1810
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001811 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001812
Sven Eckelmann807736f2012-07-15 22:26:51 +02001813 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001814}
1815
Sven Eckelmann56303d32012-06-05 22:31:31 +02001816static bool
1817_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1818 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001819{
1820 bool ret = false;
1821
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001822 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1823 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001824 ret = true;
1825
1826 return ret;
1827}
1828
Antonio Quartullic018ad32013-06-04 12:11:39 +02001829/**
1830 * batadv_transtable_search - get the mesh destination for a given client
1831 * @bat_priv: the bat priv with all the soft interface information
1832 * @src: mac address of the source client
1833 * @addr: mac address of the destination client
1834 * @vid: VLAN identifier
1835 *
1836 * Returns a pointer to the originator that was selected as destination in the
1837 * mesh for contacting the client 'addr', NULL otherwise.
1838 * In case of multiple originators serving the same client, the function returns
1839 * the best one (best in terms of metric towards the destination node).
1840 *
1841 * If the two clients are AP isolated the function returns NULL.
1842 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001843struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1844 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001845 const uint8_t *addr,
1846 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001847{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001848 struct batadv_tt_local_entry *tt_local_entry = NULL;
1849 struct batadv_tt_global_entry *tt_global_entry = NULL;
1850 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001851 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001852 bool ap_isolation_enabled = false;
1853 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001854
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001855 /* if the AP isolation is requested on a VLAN, then check for its
1856 * setting in the proper VLAN private data structure
1857 */
1858 vlan = batadv_softif_vlan_get(bat_priv, vid);
1859 if (vlan) {
1860 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1861 batadv_softif_vlan_free_ref(vlan);
1862 }
1863
1864 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001865 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001866 if (!tt_local_entry ||
1867 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001868 goto out;
1869 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001870
Antonio Quartullic018ad32013-06-04 12:11:39 +02001871 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001872 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001873 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001874
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001875 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001876 * isolation
1877 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001878 if (tt_local_entry &&
1879 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001880 goto out;
1881
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001882 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001883 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001884 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001885 if (best_entry)
1886 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001887 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1888 orig_node = NULL;
1889 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001890
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001891out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001892 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001893 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001894 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001895 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001896
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001897 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001898}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899
Antonio Quartulliced72932013-04-24 16:37:51 +02001900/**
1901 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1902 * to the given orig_node
1903 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001904 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001905 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001906 *
1907 * This function computes the checksum for the global table corresponding to a
1908 * specific originator. In particular, the checksum is computed as follows: For
1909 * each client connected to the originator the CRC32C of the MAC address and the
1910 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1911 * together.
1912 *
1913 * The idea behind is that CRC32C should be used as much as possible in order to
1914 * produce a unique hash of the table, but since the order which is used to feed
1915 * the CRC32C function affects the result and since every node in the network
1916 * probably sorts the clients differently, the hash function cannot be directly
1917 * computed over the entire table. Hence the CRC32C is used only on
1918 * the single client entry, while all the results are then xor'ed together
1919 * because the XOR operation can combine them all while trying to reduce the
1920 * noise as much as possible.
1921 *
1922 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001923 */
1924static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001925 struct batadv_orig_node *orig_node,
1926 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001928 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001929 struct batadv_tt_common_entry *tt_common;
1930 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001931 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001932 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933
1934 for (i = 0; i < hash->size; i++) {
1935 head = &hash->table[i];
1936
1937 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001938 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001939 tt_global = container_of(tt_common,
1940 struct batadv_tt_global_entry,
1941 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001942 /* compute the CRC only for entries belonging to the
1943 * VLAN identified by the vid passed as parameter
1944 */
1945 if (tt_common->vid != vid)
1946 continue;
1947
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001948 /* Roaming clients are in the global table for
1949 * consistency only. They don't have to be
1950 * taken into account while computing the
1951 * global crc
1952 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001953 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001954 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001955 /* Temporary clients have not been announced yet, so
1956 * they have to be skipped while computing the global
1957 * crc
1958 */
1959 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1960 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001961
1962 /* find out if this global entry is announced by this
1963 * originator
1964 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001965 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001966 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001967 continue;
1968
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001969 crc_tmp = crc32c(0, &tt_common->vid,
1970 sizeof(tt_common->vid));
1971 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972 }
1973 rcu_read_unlock();
1974 }
1975
Antonio Quartulliced72932013-04-24 16:37:51 +02001976 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001977}
1978
Antonio Quartulliced72932013-04-24 16:37:51 +02001979/**
1980 * batadv_tt_local_crc - calculates the checksum of the local table
1981 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001982 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001983 *
1984 * For details about the computation, please refer to the documentation for
1985 * batadv_tt_global_crc().
1986 *
1987 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001988 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001989static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
1990 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001992 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001993 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001994 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001995 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996
1997 for (i = 0; i < hash->size; i++) {
1998 head = &hash->table[i];
1999
2000 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002001 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002002 /* compute the CRC only for entries belonging to the
2003 * VLAN identified by vid
2004 */
2005 if (tt_common->vid != vid)
2006 continue;
2007
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002008 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002009 * account while computing the CRC
2010 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002011 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002012 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002013
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002014 crc_tmp = crc32c(0, &tt_common->vid,
2015 sizeof(tt_common->vid));
2016 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002017 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002018 rcu_read_unlock();
2019 }
2020
Antonio Quartulliced72932013-04-24 16:37:51 +02002021 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002022}
2023
Sven Eckelmann56303d32012-06-05 22:31:31 +02002024static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002026 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002027
Sven Eckelmann807736f2012-07-15 22:26:51 +02002028 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002029
Sven Eckelmann807736f2012-07-15 22:26:51 +02002030 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031 list_del(&node->list);
2032 kfree(node);
2033 }
2034
Sven Eckelmann807736f2012-07-15 22:26:51 +02002035 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002036}
2037
Sven Eckelmann56303d32012-06-05 22:31:31 +02002038static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2039 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002040 const void *tt_buff,
2041 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002044 * last OGM (the OGM could carry no changes)
2045 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002046 spin_lock_bh(&orig_node->tt_buff_lock);
2047 if (tt_buff_len > 0) {
2048 kfree(orig_node->tt_buff);
2049 orig_node->tt_buff_len = 0;
2050 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2051 if (orig_node->tt_buff) {
2052 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2053 orig_node->tt_buff_len = tt_buff_len;
2054 }
2055 }
2056 spin_unlock_bh(&orig_node->tt_buff_lock);
2057}
2058
Sven Eckelmann56303d32012-06-05 22:31:31 +02002059static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002061 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002062
Sven Eckelmann807736f2012-07-15 22:26:51 +02002063 spin_lock_bh(&bat_priv->tt.req_list_lock);
2064 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002065 if (batadv_has_timed_out(node->issued_at,
2066 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002067 list_del(&node->list);
2068 kfree(node);
2069 }
2070 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002071 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072}
2073
2074/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002075 * has already been issued for this orig_node, NULL otherwise
2076 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002077static struct batadv_tt_req_node *
2078batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2079 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002081 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002082
Sven Eckelmann807736f2012-07-15 22:26:51 +02002083 spin_lock_bh(&bat_priv->tt.req_list_lock);
2084 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002085 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2086 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002087 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002088 goto unlock;
2089 }
2090
2091 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2092 if (!tt_req_node)
2093 goto unlock;
2094
2095 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2096 tt_req_node->issued_at = jiffies;
2097
Sven Eckelmann807736f2012-07-15 22:26:51 +02002098 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002099unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002100 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101 return tt_req_node;
2102}
2103
Marek Lindner335fbe02013-04-23 21:40:02 +08002104/**
2105 * batadv_tt_local_valid - verify that given tt entry is a valid one
2106 * @entry_ptr: to be checked local tt entry
2107 * @data_ptr: not used but definition required to satisfy the callback prototype
2108 *
2109 * Returns 1 if the entry is a valid, 0 otherwise.
2110 */
2111static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002112{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002113 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002114
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002115 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002116 return 0;
2117 return 1;
2118}
2119
Sven Eckelmanna5130882012-05-16 20:23:16 +02002120static int batadv_tt_global_valid(const void *entry_ptr,
2121 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002123 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2124 const struct batadv_tt_global_entry *tt_global_entry;
2125 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002126
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002127 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2128 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002129 return 0;
2130
Sven Eckelmann56303d32012-06-05 22:31:31 +02002131 tt_global_entry = container_of(tt_common_entry,
2132 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002133 common);
2134
Sven Eckelmanna5130882012-05-16 20:23:16 +02002135 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002136}
2137
Marek Lindner335fbe02013-04-23 21:40:02 +08002138/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002139 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2140 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002141 * @bat_priv: the bat priv with all the soft interface information
2142 * @hash: hash table containing the tt entries
2143 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002144 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002145 * @valid_cb: function to filter tt change entries
2146 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002147 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002148static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2149 struct batadv_hashtable *hash,
2150 void *tvlv_buff, uint16_t tt_len,
2151 int (*valid_cb)(const void *, const void *),
2152 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002153{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002155 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002156 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002157 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002158 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002160 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002161 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002162
2163 rcu_read_lock();
2164 for (i = 0; i < hash->size; i++) {
2165 head = &hash->table[i];
2166
Sasha Levinb67bfe02013-02-27 17:06:00 -08002167 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002168 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002169 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170 break;
2171
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002172 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002173 continue;
2174
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002175 memcpy(tt_change->addr, tt_common_entry->addr,
2176 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002177 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002178 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08002179 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002180
Marek Lindner335fbe02013-04-23 21:40:02 +08002181 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002182 tt_change++;
2183 }
2184 }
2185 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002186}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002187
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002188/**
2189 * batadv_tt_global_check_crc - check if all the CRCs are correct
2190 * @orig_node: originator for which the CRCs have to be checked
2191 * @tt_vlan: pointer to the first tvlv VLAN entry
2192 * @num_vlan: number of tvlv VLAN entries
2193 * @create: if true, create VLAN objects if not found
2194 *
2195 * Return true if all the received CRCs match the locally stored ones, false
2196 * otherwise
2197 */
2198static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2199 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2200 uint16_t num_vlan)
2201{
2202 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2203 struct batadv_orig_node_vlan *vlan;
2204 int i;
2205
2206 /* check if each received CRC matches the locally stored one */
2207 for (i = 0; i < num_vlan; i++) {
2208 tt_vlan_tmp = tt_vlan + i;
2209
2210 /* if orig_node is a backbone node for this VLAN, don't check
2211 * the CRC as we ignore all the global entries over it
2212 */
2213 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002214 orig_node->orig,
2215 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002216 continue;
2217
2218 vlan = batadv_orig_node_vlan_get(orig_node,
2219 ntohs(tt_vlan_tmp->vid));
2220 if (!vlan)
2221 return false;
2222
2223 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2224 return false;
2225 }
2226
2227 return true;
2228}
2229
2230/**
2231 * batadv_tt_local_update_crc - update all the local CRCs
2232 * @bat_priv: the bat priv with all the soft interface information
2233 */
2234static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2235{
2236 struct batadv_softif_vlan *vlan;
2237
2238 /* recompute the global CRC for each VLAN */
2239 rcu_read_lock();
2240 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2241 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2242 }
2243 rcu_read_unlock();
2244}
2245
2246/**
2247 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2248 * @bat_priv: the bat priv with all the soft interface information
2249 * @orig_node: the orig_node for which the CRCs have to be updated
2250 */
2251static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2252 struct batadv_orig_node *orig_node)
2253{
2254 struct batadv_orig_node_vlan *vlan;
2255 uint32_t crc;
2256
2257 /* recompute the global CRC for each VLAN */
2258 rcu_read_lock();
2259 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2260 /* if orig_node is a backbone node for this VLAN, don't compute
2261 * the CRC as we ignore all the global entries over it
2262 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002263 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2264 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002265 continue;
2266
2267 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2268 vlan->tt.crc = crc;
2269 }
2270 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002271}
2272
Antonio Quartulliced72932013-04-24 16:37:51 +02002273/**
2274 * batadv_send_tt_request - send a TT Request message to a given node
2275 * @bat_priv: the bat priv with all the soft interface information
2276 * @dst_orig_node: the destination of the message
2277 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002278 * @tt_vlan: pointer to the first tvlv VLAN object to request
2279 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002280 * @full_table: ask for the entire translation table if true, while only for the
2281 * last TT diff otherwise
2282 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002283static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2284 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002285 uint8_t ttvn,
2286 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2287 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002288{
Marek Lindner335fbe02013-04-23 21:40:02 +08002289 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002290 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002291 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2292 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002293 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002294 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002295
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002296 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002297 if (!primary_if)
2298 goto out;
2299
2300 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002301 * reply from the same orig_node yet
2302 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002303 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002304 if (!tt_req_node)
2305 goto out;
2306
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002307 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2308 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002309 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310 goto out;
2311
Marek Lindner335fbe02013-04-23 21:40:02 +08002312 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2313 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002314 tvlv_tt_data->num_vlan = htons(num_vlan);
2315
2316 /* send all the CRCs within the request. This is needed by intermediate
2317 * nodes to ensure they have the correct table before replying
2318 */
2319 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2320 for (i = 0; i < num_vlan; i++) {
2321 tt_vlan_req->vid = tt_vlan->vid;
2322 tt_vlan_req->crc = tt_vlan->crc;
2323
2324 tt_vlan_req++;
2325 tt_vlan++;
2326 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002327
2328 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002329 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002330
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002331 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002332 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002333
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002334 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002335 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2336 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002337 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002338 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002339
2340out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002341 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002342 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002343 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002344 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002345 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002346 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002347 kfree(tt_req_node);
2348 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002349 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002350 return ret;
2351}
2352
Marek Lindner335fbe02013-04-23 21:40:02 +08002353/**
2354 * batadv_send_other_tt_response - send reply to tt request concerning another
2355 * node's translation table
2356 * @bat_priv: the bat priv with all the soft interface information
2357 * @tt_data: tt data containing the tt request information
2358 * @req_src: mac address of tt request sender
2359 * @req_dst: mac address of tt request recipient
2360 *
2361 * Returns true if tt request reply was sent, false otherwise.
2362 */
2363static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2364 struct batadv_tvlv_tt_data *tt_data,
2365 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002366{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002367 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002368 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002369 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002370 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002371 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002372 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002373 uint8_t orig_ttvn, req_ttvn;
2374 uint16_t tvlv_len;
2375 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002376
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002377 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002378 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002379 req_src, tt_data->ttvn, req_dst,
2380 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002381
2382 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002383 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384 if (!req_dst_orig_node)
2385 goto out;
2386
Marek Lindner335fbe02013-04-23 21:40:02 +08002387 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002388 if (!res_dst_orig_node)
2389 goto out;
2390
Antonio Quartullia73105b2011-04-27 14:27:44 +02002391 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002392 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002393
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002394 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002395 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002396 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002397 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2398 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002399 goto out;
2400
Antonio Quartulli015758d2011-07-09 17:52:13 +02002401 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002402 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002403 !req_dst_orig_node->tt_buff)
2404 full_table = true;
2405 else
2406 full_table = false;
2407
Marek Lindner335fbe02013-04-23 21:40:02 +08002408 /* TT fragmentation hasn't been implemented yet, so send as many
2409 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002410 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002411 if (!full_table) {
2412 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2413 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002414
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002415 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2416 &tvlv_tt_data,
2417 &tt_change,
2418 &tt_len);
2419 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002420 goto unlock;
2421
Antonio Quartullia73105b2011-04-27 14:27:44 +02002422 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002423 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002424 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002425 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2426 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002427 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2428 * in the initial part
2429 */
2430 tt_len = -1;
2431 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2432 &tvlv_tt_data,
2433 &tt_change,
2434 &tt_len);
2435 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002436 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002437
2438 /* fill the rest of the tvlv with the real TT entries */
2439 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2440 tt_change, tt_len,
2441 batadv_tt_global_valid,
2442 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002443 }
2444
Marek Lindnera19d3d82013-05-27 15:33:25 +08002445 /* Don't send the response, if larger than fragmented packet. */
2446 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2447 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2448 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2449 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2450 res_dst_orig_node->orig);
2451 goto out;
2452 }
2453
Marek Lindner335fbe02013-04-23 21:40:02 +08002454 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2455 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002456
2457 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002458 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002461 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2462 res_dst_orig_node->orig, req_dst_orig_node->orig,
2463 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002464
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002465 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002466
Marek Lindner335fbe02013-04-23 21:40:02 +08002467 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002468 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2469 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002470
Marek Lindner335fbe02013-04-23 21:40:02 +08002471 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002472 goto out;
2473
2474unlock:
2475 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2476
2477out:
2478 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002479 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002480 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002481 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002482 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002483 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002484}
Sven Eckelmann96412692012-06-05 22:31:30 +02002485
Marek Lindner335fbe02013-04-23 21:40:02 +08002486/**
2487 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2488 * translation table
2489 * @bat_priv: the bat priv with all the soft interface information
2490 * @tt_data: tt data containing the tt request information
2491 * @req_src: mac address of tt request sender
2492 *
2493 * Returns true if tt request reply was sent, false otherwise.
2494 */
2495static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2496 struct batadv_tvlv_tt_data *tt_data,
2497 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002498{
Marek Lindner335fbe02013-04-23 21:40:02 +08002499 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002500 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002501 struct batadv_tvlv_tt_change *tt_change;
2502 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002503 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002504 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002505 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002506 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002507
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002508 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002509 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002510 req_src, tt_data->ttvn,
2511 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002512
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002513 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002514
Sven Eckelmann807736f2012-07-15 22:26:51 +02002515 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002516 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002517
Marek Lindner335fbe02013-04-23 21:40:02 +08002518 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002519 if (!orig_node)
2520 goto out;
2521
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002522 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002523 if (!primary_if)
2524 goto out;
2525
2526 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002527 * is too big send the whole local translation table
2528 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002529 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002530 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002531 full_table = true;
2532 else
2533 full_table = false;
2534
Marek Lindner335fbe02013-04-23 21:40:02 +08002535 /* TT fragmentation hasn't been implemented yet, so send as many
2536 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002537 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002538 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002539 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002540
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002541 tt_len = bat_priv->tt.last_changeset_len;
2542 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2543 &tvlv_tt_data,
2544 &tt_change,
2545 &tt_len);
2546 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002547 goto unlock;
2548
Marek Lindner335fbe02013-04-23 21:40:02 +08002549 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002550 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002551 bat_priv->tt.last_changeset_len);
2552 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002553 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002554 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002555
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002556 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2557 * in the initial part
2558 */
2559 tt_len = -1;
2560 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2561 &tvlv_tt_data,
2562 &tt_change,
2563 &tt_len);
2564 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002565 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002566
2567 /* fill the rest of the tvlv with the real TT entries */
2568 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2569 tt_change, tt_len,
2570 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002571 }
2572
Marek Lindner335fbe02013-04-23 21:40:02 +08002573 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2574 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002575
2576 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002577 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002578
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002579 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002580 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2581 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002582
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002583 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002584
Marek Lindner335fbe02013-04-23 21:40:02 +08002585 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002586 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2587 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002588
Antonio Quartullia73105b2011-04-27 14:27:44 +02002589 goto out;
2590
2591unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002592 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002593out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002594 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002595 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002596 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002597 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002598 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002599 kfree(tvlv_tt_data);
2600 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002601 return true;
2602}
2603
Marek Lindner335fbe02013-04-23 21:40:02 +08002604/**
2605 * batadv_send_tt_response - send reply to tt request
2606 * @bat_priv: the bat priv with all the soft interface information
2607 * @tt_data: tt data containing the tt request information
2608 * @req_src: mac address of tt request sender
2609 * @req_dst: mac address of tt request recipient
2610 *
2611 * Returns true if tt request reply was sent, false otherwise.
2612 */
2613static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2614 struct batadv_tvlv_tt_data *tt_data,
2615 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002617 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002618 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002619 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002620 return batadv_send_other_tt_response(bat_priv, tt_data,
2621 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002622}
2623
Sven Eckelmann56303d32012-06-05 22:31:31 +02002624static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2625 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002626 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002627 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002628{
2629 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002630 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002631
2632 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002633 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2634 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002635 batadv_tt_global_del(bat_priv, orig_node,
2636 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002637 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002638 "tt removed by changes",
2639 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002640 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002641 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002642 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002643 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002644 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002645 /* In case of problem while storing a
2646 * global_entry, we stop the updating
2647 * procedure without committing the
2648 * ttvn change. This will avoid to send
2649 * corrupted data on tt_request
2650 */
2651 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002652 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002653 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002654 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002655}
2656
Sven Eckelmann56303d32012-06-05 22:31:31 +02002657static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002658 struct batadv_tvlv_tt_change *tt_change,
2659 uint8_t ttvn, uint8_t *resp_src,
2660 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002662 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002663
Marek Lindner335fbe02013-04-23 21:40:02 +08002664 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665 if (!orig_node)
2666 goto out;
2667
2668 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002669 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2670 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002671
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002672 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2673 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002674
2675 spin_lock_bh(&orig_node->tt_buff_lock);
2676 kfree(orig_node->tt_buff);
2677 orig_node->tt_buff_len = 0;
2678 orig_node->tt_buff = NULL;
2679 spin_unlock_bh(&orig_node->tt_buff_lock);
2680
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002681 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002682
2683out:
2684 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002685 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002686}
2687
Sven Eckelmann56303d32012-06-05 22:31:31 +02002688static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2689 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002690 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002691 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002692{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002693 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2694 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002695
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002696 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2697 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002698 atomic_set(&orig_node->last_ttvn, ttvn);
2699}
2700
Antonio Quartullic018ad32013-06-04 12:11:39 +02002701/**
2702 * batadv_is_my_client - check if a client is served by the local node
2703 * @bat_priv: the bat priv with all the soft interface information
2704 * @addr: the mac adress of the client to check
2705 * @vid: VLAN identifier
2706 *
2707 * Returns true if the client is served by this node, false otherwise.
2708 */
2709bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2710 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002711{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002712 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002713 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002714
Antonio Quartullic018ad32013-06-04 12:11:39 +02002715 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002716 if (!tt_local_entry)
2717 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002718 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002719 * consistency purpose)
2720 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002721 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2722 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002723 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002724 ret = true;
2725out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002726 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002727 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002728 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002729}
2730
Marek Lindner335fbe02013-04-23 21:40:02 +08002731/**
2732 * batadv_handle_tt_response - process incoming tt reply
2733 * @bat_priv: the bat priv with all the soft interface information
2734 * @tt_data: tt data containing the tt request information
2735 * @resp_src: mac address of tt reply sender
2736 * @num_entries: number of tt change entries appended to the tt data
2737 */
2738static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2739 struct batadv_tvlv_tt_data *tt_data,
2740 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002741{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002742 struct batadv_tt_req_node *node, *safe;
2743 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002744 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002745 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2746 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002747
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002748 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002749 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002750 resp_src, tt_data->ttvn, num_entries,
2751 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002752
Marek Lindner335fbe02013-04-23 21:40:02 +08002753 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002754 if (!orig_node)
2755 goto out;
2756
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002757 spin_lock_bh(&orig_node->tt_lock);
2758
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002759 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2760 change_offset *= ntohs(tt_data->num_vlan);
2761 change_offset += sizeof(*tt_data);
2762 tvlv_ptr += change_offset;
2763
2764 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002765 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002766 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2767 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002768 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002769 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2770 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002771 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002772
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002773 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002774 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002775
2776 spin_unlock_bh(&orig_node->tt_lock);
2777
Antonio Quartullia73105b2011-04-27 14:27:44 +02002778 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002779 spin_lock_bh(&bat_priv->tt.req_list_lock);
2780 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002781 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002782 continue;
2783 list_del(&node->list);
2784 kfree(node);
2785 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002786
Sven Eckelmann807736f2012-07-15 22:26:51 +02002787 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002788out:
2789 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002790 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002791}
2792
Sven Eckelmann56303d32012-06-05 22:31:31 +02002793static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002795 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002796
Sven Eckelmann807736f2012-07-15 22:26:51 +02002797 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002798
Sven Eckelmann807736f2012-07-15 22:26:51 +02002799 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002800 list_del(&node->list);
2801 kfree(node);
2802 }
2803
Sven Eckelmann807736f2012-07-15 22:26:51 +02002804 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002805}
2806
Sven Eckelmann56303d32012-06-05 22:31:31 +02002807static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002808{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002809 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002810
Sven Eckelmann807736f2012-07-15 22:26:51 +02002811 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2812 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002813 if (!batadv_has_timed_out(node->first_time,
2814 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002815 continue;
2816
2817 list_del(&node->list);
2818 kfree(node);
2819 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002820 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002821}
2822
2823/* This function checks whether the client already reached the
2824 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2825 * will not be sent.
2826 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002827 * returns true if the ROAMING_ADV can be sent, false otherwise
2828 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002829static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002830 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002831{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002832 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002833 bool ret = false;
2834
Sven Eckelmann807736f2012-07-15 22:26:51 +02002835 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002836 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002837 * reply from the same orig_node yet
2838 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002839 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002840 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002841 continue;
2842
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002843 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002844 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002845 continue;
2846
Sven Eckelmann3e348192012-05-16 20:23:22 +02002847 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002848 /* Sorry, you roamed too many times! */
2849 goto unlock;
2850 ret = true;
2851 break;
2852 }
2853
2854 if (!ret) {
2855 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2856 if (!tt_roam_node)
2857 goto unlock;
2858
2859 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002860 atomic_set(&tt_roam_node->counter,
2861 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002862 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2863
Sven Eckelmann807736f2012-07-15 22:26:51 +02002864 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002865 ret = true;
2866 }
2867
2868unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002869 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002870 return ret;
2871}
2872
Antonio Quartullic018ad32013-06-04 12:11:39 +02002873/**
2874 * batadv_send_roam_adv - send a roaming advertisement message
2875 * @bat_priv: the bat priv with all the soft interface information
2876 * @client: mac address of the roaming client
2877 * @vid: VLAN identifier
2878 * @orig_node: message destination
2879 *
2880 * Send a ROAMING_ADV message to the node which was previously serving this
2881 * client. This is done to inform the node that from now on all traffic destined
2882 * for this particular roamed client has to be forwarded to the sender of the
2883 * roaming message.
2884 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002885static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002886 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002887 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002888{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002889 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002890 struct batadv_tvlv_roam_adv tvlv_roam;
2891
2892 primary_if = batadv_primary_if_get_selected(bat_priv);
2893 if (!primary_if)
2894 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002895
2896 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002897 * already roamed to us too many times
2898 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002899 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002900 goto out;
2901
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002902 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002903 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2904 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002905
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002906 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002907
Marek Lindner122edaa2013-04-23 21:40:03 +08002908 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002909 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002910
2911 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2912 orig_node->orig, BATADV_TVLV_ROAM, 1,
2913 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002914
2915out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002916 if (primary_if)
2917 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002918}
2919
Sven Eckelmanna5130882012-05-16 20:23:16 +02002920static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002921{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002922 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002923 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002924 struct batadv_priv *bat_priv;
2925
2926 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002927 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2928 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002929
Marek Lindnera19d3d82013-05-27 15:33:25 +08002930 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002931 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002932 batadv_tt_req_purge(bat_priv);
2933 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002934
Antonio Quartulli72414442012-12-25 13:14:37 +01002935 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2936 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002937}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002938
Sven Eckelmann56303d32012-06-05 22:31:31 +02002939void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002940{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002941 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2942 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2943
Sven Eckelmann807736f2012-07-15 22:26:51 +02002944 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002945
Sven Eckelmanna5130882012-05-16 20:23:16 +02002946 batadv_tt_local_table_free(bat_priv);
2947 batadv_tt_global_table_free(bat_priv);
2948 batadv_tt_req_list_free(bat_priv);
2949 batadv_tt_changes_list_free(bat_priv);
2950 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002951
Sven Eckelmann807736f2012-07-15 22:26:51 +02002952 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002953}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002954
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002955/**
2956 * batadv_tt_local_set_flags - set or unset the specified flags on the local
2957 * table and possibly count them in the TT size
2958 * @bat_priv: the bat priv with all the soft interface information
2959 * @flags: the flag to switch
2960 * @enable: whether to set or unset the flag
2961 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002962 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002963static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2964 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002965{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002966 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2967 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002968 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002969 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002970 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002971
2972 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002973 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002974
2975 for (i = 0; i < hash->size; i++) {
2976 head = &hash->table[i];
2977
2978 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002979 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002980 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002981 if (enable) {
2982 if ((tt_common_entry->flags & flags) == flags)
2983 continue;
2984 tt_common_entry->flags |= flags;
2985 } else {
2986 if (!(tt_common_entry->flags & flags))
2987 continue;
2988 tt_common_entry->flags &= ~flags;
2989 }
2990 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002991
2992 if (!count)
2993 continue;
2994
2995 batadv_tt_local_size_inc(bat_priv,
2996 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002997 }
2998 rcu_read_unlock();
2999 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003000}
3001
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003002/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003003static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003004{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003005 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003006 struct batadv_tt_common_entry *tt_common;
3007 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003008 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003009 struct hlist_head *head;
3010 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003011 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003012
3013 if (!hash)
3014 return;
3015
3016 for (i = 0; i < hash->size; i++) {
3017 head = &hash->table[i];
3018 list_lock = &hash->list_locks[i];
3019
3020 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003021 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003022 hash_entry) {
3023 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003024 continue;
3025
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003026 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003027 "Deleting local tt entry (%pM, vid: %d): pending\n",
3028 tt_common->addr,
3029 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003030
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003031 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003032 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003033 tt_local = container_of(tt_common,
3034 struct batadv_tt_local_entry,
3035 common);
3036 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003037 }
3038 spin_unlock_bh(list_lock);
3039 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003040}
3041
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003042/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003043 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3044 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003045 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003046 *
3047 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003048 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003049static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003050{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003051 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3052 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3053 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003054 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003055 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003056
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003057 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003058
Sven Eckelmanna5130882012-05-16 20:23:16 +02003059 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003060 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003061
3062 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003063 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003064 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003065 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003066 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003067
3068 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003069 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003070 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003071}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003072
Marek Lindnera19d3d82013-05-27 15:33:25 +08003073/**
3074 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3075 * have been queued in the time since the last commit
3076 * @bat_priv: the bat priv with all the soft interface information
3077 */
3078void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3079{
3080 spin_lock_bh(&bat_priv->tt.commit_lock);
3081 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003082 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003083}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003084
Sven Eckelmann56303d32012-06-05 22:31:31 +02003085bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003086 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003087{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003088 struct batadv_tt_local_entry *tt_local_entry = NULL;
3089 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003090 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003091 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003092
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003093 vlan = batadv_softif_vlan_get(bat_priv, vid);
3094 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003095 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003096
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003097 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003098 if (!tt_local_entry)
3099 goto out;
3100
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003101 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003102 if (!tt_global_entry)
3103 goto out;
3104
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003105 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003106 goto out;
3107
Marek Lindner5870adc2012-06-20 17:16:05 +02003108 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003109
3110out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003111 if (vlan)
3112 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003113 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003114 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003115 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003116 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003117 return ret;
3118}
Marek Lindnera943cac2011-07-30 13:10:18 +02003119
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003120/**
3121 * batadv_tt_update_orig - update global translation table with new tt
3122 * information received via ogms
3123 * @bat_priv: the bat priv with all the soft interface information
3124 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003125 * @tt_vlan: pointer to the first tvlv VLAN entry
3126 * @tt_num_vlan: number of tvlv VLAN entries
3127 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003128 * @tt_num_changes: number of tt changes inside the tt buffer
3129 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003130 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003131 */
3132static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3133 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003134 const void *tt_buff, uint16_t tt_num_vlan,
3135 struct batadv_tvlv_tt_change *tt_change,
3136 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003137{
3138 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003139 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003140 bool full_table = true;
3141
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003142 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003143 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003144 * increased by one -> we can apply the attached changes
3145 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003146 if ((!orig_node->tt_initialised && ttvn == 1) ||
3147 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003148 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003149 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3150 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003151 * In this case send a tt request
3152 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003153 if (!tt_num_changes) {
3154 full_table = false;
3155 goto request_table;
3156 }
3157
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003158 spin_lock_bh(&orig_node->tt_lock);
3159
Marek Lindner335fbe02013-04-23 21:40:02 +08003160 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003161 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003162 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003163
3164 /* Even if we received the precomputed crc with the OGM, we
3165 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003166 * in the global table
3167 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003168 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003169
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003170 spin_unlock_bh(&orig_node->tt_lock);
3171
Marek Lindnera943cac2011-07-30 13:10:18 +02003172 /* The ttvn alone is not enough to guarantee consistency
3173 * because a single value could represent different states
3174 * (due to the wrap around). Thus a node has to check whether
3175 * the resulting table (after applying the changes) is still
3176 * consistent or not. E.g. a node could disconnect while its
3177 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3178 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003179 * inconsistency
3180 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003181 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3182 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003183 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003184 } else {
3185 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003186 * in sync anymore -> request fresh tt data
3187 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003188 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003189 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3190 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003191request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003192 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003193 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3194 orig_node->orig, ttvn, orig_ttvn,
3195 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003196 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003197 tt_vlan, tt_num_vlan,
3198 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003199 return;
3200 }
3201 }
3202}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003203
Antonio Quartullic018ad32013-06-04 12:11:39 +02003204/**
3205 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3206 * @bat_priv: the bat priv with all the soft interface information
3207 * @addr: the mac address of the client to check
3208 * @vid: VLAN identifier
3209 *
3210 * Returns true if we know that the client has moved from its old originator
3211 * to another one. This entry is still kept for consistency purposes and will be
3212 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003213 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003214bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003215 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003216{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003217 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003218 bool ret = false;
3219
Antonio Quartullic018ad32013-06-04 12:11:39 +02003220 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003221 if (!tt_global_entry)
3222 goto out;
3223
Antonio Quartullic1d07432013-01-15 22:17:19 +10003224 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003225 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003226out:
3227 return ret;
3228}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003229
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003230/**
3231 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3232 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003233 * @addr: the mac address of the local client to query
3234 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003235 *
3236 * Returns true if the local client is known to be roaming (it is not served by
3237 * this node anymore) or not. If yes, the client is still present in the table
3238 * to keep the latter consistent with the node TTVN
3239 */
3240bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003241 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003242{
3243 struct batadv_tt_local_entry *tt_local_entry;
3244 bool ret = false;
3245
Antonio Quartullic018ad32013-06-04 12:11:39 +02003246 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003247 if (!tt_local_entry)
3248 goto out;
3249
3250 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3251 batadv_tt_local_entry_free_ref(tt_local_entry);
3252out:
3253 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003254}
3255
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003256bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3257 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003258 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003259 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003260{
3261 bool ret = false;
3262
Antonio Quartulli16052782013-06-04 12:11:41 +02003263 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003264 BATADV_TT_CLIENT_TEMP,
3265 atomic_read(&orig_node->last_ttvn)))
3266 goto out;
3267
3268 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003269 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3270 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003271 ret = true;
3272out:
3273 return ret;
3274}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003275
3276/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003277 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3278 * maximum packet size that can be transported through the mesh
3279 * @soft_iface: netdev struct of the mesh interface
3280 *
3281 * Remove entries older than 'timeout' and half timeout if more entries need
3282 * to be removed.
3283 */
3284void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3285{
3286 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3287 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3288 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3289 bool reduced = false;
3290
3291 spin_lock_bh(&bat_priv->tt.commit_lock);
3292
3293 while (true) {
3294 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3295 if (packet_size_max >= table_size)
3296 break;
3297
3298 batadv_tt_local_purge(bat_priv, timeout);
3299 batadv_tt_local_purge_pending_clients(bat_priv);
3300
3301 timeout /= 2;
3302 reduced = true;
3303 net_ratelimited_function(batadv_info, soft_iface,
3304 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3305 packet_size_max);
3306 }
3307
3308 /* commit these changes immediately, to avoid synchronization problem
3309 * with the TTVN
3310 */
3311 if (reduced)
3312 batadv_tt_local_commit_changes_nolock(bat_priv);
3313
3314 spin_unlock_bh(&bat_priv->tt.commit_lock);
3315}
3316
3317/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003318 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3319 * @bat_priv: the bat priv with all the soft interface information
3320 * @orig: the orig_node of the ogm
3321 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3322 * @tvlv_value: tvlv buffer containing the gateway data
3323 * @tvlv_value_len: tvlv buffer length
3324 */
3325static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3326 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003327 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003328 uint16_t tvlv_value_len)
3329{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003330 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3331 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003332 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003333 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003334
3335 if (tvlv_value_len < sizeof(*tt_data))
3336 return;
3337
3338 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3339 tvlv_value_len -= sizeof(*tt_data);
3340
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003341 num_vlan = ntohs(tt_data->num_vlan);
3342
3343 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3344 return;
3345
3346 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3347 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3348 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3349
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003350 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003351
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003352 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3353 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003354}
3355
3356/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003357 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3358 * container
3359 * @bat_priv: the bat priv with all the soft interface information
3360 * @src: mac address of tt tvlv sender
3361 * @dst: mac address of tt tvlv recipient
3362 * @tvlv_value: tvlv buffer containing the tt data
3363 * @tvlv_value_len: tvlv buffer length
3364 *
3365 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3366 * otherwise.
3367 */
3368static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3369 uint8_t *src, uint8_t *dst,
3370 void *tvlv_value,
3371 uint16_t tvlv_value_len)
3372{
3373 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003374 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003375 char tt_flag;
3376 bool ret;
3377
3378 if (tvlv_value_len < sizeof(*tt_data))
3379 return NET_RX_SUCCESS;
3380
3381 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3382 tvlv_value_len -= sizeof(*tt_data);
3383
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003384 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3385 tt_vlan_len *= ntohs(tt_data->num_vlan);
3386
3387 if (tvlv_value_len < tt_vlan_len)
3388 return NET_RX_SUCCESS;
3389
3390 tvlv_value_len -= tt_vlan_len;
3391 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003392
3393 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3394 case BATADV_TT_REQUEST:
3395 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3396
3397 /* If this node cannot provide a TT response the tt_request is
3398 * forwarded
3399 */
3400 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3401 if (!ret) {
3402 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3403 tt_flag = 'F';
3404 else
3405 tt_flag = '.';
3406
3407 batadv_dbg(BATADV_DBG_TT, bat_priv,
3408 "Routing TT_REQUEST to %pM [%c]\n",
3409 dst, tt_flag);
3410 /* tvlv API will re-route the packet */
3411 return NET_RX_DROP;
3412 }
3413 break;
3414 case BATADV_TT_RESPONSE:
3415 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3416
3417 if (batadv_is_my_mac(bat_priv, dst)) {
3418 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003419 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003420 return NET_RX_SUCCESS;
3421 }
3422
3423 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3424 tt_flag = 'F';
3425 else
3426 tt_flag = '.';
3427
3428 batadv_dbg(BATADV_DBG_TT, bat_priv,
3429 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3430
3431 /* tvlv API will re-route the packet */
3432 return NET_RX_DROP;
3433 }
3434
3435 return NET_RX_SUCCESS;
3436}
3437
3438/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003439 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3440 * @bat_priv: the bat priv with all the soft interface information
3441 * @src: mac address of tt tvlv sender
3442 * @dst: mac address of tt tvlv recipient
3443 * @tvlv_value: tvlv buffer containing the tt data
3444 * @tvlv_value_len: tvlv buffer length
3445 *
3446 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3447 * otherwise.
3448 */
3449static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3450 uint8_t *src, uint8_t *dst,
3451 void *tvlv_value,
3452 uint16_t tvlv_value_len)
3453{
3454 struct batadv_tvlv_roam_adv *roaming_adv;
3455 struct batadv_orig_node *orig_node = NULL;
3456
3457 /* If this node is not the intended recipient of the
3458 * roaming advertisement the packet is forwarded
3459 * (the tvlv API will re-route the packet).
3460 */
3461 if (!batadv_is_my_mac(bat_priv, dst))
3462 return NET_RX_DROP;
3463
Marek Lindner122edaa2013-04-23 21:40:03 +08003464 if (tvlv_value_len < sizeof(*roaming_adv))
3465 goto out;
3466
3467 orig_node = batadv_orig_hash_find(bat_priv, src);
3468 if (!orig_node)
3469 goto out;
3470
3471 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3472 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3473
3474 batadv_dbg(BATADV_DBG_TT, bat_priv,
3475 "Received ROAMING_ADV from %pM (client %pM)\n",
3476 src, roaming_adv->client);
3477
3478 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003479 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003480 atomic_read(&orig_node->last_ttvn) + 1);
3481
3482out:
3483 if (orig_node)
3484 batadv_orig_node_free_ref(orig_node);
3485 return NET_RX_SUCCESS;
3486}
3487
3488/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003489 * batadv_tt_init - initialise the translation table internals
3490 * @bat_priv: the bat priv with all the soft interface information
3491 *
3492 * Return 0 on success or negative error number in case of failure.
3493 */
3494int batadv_tt_init(struct batadv_priv *bat_priv)
3495{
3496 int ret;
3497
3498 ret = batadv_tt_local_init(bat_priv);
3499 if (ret < 0)
3500 return ret;
3501
3502 ret = batadv_tt_global_init(bat_priv);
3503 if (ret < 0)
3504 return ret;
3505
3506 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003507 batadv_tt_tvlv_unicast_handler_v1,
3508 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003509
Marek Lindner122edaa2013-04-23 21:40:03 +08003510 batadv_tvlv_handler_register(bat_priv, NULL,
3511 batadv_roam_tvlv_unicast_handler_v1,
3512 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3513
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003514 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3515 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3516 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3517
3518 return 1;
3519}