blob: 4c28251dd8e67dd822892f89773328cb20630f7b [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartulliced72932013-04-24 16:37:51 +020030#include <linux/crc32c.h>
Antonio Quartullia73105b2011-04-27 14:27:44 +020031
Antonio Quartullidec05072012-11-10 11:00:32 +010032/* hash class keys */
33static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34static struct lock_class_key batadv_tt_global_hash_lock_class_key;
35
Sven Eckelmann56303d32012-06-05 22:31:31 +020036static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020037 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020038 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020039static void batadv_tt_purge(struct work_struct *work);
40static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020041batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020042static void batadv_tt_global_del(struct batadv_priv *bat_priv,
43 struct batadv_orig_node *orig_node,
44 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020045 unsigned short vid, const char *message,
46 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Marek Lindner7aadf882011-02-18 12:28:09 +000048/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020049static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000050{
Sven Eckelmann56303d32012-06-05 22:31:31 +020051 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020052 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000053
54 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
55}
56
Antonio Quartullic018ad32013-06-04 12:11:39 +020057/**
58 * batadv_choose_tt - return the index of the tt entry in the hash table
59 * @data: pointer to the tt_common_entry object to map
60 * @size: the size of the hash table
61 *
62 * Returns the hash index where the object represented by 'data' should be
63 * stored at.
64 */
65static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
66{
67 struct batadv_tt_common_entry *tt;
68 uint32_t hash = 0;
69
70 tt = (struct batadv_tt_common_entry *)data;
71 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
72 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
73
74 hash += (hash << 3);
75 hash ^= (hash >> 11);
76 hash += (hash << 15);
77
78 return hash % size;
79}
80
81/**
82 * batadv_tt_hash_find - look for a client in the given hash table
83 * @hash: the hash table to search
84 * @addr: the mac address of the client to look for
85 * @vid: VLAN identifier
86 *
87 * Returns a pointer to the tt_common struct belonging to the searched client if
88 * found, NULL otherwise.
89 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020090static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +020091batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
92 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +000093{
Marek Lindner7aadf882011-02-18 12:28:09 +000094 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +020095 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020096 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000097
98 if (!hash)
99 return NULL;
100
Antonio Quartullic018ad32013-06-04 12:11:39 +0200101 memcpy(to_search.addr, addr, ETH_ALEN);
102 to_search.vid = vid;
103
104 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000105 head = &hash->table[index];
106
107 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200108 hlist_for_each_entry_rcu(tt, head, hash_entry) {
109 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000110 continue;
111
Antonio Quartullic018ad32013-06-04 12:11:39 +0200112 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200113 continue;
114
Antonio Quartullic018ad32013-06-04 12:11:39 +0200115 if (!atomic_inc_not_zero(&tt->refcount))
116 continue;
117
118 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000119 break;
120 }
121 rcu_read_unlock();
122
Antonio Quartullic018ad32013-06-04 12:11:39 +0200123 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100124}
125
Antonio Quartullic018ad32013-06-04 12:11:39 +0200126/**
127 * batadv_tt_local_hash_find - search the local table for a given client
128 * @bat_priv: the bat priv with all the soft interface information
129 * @addr: the mac address of the client to look for
130 * @vid: VLAN identifier
131 *
132 * Returns a pointer to the corresponding tt_local_entry struct if the client is
133 * found, NULL otherwise.
134 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200135static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200136batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
137 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100138{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200139 struct batadv_tt_common_entry *tt_common_entry;
140 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100141
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
143 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100144 if (tt_common_entry)
145 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200146 struct batadv_tt_local_entry,
147 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100148 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000149}
150
Antonio Quartullic018ad32013-06-04 12:11:39 +0200151/**
152 * batadv_tt_global_hash_find - search the global table for a given client
153 * @bat_priv: the bat priv with all the soft interface information
154 * @addr: the mac address of the client to look for
155 * @vid: VLAN identifier
156 *
157 * Returns a pointer to the corresponding tt_global_entry struct if the client
158 * is found, NULL otherwise.
159 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200161batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
162 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000163{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164 struct batadv_tt_common_entry *tt_common_entry;
165 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000166
Antonio Quartullic018ad32013-06-04 12:11:39 +0200167 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
168 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100169 if (tt_common_entry)
170 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200171 struct batadv_tt_global_entry,
172 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100173 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000174}
175
Sven Eckelmanna5130882012-05-16 20:23:16 +0200176static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200177batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200178{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100179 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
180 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200181}
182
Antonio Quartulli21026052013-05-07 00:29:22 +0200183/**
184 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
185 * tt_global_entry and possibly free it
186 * @tt_global_entry: the object to free
187 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200188static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200189batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200190{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200191 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200192 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200193 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200194 }
195}
196
Sven Eckelmanna5130882012-05-16 20:23:16 +0200197static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200198{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200200
Sven Eckelmann56303d32012-06-05 22:31:31 +0200201 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800202
203 /* We are in an rcu callback here, therefore we cannot use
204 * batadv_orig_node_free_ref() and its call_rcu():
205 * An rcu_barrier() wouldn't wait for that to finish
206 */
207 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200208 kfree(orig_entry);
209}
210
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200211/**
212 * batadv_tt_local_size_mod - change the size by v of the local table identified
213 * by vid
214 * @bat_priv: the bat priv with all the soft interface information
215 * @vid: the VLAN identifier of the sub-table to change
216 * @v: the amount to sum to the local table size
217 */
218static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
219 unsigned short vid, int v)
220{
221 struct batadv_softif_vlan *vlan;
222
223 vlan = batadv_softif_vlan_get(bat_priv, vid);
224 if (!vlan)
225 return;
226
227 atomic_add(v, &vlan->tt.num_entries);
228
229 batadv_softif_vlan_free_ref(vlan);
230}
231
232/**
233 * batadv_tt_local_size_inc - increase by one the local table size for the given
234 * vid
235 * @bat_priv: the bat priv with all the soft interface information
236 * @vid: the VLAN identifier
237 */
238static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
239 unsigned short vid)
240{
241 batadv_tt_local_size_mod(bat_priv, vid, 1);
242}
243
244/**
245 * batadv_tt_local_size_dec - decrease by one the local table size for the given
246 * vid
247 * @bat_priv: the bat priv with all the soft interface information
248 * @vid: the VLAN identifier
249 */
250static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
251 unsigned short vid)
252{
253 batadv_tt_local_size_mod(bat_priv, vid, -1);
254}
255
256/**
257 * batadv_tt_global_size_mod - change the size by v of the local table
258 * identified by vid
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 * @v: the amount to sum to the global table size
262 */
263static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
264 unsigned short vid, int v)
265{
266 struct batadv_orig_node_vlan *vlan;
267
268 vlan = batadv_orig_node_vlan_new(orig_node, vid);
269 if (!vlan)
270 return;
271
272 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
273 spin_lock_bh(&orig_node->vlan_list_lock);
274 list_del_rcu(&vlan->list);
275 spin_unlock_bh(&orig_node->vlan_list_lock);
276 batadv_orig_node_vlan_free_ref(vlan);
277 }
278
279 batadv_orig_node_vlan_free_ref(vlan);
280}
281
282/**
283 * batadv_tt_global_size_inc - increase by one the global table size for the
284 * given vid
285 * @orig_node: the originator which global table size has to be decreased
286 * @vid: the vlan identifier
287 */
288static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
289 unsigned short vid)
290{
291 batadv_tt_global_size_mod(orig_node, vid, 1);
292}
293
294/**
295 * batadv_tt_global_size_dec - decrease by one the global table size for the
296 * given vid
297 * @orig_node: the originator which global table size has to be decreased
298 * @vid: the vlan identifier
299 */
300static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
301 unsigned short vid)
302{
303 batadv_tt_global_size_mod(orig_node, vid, -1);
304}
305
Sven Eckelmanna5130882012-05-16 20:23:16 +0200306static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200307batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200308{
Antonio Quartullid657e622012-07-01 14:09:12 +0200309 if (!atomic_dec_and_test(&orig_entry->refcount))
310 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200311
Sven Eckelmanna5130882012-05-16 20:23:16 +0200312 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200313}
314
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200315/**
316 * batadv_tt_local_event - store a local TT event (ADD/DEL)
317 * @bat_priv: the bat priv with all the soft interface information
318 * @tt_local_entry: the TT entry involved in the event
319 * @event_flags: flags to store in the event structure
320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200321static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200322 struct batadv_tt_local_entry *tt_local_entry,
323 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200324{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200325 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200326 struct batadv_tt_common_entry *common = &tt_local_entry->common;
327 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200328 bool event_removed = false;
329 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200330
331 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200332 if (!tt_change_node)
333 return;
334
Antonio Quartulliff66c972011-06-30 01:14:00 +0200335 tt_change_node->change.flags = flags;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800336 tt_change_node->change.reserved = 0;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200337 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200338 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200339
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200341
342 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200343 spin_lock_bh(&bat_priv->tt.changes_list_lock);
344 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200345 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200346 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200347 continue;
348
349 /* DEL+ADD in the same orig interval have no effect and can be
350 * removed to avoid silly behaviour on the receiver side. The
351 * other way around (ADD+DEL) can happen in case of roaming of
352 * a client still in the NEW state. Roaming of NEW clients is
353 * now possible due to automatically recognition of "temporary"
354 * clients
355 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200356 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200357 if (!del_op_requested && del_op_entry)
358 goto del;
359 if (del_op_requested && !del_op_entry)
360 goto del;
361 continue;
362del:
363 list_del(&entry->list);
364 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000365 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200366 event_removed = true;
367 goto unlock;
368 }
369
Antonio Quartullia73105b2011-04-27 14:27:44 +0200370 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200371 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200372
373unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200374 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200375
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200376 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200377 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200378 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200379 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200380}
381
Marek Lindner335fbe02013-04-23 21:40:02 +0800382/**
383 * batadv_tt_len - compute length in bytes of given number of tt changes
384 * @changes_num: number of tt changes
385 *
386 * Returns computed length in bytes.
387 */
388static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389{
Marek Lindner335fbe02013-04-23 21:40:02 +0800390 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200391}
392
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200393/**
394 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
395 * @tt_len: available space
396 *
397 * Returns the number of entries.
398 */
399static uint16_t batadv_tt_entries(uint16_t tt_len)
400{
401 return tt_len / batadv_tt_len(1);
402}
403
Sven Eckelmann56303d32012-06-05 22:31:31 +0200404static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200406 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200407 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Sven Eckelmann807736f2012-07-15 22:26:51 +0200409 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Sven Eckelmann807736f2012-07-15 22:26:51 +0200411 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200412 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Antonio Quartullidec05072012-11-10 11:00:32 +0100414 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
415 &batadv_tt_local_hash_lock_class_key);
416
Sven Eckelmann5346c352012-05-05 13:27:28 +0200417 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200420static void batadv_tt_global_free(struct batadv_priv *bat_priv,
421 struct batadv_tt_global_entry *tt_global,
422 const char *message)
423{
424 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200425 "Deleting global tt entry %pM (vid: %d): %s\n",
426 tt_global->common.addr,
427 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200428
429 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200430 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200431 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200432}
433
Antonio Quartullic018ad32013-06-04 12:11:39 +0200434/**
435 * batadv_tt_local_add - add a new client to the local table or update an
436 * existing client
437 * @soft_iface: netdev struct of the mesh interface
438 * @addr: the mac address of the client to add
439 * @vid: VLAN identifier
440 * @ifindex: index of the interface where the client is connected to (useful to
441 * identify wireless clients)
442 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200443void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200444 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200446 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200447 struct batadv_tt_local_entry *tt_local;
448 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200449 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200450 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100451 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200452 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453
Antonio Quartullic018ad32013-06-04 12:11:39 +0200454 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
455 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Antonio Quartulli47c94652012-09-23 22:38:35 +0200457 if (tt_local) {
458 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200459 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200461 "Re-adding pending client %pM (vid: %d)\n",
462 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200463 /* whatever the reason why the PENDING flag was set,
464 * this is a client which was enqueued to be removed in
465 * this orig_interval. Since it popped up again, the
466 * flag can be reset like it was never enqueued
467 */
468 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
469 goto add_event;
470 }
471
472 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
473 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200474 "Roaming client %pM (vid: %d) came back to its original location\n",
475 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200476 /* the ROAM flag is set because this client roamed away
477 * and the node got a roaming_advertisement message. Now
478 * that the client popped up again at its original
479 * location such flag can be unset
480 */
481 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
482 roamed_back = true;
483 }
484 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 }
486
Antonio Quartulli47c94652012-09-23 22:38:35 +0200487 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
488 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200489 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200490
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200492 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
493 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200494 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Antonio Quartulli47c94652012-09-23 22:38:35 +0200496 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100497 /* The local entry has to be marked as NEW to avoid to send it in
498 * a full table response going out before the next ttvn increment
499 * (consistency check)
500 */
501 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200502 tt_local->common.vid = vid;
Sven Eckelmann95638772012-05-12 02:09:31 +0200503 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200504 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
505 atomic_set(&tt_local->common.refcount, 2);
506 tt_local->last_seen = jiffies;
507 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508
509 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200510 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200511 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
Sven Eckelmann807736f2012-07-15 22:26:51 +0200513 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200514 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200515 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100516
517 if (unlikely(hash_added != 0)) {
518 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200519 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100520 goto out;
521 }
522
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200523add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200524 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200525
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200526check_roaming:
527 /* Check whether it is a roaming, but don't do anything if the roaming
528 * process has already been handled
529 */
530 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200531 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200532 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200533 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800534 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200535 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200536 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200537 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200538 }
539 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200540 if (roamed_back) {
541 batadv_tt_global_free(bat_priv, tt_global,
542 "Roaming canceled");
543 tt_global = NULL;
544 } else {
545 /* The global entry has to be marked as ROAMING and
546 * has to be kept for consistency purpose
547 */
548 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
549 tt_global->roam_at = jiffies;
550 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200551 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200552
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200553out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200554 if (tt_local)
555 batadv_tt_local_entry_free_ref(tt_local);
556 if (tt_global)
557 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558}
559
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800560/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200561 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
562 * within a TT Response directed to another node
563 * @orig_node: originator for which the TT data has to be prepared
564 * @tt_data: uninitialised pointer to the address of the TVLV buffer
565 * @tt_change: uninitialised pointer to the address of the area where the TT
566 * changed can be stored
567 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
568 * function reserves the amount of space needed to send the entire global TT
569 * table. In case of success the value is updated with the real amount of
570 * reserved bytes
571
572 * Allocate the needed amount of memory for the entire TT TVLV and write its
573 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
574 * objects, one per active VLAN served by the originator node.
575 *
576 * Return the size of the allocated buffer or 0 in case of failure.
577 */
578static uint16_t
579batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
580 struct batadv_tvlv_tt_data **tt_data,
581 struct batadv_tvlv_tt_change **tt_change,
582 int32_t *tt_len)
583{
584 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
585 struct batadv_tvlv_tt_vlan_data *tt_vlan;
586 struct batadv_orig_node_vlan *vlan;
587 uint8_t *tt_change_ptr;
588
589 rcu_read_lock();
590 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
591 num_vlan++;
592 num_entries += atomic_read(&vlan->tt.num_entries);
593 }
594
595 change_offset = sizeof(**tt_data);
596 change_offset += num_vlan * sizeof(*tt_vlan);
597
598 /* if tt_len is negative, allocate the space needed by the full table */
599 if (*tt_len < 0)
600 *tt_len = batadv_tt_len(num_entries);
601
602 tvlv_len = *tt_len;
603 tvlv_len += change_offset;
604
605 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
606 if (!*tt_data) {
607 *tt_len = 0;
608 goto out;
609 }
610
611 (*tt_data)->flags = BATADV_NO_FLAGS;
612 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
613 (*tt_data)->num_vlan = htons(num_vlan);
614
615 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
616 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
617 tt_vlan->vid = htons(vlan->vid);
618 tt_vlan->crc = htonl(vlan->tt.crc);
619
620 tt_vlan++;
621 }
622
623 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
624 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
625
626out:
627 rcu_read_unlock();
628 return tvlv_len;
629}
630
631/**
632 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
633 * node
634 * @bat_priv: the bat priv with all the soft interface information
635 * @tt_data: uninitialised pointer to the address of the TVLV buffer
636 * @tt_change: uninitialised pointer to the address of the area where the TT
637 * changes can be stored
638 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
639 * function reserves the amount of space needed to send the entire local TT
640 * table. In case of success the value is updated with the real amount of
641 * reserved bytes
642 *
643 * Allocate the needed amount of memory for the entire TT TVLV and write its
644 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
645 * objects, one per active VLAN.
646 *
647 * Return the size of the allocated buffer or 0 in case of failure.
648 */
649static uint16_t
650batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
651 struct batadv_tvlv_tt_data **tt_data,
652 struct batadv_tvlv_tt_change **tt_change,
653 int32_t *tt_len)
654{
655 struct batadv_tvlv_tt_vlan_data *tt_vlan;
656 struct batadv_softif_vlan *vlan;
657 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
658 uint8_t *tt_change_ptr;
659 int change_offset;
660
661 rcu_read_lock();
662 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
663 num_vlan++;
664 num_entries += atomic_read(&vlan->tt.num_entries);
665 }
666
667 change_offset = sizeof(**tt_data);
668 change_offset += num_vlan * sizeof(*tt_vlan);
669
670 /* if tt_len is negative, allocate the space needed by the full table */
671 if (*tt_len < 0)
672 *tt_len = batadv_tt_len(num_entries);
673
674 tvlv_len = *tt_len;
675 tvlv_len += change_offset;
676
677 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
678 if (!*tt_data) {
679 tvlv_len = 0;
680 goto out;
681 }
682
683 (*tt_data)->flags = BATADV_NO_FLAGS;
684 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
685 (*tt_data)->num_vlan = htons(num_vlan);
686
687 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
688 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
689 tt_vlan->vid = htons(vlan->vid);
690 tt_vlan->crc = htonl(vlan->tt.crc);
691
692 tt_vlan++;
693 }
694
695 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
696 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
697
698out:
699 rcu_read_unlock();
700 return tvlv_len;
701}
702
703/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800704 * batadv_tt_tvlv_container_update - update the translation table tvlv container
705 * after local tt changes have been committed
706 * @bat_priv: the bat priv with all the soft interface information
707 */
708static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800710 struct batadv_tt_change_node *entry, *safe;
711 struct batadv_tvlv_tt_data *tt_data;
712 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200713 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800714 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200715 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200717 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
718 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800719
720 /* if we have too many changes for one packet don't send any
721 * and wait for the tt table request which will be fragmented
722 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800723 if (tt_diff_len > bat_priv->soft_iface->mtu)
724 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800725
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200726 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
727 &tt_change, &tt_diff_len);
728 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800729 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800730
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800731 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800732
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800733 if (tt_diff_len == 0)
734 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800735
Sven Eckelmann807736f2012-07-15 22:26:51 +0200736 spin_lock_bh(&bat_priv->tt.changes_list_lock);
737 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738
Sven Eckelmann807736f2012-07-15 22:26:51 +0200739 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100740 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800741 if (tt_diff_entries_count < tt_diff_entries_num) {
742 memcpy(tt_change + tt_diff_entries_count,
743 &entry->change,
744 sizeof(struct batadv_tvlv_tt_change));
745 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200747 list_del(&entry->list);
748 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200750 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751
Antonio Quartullia73105b2011-04-27 14:27:44 +0200752 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200753 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
754 kfree(bat_priv->tt.last_changeset);
755 bat_priv->tt.last_changeset_len = 0;
756 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800757 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800758 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800759 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800760 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200761 * instead of providing the diff
762 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800763 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200764 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800765 memcpy(bat_priv->tt.last_changeset,
766 tt_change, tt_change_len);
767 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200768 }
769 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200770 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800772container_register:
773 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200774 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800775 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776}
777
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200778int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779{
780 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200781 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200782 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200783 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100784 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200785 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200786 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200788 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200789 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100790 int last_seen_secs;
791 int last_seen_msecs;
792 unsigned long last_seen_jiffies;
793 bool no_purge;
794 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795
Marek Lindner30da63a2012-08-03 17:15:46 +0200796 primary_if = batadv_seq_print_text_primary_if_get(seq);
797 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200798 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100800 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200801 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
802 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
803 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
804 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806 for (i = 0; i < hash->size; i++) {
807 head = &hash->table[i];
808
Marek Lindner7aadf882011-02-18 12:28:09 +0000809 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800810 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000811 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100812 tt_local = container_of(tt_common_entry,
813 struct batadv_tt_local_entry,
814 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200815 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100816 last_seen_jiffies = jiffies - tt_local->last_seen;
817 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
818 last_seen_secs = last_seen_msecs / 1000;
819 last_seen_msecs = last_seen_msecs % 1000;
820
821 no_purge = tt_common_entry->flags & np_flag;
822
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200823 vlan = batadv_softif_vlan_get(bat_priv, vid);
824 if (!vlan) {
825 seq_printf(seq, "Cannot retrieve VLAN %d\n",
826 BATADV_PRINT_VID(vid));
827 continue;
828 }
829
830 seq_printf(seq,
831 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100832 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200833 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100834 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200835 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100836 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100837 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200838 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100839 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200840 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100841 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100842 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100843 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200844 no_purge ? 0 : last_seen_msecs,
845 vlan->tt.crc);
846
847 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000849 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200851out:
852 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200853 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200854 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855}
856
Sven Eckelmann56303d32012-06-05 22:31:31 +0200857static void
858batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
859 struct batadv_tt_local_entry *tt_local_entry,
860 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200862 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
Antonio Quartulli015758d2011-07-09 17:52:13 +0200864 /* The local client has to be marked as "pending to be removed" but has
865 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200866 * response issued before the net ttvn increment (consistency check)
867 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200868 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100869
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200870 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200871 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
872 tt_local_entry->common.addr,
873 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874}
875
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200876/**
877 * batadv_tt_local_remove - logically remove an entry from the local table
878 * @bat_priv: the bat priv with all the soft interface information
879 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200880 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200881 * @message: message to append to the log on deletion
882 * @roaming: true if the deletion is due to a roaming event
883 *
884 * Returns the flags assigned to the local entry before being deleted
885 */
886uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200887 const uint8_t *addr, unsigned short vid,
888 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200890 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200891 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
Antonio Quartullic018ad32013-06-04 12:11:39 +0200893 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200894 if (!tt_local_entry)
895 goto out;
896
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200897 curr_flags = tt_local_entry->common.flags;
898
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200899 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200900 /* if this global entry addition is due to a roaming, the node has to
901 * mark the local entry as "roamed" in order to correctly reroute
902 * packets later
903 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200904 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200905 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200906 /* mark the local client as ROAMed */
907 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
908 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200909
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200910 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
911 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
912 message);
913 goto out;
914 }
915 /* if this client has been added right now, it is possible to
916 * immediately purge it
917 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200918 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200919 hlist_del_rcu(&tt_local_entry->common.hash_entry);
920 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200921
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200922out:
923 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200924 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200925
926 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927}
928
Sven Eckelmann56303d32012-06-05 22:31:31 +0200929static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200930 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932 struct batadv_tt_local_entry *tt_local_entry;
933 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800934 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200935
Sasha Levinb67bfe02013-02-27 17:06:00 -0800936 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200937 hash_entry) {
938 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200939 struct batadv_tt_local_entry,
940 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200941 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
942 continue;
943
944 /* entry already marked for deletion */
945 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
946 continue;
947
948 if (!batadv_has_timed_out(tt_local_entry->last_seen,
949 BATADV_TT_LOCAL_TIMEOUT))
950 continue;
951
952 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
953 BATADV_TT_CLIENT_DEL, "timed out");
954 }
955}
956
Sven Eckelmann56303d32012-06-05 22:31:31 +0200957static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200958{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200959 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200961 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200962 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000964 for (i = 0; i < hash->size; i++) {
965 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200966 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000967
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200968 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200969 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200970 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000971 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972}
973
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200976 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200977 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200978 struct batadv_tt_common_entry *tt_common_entry;
979 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800980 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200981 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200982 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200983
Sven Eckelmann807736f2012-07-15 22:26:51 +0200984 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000985 return;
986
Sven Eckelmann807736f2012-07-15 22:26:51 +0200987 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
991 list_lock = &hash->list_locks[i];
992
993 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200995 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800996 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200997 tt_local = container_of(tt_common_entry,
998 struct batadv_tt_local_entry,
999 common);
1000 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001001 }
1002 spin_unlock_bh(list_lock);
1003 }
1004
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001005 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001006
Sven Eckelmann807736f2012-07-15 22:26:51 +02001007 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008}
1009
Sven Eckelmann56303d32012-06-05 22:31:31 +02001010static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001012 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001013 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Sven Eckelmann807736f2012-07-15 22:26:51 +02001015 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016
Sven Eckelmann807736f2012-07-15 22:26:51 +02001017 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001018 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019
Antonio Quartullidec05072012-11-10 11:00:32 +01001020 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1021 &batadv_tt_global_hash_lock_class_key);
1022
Sven Eckelmann5346c352012-05-05 13:27:28 +02001023 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024}
1025
Sven Eckelmann56303d32012-06-05 22:31:31 +02001026static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001027{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001028 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001029
Sven Eckelmann807736f2012-07-15 22:26:51 +02001030 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031
Sven Eckelmann807736f2012-07-15 22:26:51 +02001032 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001033 list) {
1034 list_del(&entry->list);
1035 kfree(entry);
1036 }
1037
Sven Eckelmann807736f2012-07-15 22:26:51 +02001038 atomic_set(&bat_priv->tt.local_changes, 0);
1039 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040}
1041
Antonio Quartullid657e622012-07-01 14:09:12 +02001042/* retrieves the orig_tt_list_entry belonging to orig_node from the
1043 * batadv_tt_global_entry list
1044 *
1045 * returns it with an increased refcounter, NULL if not found
1046 */
1047static struct batadv_tt_orig_list_entry *
1048batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1049 const struct batadv_orig_node *orig_node)
1050{
1051 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1052 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001053
1054 rcu_read_lock();
1055 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001056 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001057 if (tmp_orig_entry->orig_node != orig_node)
1058 continue;
1059 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1060 continue;
1061
1062 orig_entry = tmp_orig_entry;
1063 break;
1064 }
1065 rcu_read_unlock();
1066
1067 return orig_entry;
1068}
1069
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001071 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073static bool
1074batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1075 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076{
Antonio Quartullid657e622012-07-01 14:09:12 +02001077 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001078 bool found = false;
1079
Antonio Quartullid657e622012-07-01 14:09:12 +02001080 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1081 if (orig_entry) {
1082 found = true;
1083 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001084 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001085
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086 return found;
1087}
1088
Sven Eckelmanna5130882012-05-16 20:23:16 +02001089static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001090batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001091 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001092{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001093 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094
Antonio Quartullid657e622012-07-01 14:09:12 +02001095 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001096 if (orig_entry) {
1097 /* refresh the ttvn: the current value could be a bogus one that
1098 * was added during a "temporary client detection"
1099 */
1100 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001101 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001102 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001103
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001104 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1105 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001106 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001107
1108 INIT_HLIST_NODE(&orig_entry->list);
1109 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001110 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001111 orig_entry->orig_node = orig_node;
1112 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001113 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001114
Antonio Quartullid657e622012-07-01 14:09:12 +02001115 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001116 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001117 &tt_global->orig_list);
1118 spin_unlock_bh(&tt_global->list_lock);
1119out:
1120 if (orig_entry)
1121 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001122}
1123
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001124/**
1125 * batadv_tt_global_add - add a new TT global entry or update an existing one
1126 * @bat_priv: the bat priv with all the soft interface information
1127 * @orig_node: the originator announcing the client
1128 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001129 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001130 * @flags: TT flags that have to be set for this non-mesh client
1131 * @ttvn: the tt version number ever announcing this non-mesh client
1132 *
1133 * Add a new TT global entry for the given originator. If the entry already
1134 * exists add a new reference to the given originator (a global entry can have
1135 * references to multiple originators) and adjust the flags attribute to reflect
1136 * the function argument.
1137 * If a TT local entry exists for this non-mesh client remove it.
1138 *
1139 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001140 *
1141 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001142 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001143static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1144 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001145 const unsigned char *tt_addr,
1146 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001147 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001149 struct batadv_tt_global_entry *tt_global_entry;
1150 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001151 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001152 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001154 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001155
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001156 /* ignore global entries from backbone nodes */
1157 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1158 return true;
1159
Antonio Quartullic018ad32013-06-04 12:11:39 +02001160 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1161 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001162
1163 /* if the node already has a local client for this entry, it has to wait
1164 * for a roaming advertisement instead of manually messing up the global
1165 * table
1166 */
1167 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1168 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1169 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001170
Antonio Quartullia73105b2011-04-27 14:27:44 +02001171 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001172 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001174 goto out;
1175
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001176 common = &tt_global_entry->common;
1177 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001178 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001179
Antonio Quartullid4f44692012-05-25 00:00:54 +02001180 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001181 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001182 /* node must store current time in case of roaming. This is
1183 * needed to purge this entry out on timeout (if nobody claims
1184 * it)
1185 */
1186 if (flags & BATADV_TT_CLIENT_ROAM)
1187 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001188 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001189 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001190
1191 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1192 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001193
Sven Eckelmann807736f2012-07-15 22:26:51 +02001194 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001195 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001196 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001197 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001198
1199 if (unlikely(hash_added != 0)) {
1200 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001201 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001202 goto out_remove;
1203 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001204 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001205 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001206 /* If there is already a global entry, we can use this one for
1207 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001208 * But if we are trying to add a temporary client then here are
1209 * two options at this point:
1210 * 1) the global client is not a temporary client: the global
1211 * client has to be left as it is, temporary information
1212 * should never override any already known client state
1213 * 2) the global client is a temporary client: purge the
1214 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001215 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001216 if (flags & BATADV_TT_CLIENT_TEMP) {
1217 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1218 goto out;
1219 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1220 orig_node))
1221 goto out_remove;
1222 batadv_tt_global_del_orig_list(tt_global_entry);
1223 goto add_orig_entry;
1224 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001225
1226 /* if the client was temporary added before receiving the first
1227 * OGM announcing it, we have to clear the TEMP flag
1228 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001229 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001230
Antonio Quartullie9c001362012-11-07 15:05:33 +01001231 /* the change can carry possible "attribute" flags like the
1232 * TT_CLIENT_WIFI, therefore they have to be copied in the
1233 * client entry
1234 */
1235 tt_global_entry->common.flags |= flags;
1236
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001237 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1238 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001239 * delete + roaming change for this originator.
1240 *
1241 * We should first delete the old originator before adding the
1242 * new one.
1243 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001244 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001245 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001246 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001247 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001248 }
1249 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001250add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001251 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001252 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001253
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001254 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001255 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1256 common->addr, BATADV_PRINT_VID(common->vid),
1257 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001258 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001260out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001261
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001263 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001264 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001265 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001266 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1267
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001268 if (!(flags & BATADV_TT_CLIENT_ROAM))
1269 /* this is a normal global add. Therefore the client is not in a
1270 * roaming state anymore.
1271 */
1272 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1273
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001274out:
1275 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001276 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001277 if (tt_local_entry)
1278 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001279 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001280}
1281
Sven Eckelmann981d8902012-10-07 13:34:15 +02001282/* batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001283 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001284 * @tt_global_entry: global translation table entry to be analyzed
1285 *
1286 * This functon assumes the caller holds rcu_read_lock().
1287 * Returns best originator list entry or NULL on errors.
1288 */
1289static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001290batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1291 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001292{
Antonio Quartulli46274562013-09-03 11:10:24 +02001293 struct batadv_neigh_node *router, *best_router = NULL;
1294 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001295 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001296 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001297
1298 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001299 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001300 router = batadv_orig_node_get_router(orig_entry->orig_node);
1301 if (!router)
1302 continue;
1303
Antonio Quartulli46274562013-09-03 11:10:24 +02001304 if (best_router &&
1305 bao->bat_neigh_cmp(router, best_router) <= 0) {
1306 batadv_neigh_node_free_ref(router);
1307 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001308 }
1309
Antonio Quartulli46274562013-09-03 11:10:24 +02001310 /* release the refcount for the "old" best */
1311 if (best_router)
1312 batadv_neigh_node_free_ref(best_router);
1313
1314 best_entry = orig_entry;
1315 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001316 }
1317
Antonio Quartulli46274562013-09-03 11:10:24 +02001318 if (best_router)
1319 batadv_neigh_node_free_ref(best_router);
1320
Sven Eckelmann981d8902012-10-07 13:34:15 +02001321 return best_entry;
1322}
1323
1324/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1325 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001326 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001327 * @tt_global_entry: global translation table entry to be printed
1328 * @seq: debugfs table seq_file struct
1329 *
1330 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001331 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001332static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001333batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1334 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001335 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001336{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001337 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001338 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001339 struct batadv_orig_node_vlan *vlan;
1340 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001341 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001342 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001343
1344 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001345 flags = tt_common_entry->flags;
1346
Antonio Quartulli46274562013-09-03 11:10:24 +02001347 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001348 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001349 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1350 tt_common_entry->vid);
1351 if (!vlan) {
1352 seq_printf(seq,
1353 " * Cannot retrieve VLAN %d for originator %pM\n",
1354 BATADV_PRINT_VID(tt_common_entry->vid),
1355 best_entry->orig_node->orig);
1356 goto print_list;
1357 }
1358
Sven Eckelmann981d8902012-10-07 13:34:15 +02001359 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001360 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001361 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001362 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001363 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001364 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001365 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001366 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1367 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1368 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001369
1370 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001371 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001372
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001373print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001374 head = &tt_global_entry->orig_list;
1375
Sasha Levinb67bfe02013-02-27 17:06:00 -08001376 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001377 if (best_entry == orig_entry)
1378 continue;
1379
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001380 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1381 tt_common_entry->vid);
1382 if (!vlan) {
1383 seq_printf(seq,
1384 " + Cannot retrieve VLAN %d for originator %pM\n",
1385 BATADV_PRINT_VID(tt_common_entry->vid),
1386 orig_entry->orig_node->orig);
1387 continue;
1388 }
1389
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001390 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001391 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001392 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001393 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001394 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001395 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001396 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001397 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001398 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1399 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001400
1401 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001402 }
1403}
1404
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001405int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001406{
1407 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001408 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001409 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001410 struct batadv_tt_common_entry *tt_common_entry;
1411 struct batadv_tt_global_entry *tt_global;
1412 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001413 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001414 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001415
Marek Lindner30da63a2012-08-03 17:15:46 +02001416 primary_if = batadv_seq_print_text_primary_if_get(seq);
1417 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001418 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001419
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001420 seq_printf(seq,
1421 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001422 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001423 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1424 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1425 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001426
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001427 for (i = 0; i < hash->size; i++) {
1428 head = &hash->table[i];
1429
Marek Lindner7aadf882011-02-18 12:28:09 +00001430 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001431 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001432 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001433 tt_global = container_of(tt_common_entry,
1434 struct batadv_tt_global_entry,
1435 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001436 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001437 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001438 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001439 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001440out:
1441 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001442 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001443 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001444}
1445
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001446/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001447static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001448batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001449{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001450 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001451 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001452 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001453
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001454 spin_lock_bh(&tt_global_entry->list_lock);
1455 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001456 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1457 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001458 batadv_tt_global_size_dec(orig_entry->orig_node,
1459 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001460 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001461 }
1462 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001463}
1464
Sven Eckelmanna5130882012-05-16 20:23:16 +02001465static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001466batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1467 struct batadv_tt_global_entry *tt_global_entry,
1468 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001469 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001470{
1471 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001472 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001473 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001474 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001475
1476 spin_lock_bh(&tt_global_entry->list_lock);
1477 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001478 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001479 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001480 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001481 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001482 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001483 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001484 tt_global_entry->common.addr,
1485 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001486 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001487 batadv_tt_global_size_dec(orig_node,
1488 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001489 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001490 }
1491 }
1492 spin_unlock_bh(&tt_global_entry->list_lock);
1493}
1494
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001495/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001496 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1497 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001498 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001499static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001500batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1501 struct batadv_tt_global_entry *tt_global_entry,
1502 struct batadv_orig_node *orig_node,
1503 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001504{
1505 bool last_entry = true;
1506 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001507 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001508
1509 /* no local entry exists, case 1:
1510 * Check if this is the last one or if other entries exist.
1511 */
1512
1513 rcu_read_lock();
1514 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001515 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001516 if (orig_entry->orig_node != orig_node) {
1517 last_entry = false;
1518 break;
1519 }
1520 }
1521 rcu_read_unlock();
1522
1523 if (last_entry) {
1524 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001525 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001526 tt_global_entry->roam_at = jiffies;
1527 } else
1528 /* there is another entry, we can simply delete this
1529 * one and can still use the other one.
1530 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001531 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1532 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001533}
1534
Antonio Quartullic018ad32013-06-04 12:11:39 +02001535/**
1536 * batadv_tt_global_del - remove a client from the global table
1537 * @bat_priv: the bat priv with all the soft interface information
1538 * @orig_node: an originator serving this client
1539 * @addr: the mac address of the client
1540 * @vid: VLAN identifier
1541 * @message: a message explaining the reason for deleting the client to print
1542 * for debugging purpose
1543 * @roaming: true if the deletion has been triggered by a roaming event
1544 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001545static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1546 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001547 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001548 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001549{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001550 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001551 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001552
Antonio Quartullic018ad32013-06-04 12:11:39 +02001553 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001554 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001555 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001557 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001558 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1559 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001560
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001561 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001562 batadv_tt_global_free(bat_priv, tt_global_entry,
1563 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001564
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001565 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001566 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001567
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001568 /* if we are deleting a global entry due to a roam
1569 * event, there are two possibilities:
1570 * 1) the client roamed from node A to node B => if there
1571 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001572 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001573 * wait for node B to claim it. In case of timeout
1574 * the entry is purged.
1575 *
1576 * If there are other originators left, we directly delete
1577 * the originator.
1578 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001579 * the global entry, since it is useless now.
1580 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001581 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001582 tt_global_entry->common.addr,
1583 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001584 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001585 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001586 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001587 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001588 } else
1589 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001590 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1591 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001592
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001593
Antonio Quartullicc47f662011-04-27 14:27:57 +02001594out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001595 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001596 batadv_tt_global_entry_free_ref(tt_global_entry);
1597 if (local_entry)
1598 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599}
1600
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001601/**
1602 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1603 * given originator matching the provided vid
1604 * @bat_priv: the bat priv with all the soft interface information
1605 * @orig_node: the originator owning the entries to remove
1606 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1607 * removed
1608 * @message: debug message to print as "reason"
1609 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001610void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1611 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001612 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001613 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001615 struct batadv_tt_global_entry *tt_global;
1616 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001617 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001618 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001619 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001621 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001622 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623
Simon Wunderlich6e801492011-10-19 10:28:26 +02001624 if (!hash)
1625 return;
1626
Antonio Quartullia73105b2011-04-27 14:27:44 +02001627 for (i = 0; i < hash->size; i++) {
1628 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001629 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001630
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001631 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001632 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001633 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001634 /* remove only matching entries */
1635 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1636 continue;
1637
Sven Eckelmann56303d32012-06-05 22:31:31 +02001638 tt_global = container_of(tt_common_entry,
1639 struct batadv_tt_global_entry,
1640 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001641
Sven Eckelmann56303d32012-06-05 22:31:31 +02001642 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001643 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001644
Sven Eckelmann56303d32012-06-05 22:31:31 +02001645 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001646 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001647 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001648 "Deleting global tt entry %pM (vid: %d): %s\n",
1649 tt_global->common.addr,
1650 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001651 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001652 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001653 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001654 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001655 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001656 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001657 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001658}
1659
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001660static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1661 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001662{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001663 bool purge = false;
1664 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1665 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001666
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001667 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1668 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1669 purge = true;
1670 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001671 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001672
1673 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1674 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1675 purge = true;
1676 *msg = "Temporary client timeout\n";
1677 }
1678
1679 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001680}
1681
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001682static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001683{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001684 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001685 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001686 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001687 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001688 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001689 char *msg = NULL;
1690 struct batadv_tt_common_entry *tt_common;
1691 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001692
Antonio Quartullicc47f662011-04-27 14:27:57 +02001693 for (i = 0; i < hash->size; i++) {
1694 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001695 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001696
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001697 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001698 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001699 hash_entry) {
1700 tt_global = container_of(tt_common,
1701 struct batadv_tt_global_entry,
1702 common);
1703
1704 if (!batadv_tt_global_to_purge(tt_global, &msg))
1705 continue;
1706
1707 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001708 "Deleting global tt entry %pM (vid: %d): %s\n",
1709 tt_global->common.addr,
1710 BATADV_PRINT_VID(tt_global->common.vid),
1711 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001712
Sasha Levinb67bfe02013-02-27 17:06:00 -08001713 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001714
1715 batadv_tt_global_entry_free_ref(tt_global);
1716 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001717 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001718 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001719}
1720
Sven Eckelmann56303d32012-06-05 22:31:31 +02001721static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001722{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001723 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001724 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001725 struct batadv_tt_common_entry *tt_common_entry;
1726 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001727 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001728 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001729 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001730
Sven Eckelmann807736f2012-07-15 22:26:51 +02001731 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001732 return;
1733
Sven Eckelmann807736f2012-07-15 22:26:51 +02001734 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001735
1736 for (i = 0; i < hash->size; i++) {
1737 head = &hash->table[i];
1738 list_lock = &hash->list_locks[i];
1739
1740 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001741 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001742 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001743 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001744 tt_global = container_of(tt_common_entry,
1745 struct batadv_tt_global_entry,
1746 common);
1747 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001748 }
1749 spin_unlock_bh(list_lock);
1750 }
1751
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001752 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001753
Sven Eckelmann807736f2012-07-15 22:26:51 +02001754 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001755}
1756
Sven Eckelmann56303d32012-06-05 22:31:31 +02001757static bool
1758_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1759 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001760{
1761 bool ret = false;
1762
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001763 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1764 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001765 ret = true;
1766
1767 return ret;
1768}
1769
Antonio Quartullic018ad32013-06-04 12:11:39 +02001770/**
1771 * batadv_transtable_search - get the mesh destination for a given client
1772 * @bat_priv: the bat priv with all the soft interface information
1773 * @src: mac address of the source client
1774 * @addr: mac address of the destination client
1775 * @vid: VLAN identifier
1776 *
1777 * Returns a pointer to the originator that was selected as destination in the
1778 * mesh for contacting the client 'addr', NULL otherwise.
1779 * In case of multiple originators serving the same client, the function returns
1780 * the best one (best in terms of metric towards the destination node).
1781 *
1782 * If the two clients are AP isolated the function returns NULL.
1783 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001784struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1785 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001786 const uint8_t *addr,
1787 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001788{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001789 struct batadv_tt_local_entry *tt_local_entry = NULL;
1790 struct batadv_tt_global_entry *tt_global_entry = NULL;
1791 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001792 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001793 bool ap_isolation_enabled = false;
1794 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001795
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001796 /* if the AP isolation is requested on a VLAN, then check for its
1797 * setting in the proper VLAN private data structure
1798 */
1799 vlan = batadv_softif_vlan_get(bat_priv, vid);
1800 if (vlan) {
1801 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1802 batadv_softif_vlan_free_ref(vlan);
1803 }
1804
1805 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001806 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001807 if (!tt_local_entry ||
1808 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001809 goto out;
1810 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001811
Antonio Quartullic018ad32013-06-04 12:11:39 +02001812 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001813 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001814 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001815
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001816 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001817 * isolation
1818 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001819 if (tt_local_entry &&
1820 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001821 goto out;
1822
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001823 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001824 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001825 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001826 if (best_entry)
1827 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001828 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1829 orig_node = NULL;
1830 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001831
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001832out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001833 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001834 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001835 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001836 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001837
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001838 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001839}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840
Antonio Quartulliced72932013-04-24 16:37:51 +02001841/**
1842 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1843 * to the given orig_node
1844 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001845 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001846 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001847 *
1848 * This function computes the checksum for the global table corresponding to a
1849 * specific originator. In particular, the checksum is computed as follows: For
1850 * each client connected to the originator the CRC32C of the MAC address and the
1851 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1852 * together.
1853 *
1854 * The idea behind is that CRC32C should be used as much as possible in order to
1855 * produce a unique hash of the table, but since the order which is used to feed
1856 * the CRC32C function affects the result and since every node in the network
1857 * probably sorts the clients differently, the hash function cannot be directly
1858 * computed over the entire table. Hence the CRC32C is used only on
1859 * the single client entry, while all the results are then xor'ed together
1860 * because the XOR operation can combine them all while trying to reduce the
1861 * noise as much as possible.
1862 *
1863 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001864 */
1865static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001866 struct batadv_orig_node *orig_node,
1867 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001869 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001870 struct batadv_tt_common_entry *tt_common;
1871 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001873 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874
1875 for (i = 0; i < hash->size; i++) {
1876 head = &hash->table[i];
1877
1878 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001879 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001880 tt_global = container_of(tt_common,
1881 struct batadv_tt_global_entry,
1882 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001883 /* compute the CRC only for entries belonging to the
1884 * VLAN identified by the vid passed as parameter
1885 */
1886 if (tt_common->vid != vid)
1887 continue;
1888
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001889 /* Roaming clients are in the global table for
1890 * consistency only. They don't have to be
1891 * taken into account while computing the
1892 * global crc
1893 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001894 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001895 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001896 /* Temporary clients have not been announced yet, so
1897 * they have to be skipped while computing the global
1898 * crc
1899 */
1900 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1901 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001902
1903 /* find out if this global entry is announced by this
1904 * originator
1905 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001906 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001907 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001908 continue;
1909
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001910 crc_tmp = crc32c(0, &tt_common->vid,
1911 sizeof(tt_common->vid));
1912 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001913 }
1914 rcu_read_unlock();
1915 }
1916
Antonio Quartulliced72932013-04-24 16:37:51 +02001917 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918}
1919
Antonio Quartulliced72932013-04-24 16:37:51 +02001920/**
1921 * batadv_tt_local_crc - calculates the checksum of the local table
1922 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001923 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001924 *
1925 * For details about the computation, please refer to the documentation for
1926 * batadv_tt_global_crc().
1927 *
1928 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001929 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001930static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
1931 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001933 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001934 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001935 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001936 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937
1938 for (i = 0; i < hash->size; i++) {
1939 head = &hash->table[i];
1940
1941 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001942 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001943 /* compute the CRC only for entries belonging to the
1944 * VLAN identified by vid
1945 */
1946 if (tt_common->vid != vid)
1947 continue;
1948
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001949 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001950 * account while computing the CRC
1951 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001952 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001953 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001954
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001955 crc_tmp = crc32c(0, &tt_common->vid,
1956 sizeof(tt_common->vid));
1957 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959 rcu_read_unlock();
1960 }
1961
Antonio Quartulliced72932013-04-24 16:37:51 +02001962 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963}
1964
Sven Eckelmann56303d32012-06-05 22:31:31 +02001965static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001966{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001967 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001968
Sven Eckelmann807736f2012-07-15 22:26:51 +02001969 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970
Sven Eckelmann807736f2012-07-15 22:26:51 +02001971 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972 list_del(&node->list);
1973 kfree(node);
1974 }
1975
Sven Eckelmann807736f2012-07-15 22:26:51 +02001976 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001977}
1978
Sven Eckelmann56303d32012-06-05 22:31:31 +02001979static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1980 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001981 const void *tt_buff,
1982 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001984 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001985 * last OGM (the OGM could carry no changes)
1986 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001987 spin_lock_bh(&orig_node->tt_buff_lock);
1988 if (tt_buff_len > 0) {
1989 kfree(orig_node->tt_buff);
1990 orig_node->tt_buff_len = 0;
1991 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1992 if (orig_node->tt_buff) {
1993 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1994 orig_node->tt_buff_len = tt_buff_len;
1995 }
1996 }
1997 spin_unlock_bh(&orig_node->tt_buff_lock);
1998}
1999
Sven Eckelmann56303d32012-06-05 22:31:31 +02002000static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002002 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003
Sven Eckelmann807736f2012-07-15 22:26:51 +02002004 spin_lock_bh(&bat_priv->tt.req_list_lock);
2005 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002006 if (batadv_has_timed_out(node->issued_at,
2007 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002008 list_del(&node->list);
2009 kfree(node);
2010 }
2011 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002012 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013}
2014
2015/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002016 * has already been issued for this orig_node, NULL otherwise
2017 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002018static struct batadv_tt_req_node *
2019batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2020 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002022 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023
Sven Eckelmann807736f2012-07-15 22:26:51 +02002024 spin_lock_bh(&bat_priv->tt.req_list_lock);
2025 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002026 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2027 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002028 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002029 goto unlock;
2030 }
2031
2032 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2033 if (!tt_req_node)
2034 goto unlock;
2035
2036 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2037 tt_req_node->issued_at = jiffies;
2038
Sven Eckelmann807736f2012-07-15 22:26:51 +02002039 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002040unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002041 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042 return tt_req_node;
2043}
2044
Marek Lindner335fbe02013-04-23 21:40:02 +08002045/**
2046 * batadv_tt_local_valid - verify that given tt entry is a valid one
2047 * @entry_ptr: to be checked local tt entry
2048 * @data_ptr: not used but definition required to satisfy the callback prototype
2049 *
2050 * Returns 1 if the entry is a valid, 0 otherwise.
2051 */
2052static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002053{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002054 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002055
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002056 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002057 return 0;
2058 return 1;
2059}
2060
Sven Eckelmanna5130882012-05-16 20:23:16 +02002061static int batadv_tt_global_valid(const void *entry_ptr,
2062 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002063{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002064 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2065 const struct batadv_tt_global_entry *tt_global_entry;
2066 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002067
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002068 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2069 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002070 return 0;
2071
Sven Eckelmann56303d32012-06-05 22:31:31 +02002072 tt_global_entry = container_of(tt_common_entry,
2073 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002074 common);
2075
Sven Eckelmanna5130882012-05-16 20:23:16 +02002076 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002077}
2078
Marek Lindner335fbe02013-04-23 21:40:02 +08002079/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002080 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2081 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002082 * @bat_priv: the bat priv with all the soft interface information
2083 * @hash: hash table containing the tt entries
2084 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002085 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002086 * @valid_cb: function to filter tt change entries
2087 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002088 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002089static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2090 struct batadv_hashtable *hash,
2091 void *tvlv_buff, uint16_t tt_len,
2092 int (*valid_cb)(const void *, const void *),
2093 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002094{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002095 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002096 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002098 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002099 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002100
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002101 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002102 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002103
2104 rcu_read_lock();
2105 for (i = 0; i < hash->size; i++) {
2106 head = &hash->table[i];
2107
Sasha Levinb67bfe02013-02-27 17:06:00 -08002108 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002109 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002110 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111 break;
2112
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002113 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002114 continue;
2115
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002116 memcpy(tt_change->addr, tt_common_entry->addr,
2117 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002118 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002119 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08002120 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121
Marek Lindner335fbe02013-04-23 21:40:02 +08002122 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002123 tt_change++;
2124 }
2125 }
2126 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002127}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002128
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002129/**
2130 * batadv_tt_global_check_crc - check if all the CRCs are correct
2131 * @orig_node: originator for which the CRCs have to be checked
2132 * @tt_vlan: pointer to the first tvlv VLAN entry
2133 * @num_vlan: number of tvlv VLAN entries
2134 * @create: if true, create VLAN objects if not found
2135 *
2136 * Return true if all the received CRCs match the locally stored ones, false
2137 * otherwise
2138 */
2139static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2140 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2141 uint16_t num_vlan)
2142{
2143 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2144 struct batadv_orig_node_vlan *vlan;
2145 int i;
2146
2147 /* check if each received CRC matches the locally stored one */
2148 for (i = 0; i < num_vlan; i++) {
2149 tt_vlan_tmp = tt_vlan + i;
2150
2151 /* if orig_node is a backbone node for this VLAN, don't check
2152 * the CRC as we ignore all the global entries over it
2153 */
2154 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002155 orig_node->orig,
2156 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002157 continue;
2158
2159 vlan = batadv_orig_node_vlan_get(orig_node,
2160 ntohs(tt_vlan_tmp->vid));
2161 if (!vlan)
2162 return false;
2163
2164 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2165 return false;
2166 }
2167
2168 return true;
2169}
2170
2171/**
2172 * batadv_tt_local_update_crc - update all the local CRCs
2173 * @bat_priv: the bat priv with all the soft interface information
2174 */
2175static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2176{
2177 struct batadv_softif_vlan *vlan;
2178
2179 /* recompute the global CRC for each VLAN */
2180 rcu_read_lock();
2181 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2182 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2183 }
2184 rcu_read_unlock();
2185}
2186
2187/**
2188 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2189 * @bat_priv: the bat priv with all the soft interface information
2190 * @orig_node: the orig_node for which the CRCs have to be updated
2191 */
2192static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2193 struct batadv_orig_node *orig_node)
2194{
2195 struct batadv_orig_node_vlan *vlan;
2196 uint32_t crc;
2197
2198 /* recompute the global CRC for each VLAN */
2199 rcu_read_lock();
2200 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2201 /* if orig_node is a backbone node for this VLAN, don't compute
2202 * the CRC as we ignore all the global entries over it
2203 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002204 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2205 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002206 continue;
2207
2208 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2209 vlan->tt.crc = crc;
2210 }
2211 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002212}
2213
Antonio Quartulliced72932013-04-24 16:37:51 +02002214/**
2215 * batadv_send_tt_request - send a TT Request message to a given node
2216 * @bat_priv: the bat priv with all the soft interface information
2217 * @dst_orig_node: the destination of the message
2218 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002219 * @tt_vlan: pointer to the first tvlv VLAN object to request
2220 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002221 * @full_table: ask for the entire translation table if true, while only for the
2222 * last TT diff otherwise
2223 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002224static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2225 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002226 uint8_t ttvn,
2227 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2228 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002229{
Marek Lindner335fbe02013-04-23 21:40:02 +08002230 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002231 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002232 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2233 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002234 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002235 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002236
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002237 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002238 if (!primary_if)
2239 goto out;
2240
2241 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002242 * reply from the same orig_node yet
2243 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002244 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002245 if (!tt_req_node)
2246 goto out;
2247
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002248 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2249 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002250 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002251 goto out;
2252
Marek Lindner335fbe02013-04-23 21:40:02 +08002253 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2254 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002255 tvlv_tt_data->num_vlan = htons(num_vlan);
2256
2257 /* send all the CRCs within the request. This is needed by intermediate
2258 * nodes to ensure they have the correct table before replying
2259 */
2260 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2261 for (i = 0; i < num_vlan; i++) {
2262 tt_vlan_req->vid = tt_vlan->vid;
2263 tt_vlan_req->crc = tt_vlan->crc;
2264
2265 tt_vlan_req++;
2266 tt_vlan++;
2267 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002268
2269 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002270 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002271
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002272 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002273 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002274
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002275 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002276 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2277 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002278 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002279 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002280
2281out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002282 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002283 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002284 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002285 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002286 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002288 kfree(tt_req_node);
2289 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002290 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002291 return ret;
2292}
2293
Marek Lindner335fbe02013-04-23 21:40:02 +08002294/**
2295 * batadv_send_other_tt_response - send reply to tt request concerning another
2296 * node's translation table
2297 * @bat_priv: the bat priv with all the soft interface information
2298 * @tt_data: tt data containing the tt request information
2299 * @req_src: mac address of tt request sender
2300 * @req_dst: mac address of tt request recipient
2301 *
2302 * Returns true if tt request reply was sent, false otherwise.
2303 */
2304static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2305 struct batadv_tvlv_tt_data *tt_data,
2306 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002307{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002308 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002309 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002310 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002311 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002312 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002313 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002314 uint8_t orig_ttvn, req_ttvn;
2315 uint16_t tvlv_len;
2316 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002317
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002318 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002319 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002320 req_src, tt_data->ttvn, req_dst,
2321 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002322
2323 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002324 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002325 if (!req_dst_orig_node)
2326 goto out;
2327
Marek Lindner335fbe02013-04-23 21:40:02 +08002328 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002329 if (!res_dst_orig_node)
2330 goto out;
2331
Antonio Quartullia73105b2011-04-27 14:27:44 +02002332 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002333 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002334
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002335 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002336 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002337 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002338 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2339 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340 goto out;
2341
Antonio Quartulli015758d2011-07-09 17:52:13 +02002342 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002343 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002344 !req_dst_orig_node->tt_buff)
2345 full_table = true;
2346 else
2347 full_table = false;
2348
Marek Lindner335fbe02013-04-23 21:40:02 +08002349 /* TT fragmentation hasn't been implemented yet, so send as many
2350 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002351 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002352 if (!full_table) {
2353 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2354 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002355
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002356 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2357 &tvlv_tt_data,
2358 &tt_change,
2359 &tt_len);
2360 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002361 goto unlock;
2362
Antonio Quartullia73105b2011-04-27 14:27:44 +02002363 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002364 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002365 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002366 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2367 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002368 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2369 * in the initial part
2370 */
2371 tt_len = -1;
2372 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2373 &tvlv_tt_data,
2374 &tt_change,
2375 &tt_len);
2376 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002377 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002378
2379 /* fill the rest of the tvlv with the real TT entries */
2380 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2381 tt_change, tt_len,
2382 batadv_tt_global_valid,
2383 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384 }
2385
Marek Lindner335fbe02013-04-23 21:40:02 +08002386 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2387 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002388
2389 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002390 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002391
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002392 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002393 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2394 res_dst_orig_node->orig, req_dst_orig_node->orig,
2395 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002396
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002397 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002398
Marek Lindner335fbe02013-04-23 21:40:02 +08002399 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002400 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2401 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002402
Marek Lindner335fbe02013-04-23 21:40:02 +08002403 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002404 goto out;
2405
2406unlock:
2407 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2408
2409out:
2410 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002411 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002412 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002413 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002414 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002415 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002416}
Sven Eckelmann96412692012-06-05 22:31:30 +02002417
Marek Lindner335fbe02013-04-23 21:40:02 +08002418/**
2419 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2420 * translation table
2421 * @bat_priv: the bat priv with all the soft interface information
2422 * @tt_data: tt data containing the tt request information
2423 * @req_src: mac address of tt request sender
2424 *
2425 * Returns true if tt request reply was sent, false otherwise.
2426 */
2427static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2428 struct batadv_tvlv_tt_data *tt_data,
2429 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002430{
Marek Lindner335fbe02013-04-23 21:40:02 +08002431 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002432 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002433 struct batadv_tvlv_tt_change *tt_change;
2434 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002435 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002436 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002437 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002438 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002439
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002440 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002441 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002442 req_src, tt_data->ttvn,
2443 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002444
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002445 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002446
Sven Eckelmann807736f2012-07-15 22:26:51 +02002447 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002448 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002449
Marek Lindner335fbe02013-04-23 21:40:02 +08002450 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002451 if (!orig_node)
2452 goto out;
2453
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002454 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002455 if (!primary_if)
2456 goto out;
2457
2458 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002459 * is too big send the whole local translation table
2460 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002461 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002462 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002463 full_table = true;
2464 else
2465 full_table = false;
2466
Marek Lindner335fbe02013-04-23 21:40:02 +08002467 /* TT fragmentation hasn't been implemented yet, so send as many
2468 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002469 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002470 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002471 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002472
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002473 tt_len = bat_priv->tt.last_changeset_len;
2474 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2475 &tvlv_tt_data,
2476 &tt_change,
2477 &tt_len);
2478 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002479 goto unlock;
2480
Marek Lindner335fbe02013-04-23 21:40:02 +08002481 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002482 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002483 bat_priv->tt.last_changeset_len);
2484 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002485 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002486 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002487
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002488 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2489 * in the initial part
2490 */
2491 tt_len = -1;
2492 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2493 &tvlv_tt_data,
2494 &tt_change,
2495 &tt_len);
2496 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002497 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002498
2499 /* fill the rest of the tvlv with the real TT entries */
2500 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2501 tt_change, tt_len,
2502 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002503 }
2504
Marek Lindner335fbe02013-04-23 21:40:02 +08002505 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2506 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002507
2508 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002509 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002510
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002511 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002512 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2513 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002514
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002515 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002516
Marek Lindner335fbe02013-04-23 21:40:02 +08002517 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002518 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2519 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002520
Antonio Quartullia73105b2011-04-27 14:27:44 +02002521 goto out;
2522
2523unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002524 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002525out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002526 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002527 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002528 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002530 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002531 kfree(tvlv_tt_data);
2532 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533 return true;
2534}
2535
Marek Lindner335fbe02013-04-23 21:40:02 +08002536/**
2537 * batadv_send_tt_response - send reply to tt request
2538 * @bat_priv: the bat priv with all the soft interface information
2539 * @tt_data: tt data containing the tt request information
2540 * @req_src: mac address of tt request sender
2541 * @req_dst: mac address of tt request recipient
2542 *
2543 * Returns true if tt request reply was sent, false otherwise.
2544 */
2545static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2546 struct batadv_tvlv_tt_data *tt_data,
2547 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002548{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002549 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002550 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002551 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002552 return batadv_send_other_tt_response(bat_priv, tt_data,
2553 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002554}
2555
Sven Eckelmann56303d32012-06-05 22:31:31 +02002556static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2557 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002558 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002559 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002560{
2561 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002562 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002563
2564 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002565 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2566 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002567 batadv_tt_global_del(bat_priv, orig_node,
2568 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002569 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002570 "tt removed by changes",
2571 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002572 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002573 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002574 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002575 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002576 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002577 /* In case of problem while storing a
2578 * global_entry, we stop the updating
2579 * procedure without committing the
2580 * ttvn change. This will avoid to send
2581 * corrupted data on tt_request
2582 */
2583 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002584 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002585 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002586 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002587}
2588
Sven Eckelmann56303d32012-06-05 22:31:31 +02002589static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002590 struct batadv_tvlv_tt_change *tt_change,
2591 uint8_t ttvn, uint8_t *resp_src,
2592 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002593{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002594 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002595
Marek Lindner335fbe02013-04-23 21:40:02 +08002596 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002597 if (!orig_node)
2598 goto out;
2599
2600 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002601 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2602 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002603
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002604 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2605 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002606
2607 spin_lock_bh(&orig_node->tt_buff_lock);
2608 kfree(orig_node->tt_buff);
2609 orig_node->tt_buff_len = 0;
2610 orig_node->tt_buff = NULL;
2611 spin_unlock_bh(&orig_node->tt_buff_lock);
2612
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002613 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002614
2615out:
2616 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002617 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002618}
2619
Sven Eckelmann56303d32012-06-05 22:31:31 +02002620static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2621 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002622 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002623 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002624{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002625 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2626 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002627
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002628 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2629 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002630 atomic_set(&orig_node->last_ttvn, ttvn);
2631}
2632
Antonio Quartullic018ad32013-06-04 12:11:39 +02002633/**
2634 * batadv_is_my_client - check if a client is served by the local node
2635 * @bat_priv: the bat priv with all the soft interface information
2636 * @addr: the mac adress of the client to check
2637 * @vid: VLAN identifier
2638 *
2639 * Returns true if the client is served by this node, false otherwise.
2640 */
2641bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2642 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002643{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002644 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002645 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646
Antonio Quartullic018ad32013-06-04 12:11:39 +02002647 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002648 if (!tt_local_entry)
2649 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002650 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002651 * consistency purpose)
2652 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002653 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2654 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002655 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002656 ret = true;
2657out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002658 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002659 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002660 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661}
2662
Marek Lindner335fbe02013-04-23 21:40:02 +08002663/**
2664 * batadv_handle_tt_response - process incoming tt reply
2665 * @bat_priv: the bat priv with all the soft interface information
2666 * @tt_data: tt data containing the tt request information
2667 * @resp_src: mac address of tt reply sender
2668 * @num_entries: number of tt change entries appended to the tt data
2669 */
2670static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2671 struct batadv_tvlv_tt_data *tt_data,
2672 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002674 struct batadv_tt_req_node *node, *safe;
2675 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002676 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002677 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2678 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002679
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002680 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002681 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002682 resp_src, tt_data->ttvn, num_entries,
2683 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002684
Marek Lindner335fbe02013-04-23 21:40:02 +08002685 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002686 if (!orig_node)
2687 goto out;
2688
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002689 spin_lock_bh(&orig_node->tt_lock);
2690
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002691 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2692 change_offset *= ntohs(tt_data->num_vlan);
2693 change_offset += sizeof(*tt_data);
2694 tvlv_ptr += change_offset;
2695
2696 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002697 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002698 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2699 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002700 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002701 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2702 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002703 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002704
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002705 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002706 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002707
2708 spin_unlock_bh(&orig_node->tt_lock);
2709
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002711 spin_lock_bh(&bat_priv->tt.req_list_lock);
2712 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002713 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002714 continue;
2715 list_del(&node->list);
2716 kfree(node);
2717 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002718
Sven Eckelmann807736f2012-07-15 22:26:51 +02002719 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002720out:
2721 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002722 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002723}
2724
Sven Eckelmann56303d32012-06-05 22:31:31 +02002725static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002726{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002727 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728
Sven Eckelmann807736f2012-07-15 22:26:51 +02002729 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002730
Sven Eckelmann807736f2012-07-15 22:26:51 +02002731 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002732 list_del(&node->list);
2733 kfree(node);
2734 }
2735
Sven Eckelmann807736f2012-07-15 22:26:51 +02002736 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002737}
2738
Sven Eckelmann56303d32012-06-05 22:31:31 +02002739static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002740{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002741 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002742
Sven Eckelmann807736f2012-07-15 22:26:51 +02002743 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2744 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002745 if (!batadv_has_timed_out(node->first_time,
2746 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002747 continue;
2748
2749 list_del(&node->list);
2750 kfree(node);
2751 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002752 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002753}
2754
2755/* This function checks whether the client already reached the
2756 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2757 * will not be sent.
2758 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002759 * returns true if the ROAMING_ADV can be sent, false otherwise
2760 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002761static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002762 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002763{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002764 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002765 bool ret = false;
2766
Sven Eckelmann807736f2012-07-15 22:26:51 +02002767 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002768 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002769 * reply from the same orig_node yet
2770 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002771 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002772 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002773 continue;
2774
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002775 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002776 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002777 continue;
2778
Sven Eckelmann3e348192012-05-16 20:23:22 +02002779 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002780 /* Sorry, you roamed too many times! */
2781 goto unlock;
2782 ret = true;
2783 break;
2784 }
2785
2786 if (!ret) {
2787 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2788 if (!tt_roam_node)
2789 goto unlock;
2790
2791 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002792 atomic_set(&tt_roam_node->counter,
2793 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002794 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2795
Sven Eckelmann807736f2012-07-15 22:26:51 +02002796 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002797 ret = true;
2798 }
2799
2800unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002801 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002802 return ret;
2803}
2804
Antonio Quartullic018ad32013-06-04 12:11:39 +02002805/**
2806 * batadv_send_roam_adv - send a roaming advertisement message
2807 * @bat_priv: the bat priv with all the soft interface information
2808 * @client: mac address of the roaming client
2809 * @vid: VLAN identifier
2810 * @orig_node: message destination
2811 *
2812 * Send a ROAMING_ADV message to the node which was previously serving this
2813 * client. This is done to inform the node that from now on all traffic destined
2814 * for this particular roamed client has to be forwarded to the sender of the
2815 * roaming message.
2816 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002817static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002818 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002819 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002820{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002821 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002822 struct batadv_tvlv_roam_adv tvlv_roam;
2823
2824 primary_if = batadv_primary_if_get_selected(bat_priv);
2825 if (!primary_if)
2826 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002827
2828 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002829 * already roamed to us too many times
2830 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002831 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002832 goto out;
2833
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002834 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002835 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2836 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002837
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002838 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002839
Marek Lindner122edaa2013-04-23 21:40:03 +08002840 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002841 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002842
2843 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2844 orig_node->orig, BATADV_TVLV_ROAM, 1,
2845 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002846
2847out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002848 if (primary_if)
2849 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002850}
2851
Sven Eckelmanna5130882012-05-16 20:23:16 +02002852static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002853{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002854 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002855 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002856 struct batadv_priv *bat_priv;
2857
2858 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002859 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2860 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002861
Sven Eckelmanna5130882012-05-16 20:23:16 +02002862 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002863 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002864 batadv_tt_req_purge(bat_priv);
2865 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002866
Antonio Quartulli72414442012-12-25 13:14:37 +01002867 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2868 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002869}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002870
Sven Eckelmann56303d32012-06-05 22:31:31 +02002871void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002872{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002873 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2874 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2875
Sven Eckelmann807736f2012-07-15 22:26:51 +02002876 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002877
Sven Eckelmanna5130882012-05-16 20:23:16 +02002878 batadv_tt_local_table_free(bat_priv);
2879 batadv_tt_global_table_free(bat_priv);
2880 batadv_tt_req_list_free(bat_priv);
2881 batadv_tt_changes_list_free(bat_priv);
2882 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002883
Sven Eckelmann807736f2012-07-15 22:26:51 +02002884 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002885}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002886
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002887/**
2888 * batadv_tt_local_set_flags - set or unset the specified flags on the local
2889 * table and possibly count them in the TT size
2890 * @bat_priv: the bat priv with all the soft interface information
2891 * @flags: the flag to switch
2892 * @enable: whether to set or unset the flag
2893 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002894 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002895static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2896 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002897{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002898 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2899 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002900 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002901 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002902 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002903
2904 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002905 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002906
2907 for (i = 0; i < hash->size; i++) {
2908 head = &hash->table[i];
2909
2910 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002911 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002912 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002913 if (enable) {
2914 if ((tt_common_entry->flags & flags) == flags)
2915 continue;
2916 tt_common_entry->flags |= flags;
2917 } else {
2918 if (!(tt_common_entry->flags & flags))
2919 continue;
2920 tt_common_entry->flags &= ~flags;
2921 }
2922 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002923
2924 if (!count)
2925 continue;
2926
2927 batadv_tt_local_size_inc(bat_priv,
2928 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002929 }
2930 rcu_read_unlock();
2931 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002932}
2933
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002934/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002935static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002936{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002937 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002938 struct batadv_tt_common_entry *tt_common;
2939 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002940 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002941 struct hlist_head *head;
2942 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002943 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002944
2945 if (!hash)
2946 return;
2947
2948 for (i = 0; i < hash->size; i++) {
2949 head = &hash->table[i];
2950 list_lock = &hash->list_locks[i];
2951
2952 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002953 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002954 hash_entry) {
2955 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002956 continue;
2957
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002958 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002959 "Deleting local tt entry (%pM, vid: %d): pending\n",
2960 tt_common->addr,
2961 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002962
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002963 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002964 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002965 tt_local = container_of(tt_common,
2966 struct batadv_tt_local_entry,
2967 common);
2968 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002969 }
2970 spin_unlock_bh(list_lock);
2971 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002972}
2973
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002974/**
2975 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2976 * have been queued in the time since the last commit
2977 * @bat_priv: the bat priv with all the soft interface information
2978 */
2979void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002980{
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002981 spin_lock_bh(&bat_priv->tt.commit_lock);
2982
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002983 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2984 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2985 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002986 goto out;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002987 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002988
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002989 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002990
Sven Eckelmanna5130882012-05-16 20:23:16 +02002991 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002992 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002993
2994 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002995 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002996 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002997 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002998 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002999
3000 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003001 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003002 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003003
3004out:
3005 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003006}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003007
Sven Eckelmann56303d32012-06-05 22:31:31 +02003008bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003009 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003010{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003011 struct batadv_tt_local_entry *tt_local_entry = NULL;
3012 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003013 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003014 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003015
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003016 vlan = batadv_softif_vlan_get(bat_priv, vid);
3017 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003018 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003019
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003020 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003021 if (!tt_local_entry)
3022 goto out;
3023
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003024 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003025 if (!tt_global_entry)
3026 goto out;
3027
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003028 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003029 goto out;
3030
Marek Lindner5870adc2012-06-20 17:16:05 +02003031 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003032
3033out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003034 if (vlan)
3035 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003036 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003037 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003038 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003039 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003040 return ret;
3041}
Marek Lindnera943cac2011-07-30 13:10:18 +02003042
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003043/**
3044 * batadv_tt_update_orig - update global translation table with new tt
3045 * information received via ogms
3046 * @bat_priv: the bat priv with all the soft interface information
3047 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003048 * @tt_vlan: pointer to the first tvlv VLAN entry
3049 * @tt_num_vlan: number of tvlv VLAN entries
3050 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003051 * @tt_num_changes: number of tt changes inside the tt buffer
3052 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003053 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003054 */
3055static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3056 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003057 const void *tt_buff, uint16_t tt_num_vlan,
3058 struct batadv_tvlv_tt_change *tt_change,
3059 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003060{
3061 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003062 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003063 bool full_table = true;
3064
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003065 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003066 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003067 * increased by one -> we can apply the attached changes
3068 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003069 if ((!orig_node->tt_initialised && ttvn == 1) ||
3070 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003071 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003072 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3073 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003074 * In this case send a tt request
3075 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003076 if (!tt_num_changes) {
3077 full_table = false;
3078 goto request_table;
3079 }
3080
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003081 spin_lock_bh(&orig_node->tt_lock);
3082
Marek Lindner335fbe02013-04-23 21:40:02 +08003083 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003084 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003085 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003086
3087 /* Even if we received the precomputed crc with the OGM, we
3088 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003089 * in the global table
3090 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003091 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003092
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003093 spin_unlock_bh(&orig_node->tt_lock);
3094
Marek Lindnera943cac2011-07-30 13:10:18 +02003095 /* The ttvn alone is not enough to guarantee consistency
3096 * because a single value could represent different states
3097 * (due to the wrap around). Thus a node has to check whether
3098 * the resulting table (after applying the changes) is still
3099 * consistent or not. E.g. a node could disconnect while its
3100 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3101 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003102 * inconsistency
3103 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003104 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3105 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003106 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003107 } else {
3108 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003109 * in sync anymore -> request fresh tt data
3110 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003111 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003112 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3113 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003114request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003115 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003116 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3117 orig_node->orig, ttvn, orig_ttvn,
3118 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003119 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003120 tt_vlan, tt_num_vlan,
3121 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003122 return;
3123 }
3124 }
3125}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003126
Antonio Quartullic018ad32013-06-04 12:11:39 +02003127/**
3128 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3129 * @bat_priv: the bat priv with all the soft interface information
3130 * @addr: the mac address of the client to check
3131 * @vid: VLAN identifier
3132 *
3133 * Returns true if we know that the client has moved from its old originator
3134 * to another one. This entry is still kept for consistency purposes and will be
3135 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003136 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003137bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003138 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003139{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003140 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003141 bool ret = false;
3142
Antonio Quartullic018ad32013-06-04 12:11:39 +02003143 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003144 if (!tt_global_entry)
3145 goto out;
3146
Antonio Quartullic1d07432013-01-15 22:17:19 +10003147 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003148 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003149out:
3150 return ret;
3151}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003152
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003153/**
3154 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3155 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003156 * @addr: the mac address of the local client to query
3157 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003158 *
3159 * Returns true if the local client is known to be roaming (it is not served by
3160 * this node anymore) or not. If yes, the client is still present in the table
3161 * to keep the latter consistent with the node TTVN
3162 */
3163bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003164 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003165{
3166 struct batadv_tt_local_entry *tt_local_entry;
3167 bool ret = false;
3168
Antonio Quartullic018ad32013-06-04 12:11:39 +02003169 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003170 if (!tt_local_entry)
3171 goto out;
3172
3173 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3174 batadv_tt_local_entry_free_ref(tt_local_entry);
3175out:
3176 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003177}
3178
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003179bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3180 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003181 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003182 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003183{
3184 bool ret = false;
3185
Antonio Quartulli16052782013-06-04 12:11:41 +02003186 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003187 BATADV_TT_CLIENT_TEMP,
3188 atomic_read(&orig_node->last_ttvn)))
3189 goto out;
3190
3191 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003192 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3193 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003194 ret = true;
3195out:
3196 return ret;
3197}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003198
3199/**
3200 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3201 * @bat_priv: the bat priv with all the soft interface information
3202 * @orig: the orig_node of the ogm
3203 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3204 * @tvlv_value: tvlv buffer containing the gateway data
3205 * @tvlv_value_len: tvlv buffer length
3206 */
3207static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3208 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003209 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003210 uint16_t tvlv_value_len)
3211{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003212 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3213 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003214 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003215 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003216
3217 if (tvlv_value_len < sizeof(*tt_data))
3218 return;
3219
3220 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3221 tvlv_value_len -= sizeof(*tt_data);
3222
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003223 num_vlan = ntohs(tt_data->num_vlan);
3224
3225 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3226 return;
3227
3228 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3229 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3230 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3231
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003232 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003233
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003234 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3235 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003236}
3237
3238/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003239 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3240 * container
3241 * @bat_priv: the bat priv with all the soft interface information
3242 * @src: mac address of tt tvlv sender
3243 * @dst: mac address of tt tvlv recipient
3244 * @tvlv_value: tvlv buffer containing the tt data
3245 * @tvlv_value_len: tvlv buffer length
3246 *
3247 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3248 * otherwise.
3249 */
3250static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3251 uint8_t *src, uint8_t *dst,
3252 void *tvlv_value,
3253 uint16_t tvlv_value_len)
3254{
3255 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003256 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003257 char tt_flag;
3258 bool ret;
3259
3260 if (tvlv_value_len < sizeof(*tt_data))
3261 return NET_RX_SUCCESS;
3262
3263 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3264 tvlv_value_len -= sizeof(*tt_data);
3265
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003266 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3267 tt_vlan_len *= ntohs(tt_data->num_vlan);
3268
3269 if (tvlv_value_len < tt_vlan_len)
3270 return NET_RX_SUCCESS;
3271
3272 tvlv_value_len -= tt_vlan_len;
3273 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003274
3275 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3276 case BATADV_TT_REQUEST:
3277 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3278
3279 /* If this node cannot provide a TT response the tt_request is
3280 * forwarded
3281 */
3282 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3283 if (!ret) {
3284 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3285 tt_flag = 'F';
3286 else
3287 tt_flag = '.';
3288
3289 batadv_dbg(BATADV_DBG_TT, bat_priv,
3290 "Routing TT_REQUEST to %pM [%c]\n",
3291 dst, tt_flag);
3292 /* tvlv API will re-route the packet */
3293 return NET_RX_DROP;
3294 }
3295 break;
3296 case BATADV_TT_RESPONSE:
3297 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3298
3299 if (batadv_is_my_mac(bat_priv, dst)) {
3300 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003301 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003302 return NET_RX_SUCCESS;
3303 }
3304
3305 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3306 tt_flag = 'F';
3307 else
3308 tt_flag = '.';
3309
3310 batadv_dbg(BATADV_DBG_TT, bat_priv,
3311 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3312
3313 /* tvlv API will re-route the packet */
3314 return NET_RX_DROP;
3315 }
3316
3317 return NET_RX_SUCCESS;
3318}
3319
3320/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003321 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3322 * @bat_priv: the bat priv with all the soft interface information
3323 * @src: mac address of tt tvlv sender
3324 * @dst: mac address of tt tvlv recipient
3325 * @tvlv_value: tvlv buffer containing the tt data
3326 * @tvlv_value_len: tvlv buffer length
3327 *
3328 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3329 * otherwise.
3330 */
3331static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3332 uint8_t *src, uint8_t *dst,
3333 void *tvlv_value,
3334 uint16_t tvlv_value_len)
3335{
3336 struct batadv_tvlv_roam_adv *roaming_adv;
3337 struct batadv_orig_node *orig_node = NULL;
3338
3339 /* If this node is not the intended recipient of the
3340 * roaming advertisement the packet is forwarded
3341 * (the tvlv API will re-route the packet).
3342 */
3343 if (!batadv_is_my_mac(bat_priv, dst))
3344 return NET_RX_DROP;
3345
Marek Lindner122edaa2013-04-23 21:40:03 +08003346 if (tvlv_value_len < sizeof(*roaming_adv))
3347 goto out;
3348
3349 orig_node = batadv_orig_hash_find(bat_priv, src);
3350 if (!orig_node)
3351 goto out;
3352
3353 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3354 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3355
3356 batadv_dbg(BATADV_DBG_TT, bat_priv,
3357 "Received ROAMING_ADV from %pM (client %pM)\n",
3358 src, roaming_adv->client);
3359
3360 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003361 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003362 atomic_read(&orig_node->last_ttvn) + 1);
3363
3364out:
3365 if (orig_node)
3366 batadv_orig_node_free_ref(orig_node);
3367 return NET_RX_SUCCESS;
3368}
3369
3370/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003371 * batadv_tt_init - initialise the translation table internals
3372 * @bat_priv: the bat priv with all the soft interface information
3373 *
3374 * Return 0 on success or negative error number in case of failure.
3375 */
3376int batadv_tt_init(struct batadv_priv *bat_priv)
3377{
3378 int ret;
3379
3380 ret = batadv_tt_local_init(bat_priv);
3381 if (ret < 0)
3382 return ret;
3383
3384 ret = batadv_tt_global_init(bat_priv);
3385 if (ret < 0)
3386 return ret;
3387
3388 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003389 batadv_tt_tvlv_unicast_handler_v1,
3390 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003391
Marek Lindner122edaa2013-04-23 21:40:03 +08003392 batadv_tvlv_handler_register(bat_priv, NULL,
3393 batadv_roam_tvlv_unicast_handler_v1,
3394 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3395
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003396 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3397 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3398 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3399
3400 return 1;
3401}