blob: 9bf928c0b1882fccd2357bb311422ef25ea74f81 [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
Sven Eckelmanna5130882012-05-16 20:23:16 +0200211static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200212batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200213{
Antonio Quartullid657e622012-07-01 14:09:12 +0200214 if (!atomic_dec_and_test(&orig_entry->refcount))
215 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000216 /* to avoid race conditions, immediately decrease the tt counter */
217 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200218 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200219}
220
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200221/**
222 * batadv_tt_local_event - store a local TT event (ADD/DEL)
223 * @bat_priv: the bat priv with all the soft interface information
224 * @tt_local_entry: the TT entry involved in the event
225 * @event_flags: flags to store in the event structure
226 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200227static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200228 struct batadv_tt_local_entry *tt_local_entry,
229 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200230{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200231 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200232 struct batadv_tt_common_entry *common = &tt_local_entry->common;
233 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200234 bool event_removed = false;
235 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200236
237 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200238 if (!tt_change_node)
239 return;
240
Antonio Quartulliff66c972011-06-30 01:14:00 +0200241 tt_change_node->change.flags = flags;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800242 tt_change_node->change.reserved = 0;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200243 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200244 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200245
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200246 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200247
248 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200249 spin_lock_bh(&bat_priv->tt.changes_list_lock);
250 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200251 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200252 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200253 continue;
254
255 /* DEL+ADD in the same orig interval have no effect and can be
256 * removed to avoid silly behaviour on the receiver side. The
257 * other way around (ADD+DEL) can happen in case of roaming of
258 * a client still in the NEW state. Roaming of NEW clients is
259 * now possible due to automatically recognition of "temporary"
260 * clients
261 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200262 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200263 if (!del_op_requested && del_op_entry)
264 goto del;
265 if (del_op_requested && !del_op_entry)
266 goto del;
267 continue;
268del:
269 list_del(&entry->list);
270 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000271 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200272 event_removed = true;
273 goto unlock;
274 }
275
Antonio Quartullia73105b2011-04-27 14:27:44 +0200276 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200277 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200278
279unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200280 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200281
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200282 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200283 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200284 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200285 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200286}
287
Marek Lindner335fbe02013-04-23 21:40:02 +0800288/**
289 * batadv_tt_len - compute length in bytes of given number of tt changes
290 * @changes_num: number of tt changes
291 *
292 * Returns computed length in bytes.
293 */
294static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200295{
Marek Lindner335fbe02013-04-23 21:40:02 +0800296 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200297}
298
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200299/**
300 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
301 * @tt_len: available space
302 *
303 * Returns the number of entries.
304 */
305static uint16_t batadv_tt_entries(uint16_t tt_len)
306{
307 return tt_len / batadv_tt_len(1);
308}
309
Sven Eckelmann56303d32012-06-05 22:31:31 +0200310static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200312 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200313 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
Sven Eckelmann807736f2012-07-15 22:26:51 +0200315 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Sven Eckelmann807736f2012-07-15 22:26:51 +0200317 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200318 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Antonio Quartullidec05072012-11-10 11:00:32 +0100320 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
321 &batadv_tt_local_hash_lock_class_key);
322
Sven Eckelmann5346c352012-05-05 13:27:28 +0200323 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324}
325
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200326static void batadv_tt_global_free(struct batadv_priv *bat_priv,
327 struct batadv_tt_global_entry *tt_global,
328 const char *message)
329{
330 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200331 "Deleting global tt entry %pM (vid: %d): %s\n",
332 tt_global->common.addr,
333 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200334
335 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200336 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200337 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200338}
339
Antonio Quartullic018ad32013-06-04 12:11:39 +0200340/**
341 * batadv_tt_local_add - add a new client to the local table or update an
342 * existing client
343 * @soft_iface: netdev struct of the mesh interface
344 * @addr: the mac address of the client to add
345 * @vid: VLAN identifier
346 * @ifindex: index of the interface where the client is connected to (useful to
347 * identify wireless clients)
348 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200349void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200350 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200353 struct batadv_tt_local_entry *tt_local;
354 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200355 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200356 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100357 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200358 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Antonio Quartullic018ad32013-06-04 12:11:39 +0200360 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
361 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362
Antonio Quartulli47c94652012-09-23 22:38:35 +0200363 if (tt_local) {
364 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200365 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
366 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200367 "Re-adding pending client %pM (vid: %d)\n",
368 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200369 /* whatever the reason why the PENDING flag was set,
370 * this is a client which was enqueued to be removed in
371 * this orig_interval. Since it popped up again, the
372 * flag can be reset like it was never enqueued
373 */
374 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
375 goto add_event;
376 }
377
378 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
379 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200380 "Roaming client %pM (vid: %d) came back to its original location\n",
381 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200382 /* the ROAM flag is set because this client roamed away
383 * and the node got a roaming_advertisement message. Now
384 * that the client popped up again at its original
385 * location such flag can be unset
386 */
387 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
388 roamed_back = true;
389 }
390 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 }
392
Antonio Quartulli47c94652012-09-23 22:38:35 +0200393 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
394 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200395 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200397 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200398 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
399 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200400 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Antonio Quartulli47c94652012-09-23 22:38:35 +0200402 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100403 /* The local entry has to be marked as NEW to avoid to send it in
404 * a full table response going out before the next ttvn increment
405 * (consistency check)
406 */
407 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200408 tt_local->common.vid = vid;
Sven Eckelmann95638772012-05-12 02:09:31 +0200409 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200410 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
411 atomic_set(&tt_local->common.refcount, 2);
412 tt_local->last_seen = jiffies;
413 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414
415 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200416 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200417 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Sven Eckelmann807736f2012-07-15 22:26:51 +0200419 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200420 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200421 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100422
423 if (unlikely(hash_added != 0)) {
424 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200425 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100426 goto out;
427 }
428
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200429add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200430 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200431
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200432check_roaming:
433 /* Check whether it is a roaming, but don't do anything if the roaming
434 * process has already been handled
435 */
436 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200437 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200438 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200439 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800440 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200441 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200442 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200443 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200444 }
445 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200446 if (roamed_back) {
447 batadv_tt_global_free(bat_priv, tt_global,
448 "Roaming canceled");
449 tt_global = NULL;
450 } else {
451 /* The global entry has to be marked as ROAMING and
452 * has to be kept for consistency purpose
453 */
454 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
455 tt_global->roam_at = jiffies;
456 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200457 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200458
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200459out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200460 if (tt_local)
461 batadv_tt_local_entry_free_ref(tt_local);
462 if (tt_global)
463 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464}
465
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800466/**
467 * batadv_tt_tvlv_container_update - update the translation table tvlv container
468 * after local tt changes have been committed
469 * @bat_priv: the bat priv with all the soft interface information
470 */
471static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800473 struct batadv_tt_change_node *entry, *safe;
474 struct batadv_tvlv_tt_data *tt_data;
475 struct batadv_tvlv_tt_change *tt_change;
476 int tt_diff_len = 0, tt_change_len = 0;
477 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800479 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800480
481 /* if we have too many changes for one packet don't send any
482 * and wait for the tt table request which will be fragmented
483 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800484 if (tt_diff_len > bat_priv->soft_iface->mtu)
485 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800486
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800487 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
488 if (!tt_data)
489 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800490
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800491 tt_data->flags = BATADV_TT_OGM_DIFF;
492 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
Antonio Quartulliced72932013-04-24 16:37:51 +0200493 tt_data->crc = htonl(bat_priv->tt.local_crc);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800494
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800495 if (tt_diff_len == 0)
496 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800497
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200498 tt_diff_entries_num = batadv_tt_entries(tt_diff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
Sven Eckelmann807736f2012-07-15 22:26:51 +0200500 spin_lock_bh(&bat_priv->tt.changes_list_lock);
501 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800503 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
504
Sven Eckelmann807736f2012-07-15 22:26:51 +0200505 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100506 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800507 if (tt_diff_entries_count < tt_diff_entries_num) {
508 memcpy(tt_change + tt_diff_entries_count,
509 &entry->change,
510 sizeof(struct batadv_tvlv_tt_change));
511 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200513 list_del(&entry->list);
514 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200516 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517
Antonio Quartullia73105b2011-04-27 14:27:44 +0200518 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200519 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
520 kfree(bat_priv->tt.last_changeset);
521 bat_priv->tt.last_changeset_len = 0;
522 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800523 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800524 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800525 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800526 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200527 * instead of providing the diff
528 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800529 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200530 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800531 memcpy(bat_priv->tt.last_changeset,
532 tt_change, tt_change_len);
533 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200534 }
535 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200536 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800538container_register:
539 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
540 sizeof(*tt_data) + tt_change_len);
541 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542}
543
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200544int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545{
546 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200547 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200548 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200549 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100550 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200551 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200553 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100554 int last_seen_secs;
555 int last_seen_msecs;
556 unsigned long last_seen_jiffies;
557 bool no_purge;
558 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
Marek Lindner30da63a2012-08-03 17:15:46 +0200560 primary_if = batadv_seq_print_text_primary_if_get(seq);
561 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200562 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100564 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200565 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100566 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
567 bat_priv->tt.local_crc);
Antonio Quartulli16052782013-06-04 12:11:41 +0200568 seq_printf(seq, " %-13s %s %-7s %-10s\n", "Client", "VID",
569 "Flags", "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 for (i = 0; i < hash->size; i++) {
572 head = &hash->table[i];
573
Marek Lindner7aadf882011-02-18 12:28:09 +0000574 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800575 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000576 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100577 tt_local = container_of(tt_common_entry,
578 struct batadv_tt_local_entry,
579 common);
580 last_seen_jiffies = jiffies - tt_local->last_seen;
581 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
582 last_seen_secs = last_seen_msecs / 1000;
583 last_seen_msecs = last_seen_msecs % 1000;
584
585 no_purge = tt_common_entry->flags & np_flag;
586
Antonio Quartulli16052782013-06-04 12:11:41 +0200587 seq_printf(seq, " * %pM %4i [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100588 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200589 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100590 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200591 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100592 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100593 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200594 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100595 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200596 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100597 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100598 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100599 no_purge ? 0 : last_seen_secs,
600 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000602 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200604out:
605 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200606 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200607 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608}
609
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610static void
611batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
612 struct batadv_tt_local_entry *tt_local_entry,
613 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200615 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000616
Antonio Quartulli015758d2011-07-09 17:52:13 +0200617 /* The local client has to be marked as "pending to be removed" but has
618 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200619 * response issued before the net ttvn increment (consistency check)
620 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200621 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100622
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200623 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200624 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
625 tt_local_entry->common.addr,
626 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627}
628
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200629/**
630 * batadv_tt_local_remove - logically remove an entry from the local table
631 * @bat_priv: the bat priv with all the soft interface information
632 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200633 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200634 * @message: message to append to the log on deletion
635 * @roaming: true if the deletion is due to a roaming event
636 *
637 * Returns the flags assigned to the local entry before being deleted
638 */
639uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200640 const uint8_t *addr, unsigned short vid,
641 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200643 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200644 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Antonio Quartullic018ad32013-06-04 12:11:39 +0200646 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647 if (!tt_local_entry)
648 goto out;
649
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200650 curr_flags = tt_local_entry->common.flags;
651
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200652 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200653 /* if this global entry addition is due to a roaming, the node has to
654 * mark the local entry as "roamed" in order to correctly reroute
655 * packets later
656 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200657 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200658 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200659 /* mark the local client as ROAMed */
660 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
661 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200662
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200663 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
664 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
665 message);
666 goto out;
667 }
668 /* if this client has been added right now, it is possible to
669 * immediately purge it
670 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200671 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200672 hlist_del_rcu(&tt_local_entry->common.hash_entry);
673 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200674
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200675out:
676 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200677 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200678
679 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680}
681
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200683 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200685 struct batadv_tt_local_entry *tt_local_entry;
686 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800687 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200688
Sasha Levinb67bfe02013-02-27 17:06:00 -0800689 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200690 hash_entry) {
691 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200692 struct batadv_tt_local_entry,
693 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200694 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
695 continue;
696
697 /* entry already marked for deletion */
698 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
699 continue;
700
701 if (!batadv_has_timed_out(tt_local_entry->last_seen,
702 BATADV_TT_LOCAL_TIMEOUT))
703 continue;
704
705 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
706 BATADV_TT_CLIENT_DEL, "timed out");
707 }
708}
709
Sven Eckelmann56303d32012-06-05 22:31:31 +0200710static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200711{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200712 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200715 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717 for (i = 0; i < hash->size; i++) {
718 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200719 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200721 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200722 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200723 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725}
726
Sven Eckelmann56303d32012-06-05 22:31:31 +0200727static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000728{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200729 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200730 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200731 struct batadv_tt_common_entry *tt_common_entry;
732 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800733 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200734 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200735 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736
Sven Eckelmann807736f2012-07-15 22:26:51 +0200737 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738 return;
739
Sven Eckelmann807736f2012-07-15 22:26:51 +0200740 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200741
742 for (i = 0; i < hash->size; i++) {
743 head = &hash->table[i];
744 list_lock = &hash->list_locks[i];
745
746 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200748 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800749 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200750 tt_local = container_of(tt_common_entry,
751 struct batadv_tt_local_entry,
752 common);
753 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200754 }
755 spin_unlock_bh(list_lock);
756 }
757
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200758 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200759
Sven Eckelmann807736f2012-07-15 22:26:51 +0200760 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761}
762
Sven Eckelmann56303d32012-06-05 22:31:31 +0200763static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200765 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200766 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767
Sven Eckelmann807736f2012-07-15 22:26:51 +0200768 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769
Sven Eckelmann807736f2012-07-15 22:26:51 +0200770 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200771 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772
Antonio Quartullidec05072012-11-10 11:00:32 +0100773 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
774 &batadv_tt_global_hash_lock_class_key);
775
Sven Eckelmann5346c352012-05-05 13:27:28 +0200776 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777}
778
Sven Eckelmann56303d32012-06-05 22:31:31 +0200779static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200780{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200781 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200782
Sven Eckelmann807736f2012-07-15 22:26:51 +0200783 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200784
Sven Eckelmann807736f2012-07-15 22:26:51 +0200785 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200786 list) {
787 list_del(&entry->list);
788 kfree(entry);
789 }
790
Sven Eckelmann807736f2012-07-15 22:26:51 +0200791 atomic_set(&bat_priv->tt.local_changes, 0);
792 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200793}
794
Antonio Quartullid657e622012-07-01 14:09:12 +0200795/* retrieves the orig_tt_list_entry belonging to orig_node from the
796 * batadv_tt_global_entry list
797 *
798 * returns it with an increased refcounter, NULL if not found
799 */
800static struct batadv_tt_orig_list_entry *
801batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
802 const struct batadv_orig_node *orig_node)
803{
804 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
805 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200806
807 rcu_read_lock();
808 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800809 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200810 if (tmp_orig_entry->orig_node != orig_node)
811 continue;
812 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
813 continue;
814
815 orig_entry = tmp_orig_entry;
816 break;
817 }
818 rcu_read_unlock();
819
820 return orig_entry;
821}
822
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200823/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200824 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200826static bool
827batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
828 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200829{
Antonio Quartullid657e622012-07-01 14:09:12 +0200830 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200831 bool found = false;
832
Antonio Quartullid657e622012-07-01 14:09:12 +0200833 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
834 if (orig_entry) {
835 found = true;
836 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200837 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200838
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839 return found;
840}
841
Sven Eckelmanna5130882012-05-16 20:23:16 +0200842static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200843batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200844 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200845{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200846 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200847
Antonio Quartullid657e622012-07-01 14:09:12 +0200848 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200849 if (orig_entry) {
850 /* refresh the ttvn: the current value could be a bogus one that
851 * was added during a "temporary client detection"
852 */
853 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200854 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200855 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200856
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200857 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
858 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200859 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200860
861 INIT_HLIST_NODE(&orig_entry->list);
862 atomic_inc(&orig_node->refcount);
863 atomic_inc(&orig_node->tt_size);
864 orig_entry->orig_node = orig_node;
865 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200866 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200867
Antonio Quartullid657e622012-07-01 14:09:12 +0200868 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200869 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200870 &tt_global->orig_list);
871 spin_unlock_bh(&tt_global->list_lock);
872out:
873 if (orig_entry)
874 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200875}
876
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200877/**
878 * batadv_tt_global_add - add a new TT global entry or update an existing one
879 * @bat_priv: the bat priv with all the soft interface information
880 * @orig_node: the originator announcing the client
881 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200882 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200883 * @flags: TT flags that have to be set for this non-mesh client
884 * @ttvn: the tt version number ever announcing this non-mesh client
885 *
886 * Add a new TT global entry for the given originator. If the entry already
887 * exists add a new reference to the given originator (a global entry can have
888 * references to multiple originators) and adjust the flags attribute to reflect
889 * the function argument.
890 * If a TT local entry exists for this non-mesh client remove it.
891 *
892 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200893 *
894 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200895 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200896static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
897 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200898 const unsigned char *tt_addr,
899 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200900 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000901{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200902 struct batadv_tt_global_entry *tt_global_entry;
903 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200904 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100905 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200906 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200907 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
Antonio Quartullic018ad32013-06-04 12:11:39 +0200909 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
910 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200911
912 /* if the node already has a local client for this entry, it has to wait
913 * for a roaming advertisement instead of manually messing up the global
914 * table
915 */
916 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
917 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
918 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919
Antonio Quartullia73105b2011-04-27 14:27:44 +0200920 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200921 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200922 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200923 goto out;
924
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200925 common = &tt_global_entry->common;
926 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200927 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200928
Antonio Quartullid4f44692012-05-25 00:00:54 +0200929 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200930 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200931 /* node must store current time in case of roaming. This is
932 * needed to purge this entry out on timeout (if nobody claims
933 * it)
934 */
935 if (flags & BATADV_TT_CLIENT_ROAM)
936 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200937 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200938 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200939
940 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
941 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200942
Sven Eckelmann807736f2012-07-15 22:26:51 +0200943 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200944 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200945 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200946 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100947
948 if (unlikely(hash_added != 0)) {
949 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200950 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100951 goto out_remove;
952 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200953 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200954 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200955 /* If there is already a global entry, we can use this one for
956 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200957 * But if we are trying to add a temporary client then here are
958 * two options at this point:
959 * 1) the global client is not a temporary client: the global
960 * client has to be left as it is, temporary information
961 * should never override any already known client state
962 * 2) the global client is a temporary client: purge the
963 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200964 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200965 if (flags & BATADV_TT_CLIENT_TEMP) {
966 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
967 goto out;
968 if (batadv_tt_global_entry_has_orig(tt_global_entry,
969 orig_node))
970 goto out_remove;
971 batadv_tt_global_del_orig_list(tt_global_entry);
972 goto add_orig_entry;
973 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200974
975 /* if the client was temporary added before receiving the first
976 * OGM announcing it, we have to clear the TEMP flag
977 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200978 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200979
Antonio Quartullie9c001362012-11-07 15:05:33 +0100980 /* the change can carry possible "attribute" flags like the
981 * TT_CLIENT_WIFI, therefore they have to be copied in the
982 * client entry
983 */
984 tt_global_entry->common.flags |= flags;
985
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200986 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
987 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200988 * delete + roaming change for this originator.
989 *
990 * We should first delete the old originator before adding the
991 * new one.
992 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200993 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200994 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200995 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200996 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997 }
998 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200999add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001000 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001001 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001002
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001003 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001004 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1005 common->addr, BATADV_PRINT_VID(common->vid),
1006 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001007 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001008
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001009out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001010
Antonio Quartullia73105b2011-04-27 14:27:44 +02001011 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001012 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001013 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001014 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001015 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1016
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001017 if (!(flags & BATADV_TT_CLIENT_ROAM))
1018 /* this is a normal global add. Therefore the client is not in a
1019 * roaming state anymore.
1020 */
1021 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1022
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001023out:
1024 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001025 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001026 if (tt_local_entry)
1027 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001028 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029}
1030
Sven Eckelmann981d8902012-10-07 13:34:15 +02001031/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1032 * @tt_global_entry: global translation table entry to be analyzed
1033 *
1034 * This functon assumes the caller holds rcu_read_lock().
1035 * Returns best originator list entry or NULL on errors.
1036 */
1037static struct batadv_tt_orig_list_entry *
1038batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
1039{
1040 struct batadv_neigh_node *router = NULL;
1041 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001042 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1043 int best_tq = 0;
1044
1045 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001046 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001047 router = batadv_orig_node_get_router(orig_entry->orig_node);
1048 if (!router)
1049 continue;
1050
1051 if (router->tq_avg > best_tq) {
1052 best_entry = orig_entry;
1053 best_tq = router->tq_avg;
1054 }
1055
1056 batadv_neigh_node_free_ref(router);
1057 }
1058
1059 return best_entry;
1060}
1061
1062/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1063 * for this global entry
1064 * @tt_global_entry: global translation table entry to be printed
1065 * @seq: debugfs table seq_file struct
1066 *
1067 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001068 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001069static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001070batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001071 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072{
1073 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001074 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076 uint16_t flags;
1077 uint8_t last_ttvn;
1078
1079 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001080 flags = tt_common_entry->flags;
1081
1082 best_entry = batadv_transtable_best_orig(tt_global_entry);
1083 if (best_entry) {
1084 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001085 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001086 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001087 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001088 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001089 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001090 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001091 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1092 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1093 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1094 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095
1096 head = &tt_global_entry->orig_list;
1097
Sasha Levinb67bfe02013-02-27 17:06:00 -08001098 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001099 if (best_entry == orig_entry)
1100 continue;
1101
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001102 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001103 seq_printf(seq,
1104 " %c %pM %4d (%3u) via %pM (%3u) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001105 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001106 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001107 orig_entry->ttvn, orig_entry->orig_node->orig,
1108 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001109 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001110 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1111 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001112 }
1113}
1114
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001115int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116{
1117 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001118 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001119 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001120 struct batadv_tt_common_entry *tt_common_entry;
1121 struct batadv_tt_global_entry *tt_global;
1122 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001124 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125
Marek Lindner30da63a2012-08-03 17:15:46 +02001126 primary_if = batadv_seq_print_text_primary_if_get(seq);
1127 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001128 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001130 seq_printf(seq,
1131 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001132 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001133 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1134 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1135 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001136
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137 for (i = 0; i < hash->size; i++) {
1138 head = &hash->table[i];
1139
Marek Lindner7aadf882011-02-18 12:28:09 +00001140 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001141 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001142 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143 tt_global = container_of(tt_common_entry,
1144 struct batadv_tt_global_entry,
1145 common);
1146 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001148 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001150out:
1151 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001152 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001153 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154}
1155
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001156/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001157static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001158batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001160 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001161 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001162 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164 spin_lock_bh(&tt_global_entry->list_lock);
1165 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001166 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1167 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001168 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001169 }
1170 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001171}
1172
Sven Eckelmanna5130882012-05-16 20:23:16 +02001173static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001174batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1175 struct batadv_tt_global_entry *tt_global_entry,
1176 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001177 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001178{
1179 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001180 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001182 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001183
1184 spin_lock_bh(&tt_global_entry->list_lock);
1185 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001187 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001188 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001189 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001190 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001191 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001192 tt_global_entry->common.addr,
1193 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001194 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001195 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001196 }
1197 }
1198 spin_unlock_bh(&tt_global_entry->list_lock);
1199}
1200
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001201/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001202 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1203 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001204 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001205static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001206batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1207 struct batadv_tt_global_entry *tt_global_entry,
1208 struct batadv_orig_node *orig_node,
1209 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001210{
1211 bool last_entry = true;
1212 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001213 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001214
1215 /* no local entry exists, case 1:
1216 * Check if this is the last one or if other entries exist.
1217 */
1218
1219 rcu_read_lock();
1220 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001221 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001222 if (orig_entry->orig_node != orig_node) {
1223 last_entry = false;
1224 break;
1225 }
1226 }
1227 rcu_read_unlock();
1228
1229 if (last_entry) {
1230 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001231 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001232 tt_global_entry->roam_at = jiffies;
1233 } else
1234 /* there is another entry, we can simply delete this
1235 * one and can still use the other one.
1236 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001237 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1238 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001239}
1240
Antonio Quartullic018ad32013-06-04 12:11:39 +02001241/**
1242 * batadv_tt_global_del - remove a client from the global table
1243 * @bat_priv: the bat priv with all the soft interface information
1244 * @orig_node: an originator serving this client
1245 * @addr: the mac address of the client
1246 * @vid: VLAN identifier
1247 * @message: a message explaining the reason for deleting the client to print
1248 * for debugging purpose
1249 * @roaming: true if the deletion has been triggered by a roaming event
1250 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001251static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1252 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001253 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001254 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001255{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001256 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001257 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001258
Antonio Quartullic018ad32013-06-04 12:11:39 +02001259 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001260 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001261 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001263 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001264 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1265 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001266
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001268 batadv_tt_global_free(bat_priv, tt_global_entry,
1269 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001270
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001271 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001272 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001273
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001274 /* if we are deleting a global entry due to a roam
1275 * event, there are two possibilities:
1276 * 1) the client roamed from node A to node B => if there
1277 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001278 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001279 * wait for node B to claim it. In case of timeout
1280 * the entry is purged.
1281 *
1282 * If there are other originators left, we directly delete
1283 * the originator.
1284 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001285 * the global entry, since it is useless now.
1286 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001287 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001288 tt_global_entry->common.addr,
1289 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001290 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001291 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001292 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001293 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001294 } else
1295 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001296 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1297 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001298
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001299
Antonio Quartullicc47f662011-04-27 14:27:57 +02001300out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001301 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001302 batadv_tt_global_entry_free_ref(tt_global_entry);
1303 if (local_entry)
1304 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001305}
1306
Sven Eckelmann56303d32012-06-05 22:31:31 +02001307void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1308 struct batadv_orig_node *orig_node,
1309 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001310{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001311 struct batadv_tt_global_entry *tt_global;
1312 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001313 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001314 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001315 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001316 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001317 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001318 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001319
Simon Wunderlich6e801492011-10-19 10:28:26 +02001320 if (!hash)
1321 return;
1322
Antonio Quartullia73105b2011-04-27 14:27:44 +02001323 for (i = 0; i < hash->size; i++) {
1324 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001325 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001326
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001327 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001328 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001329 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001330 tt_global = container_of(tt_common_entry,
1331 struct batadv_tt_global_entry,
1332 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001333
Sven Eckelmann56303d32012-06-05 22:31:31 +02001334 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001335 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001336
Sven Eckelmann56303d32012-06-05 22:31:31 +02001337 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001338 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001339 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001340 "Deleting global tt entry %pM (vid: %d): %s\n",
1341 tt_global->common.addr,
1342 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001343 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001344 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001345 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001346 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001347 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001348 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001349 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350}
1351
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001352static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1353 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001354{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001355 bool purge = false;
1356 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1357 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001358
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001359 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1360 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1361 purge = true;
1362 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001363 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001364
1365 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1366 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1367 purge = true;
1368 *msg = "Temporary client timeout\n";
1369 }
1370
1371 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001372}
1373
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001374static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001375{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001376 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001377 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001379 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001380 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001381 char *msg = NULL;
1382 struct batadv_tt_common_entry *tt_common;
1383 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001384
Antonio Quartullicc47f662011-04-27 14:27:57 +02001385 for (i = 0; i < hash->size; i++) {
1386 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001387 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001388
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001389 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001390 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001391 hash_entry) {
1392 tt_global = container_of(tt_common,
1393 struct batadv_tt_global_entry,
1394 common);
1395
1396 if (!batadv_tt_global_to_purge(tt_global, &msg))
1397 continue;
1398
1399 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001400 "Deleting global tt entry %pM (vid: %d): %s\n",
1401 tt_global->common.addr,
1402 BATADV_PRINT_VID(tt_global->common.vid),
1403 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001404
Sasha Levinb67bfe02013-02-27 17:06:00 -08001405 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001406
1407 batadv_tt_global_entry_free_ref(tt_global);
1408 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001409 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001410 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001411}
1412
Sven Eckelmann56303d32012-06-05 22:31:31 +02001413static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001414{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001415 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001416 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001417 struct batadv_tt_common_entry *tt_common_entry;
1418 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001419 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001420 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001421 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001422
Sven Eckelmann807736f2012-07-15 22:26:51 +02001423 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001424 return;
1425
Sven Eckelmann807736f2012-07-15 22:26:51 +02001426 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001427
1428 for (i = 0; i < hash->size; i++) {
1429 head = &hash->table[i];
1430 list_lock = &hash->list_locks[i];
1431
1432 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001433 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001434 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001435 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001436 tt_global = container_of(tt_common_entry,
1437 struct batadv_tt_global_entry,
1438 common);
1439 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001440 }
1441 spin_unlock_bh(list_lock);
1442 }
1443
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001444 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001445
Sven Eckelmann807736f2012-07-15 22:26:51 +02001446 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001447}
1448
Sven Eckelmann56303d32012-06-05 22:31:31 +02001449static bool
1450_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1451 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001452{
1453 bool ret = false;
1454
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001455 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1456 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001457 ret = true;
1458
1459 return ret;
1460}
1461
Antonio Quartullic018ad32013-06-04 12:11:39 +02001462/**
1463 * batadv_transtable_search - get the mesh destination for a given client
1464 * @bat_priv: the bat priv with all the soft interface information
1465 * @src: mac address of the source client
1466 * @addr: mac address of the destination client
1467 * @vid: VLAN identifier
1468 *
1469 * Returns a pointer to the originator that was selected as destination in the
1470 * mesh for contacting the client 'addr', NULL otherwise.
1471 * In case of multiple originators serving the same client, the function returns
1472 * the best one (best in terms of metric towards the destination node).
1473 *
1474 * If the two clients are AP isolated the function returns NULL.
1475 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001476struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1477 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001478 const uint8_t *addr,
1479 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001480{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001481 struct batadv_tt_local_entry *tt_local_entry = NULL;
1482 struct batadv_tt_global_entry *tt_global_entry = NULL;
1483 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001484 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001485
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001486 if (src && atomic_read(&bat_priv->ap_isolation)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001487 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001488 if (!tt_local_entry ||
1489 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001490 goto out;
1491 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001492
Antonio Quartullic018ad32013-06-04 12:11:39 +02001493 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001494 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001495 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001496
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001497 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001498 * isolation
1499 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001500 if (tt_local_entry &&
1501 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001502 goto out;
1503
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001504 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001505 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001506 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001507 if (best_entry)
1508 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001509 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1510 orig_node = NULL;
1511 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001512
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001513out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001514 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001515 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001516 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001517 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001518
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001519 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001520}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521
Antonio Quartulliced72932013-04-24 16:37:51 +02001522/**
1523 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1524 * to the given orig_node
1525 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001526 * @orig_node: originator for which the CRC should be computed
1527 *
1528 * This function computes the checksum for the global table corresponding to a
1529 * specific originator. In particular, the checksum is computed as follows: For
1530 * each client connected to the originator the CRC32C of the MAC address and the
1531 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1532 * together.
1533 *
1534 * The idea behind is that CRC32C should be used as much as possible in order to
1535 * produce a unique hash of the table, but since the order which is used to feed
1536 * the CRC32C function affects the result and since every node in the network
1537 * probably sorts the clients differently, the hash function cannot be directly
1538 * computed over the entire table. Hence the CRC32C is used only on
1539 * the single client entry, while all the results are then xor'ed together
1540 * because the XOR operation can combine them all while trying to reduce the
1541 * noise as much as possible.
1542 *
1543 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001544 */
1545static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001546 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001547{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001548 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001549 struct batadv_tt_common_entry *tt_common;
1550 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001551 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001552 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553
1554 for (i = 0; i < hash->size; i++) {
1555 head = &hash->table[i];
1556
1557 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001558 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001559 tt_global = container_of(tt_common,
1560 struct batadv_tt_global_entry,
1561 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001562 /* Roaming clients are in the global table for
1563 * consistency only. They don't have to be
1564 * taken into account while computing the
1565 * global crc
1566 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001567 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001568 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001569 /* Temporary clients have not been announced yet, so
1570 * they have to be skipped while computing the global
1571 * crc
1572 */
1573 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1574 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001575
1576 /* find out if this global entry is announced by this
1577 * originator
1578 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001579 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001580 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001581 continue;
1582
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001583 crc_tmp = crc32c(0, &tt_common->vid,
1584 sizeof(tt_common->vid));
1585 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586 }
1587 rcu_read_unlock();
1588 }
1589
Antonio Quartulliced72932013-04-24 16:37:51 +02001590 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001591}
1592
Antonio Quartulliced72932013-04-24 16:37:51 +02001593/**
1594 * batadv_tt_local_crc - calculates the checksum of the local table
1595 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001596 *
1597 * For details about the computation, please refer to the documentation for
1598 * batadv_tt_global_crc().
1599 *
1600 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001601 */
1602static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001603{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001604 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001605 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001607 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608
1609 for (i = 0; i < hash->size; i++) {
1610 head = &hash->table[i];
1611
1612 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001613 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001614 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001615 * account while computing the CRC
1616 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001617 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001618 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001619
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001620 crc_tmp = crc32c(0, &tt_common->vid,
1621 sizeof(tt_common->vid));
1622 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 rcu_read_unlock();
1625 }
1626
Antonio Quartulliced72932013-04-24 16:37:51 +02001627 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628}
1629
Sven Eckelmann56303d32012-06-05 22:31:31 +02001630static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001632 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001633
Sven Eckelmann807736f2012-07-15 22:26:51 +02001634 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635
Sven Eckelmann807736f2012-07-15 22:26:51 +02001636 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637 list_del(&node->list);
1638 kfree(node);
1639 }
1640
Sven Eckelmann807736f2012-07-15 22:26:51 +02001641 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642}
1643
Sven Eckelmann56303d32012-06-05 22:31:31 +02001644static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1645 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001646 const void *tt_buff,
1647 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001650 * last OGM (the OGM could carry no changes)
1651 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001652 spin_lock_bh(&orig_node->tt_buff_lock);
1653 if (tt_buff_len > 0) {
1654 kfree(orig_node->tt_buff);
1655 orig_node->tt_buff_len = 0;
1656 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1657 if (orig_node->tt_buff) {
1658 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1659 orig_node->tt_buff_len = tt_buff_len;
1660 }
1661 }
1662 spin_unlock_bh(&orig_node->tt_buff_lock);
1663}
1664
Sven Eckelmann56303d32012-06-05 22:31:31 +02001665static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001667 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668
Sven Eckelmann807736f2012-07-15 22:26:51 +02001669 spin_lock_bh(&bat_priv->tt.req_list_lock);
1670 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001671 if (batadv_has_timed_out(node->issued_at,
1672 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 list_del(&node->list);
1674 kfree(node);
1675 }
1676 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001677 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001678}
1679
1680/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001681 * has already been issued for this orig_node, NULL otherwise
1682 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001683static struct batadv_tt_req_node *
1684batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1685 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001687 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688
Sven Eckelmann807736f2012-07-15 22:26:51 +02001689 spin_lock_bh(&bat_priv->tt.req_list_lock);
1690 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001691 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1692 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001693 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 goto unlock;
1695 }
1696
1697 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1698 if (!tt_req_node)
1699 goto unlock;
1700
1701 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1702 tt_req_node->issued_at = jiffies;
1703
Sven Eckelmann807736f2012-07-15 22:26:51 +02001704 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001706 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 return tt_req_node;
1708}
1709
Marek Lindner335fbe02013-04-23 21:40:02 +08001710/**
1711 * batadv_tt_local_valid - verify that given tt entry is a valid one
1712 * @entry_ptr: to be checked local tt entry
1713 * @data_ptr: not used but definition required to satisfy the callback prototype
1714 *
1715 * Returns 1 if the entry is a valid, 0 otherwise.
1716 */
1717static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001718{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001719 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001720
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001721 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001722 return 0;
1723 return 1;
1724}
1725
Sven Eckelmanna5130882012-05-16 20:23:16 +02001726static int batadv_tt_global_valid(const void *entry_ptr,
1727 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1730 const struct batadv_tt_global_entry *tt_global_entry;
1731 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001732
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001733 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1734 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001735 return 0;
1736
Sven Eckelmann56303d32012-06-05 22:31:31 +02001737 tt_global_entry = container_of(tt_common_entry,
1738 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001739 common);
1740
Sven Eckelmanna5130882012-05-16 20:23:16 +02001741 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742}
1743
Marek Lindner335fbe02013-04-23 21:40:02 +08001744/**
1745 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1746 * tt entries from the specified tt hash
1747 * @bat_priv: the bat priv with all the soft interface information
1748 * @hash: hash table containing the tt entries
1749 * @tt_len: expected tvlv tt data buffer length in number of bytes
1750 * @valid_cb: function to filter tt change entries
1751 * @cb_data: data passed to the filter function as argument
1752 *
1753 * Returns pointer to allocated tvlv tt data buffer if operation was
1754 * successful or NULL otherwise.
1755 */
1756static struct batadv_tvlv_tt_data *
1757batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1758 struct batadv_hashtable *hash, uint16_t tt_len,
1759 int (*valid_cb)(const void *, const void *),
1760 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001762 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001763 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1764 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001766 uint16_t tt_tot, tt_num_entries = 0;
1767 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001768 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001769
Marek Lindner335fbe02013-04-23 21:40:02 +08001770 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1771 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1772 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001773 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774
Antonio Quartulli298e6e62013-05-28 13:14:27 +02001775 tt_tot = batadv_tt_entries(tt_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08001776
1777 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1778 GFP_ATOMIC);
1779 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780 goto out;
1781
Marek Lindner335fbe02013-04-23 21:40:02 +08001782 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783
1784 rcu_read_lock();
1785 for (i = 0; i < hash->size; i++) {
1786 head = &hash->table[i];
1787
Sasha Levinb67bfe02013-02-27 17:06:00 -08001788 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001790 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 break;
1792
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001793 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794 continue;
1795
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001796 memcpy(tt_change->addr, tt_common_entry->addr,
1797 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001798 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001799 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08001800 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801
Marek Lindner335fbe02013-04-23 21:40:02 +08001802 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001803 tt_change++;
1804 }
1805 }
1806 rcu_read_unlock();
1807
1808out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001809 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810}
1811
Antonio Quartulliced72932013-04-24 16:37:51 +02001812/**
1813 * batadv_send_tt_request - send a TT Request message to a given node
1814 * @bat_priv: the bat priv with all the soft interface information
1815 * @dst_orig_node: the destination of the message
1816 * @ttvn: the version number that the source of the message is looking for
1817 * @tt_crc: the CRC associated with the version number
1818 * @full_table: ask for the entire translation table if true, while only for the
1819 * last TT diff otherwise
1820 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001821static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1822 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001823 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001824 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825{
Marek Lindner335fbe02013-04-23 21:40:02 +08001826 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001827 struct batadv_hard_iface *primary_if;
1828 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001829 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001830
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001831 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832 if (!primary_if)
1833 goto out;
1834
1835 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001836 * reply from the same orig_node yet
1837 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001838 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839 if (!tt_req_node)
1840 goto out;
1841
Marek Lindner335fbe02013-04-23 21:40:02 +08001842 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1843 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 goto out;
1845
Marek Lindner335fbe02013-04-23 21:40:02 +08001846 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1847 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001848 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001849
1850 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001851 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001853 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001854 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001856 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001857 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1858 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1859 tvlv_tt_data, sizeof(*tvlv_tt_data));
1860 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861
1862out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001864 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001866 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001867 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001868 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869 kfree(tt_req_node);
1870 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001871 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872 return ret;
1873}
1874
Marek Lindner335fbe02013-04-23 21:40:02 +08001875/**
1876 * batadv_send_other_tt_response - send reply to tt request concerning another
1877 * node's translation table
1878 * @bat_priv: the bat priv with all the soft interface information
1879 * @tt_data: tt data containing the tt request information
1880 * @req_src: mac address of tt request sender
1881 * @req_dst: mac address of tt request recipient
1882 *
1883 * Returns true if tt request reply was sent, false otherwise.
1884 */
1885static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1886 struct batadv_tvlv_tt_data *tt_data,
1887 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001888{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001889 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001890 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001891 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1892 uint8_t orig_ttvn, req_ttvn;
1893 uint16_t tt_len;
1894 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001896 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001897 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001898 req_src, tt_data->ttvn, req_dst,
1899 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001900
1901 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001902 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903 if (!req_dst_orig_node)
1904 goto out;
1905
Marek Lindner335fbe02013-04-23 21:40:02 +08001906 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001907 if (!res_dst_orig_node)
1908 goto out;
1909
Antonio Quartullia73105b2011-04-27 14:27:44 +02001910 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001911 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001912
Marek Lindner335fbe02013-04-23 21:40:02 +08001913 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001915 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001916 goto out;
1917
Antonio Quartulli015758d2011-07-09 17:52:13 +02001918 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001919 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920 !req_dst_orig_node->tt_buff)
1921 full_table = true;
1922 else
1923 full_table = false;
1924
Marek Lindner335fbe02013-04-23 21:40:02 +08001925 /* TT fragmentation hasn't been implemented yet, so send as many
1926 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001927 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928 if (!full_table) {
1929 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1930 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001931
Marek Lindner335fbe02013-04-23 21:40:02 +08001932 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1933 GFP_ATOMIC);
1934 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001935 goto unlock;
1936
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001938 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001940 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1941 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001942 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001943 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001944
Marek Lindner335fbe02013-04-23 21:40:02 +08001945 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1946 bat_priv->tt.global_hash,
1947 tt_len,
1948 batadv_tt_global_valid,
1949 req_dst_orig_node);
1950 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001951 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952 }
1953
Marek Lindner335fbe02013-04-23 21:40:02 +08001954 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1955 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956
1957 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001958 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001960 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001961 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1962 res_dst_orig_node->orig, req_dst_orig_node->orig,
1963 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001965 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001966
Marek Lindner335fbe02013-04-23 21:40:02 +08001967 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1968 req_src, BATADV_TVLV_TT, 1,
1969 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001970
Marek Lindner335fbe02013-04-23 21:40:02 +08001971 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972 goto out;
1973
1974unlock:
1975 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1976
1977out:
1978 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001979 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001980 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001981 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001982 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001984}
Sven Eckelmann96412692012-06-05 22:31:30 +02001985
Marek Lindner335fbe02013-04-23 21:40:02 +08001986/**
1987 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1988 * translation table
1989 * @bat_priv: the bat priv with all the soft interface information
1990 * @tt_data: tt data containing the tt request information
1991 * @req_src: mac address of tt request sender
1992 *
1993 * Returns true if tt request reply was sent, false otherwise.
1994 */
1995static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1996 struct batadv_tvlv_tt_data *tt_data,
1997 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998{
Marek Lindner335fbe02013-04-23 21:40:02 +08001999 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02002000 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002001 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002002 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08002004 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002005
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002006 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002007 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002008 req_src, tt_data->ttvn,
2009 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002010
2011
Sven Eckelmann807736f2012-07-15 22:26:51 +02002012 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002013 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014
Marek Lindner335fbe02013-04-23 21:40:02 +08002015 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002016 if (!orig_node)
2017 goto out;
2018
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002019 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002020 if (!primary_if)
2021 goto out;
2022
2023 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002024 * is too big send the whole local translation table
2025 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002026 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002027 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028 full_table = true;
2029 else
2030 full_table = false;
2031
Marek Lindner335fbe02013-04-23 21:40:02 +08002032 /* TT fragmentation hasn't been implemented yet, so send as many
2033 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002034 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002035 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002036 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2037 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002038
Marek Lindner335fbe02013-04-23 21:40:02 +08002039 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
2040 GFP_ATOMIC);
2041 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042 goto unlock;
2043
Marek Lindner335fbe02013-04-23 21:40:02 +08002044 /* Copy the last orig_node's OGM buffer */
2045 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002046 bat_priv->tt.last_changeset_len);
2047 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002048 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002049 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08002050 tt_len = batadv_tt_len(tt_len);
2051 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002052
Marek Lindner335fbe02013-04-23 21:40:02 +08002053 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
2054 bat_priv->tt.local_hash,
2055 tt_len,
2056 batadv_tt_local_valid,
2057 NULL);
2058 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060 }
2061
Marek Lindner335fbe02013-04-23 21:40:02 +08002062 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2063 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002064
2065 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002066 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002067
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002068 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002069 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2070 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002072 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002073
Marek Lindner335fbe02013-04-23 21:40:02 +08002074 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2075 req_src, BATADV_TVLV_TT, 1,
2076 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
2077
Antonio Quartullia73105b2011-04-27 14:27:44 +02002078 goto out;
2079
2080unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002081 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002082out:
2083 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002084 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002085 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002086 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002087 kfree(tvlv_tt_data);
2088 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002089 return true;
2090}
2091
Marek Lindner335fbe02013-04-23 21:40:02 +08002092/**
2093 * batadv_send_tt_response - send reply to tt request
2094 * @bat_priv: the bat priv with all the soft interface information
2095 * @tt_data: tt data containing the tt request information
2096 * @req_src: mac address of tt request sender
2097 * @req_dst: mac address of tt request recipient
2098 *
2099 * Returns true if tt request reply was sent, false otherwise.
2100 */
2101static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2102 struct batadv_tvlv_tt_data *tt_data,
2103 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104{
Marek Lindner335fbe02013-04-23 21:40:02 +08002105 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002106 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08002107 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002108 return true;
2109
Marek Lindner335fbe02013-04-23 21:40:02 +08002110 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002111 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002112 return batadv_send_other_tt_response(bat_priv, tt_data,
2113 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002114 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002115}
2116
Sven Eckelmann56303d32012-06-05 22:31:31 +02002117static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2118 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002119 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002120 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121{
2122 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002123 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124
2125 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002126 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2127 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002128 batadv_tt_global_del(bat_priv, orig_node,
2129 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002130 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002131 "tt removed by changes",
2132 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002133 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002134 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002135 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002136 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002137 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002138 /* In case of problem while storing a
2139 * global_entry, we stop the updating
2140 * procedure without committing the
2141 * ttvn change. This will avoid to send
2142 * corrupted data on tt_request
2143 */
2144 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002145 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002146 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002147 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002148}
2149
Sven Eckelmann56303d32012-06-05 22:31:31 +02002150static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002151 struct batadv_tvlv_tt_data *tt_data,
2152 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002153{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002154 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002155
Marek Lindner335fbe02013-04-23 21:40:02 +08002156 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002157 if (!orig_node)
2158 goto out;
2159
2160 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002161 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002162
Sven Eckelmanna5130882012-05-16 20:23:16 +02002163 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002164 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2165 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166
2167 spin_lock_bh(&orig_node->tt_buff_lock);
2168 kfree(orig_node->tt_buff);
2169 orig_node->tt_buff_len = 0;
2170 orig_node->tt_buff = NULL;
2171 spin_unlock_bh(&orig_node->tt_buff_lock);
2172
Marek Lindner335fbe02013-04-23 21:40:02 +08002173 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002174
2175out:
2176 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002177 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002178}
2179
Sven Eckelmann56303d32012-06-05 22:31:31 +02002180static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2181 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002182 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002183 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002184{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002185 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2186 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002187
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002188 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2189 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002190 atomic_set(&orig_node->last_ttvn, ttvn);
2191}
2192
Antonio Quartullic018ad32013-06-04 12:11:39 +02002193/**
2194 * batadv_is_my_client - check if a client is served by the local node
2195 * @bat_priv: the bat priv with all the soft interface information
2196 * @addr: the mac adress of the client to check
2197 * @vid: VLAN identifier
2198 *
2199 * Returns true if the client is served by this node, false otherwise.
2200 */
2201bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2202 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002203{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002204 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002205 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002206
Antonio Quartullic018ad32013-06-04 12:11:39 +02002207 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002208 if (!tt_local_entry)
2209 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002210 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002211 * consistency purpose)
2212 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002213 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2214 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002215 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002216 ret = true;
2217out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002218 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002219 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002220 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002221}
2222
Marek Lindner335fbe02013-04-23 21:40:02 +08002223/**
2224 * batadv_handle_tt_response - process incoming tt reply
2225 * @bat_priv: the bat priv with all the soft interface information
2226 * @tt_data: tt data containing the tt request information
2227 * @resp_src: mac address of tt reply sender
2228 * @num_entries: number of tt change entries appended to the tt data
2229 */
2230static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2231 struct batadv_tvlv_tt_data *tt_data,
2232 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002233{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002234 struct batadv_tt_req_node *node, *safe;
2235 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002236 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002237
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002238 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002239 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002240 resp_src, tt_data->ttvn, num_entries,
2241 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002242
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002243 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002244 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002245 goto out;
2246
Marek Lindner335fbe02013-04-23 21:40:02 +08002247 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002248 if (!orig_node)
2249 goto out;
2250
Marek Lindner335fbe02013-04-23 21:40:02 +08002251 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2252 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002253 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002254 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2255 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2256 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002257 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002258
2259 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002260 spin_lock_bh(&bat_priv->tt.req_list_lock);
2261 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002262 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002263 continue;
2264 list_del(&node->list);
2265 kfree(node);
2266 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002267 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002268
2269 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002270 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002271out:
2272 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002273 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002274}
2275
Sven Eckelmann56303d32012-06-05 22:31:31 +02002276static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002277{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002278 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002279
Sven Eckelmann807736f2012-07-15 22:26:51 +02002280 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002281
Sven Eckelmann807736f2012-07-15 22:26:51 +02002282 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002283 list_del(&node->list);
2284 kfree(node);
2285 }
2286
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002288}
2289
Sven Eckelmann56303d32012-06-05 22:31:31 +02002290static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002291{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002292 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002293
Sven Eckelmann807736f2012-07-15 22:26:51 +02002294 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2295 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002296 if (!batadv_has_timed_out(node->first_time,
2297 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002298 continue;
2299
2300 list_del(&node->list);
2301 kfree(node);
2302 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002303 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002304}
2305
2306/* This function checks whether the client already reached the
2307 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2308 * will not be sent.
2309 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002310 * returns true if the ROAMING_ADV can be sent, false otherwise
2311 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002312static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002313 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002314{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002315 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002316 bool ret = false;
2317
Sven Eckelmann807736f2012-07-15 22:26:51 +02002318 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002319 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002320 * reply from the same orig_node yet
2321 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002322 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002323 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002324 continue;
2325
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002326 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002327 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002328 continue;
2329
Sven Eckelmann3e348192012-05-16 20:23:22 +02002330 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002331 /* Sorry, you roamed too many times! */
2332 goto unlock;
2333 ret = true;
2334 break;
2335 }
2336
2337 if (!ret) {
2338 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2339 if (!tt_roam_node)
2340 goto unlock;
2341
2342 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002343 atomic_set(&tt_roam_node->counter,
2344 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002345 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2346
Sven Eckelmann807736f2012-07-15 22:26:51 +02002347 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002348 ret = true;
2349 }
2350
2351unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002352 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002353 return ret;
2354}
2355
Antonio Quartullic018ad32013-06-04 12:11:39 +02002356/**
2357 * batadv_send_roam_adv - send a roaming advertisement message
2358 * @bat_priv: the bat priv with all the soft interface information
2359 * @client: mac address of the roaming client
2360 * @vid: VLAN identifier
2361 * @orig_node: message destination
2362 *
2363 * Send a ROAMING_ADV message to the node which was previously serving this
2364 * client. This is done to inform the node that from now on all traffic destined
2365 * for this particular roamed client has to be forwarded to the sender of the
2366 * roaming message.
2367 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002368static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002369 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002370 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002371{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002372 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002373 struct batadv_tvlv_roam_adv tvlv_roam;
2374
2375 primary_if = batadv_primary_if_get_selected(bat_priv);
2376 if (!primary_if)
2377 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002378
2379 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002380 * already roamed to us too many times
2381 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002382 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002383 goto out;
2384
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002385 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002386 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2387 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002388
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002389 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002390
Marek Lindner122edaa2013-04-23 21:40:03 +08002391 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002392 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002393
2394 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2395 orig_node->orig, BATADV_TVLV_ROAM, 1,
2396 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002397
2398out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002399 if (primary_if)
2400 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002401}
2402
Sven Eckelmanna5130882012-05-16 20:23:16 +02002403static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002404{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002405 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002406 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002407 struct batadv_priv *bat_priv;
2408
2409 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002410 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2411 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002412
Sven Eckelmanna5130882012-05-16 20:23:16 +02002413 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002414 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002415 batadv_tt_req_purge(bat_priv);
2416 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002417
Antonio Quartulli72414442012-12-25 13:14:37 +01002418 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2419 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002420}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002421
Sven Eckelmann56303d32012-06-05 22:31:31 +02002422void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002423{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002424 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2425 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2426
Sven Eckelmann807736f2012-07-15 22:26:51 +02002427 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002428
Sven Eckelmanna5130882012-05-16 20:23:16 +02002429 batadv_tt_local_table_free(bat_priv);
2430 batadv_tt_global_table_free(bat_priv);
2431 batadv_tt_req_list_free(bat_priv);
2432 batadv_tt_changes_list_free(bat_priv);
2433 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002434
Sven Eckelmann807736f2012-07-15 22:26:51 +02002435 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002436}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002437
Antonio Quartulli697f2532011-11-07 16:47:01 +01002438/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002439 * in the given hash table and returns the number of modified entries
2440 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002441static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2442 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002443{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002444 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002445 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002446 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002447 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002448
2449 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002450 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002451
2452 for (i = 0; i < hash->size; i++) {
2453 head = &hash->table[i];
2454
2455 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002456 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002457 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002458 if (enable) {
2459 if ((tt_common_entry->flags & flags) == flags)
2460 continue;
2461 tt_common_entry->flags |= flags;
2462 } else {
2463 if (!(tt_common_entry->flags & flags))
2464 continue;
2465 tt_common_entry->flags &= ~flags;
2466 }
2467 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002468 }
2469 rcu_read_unlock();
2470 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002471out:
2472 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002473}
2474
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002475/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002476static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002477{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002478 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002479 struct batadv_tt_common_entry *tt_common;
2480 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002481 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002482 struct hlist_head *head;
2483 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002484 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002485
2486 if (!hash)
2487 return;
2488
2489 for (i = 0; i < hash->size; i++) {
2490 head = &hash->table[i];
2491 list_lock = &hash->list_locks[i];
2492
2493 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002494 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002495 hash_entry) {
2496 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002497 continue;
2498
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002499 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002500 "Deleting local tt entry (%pM, vid: %d): pending\n",
2501 tt_common->addr,
2502 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002503
Sven Eckelmann807736f2012-07-15 22:26:51 +02002504 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002505 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002506 tt_local = container_of(tt_common,
2507 struct batadv_tt_local_entry,
2508 common);
2509 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002510 }
2511 spin_unlock_bh(list_lock);
2512 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002513}
2514
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002515/**
2516 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2517 * have been queued in the time since the last commit
2518 * @bat_priv: the bat priv with all the soft interface information
2519 */
2520void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002521{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002522 uint16_t changed_num = 0;
2523
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002524 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2525 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2526 batadv_tt_tvlv_container_update(bat_priv);
2527 return;
2528 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002529
Sven Eckelmann807736f2012-07-15 22:26:51 +02002530 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002531 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002532
2533 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002534 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002535 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002536 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002537
2538 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002539 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002540 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002541 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002542 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002543
2544 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002545 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002546 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002547}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002548
Sven Eckelmann56303d32012-06-05 22:31:31 +02002549bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002550 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002551{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002552 struct batadv_tt_local_entry *tt_local_entry = NULL;
2553 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002554 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002555
2556 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002557 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002558
Antonio Quartullic018ad32013-06-04 12:11:39 +02002559 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst,
2560 BATADV_NO_FLAGS);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002561 if (!tt_local_entry)
2562 goto out;
2563
Antonio Quartullic018ad32013-06-04 12:11:39 +02002564 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src,
2565 BATADV_NO_FLAGS);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002566 if (!tt_global_entry)
2567 goto out;
2568
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002569 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002570 goto out;
2571
Marek Lindner5870adc2012-06-20 17:16:05 +02002572 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002573
2574out:
2575 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002576 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002577 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002578 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002579 return ret;
2580}
Marek Lindnera943cac2011-07-30 13:10:18 +02002581
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002582/**
2583 * batadv_tt_update_orig - update global translation table with new tt
2584 * information received via ogms
2585 * @bat_priv: the bat priv with all the soft interface information
2586 * @orig: the orig_node of the ogm
2587 * @tt_buff: buffer holding the tt information
2588 * @tt_num_changes: number of tt changes inside the tt buffer
2589 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002590 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002591 */
2592static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2593 struct batadv_orig_node *orig_node,
2594 const unsigned char *tt_buff,
2595 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002596 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002597{
2598 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2599 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002600 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002601
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002602 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002603 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002604 return;
2605
Antonio Quartulli17071572011-11-07 16:36:40 +01002606 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002607 * increased by one -> we can apply the attached changes
2608 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002609 if ((!orig_node->tt_initialised && ttvn == 1) ||
2610 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002611 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002612 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2613 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002614 * In this case send a tt request
2615 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002616 if (!tt_num_changes) {
2617 full_table = false;
2618 goto request_table;
2619 }
2620
Marek Lindner335fbe02013-04-23 21:40:02 +08002621 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002622 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002623 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002624
2625 /* Even if we received the precomputed crc with the OGM, we
2626 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002627 * in the global table
2628 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002629 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002630
2631 /* The ttvn alone is not enough to guarantee consistency
2632 * because a single value could represent different states
2633 * (due to the wrap around). Thus a node has to check whether
2634 * the resulting table (after applying the changes) is still
2635 * consistent or not. E.g. a node could disconnect while its
2636 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2637 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002638 * inconsistency
2639 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002640 if (orig_node->tt_crc != tt_crc)
2641 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002642 } else {
2643 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002644 * in sync anymore -> request fresh tt data
2645 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002646 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2647 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002648request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002649 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002650 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002651 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2652 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002653 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2654 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002655 return;
2656 }
2657 }
2658}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002659
Antonio Quartullic018ad32013-06-04 12:11:39 +02002660/**
2661 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
2662 * @bat_priv: the bat priv with all the soft interface information
2663 * @addr: the mac address of the client to check
2664 * @vid: VLAN identifier
2665 *
2666 * Returns true if we know that the client has moved from its old originator
2667 * to another one. This entry is still kept for consistency purposes and will be
2668 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002669 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002670bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002671 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002672{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002673 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002674 bool ret = false;
2675
Antonio Quartullic018ad32013-06-04 12:11:39 +02002676 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002677 if (!tt_global_entry)
2678 goto out;
2679
Antonio Quartullic1d07432013-01-15 22:17:19 +10002680 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002681 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002682out:
2683 return ret;
2684}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002685
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002686/**
2687 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2688 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02002689 * @addr: the mac address of the local client to query
2690 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002691 *
2692 * Returns true if the local client is known to be roaming (it is not served by
2693 * this node anymore) or not. If yes, the client is still present in the table
2694 * to keep the latter consistent with the node TTVN
2695 */
2696bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002697 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002698{
2699 struct batadv_tt_local_entry *tt_local_entry;
2700 bool ret = false;
2701
Antonio Quartullic018ad32013-06-04 12:11:39 +02002702 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002703 if (!tt_local_entry)
2704 goto out;
2705
2706 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2707 batadv_tt_local_entry_free_ref(tt_local_entry);
2708out:
2709 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002710}
2711
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002712bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2713 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002714 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02002715 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002716{
2717 bool ret = false;
2718
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002719 /* if the originator is a backbone node (meaning it belongs to the same
2720 * LAN of this node) the temporary client must not be added because to
2721 * reach such destination the node must use the LAN instead of the mesh
2722 */
2723 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2724 goto out;
2725
Antonio Quartulli16052782013-06-04 12:11:41 +02002726 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002727 BATADV_TT_CLIENT_TEMP,
2728 atomic_read(&orig_node->last_ttvn)))
2729 goto out;
2730
2731 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002732 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
2733 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002734 ret = true;
2735out:
2736 return ret;
2737}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002738
2739/**
2740 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2741 * @bat_priv: the bat priv with all the soft interface information
2742 * @orig: the orig_node of the ogm
2743 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2744 * @tvlv_value: tvlv buffer containing the gateway data
2745 * @tvlv_value_len: tvlv buffer length
2746 */
2747static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2748 struct batadv_orig_node *orig,
2749 uint8_t flags,
2750 void *tvlv_value,
2751 uint16_t tvlv_value_len)
2752{
2753 struct batadv_tvlv_tt_data *tt_data;
2754 uint16_t num_entries;
2755
2756 if (tvlv_value_len < sizeof(*tt_data))
2757 return;
2758
2759 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2760 tvlv_value_len -= sizeof(*tt_data);
2761
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002762 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002763
2764 batadv_tt_update_orig(bat_priv, orig,
2765 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002766 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002767}
2768
2769/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002770 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2771 * container
2772 * @bat_priv: the bat priv with all the soft interface information
2773 * @src: mac address of tt tvlv sender
2774 * @dst: mac address of tt tvlv recipient
2775 * @tvlv_value: tvlv buffer containing the tt data
2776 * @tvlv_value_len: tvlv buffer length
2777 *
2778 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2779 * otherwise.
2780 */
2781static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2782 uint8_t *src, uint8_t *dst,
2783 void *tvlv_value,
2784 uint16_t tvlv_value_len)
2785{
2786 struct batadv_tvlv_tt_data *tt_data;
2787 uint16_t num_entries;
2788 char tt_flag;
2789 bool ret;
2790
2791 if (tvlv_value_len < sizeof(*tt_data))
2792 return NET_RX_SUCCESS;
2793
2794 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2795 tvlv_value_len -= sizeof(*tt_data);
2796
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002797 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002798
2799 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2800 case BATADV_TT_REQUEST:
2801 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2802
2803 /* If this node cannot provide a TT response the tt_request is
2804 * forwarded
2805 */
2806 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2807 if (!ret) {
2808 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2809 tt_flag = 'F';
2810 else
2811 tt_flag = '.';
2812
2813 batadv_dbg(BATADV_DBG_TT, bat_priv,
2814 "Routing TT_REQUEST to %pM [%c]\n",
2815 dst, tt_flag);
2816 /* tvlv API will re-route the packet */
2817 return NET_RX_DROP;
2818 }
2819 break;
2820 case BATADV_TT_RESPONSE:
2821 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2822
2823 if (batadv_is_my_mac(bat_priv, dst)) {
2824 batadv_handle_tt_response(bat_priv, tt_data,
2825 src, num_entries);
2826 return NET_RX_SUCCESS;
2827 }
2828
2829 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2830 tt_flag = 'F';
2831 else
2832 tt_flag = '.';
2833
2834 batadv_dbg(BATADV_DBG_TT, bat_priv,
2835 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2836
2837 /* tvlv API will re-route the packet */
2838 return NET_RX_DROP;
2839 }
2840
2841 return NET_RX_SUCCESS;
2842}
2843
2844/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002845 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2846 * @bat_priv: the bat priv with all the soft interface information
2847 * @src: mac address of tt tvlv sender
2848 * @dst: mac address of tt tvlv recipient
2849 * @tvlv_value: tvlv buffer containing the tt data
2850 * @tvlv_value_len: tvlv buffer length
2851 *
2852 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2853 * otherwise.
2854 */
2855static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2856 uint8_t *src, uint8_t *dst,
2857 void *tvlv_value,
2858 uint16_t tvlv_value_len)
2859{
2860 struct batadv_tvlv_roam_adv *roaming_adv;
2861 struct batadv_orig_node *orig_node = NULL;
2862
2863 /* If this node is not the intended recipient of the
2864 * roaming advertisement the packet is forwarded
2865 * (the tvlv API will re-route the packet).
2866 */
2867 if (!batadv_is_my_mac(bat_priv, dst))
2868 return NET_RX_DROP;
2869
2870 /* check if it is a backbone gateway. we don't accept
2871 * roaming advertisement from it, as it has the same
2872 * entries as we have.
2873 */
2874 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2875 goto out;
2876
2877 if (tvlv_value_len < sizeof(*roaming_adv))
2878 goto out;
2879
2880 orig_node = batadv_orig_hash_find(bat_priv, src);
2881 if (!orig_node)
2882 goto out;
2883
2884 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2885 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2886
2887 batadv_dbg(BATADV_DBG_TT, bat_priv,
2888 "Received ROAMING_ADV from %pM (client %pM)\n",
2889 src, roaming_adv->client);
2890
2891 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002892 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08002893 atomic_read(&orig_node->last_ttvn) + 1);
2894
2895out:
2896 if (orig_node)
2897 batadv_orig_node_free_ref(orig_node);
2898 return NET_RX_SUCCESS;
2899}
2900
2901/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002902 * batadv_tt_init - initialise the translation table internals
2903 * @bat_priv: the bat priv with all the soft interface information
2904 *
2905 * Return 0 on success or negative error number in case of failure.
2906 */
2907int batadv_tt_init(struct batadv_priv *bat_priv)
2908{
2909 int ret;
2910
2911 ret = batadv_tt_local_init(bat_priv);
2912 if (ret < 0)
2913 return ret;
2914
2915 ret = batadv_tt_global_init(bat_priv);
2916 if (ret < 0)
2917 return ret;
2918
2919 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002920 batadv_tt_tvlv_unicast_handler_v1,
2921 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002922
Marek Lindner122edaa2013-04-23 21:40:03 +08002923 batadv_tvlv_handler_register(bat_priv, NULL,
2924 batadv_roam_tvlv_unicast_handler_v1,
2925 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2926
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002927 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2928 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2929 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2930
2931 return 1;
2932}