blob: 00f4faa69c0778e5f7b7c4889cde27e11b28d21e [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;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001485 bool ap_isolation_enabled = false;
1486 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001487
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001488 /* if the AP isolation is requested on a VLAN, then check for its
1489 * setting in the proper VLAN private data structure
1490 */
1491 vlan = batadv_softif_vlan_get(bat_priv, vid);
1492 if (vlan) {
1493 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1494 batadv_softif_vlan_free_ref(vlan);
1495 }
1496
1497 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001498 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001499 if (!tt_local_entry ||
1500 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001501 goto out;
1502 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001503
Antonio Quartullic018ad32013-06-04 12:11:39 +02001504 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001505 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001506 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001507
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001508 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001509 * isolation
1510 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001511 if (tt_local_entry &&
1512 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001513 goto out;
1514
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001515 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001516 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001517 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001518 if (best_entry)
1519 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001520 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1521 orig_node = NULL;
1522 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001523
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001524out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001525 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001526 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001527 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001528 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001529
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001530 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001531}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001532
Antonio Quartulliced72932013-04-24 16:37:51 +02001533/**
1534 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1535 * to the given orig_node
1536 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001537 * @orig_node: originator for which the CRC should be computed
1538 *
1539 * This function computes the checksum for the global table corresponding to a
1540 * specific originator. In particular, the checksum is computed as follows: For
1541 * each client connected to the originator the CRC32C of the MAC address and the
1542 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1543 * together.
1544 *
1545 * The idea behind is that CRC32C should be used as much as possible in order to
1546 * produce a unique hash of the table, but since the order which is used to feed
1547 * the CRC32C function affects the result and since every node in the network
1548 * probably sorts the clients differently, the hash function cannot be directly
1549 * computed over the entire table. Hence the CRC32C is used only on
1550 * the single client entry, while all the results are then xor'ed together
1551 * because the XOR operation can combine them all while trying to reduce the
1552 * noise as much as possible.
1553 *
1554 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001555 */
1556static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001557 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001559 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001560 struct batadv_tt_common_entry *tt_common;
1561 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001563 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001564
1565 for (i = 0; i < hash->size; i++) {
1566 head = &hash->table[i];
1567
1568 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001569 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001570 tt_global = container_of(tt_common,
1571 struct batadv_tt_global_entry,
1572 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001573 /* Roaming clients are in the global table for
1574 * consistency only. They don't have to be
1575 * taken into account while computing the
1576 * global crc
1577 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001578 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001579 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001580 /* Temporary clients have not been announced yet, so
1581 * they have to be skipped while computing the global
1582 * crc
1583 */
1584 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1585 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001586
1587 /* find out if this global entry is announced by this
1588 * originator
1589 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001590 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001591 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001592 continue;
1593
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001594 crc_tmp = crc32c(0, &tt_common->vid,
1595 sizeof(tt_common->vid));
1596 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001597 }
1598 rcu_read_unlock();
1599 }
1600
Antonio Quartulliced72932013-04-24 16:37:51 +02001601 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001602}
1603
Antonio Quartulliced72932013-04-24 16:37:51 +02001604/**
1605 * batadv_tt_local_crc - calculates the checksum of the local table
1606 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001607 *
1608 * For details about the computation, please refer to the documentation for
1609 * batadv_tt_global_crc().
1610 *
1611 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001612 */
1613static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001615 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001616 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001618 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619
1620 for (i = 0; i < hash->size; i++) {
1621 head = &hash->table[i];
1622
1623 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001624 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001625 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001626 * account while computing the CRC
1627 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001628 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001629 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001630
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001631 crc_tmp = crc32c(0, &tt_common->vid,
1632 sizeof(tt_common->vid));
1633 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001634 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 rcu_read_unlock();
1636 }
1637
Antonio Quartulliced72932013-04-24 16:37:51 +02001638 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639}
1640
Sven Eckelmann56303d32012-06-05 22:31:31 +02001641static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001643 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644
Sven Eckelmann807736f2012-07-15 22:26:51 +02001645 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001646
Sven Eckelmann807736f2012-07-15 22:26:51 +02001647 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648 list_del(&node->list);
1649 kfree(node);
1650 }
1651
Sven Eckelmann807736f2012-07-15 22:26:51 +02001652 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653}
1654
Sven Eckelmann56303d32012-06-05 22:31:31 +02001655static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1656 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001657 const void *tt_buff,
1658 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001660 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001661 * last OGM (the OGM could carry no changes)
1662 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001663 spin_lock_bh(&orig_node->tt_buff_lock);
1664 if (tt_buff_len > 0) {
1665 kfree(orig_node->tt_buff);
1666 orig_node->tt_buff_len = 0;
1667 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1668 if (orig_node->tt_buff) {
1669 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1670 orig_node->tt_buff_len = tt_buff_len;
1671 }
1672 }
1673 spin_unlock_bh(&orig_node->tt_buff_lock);
1674}
1675
Sven Eckelmann56303d32012-06-05 22:31:31 +02001676static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001678 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679
Sven Eckelmann807736f2012-07-15 22:26:51 +02001680 spin_lock_bh(&bat_priv->tt.req_list_lock);
1681 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001682 if (batadv_has_timed_out(node->issued_at,
1683 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684 list_del(&node->list);
1685 kfree(node);
1686 }
1687 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001688 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001689}
1690
1691/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001692 * has already been issued for this orig_node, NULL otherwise
1693 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001694static struct batadv_tt_req_node *
1695batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1696 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001698 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001699
Sven Eckelmann807736f2012-07-15 22:26:51 +02001700 spin_lock_bh(&bat_priv->tt.req_list_lock);
1701 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001702 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1703 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001704 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705 goto unlock;
1706 }
1707
1708 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1709 if (!tt_req_node)
1710 goto unlock;
1711
1712 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1713 tt_req_node->issued_at = jiffies;
1714
Sven Eckelmann807736f2012-07-15 22:26:51 +02001715 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 return tt_req_node;
1719}
1720
Marek Lindner335fbe02013-04-23 21:40:02 +08001721/**
1722 * batadv_tt_local_valid - verify that given tt entry is a valid one
1723 * @entry_ptr: to be checked local tt entry
1724 * @data_ptr: not used but definition required to satisfy the callback prototype
1725 *
1726 * Returns 1 if the entry is a valid, 0 otherwise.
1727 */
1728static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001729{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001730 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001731
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001732 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001733 return 0;
1734 return 1;
1735}
1736
Sven Eckelmanna5130882012-05-16 20:23:16 +02001737static int batadv_tt_global_valid(const void *entry_ptr,
1738 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001740 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1741 const struct batadv_tt_global_entry *tt_global_entry;
1742 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001744 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1745 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001746 return 0;
1747
Sven Eckelmann56303d32012-06-05 22:31:31 +02001748 tt_global_entry = container_of(tt_common_entry,
1749 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001750 common);
1751
Sven Eckelmanna5130882012-05-16 20:23:16 +02001752 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753}
1754
Marek Lindner335fbe02013-04-23 21:40:02 +08001755/**
1756 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1757 * tt entries from the specified tt hash
1758 * @bat_priv: the bat priv with all the soft interface information
1759 * @hash: hash table containing the tt entries
1760 * @tt_len: expected tvlv tt data buffer length in number of bytes
1761 * @valid_cb: function to filter tt change entries
1762 * @cb_data: data passed to the filter function as argument
1763 *
1764 * Returns pointer to allocated tvlv tt data buffer if operation was
1765 * successful or NULL otherwise.
1766 */
1767static struct batadv_tvlv_tt_data *
1768batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1769 struct batadv_hashtable *hash, uint16_t tt_len,
1770 int (*valid_cb)(const void *, const void *),
1771 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001773 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001774 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1775 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001777 uint16_t tt_tot, tt_num_entries = 0;
1778 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001779 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Marek Lindner335fbe02013-04-23 21:40:02 +08001781 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1782 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1783 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785
Antonio Quartulli298e6e62013-05-28 13:14:27 +02001786 tt_tot = batadv_tt_entries(tt_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08001787
1788 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1789 GFP_ATOMIC);
1790 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 goto out;
1792
Marek Lindner335fbe02013-04-23 21:40:02 +08001793 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794
1795 rcu_read_lock();
1796 for (i = 0; i < hash->size; i++) {
1797 head = &hash->table[i];
1798
Sasha Levinb67bfe02013-02-27 17:06:00 -08001799 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001800 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001801 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001802 break;
1803
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001804 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805 continue;
1806
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001807 memcpy(tt_change->addr, tt_common_entry->addr,
1808 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001809 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001810 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08001811 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812
Marek Lindner335fbe02013-04-23 21:40:02 +08001813 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814 tt_change++;
1815 }
1816 }
1817 rcu_read_unlock();
1818
1819out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001820 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001821}
1822
Antonio Quartulliced72932013-04-24 16:37:51 +02001823/**
1824 * batadv_send_tt_request - send a TT Request message to a given node
1825 * @bat_priv: the bat priv with all the soft interface information
1826 * @dst_orig_node: the destination of the message
1827 * @ttvn: the version number that the source of the message is looking for
1828 * @tt_crc: the CRC associated with the version number
1829 * @full_table: ask for the entire translation table if true, while only for the
1830 * last TT diff otherwise
1831 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001832static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1833 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001834 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001835 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836{
Marek Lindner335fbe02013-04-23 21:40:02 +08001837 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001838 struct batadv_hard_iface *primary_if;
1839 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001840 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001842 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 if (!primary_if)
1844 goto out;
1845
1846 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001847 * reply from the same orig_node yet
1848 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001849 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001850 if (!tt_req_node)
1851 goto out;
1852
Marek Lindner335fbe02013-04-23 21:40:02 +08001853 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1854 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 goto out;
1856
Marek Lindner335fbe02013-04-23 21:40:02 +08001857 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1858 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001859 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001860
1861 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001862 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001864 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001865 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001866
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001867 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001868 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1869 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1870 tvlv_tt_data, sizeof(*tvlv_tt_data));
1871 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872
1873out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001875 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001877 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001878 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001879 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001880 kfree(tt_req_node);
1881 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001882 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 return ret;
1884}
1885
Marek Lindner335fbe02013-04-23 21:40:02 +08001886/**
1887 * batadv_send_other_tt_response - send reply to tt request concerning another
1888 * node's translation table
1889 * @bat_priv: the bat priv with all the soft interface information
1890 * @tt_data: tt data containing the tt request information
1891 * @req_src: mac address of tt request sender
1892 * @req_dst: mac address of tt request recipient
1893 *
1894 * Returns true if tt request reply was sent, false otherwise.
1895 */
1896static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1897 struct batadv_tvlv_tt_data *tt_data,
1898 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001900 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001901 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001902 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1903 uint8_t orig_ttvn, req_ttvn;
1904 uint16_t tt_len;
1905 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001906
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001907 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001908 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001909 req_src, tt_data->ttvn, req_dst,
1910 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001911
1912 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001913 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 if (!req_dst_orig_node)
1915 goto out;
1916
Marek Lindner335fbe02013-04-23 21:40:02 +08001917 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918 if (!res_dst_orig_node)
1919 goto out;
1920
Antonio Quartullia73105b2011-04-27 14:27:44 +02001921 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001922 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001923
Marek Lindner335fbe02013-04-23 21:40:02 +08001924 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001926 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927 goto out;
1928
Antonio Quartulli015758d2011-07-09 17:52:13 +02001929 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001930 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001931 !req_dst_orig_node->tt_buff)
1932 full_table = true;
1933 else
1934 full_table = false;
1935
Marek Lindner335fbe02013-04-23 21:40:02 +08001936 /* TT fragmentation hasn't been implemented yet, so send as many
1937 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001938 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939 if (!full_table) {
1940 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1941 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942
Marek Lindner335fbe02013-04-23 21:40:02 +08001943 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1944 GFP_ATOMIC);
1945 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001946 goto unlock;
1947
Antonio Quartullia73105b2011-04-27 14:27:44 +02001948 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001949 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001950 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001951 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1952 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001953 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001954 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955
Marek Lindner335fbe02013-04-23 21:40:02 +08001956 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1957 bat_priv->tt.global_hash,
1958 tt_len,
1959 batadv_tt_global_valid,
1960 req_dst_orig_node);
1961 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963 }
1964
Marek Lindner335fbe02013-04-23 21:40:02 +08001965 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1966 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967
1968 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001969 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001971 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001972 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1973 res_dst_orig_node->orig, req_dst_orig_node->orig,
1974 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001975
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001976 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001977
Marek Lindner335fbe02013-04-23 21:40:02 +08001978 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1979 req_src, BATADV_TVLV_TT, 1,
1980 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001981
Marek Lindner335fbe02013-04-23 21:40:02 +08001982 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983 goto out;
1984
1985unlock:
1986 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1987
1988out:
1989 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001990 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001992 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001993 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001994 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001995}
Sven Eckelmann96412692012-06-05 22:31:30 +02001996
Marek Lindner335fbe02013-04-23 21:40:02 +08001997/**
1998 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1999 * translation table
2000 * @bat_priv: the bat priv with all the soft interface information
2001 * @tt_data: tt data containing the tt request information
2002 * @req_src: mac address of tt request sender
2003 *
2004 * Returns true if tt request reply was sent, false otherwise.
2005 */
2006static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2007 struct batadv_tvlv_tt_data *tt_data,
2008 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009{
Marek Lindner335fbe02013-04-23 21:40:02 +08002010 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02002011 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002012 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002013 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08002015 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002016
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002017 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002018 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002019 req_src, tt_data->ttvn,
2020 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002022 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023
Sven Eckelmann807736f2012-07-15 22:26:51 +02002024 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002025 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
Marek Lindner335fbe02013-04-23 21:40:02 +08002027 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028 if (!orig_node)
2029 goto out;
2030
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002031 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002032 if (!primary_if)
2033 goto out;
2034
2035 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002036 * is too big send the whole local translation table
2037 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002038 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002039 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002040 full_table = true;
2041 else
2042 full_table = false;
2043
Marek Lindner335fbe02013-04-23 21:40:02 +08002044 /* TT fragmentation hasn't been implemented yet, so send as many
2045 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002046 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002047 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002048 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2049 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050
Marek Lindner335fbe02013-04-23 21:40:02 +08002051 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
2052 GFP_ATOMIC);
2053 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002054 goto unlock;
2055
Marek Lindner335fbe02013-04-23 21:40:02 +08002056 /* Copy the last orig_node's OGM buffer */
2057 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002058 bat_priv->tt.last_changeset_len);
2059 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002061 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08002062 tt_len = batadv_tt_len(tt_len);
2063 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002064
Marek Lindner335fbe02013-04-23 21:40:02 +08002065 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
2066 bat_priv->tt.local_hash,
2067 tt_len,
2068 batadv_tt_local_valid,
2069 NULL);
2070 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072 }
2073
Marek Lindner335fbe02013-04-23 21:40:02 +08002074 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2075 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002076
2077 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002078 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002079
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002080 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002081 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2082 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002084 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002085
Marek Lindner335fbe02013-04-23 21:40:02 +08002086 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2087 req_src, BATADV_TVLV_TT, 1,
2088 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
2089
Antonio Quartullia73105b2011-04-27 14:27:44 +02002090 goto out;
2091
2092unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002093 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002094out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002095 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002097 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002098 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002099 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002100 kfree(tvlv_tt_data);
2101 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002102 return true;
2103}
2104
Marek Lindner335fbe02013-04-23 21:40:02 +08002105/**
2106 * batadv_send_tt_response - send reply to tt request
2107 * @bat_priv: the bat priv with all the soft interface information
2108 * @tt_data: tt data containing the tt request information
2109 * @req_src: mac address of tt request sender
2110 * @req_dst: mac address of tt request recipient
2111 *
2112 * Returns true if tt request reply was sent, false otherwise.
2113 */
2114static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2115 struct batadv_tvlv_tt_data *tt_data,
2116 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002117{
Marek Lindner335fbe02013-04-23 21:40:02 +08002118 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002119 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08002120 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002121 return true;
2122
Marek Lindner335fbe02013-04-23 21:40:02 +08002123 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002124 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002125 return batadv_send_other_tt_response(bat_priv, tt_data,
2126 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002127 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002128}
2129
Sven Eckelmann56303d32012-06-05 22:31:31 +02002130static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2131 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002132 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002133 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002134{
2135 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002136 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002137
2138 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002139 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2140 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002141 batadv_tt_global_del(bat_priv, orig_node,
2142 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002143 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002144 "tt removed by changes",
2145 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002146 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002147 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002148 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002149 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002150 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002151 /* In case of problem while storing a
2152 * global_entry, we stop the updating
2153 * procedure without committing the
2154 * ttvn change. This will avoid to send
2155 * corrupted data on tt_request
2156 */
2157 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002158 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002160 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002161}
2162
Sven Eckelmann56303d32012-06-05 22:31:31 +02002163static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002164 struct batadv_tvlv_tt_data *tt_data,
2165 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002167 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002168
Marek Lindner335fbe02013-04-23 21:40:02 +08002169 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170 if (!orig_node)
2171 goto out;
2172
2173 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002174 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002175
Sven Eckelmanna5130882012-05-16 20:23:16 +02002176 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002177 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2178 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002179
2180 spin_lock_bh(&orig_node->tt_buff_lock);
2181 kfree(orig_node->tt_buff);
2182 orig_node->tt_buff_len = 0;
2183 orig_node->tt_buff = NULL;
2184 spin_unlock_bh(&orig_node->tt_buff_lock);
2185
Marek Lindner335fbe02013-04-23 21:40:02 +08002186 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002187
2188out:
2189 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002190 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002191}
2192
Sven Eckelmann56303d32012-06-05 22:31:31 +02002193static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2194 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002195 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002196 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002197{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002198 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2199 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002200
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002201 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2202 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002203 atomic_set(&orig_node->last_ttvn, ttvn);
2204}
2205
Antonio Quartullic018ad32013-06-04 12:11:39 +02002206/**
2207 * batadv_is_my_client - check if a client is served by the local node
2208 * @bat_priv: the bat priv with all the soft interface information
2209 * @addr: the mac adress of the client to check
2210 * @vid: VLAN identifier
2211 *
2212 * Returns true if the client is served by this node, false otherwise.
2213 */
2214bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2215 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002216{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002217 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002218 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002219
Antonio Quartullic018ad32013-06-04 12:11:39 +02002220 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002221 if (!tt_local_entry)
2222 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002223 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002224 * consistency purpose)
2225 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002226 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2227 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002228 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002229 ret = true;
2230out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002232 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002233 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002234}
2235
Marek Lindner335fbe02013-04-23 21:40:02 +08002236/**
2237 * batadv_handle_tt_response - process incoming tt reply
2238 * @bat_priv: the bat priv with all the soft interface information
2239 * @tt_data: tt data containing the tt request information
2240 * @resp_src: mac address of tt reply sender
2241 * @num_entries: number of tt change entries appended to the tt data
2242 */
2243static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2244 struct batadv_tvlv_tt_data *tt_data,
2245 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002246{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002247 struct batadv_tt_req_node *node, *safe;
2248 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002249 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002250
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002251 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002252 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002253 resp_src, tt_data->ttvn, num_entries,
2254 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002255
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002256 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002257 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002258 goto out;
2259
Marek Lindner335fbe02013-04-23 21:40:02 +08002260 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002261 if (!orig_node)
2262 goto out;
2263
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002264 spin_lock_bh(&orig_node->tt_lock);
2265
Marek Lindner335fbe02013-04-23 21:40:02 +08002266 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2267 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002268 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002269 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2270 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2271 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002272 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002273
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002274 /* Recalculate the CRC for this orig_node and store it */
2275 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2276
2277 spin_unlock_bh(&orig_node->tt_lock);
2278
Antonio Quartullia73105b2011-04-27 14:27:44 +02002279 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002280 spin_lock_bh(&bat_priv->tt.req_list_lock);
2281 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002282 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002283 continue;
2284 list_del(&node->list);
2285 kfree(node);
2286 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002288out:
2289 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002290 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002291}
2292
Sven Eckelmann56303d32012-06-05 22:31:31 +02002293static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002294{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002295 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002296
Sven Eckelmann807736f2012-07-15 22:26:51 +02002297 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002298
Sven Eckelmann807736f2012-07-15 22:26:51 +02002299 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002300 list_del(&node->list);
2301 kfree(node);
2302 }
2303
Sven Eckelmann807736f2012-07-15 22:26:51 +02002304 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002305}
2306
Sven Eckelmann56303d32012-06-05 22:31:31 +02002307static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002308{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002309 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002310
Sven Eckelmann807736f2012-07-15 22:26:51 +02002311 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2312 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002313 if (!batadv_has_timed_out(node->first_time,
2314 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002315 continue;
2316
2317 list_del(&node->list);
2318 kfree(node);
2319 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002320 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002321}
2322
2323/* This function checks whether the client already reached the
2324 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2325 * will not be sent.
2326 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002327 * returns true if the ROAMING_ADV can be sent, false otherwise
2328 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002329static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002330 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002331{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002332 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002333 bool ret = false;
2334
Sven Eckelmann807736f2012-07-15 22:26:51 +02002335 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002336 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002337 * reply from the same orig_node yet
2338 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002339 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002340 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002341 continue;
2342
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002343 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002344 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002345 continue;
2346
Sven Eckelmann3e348192012-05-16 20:23:22 +02002347 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002348 /* Sorry, you roamed too many times! */
2349 goto unlock;
2350 ret = true;
2351 break;
2352 }
2353
2354 if (!ret) {
2355 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2356 if (!tt_roam_node)
2357 goto unlock;
2358
2359 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002360 atomic_set(&tt_roam_node->counter,
2361 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002362 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2363
Sven Eckelmann807736f2012-07-15 22:26:51 +02002364 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002365 ret = true;
2366 }
2367
2368unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002369 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002370 return ret;
2371}
2372
Antonio Quartullic018ad32013-06-04 12:11:39 +02002373/**
2374 * batadv_send_roam_adv - send a roaming advertisement message
2375 * @bat_priv: the bat priv with all the soft interface information
2376 * @client: mac address of the roaming client
2377 * @vid: VLAN identifier
2378 * @orig_node: message destination
2379 *
2380 * Send a ROAMING_ADV message to the node which was previously serving this
2381 * client. This is done to inform the node that from now on all traffic destined
2382 * for this particular roamed client has to be forwarded to the sender of the
2383 * roaming message.
2384 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002385static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002386 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002387 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002388{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002389 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002390 struct batadv_tvlv_roam_adv tvlv_roam;
2391
2392 primary_if = batadv_primary_if_get_selected(bat_priv);
2393 if (!primary_if)
2394 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002395
2396 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002397 * already roamed to us too many times
2398 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002399 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002400 goto out;
2401
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002402 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002403 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2404 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002405
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002406 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002407
Marek Lindner122edaa2013-04-23 21:40:03 +08002408 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002409 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002410
2411 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2412 orig_node->orig, BATADV_TVLV_ROAM, 1,
2413 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002414
2415out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002416 if (primary_if)
2417 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002418}
2419
Sven Eckelmanna5130882012-05-16 20:23:16 +02002420static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002421{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002422 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002423 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002424 struct batadv_priv *bat_priv;
2425
2426 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002427 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2428 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002429
Sven Eckelmanna5130882012-05-16 20:23:16 +02002430 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002431 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002432 batadv_tt_req_purge(bat_priv);
2433 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002434
Antonio Quartulli72414442012-12-25 13:14:37 +01002435 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2436 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002437}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002438
Sven Eckelmann56303d32012-06-05 22:31:31 +02002439void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002440{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002441 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2442 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2443
Sven Eckelmann807736f2012-07-15 22:26:51 +02002444 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002445
Sven Eckelmanna5130882012-05-16 20:23:16 +02002446 batadv_tt_local_table_free(bat_priv);
2447 batadv_tt_global_table_free(bat_priv);
2448 batadv_tt_req_list_free(bat_priv);
2449 batadv_tt_changes_list_free(bat_priv);
2450 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002451
Sven Eckelmann807736f2012-07-15 22:26:51 +02002452 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002453}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002454
Antonio Quartulli697f2532011-11-07 16:47:01 +01002455/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002456 * in the given hash table and returns the number of modified entries
2457 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002458static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2459 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002460{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002461 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002462 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002463 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002464 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002465
2466 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002467 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002468
2469 for (i = 0; i < hash->size; i++) {
2470 head = &hash->table[i];
2471
2472 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002473 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002474 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002475 if (enable) {
2476 if ((tt_common_entry->flags & flags) == flags)
2477 continue;
2478 tt_common_entry->flags |= flags;
2479 } else {
2480 if (!(tt_common_entry->flags & flags))
2481 continue;
2482 tt_common_entry->flags &= ~flags;
2483 }
2484 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002485 }
2486 rcu_read_unlock();
2487 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002488out:
2489 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002490}
2491
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002492/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002493static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002494{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002495 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002496 struct batadv_tt_common_entry *tt_common;
2497 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002498 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002499 struct hlist_head *head;
2500 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002501 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002502
2503 if (!hash)
2504 return;
2505
2506 for (i = 0; i < hash->size; i++) {
2507 head = &hash->table[i];
2508 list_lock = &hash->list_locks[i];
2509
2510 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002511 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002512 hash_entry) {
2513 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002514 continue;
2515
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002516 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002517 "Deleting local tt entry (%pM, vid: %d): pending\n",
2518 tt_common->addr,
2519 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002520
Sven Eckelmann807736f2012-07-15 22:26:51 +02002521 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002522 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002523 tt_local = container_of(tt_common,
2524 struct batadv_tt_local_entry,
2525 common);
2526 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002527 }
2528 spin_unlock_bh(list_lock);
2529 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002530}
2531
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002532/**
2533 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2534 * have been queued in the time since the last commit
2535 * @bat_priv: the bat priv with all the soft interface information
2536 */
2537void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002538{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002539 uint16_t changed_num = 0;
2540
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002541 spin_lock_bh(&bat_priv->tt.commit_lock);
2542
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002543 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2544 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2545 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002546 goto out;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002547 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002548
Sven Eckelmann807736f2012-07-15 22:26:51 +02002549 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002550 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002551
2552 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002553 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002554 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002555 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002556
2557 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002558 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002559 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002560 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002561 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002562
2563 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002564 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002565 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002566
2567out:
2568 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002569}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002570
Sven Eckelmann56303d32012-06-05 22:31:31 +02002571bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002572 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002573{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002574 struct batadv_tt_local_entry *tt_local_entry = NULL;
2575 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002576 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02002577 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002578
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002579 vlan = batadv_softif_vlan_get(bat_priv, vid);
2580 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002581 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002582
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002583 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002584 if (!tt_local_entry)
2585 goto out;
2586
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002587 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002588 if (!tt_global_entry)
2589 goto out;
2590
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002591 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002592 goto out;
2593
Marek Lindner5870adc2012-06-20 17:16:05 +02002594 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002595
2596out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02002597 if (vlan)
2598 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002599 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002600 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002601 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002602 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002603 return ret;
2604}
Marek Lindnera943cac2011-07-30 13:10:18 +02002605
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002606/**
2607 * batadv_tt_update_orig - update global translation table with new tt
2608 * information received via ogms
2609 * @bat_priv: the bat priv with all the soft interface information
2610 * @orig: the orig_node of the ogm
2611 * @tt_buff: buffer holding the tt information
2612 * @tt_num_changes: number of tt changes inside the tt buffer
2613 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002614 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002615 */
2616static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2617 struct batadv_orig_node *orig_node,
2618 const unsigned char *tt_buff,
2619 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002620 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002621{
2622 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2623 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002624 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002625
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002626 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002627 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002628 return;
2629
Antonio Quartulli17071572011-11-07 16:36:40 +01002630 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002631 * increased by one -> we can apply the attached changes
2632 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002633 if ((!orig_node->tt_initialised && ttvn == 1) ||
2634 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002635 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002636 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2637 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002638 * In this case send a tt request
2639 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002640 if (!tt_num_changes) {
2641 full_table = false;
2642 goto request_table;
2643 }
2644
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002645 spin_lock_bh(&orig_node->tt_lock);
2646
Marek Lindner335fbe02013-04-23 21:40:02 +08002647 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002648 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002649 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002650
2651 /* Even if we received the precomputed crc with the OGM, we
2652 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002653 * in the global table
2654 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002655 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002656
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002657 spin_unlock_bh(&orig_node->tt_lock);
2658
Marek Lindnera943cac2011-07-30 13:10:18 +02002659 /* The ttvn alone is not enough to guarantee consistency
2660 * because a single value could represent different states
2661 * (due to the wrap around). Thus a node has to check whether
2662 * the resulting table (after applying the changes) is still
2663 * consistent or not. E.g. a node could disconnect while its
2664 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2665 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002666 * inconsistency
2667 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002668 if (orig_node->tt_crc != tt_crc)
2669 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002670 } else {
2671 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002672 * in sync anymore -> request fresh tt data
2673 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002674 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2675 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002676request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002677 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002678 "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 +02002679 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2680 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002681 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2682 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002683 return;
2684 }
2685 }
2686}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002687
Antonio Quartullic018ad32013-06-04 12:11:39 +02002688/**
2689 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
2690 * @bat_priv: the bat priv with all the soft interface information
2691 * @addr: the mac address of the client to check
2692 * @vid: VLAN identifier
2693 *
2694 * Returns true if we know that the client has moved from its old originator
2695 * to another one. This entry is still kept for consistency purposes and will be
2696 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002697 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002698bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002699 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002700{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002701 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002702 bool ret = false;
2703
Antonio Quartullic018ad32013-06-04 12:11:39 +02002704 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002705 if (!tt_global_entry)
2706 goto out;
2707
Antonio Quartullic1d07432013-01-15 22:17:19 +10002708 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002709 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002710out:
2711 return ret;
2712}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002713
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002714/**
2715 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2716 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02002717 * @addr: the mac address of the local client to query
2718 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002719 *
2720 * Returns true if the local client is known to be roaming (it is not served by
2721 * this node anymore) or not. If yes, the client is still present in the table
2722 * to keep the latter consistent with the node TTVN
2723 */
2724bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002725 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002726{
2727 struct batadv_tt_local_entry *tt_local_entry;
2728 bool ret = false;
2729
Antonio Quartullic018ad32013-06-04 12:11:39 +02002730 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002731 if (!tt_local_entry)
2732 goto out;
2733
2734 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2735 batadv_tt_local_entry_free_ref(tt_local_entry);
2736out:
2737 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002738}
2739
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002740bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2741 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002742 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02002743 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002744{
2745 bool ret = false;
2746
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002747 /* if the originator is a backbone node (meaning it belongs to the same
2748 * LAN of this node) the temporary client must not be added because to
2749 * reach such destination the node must use the LAN instead of the mesh
2750 */
2751 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2752 goto out;
2753
Antonio Quartulli16052782013-06-04 12:11:41 +02002754 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002755 BATADV_TT_CLIENT_TEMP,
2756 atomic_read(&orig_node->last_ttvn)))
2757 goto out;
2758
2759 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002760 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
2761 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002762 ret = true;
2763out:
2764 return ret;
2765}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002766
2767/**
2768 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2769 * @bat_priv: the bat priv with all the soft interface information
2770 * @orig: the orig_node of the ogm
2771 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2772 * @tvlv_value: tvlv buffer containing the gateway data
2773 * @tvlv_value_len: tvlv buffer length
2774 */
2775static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2776 struct batadv_orig_node *orig,
2777 uint8_t flags,
2778 void *tvlv_value,
2779 uint16_t tvlv_value_len)
2780{
2781 struct batadv_tvlv_tt_data *tt_data;
2782 uint16_t num_entries;
2783
2784 if (tvlv_value_len < sizeof(*tt_data))
2785 return;
2786
2787 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2788 tvlv_value_len -= sizeof(*tt_data);
2789
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002790 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002791
2792 batadv_tt_update_orig(bat_priv, orig,
2793 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002794 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002795}
2796
2797/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002798 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2799 * container
2800 * @bat_priv: the bat priv with all the soft interface information
2801 * @src: mac address of tt tvlv sender
2802 * @dst: mac address of tt tvlv recipient
2803 * @tvlv_value: tvlv buffer containing the tt data
2804 * @tvlv_value_len: tvlv buffer length
2805 *
2806 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2807 * otherwise.
2808 */
2809static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2810 uint8_t *src, uint8_t *dst,
2811 void *tvlv_value,
2812 uint16_t tvlv_value_len)
2813{
2814 struct batadv_tvlv_tt_data *tt_data;
2815 uint16_t num_entries;
2816 char tt_flag;
2817 bool ret;
2818
2819 if (tvlv_value_len < sizeof(*tt_data))
2820 return NET_RX_SUCCESS;
2821
2822 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2823 tvlv_value_len -= sizeof(*tt_data);
2824
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002825 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002826
2827 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2828 case BATADV_TT_REQUEST:
2829 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2830
2831 /* If this node cannot provide a TT response the tt_request is
2832 * forwarded
2833 */
2834 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2835 if (!ret) {
2836 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2837 tt_flag = 'F';
2838 else
2839 tt_flag = '.';
2840
2841 batadv_dbg(BATADV_DBG_TT, bat_priv,
2842 "Routing TT_REQUEST to %pM [%c]\n",
2843 dst, tt_flag);
2844 /* tvlv API will re-route the packet */
2845 return NET_RX_DROP;
2846 }
2847 break;
2848 case BATADV_TT_RESPONSE:
2849 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2850
2851 if (batadv_is_my_mac(bat_priv, dst)) {
2852 batadv_handle_tt_response(bat_priv, tt_data,
2853 src, num_entries);
2854 return NET_RX_SUCCESS;
2855 }
2856
2857 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2858 tt_flag = 'F';
2859 else
2860 tt_flag = '.';
2861
2862 batadv_dbg(BATADV_DBG_TT, bat_priv,
2863 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2864
2865 /* tvlv API will re-route the packet */
2866 return NET_RX_DROP;
2867 }
2868
2869 return NET_RX_SUCCESS;
2870}
2871
2872/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002873 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2874 * @bat_priv: the bat priv with all the soft interface information
2875 * @src: mac address of tt tvlv sender
2876 * @dst: mac address of tt tvlv recipient
2877 * @tvlv_value: tvlv buffer containing the tt data
2878 * @tvlv_value_len: tvlv buffer length
2879 *
2880 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2881 * otherwise.
2882 */
2883static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2884 uint8_t *src, uint8_t *dst,
2885 void *tvlv_value,
2886 uint16_t tvlv_value_len)
2887{
2888 struct batadv_tvlv_roam_adv *roaming_adv;
2889 struct batadv_orig_node *orig_node = NULL;
2890
2891 /* If this node is not the intended recipient of the
2892 * roaming advertisement the packet is forwarded
2893 * (the tvlv API will re-route the packet).
2894 */
2895 if (!batadv_is_my_mac(bat_priv, dst))
2896 return NET_RX_DROP;
2897
2898 /* check if it is a backbone gateway. we don't accept
2899 * roaming advertisement from it, as it has the same
2900 * entries as we have.
2901 */
2902 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2903 goto out;
2904
2905 if (tvlv_value_len < sizeof(*roaming_adv))
2906 goto out;
2907
2908 orig_node = batadv_orig_hash_find(bat_priv, src);
2909 if (!orig_node)
2910 goto out;
2911
2912 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2913 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2914
2915 batadv_dbg(BATADV_DBG_TT, bat_priv,
2916 "Received ROAMING_ADV from %pM (client %pM)\n",
2917 src, roaming_adv->client);
2918
2919 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002920 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08002921 atomic_read(&orig_node->last_ttvn) + 1);
2922
2923out:
2924 if (orig_node)
2925 batadv_orig_node_free_ref(orig_node);
2926 return NET_RX_SUCCESS;
2927}
2928
2929/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002930 * batadv_tt_init - initialise the translation table internals
2931 * @bat_priv: the bat priv with all the soft interface information
2932 *
2933 * Return 0 on success or negative error number in case of failure.
2934 */
2935int batadv_tt_init(struct batadv_priv *bat_priv)
2936{
2937 int ret;
2938
2939 ret = batadv_tt_local_init(bat_priv);
2940 if (ret < 0)
2941 return ret;
2942
2943 ret = batadv_tt_global_init(bat_priv);
2944 if (ret < 0)
2945 return ret;
2946
2947 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002948 batadv_tt_tvlv_unicast_handler_v1,
2949 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002950
Marek Lindner122edaa2013-04-23 21:40:03 +08002951 batadv_tvlv_handler_register(bat_priv, NULL,
2952 batadv_roam_tvlv_unicast_handler_v1,
2953 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2954
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002955 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2956 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2957 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2958
2959 return 1;
2960}