blob: c8fc303ae71e8a8ea52606b91413d91e597f9811 [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,
331 "Deleting global tt entry %pM: %s\n",
332 tt_global->common.addr, message);
333
334 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200335 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200336 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200337}
338
Antonio Quartullic018ad32013-06-04 12:11:39 +0200339/**
340 * batadv_tt_local_add - add a new client to the local table or update an
341 * existing client
342 * @soft_iface: netdev struct of the mesh interface
343 * @addr: the mac address of the client to add
344 * @vid: VLAN identifier
345 * @ifindex: index of the interface where the client is connected to (useful to
346 * identify wireless clients)
347 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200348void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200349 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200351 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200352 struct batadv_tt_local_entry *tt_local;
353 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200354 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200355 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100356 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200357 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358
Antonio Quartullic018ad32013-06-04 12:11:39 +0200359 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
360 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361
Antonio Quartulli47c94652012-09-23 22:38:35 +0200362 if (tt_local) {
363 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200364 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
365 batadv_dbg(BATADV_DBG_TT, bat_priv,
366 "Re-adding pending client %pM\n", addr);
367 /* whatever the reason why the PENDING flag was set,
368 * this is a client which was enqueued to be removed in
369 * this orig_interval. Since it popped up again, the
370 * flag can be reset like it was never enqueued
371 */
372 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
373 goto add_event;
374 }
375
376 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
377 batadv_dbg(BATADV_DBG_TT, bat_priv,
378 "Roaming client %pM came back to its original location\n",
379 addr);
380 /* the ROAM flag is set because this client roamed away
381 * and the node got a roaming_advertisement message. Now
382 * that the client popped up again at its original
383 * location such flag can be unset
384 */
385 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
386 roamed_back = true;
387 }
388 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 }
390
Antonio Quartulli47c94652012-09-23 22:38:35 +0200391 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
392 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200393 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200394
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200395 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200396 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200397 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Antonio Quartulli47c94652012-09-23 22:38:35 +0200399 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100400 /* The local entry has to be marked as NEW to avoid to send it in
401 * a full table response going out before the next ttvn increment
402 * (consistency check)
403 */
404 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200405 tt_local->common.vid = vid;
Sven Eckelmann95638772012-05-12 02:09:31 +0200406 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200407 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
408 atomic_set(&tt_local->common.refcount, 2);
409 tt_local->last_seen = jiffies;
410 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
412 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200413 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200414 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Sven Eckelmann807736f2012-07-15 22:26:51 +0200416 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200417 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200418 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100419
420 if (unlikely(hash_added != 0)) {
421 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200422 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100423 goto out;
424 }
425
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200426add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200427 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200428
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200429check_roaming:
430 /* Check whether it is a roaming, but don't do anything if the roaming
431 * process has already been handled
432 */
433 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200434 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200435 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200436 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800437 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200438 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200439 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200440 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200441 }
442 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200443 if (roamed_back) {
444 batadv_tt_global_free(bat_priv, tt_global,
445 "Roaming canceled");
446 tt_global = NULL;
447 } else {
448 /* The global entry has to be marked as ROAMING and
449 * has to be kept for consistency purpose
450 */
451 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
452 tt_global->roam_at = jiffies;
453 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200454 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200455
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200456out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200457 if (tt_local)
458 batadv_tt_local_entry_free_ref(tt_local);
459 if (tt_global)
460 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461}
462
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800463/**
464 * batadv_tt_tvlv_container_update - update the translation table tvlv container
465 * after local tt changes have been committed
466 * @bat_priv: the bat priv with all the soft interface information
467 */
468static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800470 struct batadv_tt_change_node *entry, *safe;
471 struct batadv_tvlv_tt_data *tt_data;
472 struct batadv_tvlv_tt_change *tt_change;
473 int tt_diff_len = 0, tt_change_len = 0;
474 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800476 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800477
478 /* if we have too many changes for one packet don't send any
479 * and wait for the tt table request which will be fragmented
480 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800481 if (tt_diff_len > bat_priv->soft_iface->mtu)
482 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800483
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800484 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
485 if (!tt_data)
486 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800487
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800488 tt_data->flags = BATADV_TT_OGM_DIFF;
489 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
Antonio Quartulliced72932013-04-24 16:37:51 +0200490 tt_data->crc = htonl(bat_priv->tt.local_crc);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800491
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800492 if (tt_diff_len == 0)
493 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800494
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200495 tt_diff_entries_num = batadv_tt_entries(tt_diff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmann807736f2012-07-15 22:26:51 +0200497 spin_lock_bh(&bat_priv->tt.changes_list_lock);
498 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800500 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
501
Sven Eckelmann807736f2012-07-15 22:26:51 +0200502 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100503 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800504 if (tt_diff_entries_count < tt_diff_entries_num) {
505 memcpy(tt_change + tt_diff_entries_count,
506 &entry->change,
507 sizeof(struct batadv_tvlv_tt_change));
508 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200510 list_del(&entry->list);
511 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200513 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200516 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
517 kfree(bat_priv->tt.last_changeset);
518 bat_priv->tt.last_changeset_len = 0;
519 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800520 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800521 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800522 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800523 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200524 * instead of providing the diff
525 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800526 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200527 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800528 memcpy(bat_priv->tt.last_changeset,
529 tt_change, tt_change_len);
530 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200531 }
532 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200533 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800535container_register:
536 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
537 sizeof(*tt_data) + tt_change_len);
538 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539}
540
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200541int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542{
543 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200544 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200545 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200546 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100547 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200548 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200550 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100551 int last_seen_secs;
552 int last_seen_msecs;
553 unsigned long last_seen_jiffies;
554 bool no_purge;
555 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556
Marek Lindner30da63a2012-08-03 17:15:46 +0200557 primary_if = batadv_seq_print_text_primary_if_get(seq);
558 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100561 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200562 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100563 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
564 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100565 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
566 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 for (i = 0; i < hash->size; i++) {
569 head = &hash->table[i];
570
Marek Lindner7aadf882011-02-18 12:28:09 +0000571 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800572 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000573 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100574 tt_local = container_of(tt_common_entry,
575 struct batadv_tt_local_entry,
576 common);
577 last_seen_jiffies = jiffies - tt_local->last_seen;
578 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
579 last_seen_secs = last_seen_msecs / 1000;
580 last_seen_msecs = last_seen_msecs % 1000;
581
582 no_purge = tt_common_entry->flags & np_flag;
583
584 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100585 tt_common_entry->addr,
586 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200587 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100588 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100589 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200590 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100591 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200592 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100593 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100594 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100595 no_purge ? 0 : last_seen_secs,
596 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000598 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200600out:
601 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200602 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200603 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604}
605
Sven Eckelmann56303d32012-06-05 22:31:31 +0200606static void
607batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
608 struct batadv_tt_local_entry *tt_local_entry,
609 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200611 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
Antonio Quartulli015758d2011-07-09 17:52:13 +0200613 /* The local client has to be marked as "pending to be removed" but has
614 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200615 * response issued before the net ttvn increment (consistency check)
616 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200617 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100618
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200619 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200620 "Local tt entry (%pM) pending to be removed: %s\n",
621 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622}
623
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200624/**
625 * batadv_tt_local_remove - logically remove an entry from the local table
626 * @bat_priv: the bat priv with all the soft interface information
627 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200628 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200629 * @message: message to append to the log on deletion
630 * @roaming: true if the deletion is due to a roaming event
631 *
632 * Returns the flags assigned to the local entry before being deleted
633 */
634uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200635 const uint8_t *addr, unsigned short vid,
636 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200638 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200639 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640
Antonio Quartullic018ad32013-06-04 12:11:39 +0200641 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200642 if (!tt_local_entry)
643 goto out;
644
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200645 curr_flags = tt_local_entry->common.flags;
646
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200647 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200648 /* if this global entry addition is due to a roaming, the node has to
649 * mark the local entry as "roamed" in order to correctly reroute
650 * packets later
651 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200652 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200653 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200654 /* mark the local client as ROAMed */
655 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
656 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200657
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200658 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
659 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
660 message);
661 goto out;
662 }
663 /* if this client has been added right now, it is possible to
664 * immediately purge it
665 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200666 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200667 hlist_del_rcu(&tt_local_entry->common.hash_entry);
668 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200669
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200670out:
671 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200672 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200673
674 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675}
676
Sven Eckelmann56303d32012-06-05 22:31:31 +0200677static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200678 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_tt_local_entry *tt_local_entry;
681 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800682 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200683
Sasha Levinb67bfe02013-02-27 17:06:00 -0800684 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200685 hash_entry) {
686 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687 struct batadv_tt_local_entry,
688 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200689 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
690 continue;
691
692 /* entry already marked for deletion */
693 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
694 continue;
695
696 if (!batadv_has_timed_out(tt_local_entry->last_seen,
697 BATADV_TT_LOCAL_TIMEOUT))
698 continue;
699
700 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
701 BATADV_TT_CLIENT_DEL, "timed out");
702 }
703}
704
Sven Eckelmann56303d32012-06-05 22:31:31 +0200705static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200706{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200707 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200709 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200710 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712 for (i = 0; i < hash->size; i++) {
713 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200716 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200717 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200718 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720}
721
Sven Eckelmann56303d32012-06-05 22:31:31 +0200722static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200724 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200725 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200726 struct batadv_tt_common_entry *tt_common_entry;
727 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800728 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200729 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200730 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200731
Sven Eckelmann807736f2012-07-15 22:26:51 +0200732 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 return;
734
Sven Eckelmann807736f2012-07-15 22:26:51 +0200735 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736
737 for (i = 0; i < hash->size; i++) {
738 head = &hash->table[i];
739 list_lock = &hash->list_locks[i];
740
741 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800742 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200743 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800744 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200745 tt_local = container_of(tt_common_entry,
746 struct batadv_tt_local_entry,
747 common);
748 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200749 }
750 spin_unlock_bh(list_lock);
751 }
752
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200753 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200754
Sven Eckelmann807736f2012-07-15 22:26:51 +0200755 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756}
757
Sven Eckelmann56303d32012-06-05 22:31:31 +0200758static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200760 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200761 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762
Sven Eckelmann807736f2012-07-15 22:26:51 +0200763 bat_priv->tt.global_hash = batadv_hash_new(1024);
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 -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767
Antonio Quartullidec05072012-11-10 11:00:32 +0100768 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
769 &batadv_tt_global_hash_lock_class_key);
770
Sven Eckelmann5346c352012-05-05 13:27:28 +0200771 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772}
773
Sven Eckelmann56303d32012-06-05 22:31:31 +0200774static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200775{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200776 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200777
Sven Eckelmann807736f2012-07-15 22:26:51 +0200778 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200779
Sven Eckelmann807736f2012-07-15 22:26:51 +0200780 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200781 list) {
782 list_del(&entry->list);
783 kfree(entry);
784 }
785
Sven Eckelmann807736f2012-07-15 22:26:51 +0200786 atomic_set(&bat_priv->tt.local_changes, 0);
787 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200788}
789
Antonio Quartullid657e622012-07-01 14:09:12 +0200790/* retrieves the orig_tt_list_entry belonging to orig_node from the
791 * batadv_tt_global_entry list
792 *
793 * returns it with an increased refcounter, NULL if not found
794 */
795static struct batadv_tt_orig_list_entry *
796batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
797 const struct batadv_orig_node *orig_node)
798{
799 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
800 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200801
802 rcu_read_lock();
803 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800804 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200805 if (tmp_orig_entry->orig_node != orig_node)
806 continue;
807 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
808 continue;
809
810 orig_entry = tmp_orig_entry;
811 break;
812 }
813 rcu_read_unlock();
814
815 return orig_entry;
816}
817
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200818/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200819 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200820 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200821static bool
822batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
823 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200824{
Antonio Quartullid657e622012-07-01 14:09:12 +0200825 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200826 bool found = false;
827
Antonio Quartullid657e622012-07-01 14:09:12 +0200828 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
829 if (orig_entry) {
830 found = true;
831 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200832 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200833
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200834 return found;
835}
836
Sven Eckelmanna5130882012-05-16 20:23:16 +0200837static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200838batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200839 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200840{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200841 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200842
Antonio Quartullid657e622012-07-01 14:09:12 +0200843 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200844 if (orig_entry) {
845 /* refresh the ttvn: the current value could be a bogus one that
846 * was added during a "temporary client detection"
847 */
848 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200849 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200850 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200851
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200852 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
853 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200854 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200855
856 INIT_HLIST_NODE(&orig_entry->list);
857 atomic_inc(&orig_node->refcount);
858 atomic_inc(&orig_node->tt_size);
859 orig_entry->orig_node = orig_node;
860 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200861 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200862
Antonio Quartullid657e622012-07-01 14:09:12 +0200863 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200864 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200865 &tt_global->orig_list);
866 spin_unlock_bh(&tt_global->list_lock);
867out:
868 if (orig_entry)
869 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200870}
871
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200872/**
873 * batadv_tt_global_add - add a new TT global entry or update an existing one
874 * @bat_priv: the bat priv with all the soft interface information
875 * @orig_node: the originator announcing the client
876 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200877 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200878 * @flags: TT flags that have to be set for this non-mesh client
879 * @ttvn: the tt version number ever announcing this non-mesh client
880 *
881 * Add a new TT global entry for the given originator. If the entry already
882 * exists add a new reference to the given originator (a global entry can have
883 * references to multiple originators) and adjust the flags attribute to reflect
884 * the function argument.
885 * If a TT local entry exists for this non-mesh client remove it.
886 *
887 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200888 *
889 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200890 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200891static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
892 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200893 const unsigned char *tt_addr,
894 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200895 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200897 struct batadv_tt_global_entry *tt_global_entry;
898 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200899 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100900 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200901 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200902 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000903
Antonio Quartullic018ad32013-06-04 12:11:39 +0200904 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
905 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200906
907 /* if the node already has a local client for this entry, it has to wait
908 * for a roaming advertisement instead of manually messing up the global
909 * table
910 */
911 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
912 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
913 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000914
Antonio Quartullia73105b2011-04-27 14:27:44 +0200915 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200916 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200917 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200918 goto out;
919
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200920 common = &tt_global_entry->common;
921 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200922 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200923
Antonio Quartullid4f44692012-05-25 00:00:54 +0200924 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200925 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200926 /* node must store current time in case of roaming. This is
927 * needed to purge this entry out on timeout (if nobody claims
928 * it)
929 */
930 if (flags & BATADV_TT_CLIENT_ROAM)
931 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200932 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200933 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200934
935 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
936 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200937
Sven Eckelmann807736f2012-07-15 22:26:51 +0200938 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200939 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200940 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200941 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100942
943 if (unlikely(hash_added != 0)) {
944 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200945 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100946 goto out_remove;
947 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200949 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200950 /* If there is already a global entry, we can use this one for
951 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200952 * But if we are trying to add a temporary client then here are
953 * two options at this point:
954 * 1) the global client is not a temporary client: the global
955 * client has to be left as it is, temporary information
956 * should never override any already known client state
957 * 2) the global client is a temporary client: purge the
958 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200959 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200960 if (flags & BATADV_TT_CLIENT_TEMP) {
961 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
962 goto out;
963 if (batadv_tt_global_entry_has_orig(tt_global_entry,
964 orig_node))
965 goto out_remove;
966 batadv_tt_global_del_orig_list(tt_global_entry);
967 goto add_orig_entry;
968 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200969
970 /* if the client was temporary added before receiving the first
971 * OGM announcing it, we have to clear the TEMP flag
972 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200973 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200974
Antonio Quartullie9c001362012-11-07 15:05:33 +0100975 /* the change can carry possible "attribute" flags like the
976 * TT_CLIENT_WIFI, therefore they have to be copied in the
977 * client entry
978 */
979 tt_global_entry->common.flags |= flags;
980
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200981 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
982 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200983 * delete + roaming change for this originator.
984 *
985 * We should first delete the old originator before adding the
986 * new one.
987 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200988 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200989 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200990 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200991 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000992 }
993 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200994add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200995 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200996 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200997
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200998 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200999 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001000 common->addr, orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001001 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001002
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001003out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001004
Antonio Quartullia73105b2011-04-27 14:27:44 +02001005 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001006 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001007 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001008 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001009 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1010
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001011 if (!(flags & BATADV_TT_CLIENT_ROAM))
1012 /* this is a normal global add. Therefore the client is not in a
1013 * roaming state anymore.
1014 */
1015 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1016
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001017out:
1018 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001019 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001020 if (tt_local_entry)
1021 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001022 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023}
1024
Sven Eckelmann981d8902012-10-07 13:34:15 +02001025/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1026 * @tt_global_entry: global translation table entry to be analyzed
1027 *
1028 * This functon assumes the caller holds rcu_read_lock().
1029 * Returns best originator list entry or NULL on errors.
1030 */
1031static struct batadv_tt_orig_list_entry *
1032batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
1033{
1034 struct batadv_neigh_node *router = NULL;
1035 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001036 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1037 int best_tq = 0;
1038
1039 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001040 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001041 router = batadv_orig_node_get_router(orig_entry->orig_node);
1042 if (!router)
1043 continue;
1044
1045 if (router->tq_avg > best_tq) {
1046 best_entry = orig_entry;
1047 best_tq = router->tq_avg;
1048 }
1049
1050 batadv_neigh_node_free_ref(router);
1051 }
1052
1053 return best_entry;
1054}
1055
1056/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1057 * for this global entry
1058 * @tt_global_entry: global translation table entry to be printed
1059 * @seq: debugfs table seq_file struct
1060 *
1061 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001062 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001063static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001064batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001065 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001066{
1067 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001068 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070 uint16_t flags;
1071 uint8_t last_ttvn;
1072
1073 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001074 flags = tt_common_entry->flags;
1075
1076 best_entry = batadv_transtable_best_orig(tt_global_entry);
1077 if (best_entry) {
1078 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001079 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +02001080 " %c %pM (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001081 '*', tt_global_entry->common.addr,
1082 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001083 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001084 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1085 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1086 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1087 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001088
1089 head = &tt_global_entry->orig_list;
1090
Sasha Levinb67bfe02013-02-27 17:06:00 -08001091 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001092 if (best_entry == orig_entry)
1093 continue;
1094
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001096 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1097 '+', tt_global_entry->common.addr,
1098 orig_entry->ttvn, orig_entry->orig_node->orig,
1099 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001100 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001101 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1102 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001103 }
1104}
1105
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001106int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001107{
1108 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001109 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001110 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111 struct batadv_tt_common_entry *tt_common_entry;
1112 struct batadv_tt_global_entry *tt_global;
1113 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001114 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001115 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116
Marek Lindner30da63a2012-08-03 17:15:46 +02001117 primary_if = batadv_seq_print_text_primary_if_get(seq);
1118 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001119 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001120
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001121 seq_printf(seq,
1122 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123 net_dev->name);
Antonio Quartulliced72932013-04-24 16:37:51 +02001124 seq_printf(seq, " %-13s %s %-15s %s (%-10s) %s\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001125 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1126 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001127
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128 for (i = 0; i < hash->size; i++) {
1129 head = &hash->table[i];
1130
Marek Lindner7aadf882011-02-18 12:28:09 +00001131 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001132 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001133 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001134 tt_global = container_of(tt_common_entry,
1135 struct batadv_tt_global_entry,
1136 common);
1137 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001138 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001139 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001140 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001141out:
1142 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001143 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001144 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145}
1146
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001147/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001148static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001149batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001150{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001152 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001154
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001155 spin_lock_bh(&tt_global_entry->list_lock);
1156 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001157 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1158 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001159 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001160 }
1161 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001162}
1163
Sven Eckelmanna5130882012-05-16 20:23:16 +02001164static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001165batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1166 struct batadv_tt_global_entry *tt_global_entry,
1167 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001168 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001169{
1170 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001171 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001172 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001173
1174 spin_lock_bh(&tt_global_entry->list_lock);
1175 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001176 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001177 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001178 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001179 "Deleting %pM from global tt entry %pM: %s\n",
1180 orig_node->orig,
1181 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001182 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001183 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001184 }
1185 }
1186 spin_unlock_bh(&tt_global_entry->list_lock);
1187}
1188
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001189/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001190 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1191 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001192 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001193static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001194batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1195 struct batadv_tt_global_entry *tt_global_entry,
1196 struct batadv_orig_node *orig_node,
1197 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001198{
1199 bool last_entry = true;
1200 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001201 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001202
1203 /* no local entry exists, case 1:
1204 * Check if this is the last one or if other entries exist.
1205 */
1206
1207 rcu_read_lock();
1208 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001210 if (orig_entry->orig_node != orig_node) {
1211 last_entry = false;
1212 break;
1213 }
1214 }
1215 rcu_read_unlock();
1216
1217 if (last_entry) {
1218 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001219 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001220 tt_global_entry->roam_at = jiffies;
1221 } else
1222 /* there is another entry, we can simply delete this
1223 * one and can still use the other one.
1224 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001225 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1226 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001227}
1228
Antonio Quartullic018ad32013-06-04 12:11:39 +02001229/**
1230 * batadv_tt_global_del - remove a client from the global table
1231 * @bat_priv: the bat priv with all the soft interface information
1232 * @orig_node: an originator serving this client
1233 * @addr: the mac address of the client
1234 * @vid: VLAN identifier
1235 * @message: a message explaining the reason for deleting the client to print
1236 * for debugging purpose
1237 * @roaming: true if the deletion has been triggered by a roaming event
1238 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001239static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1240 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001241 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001242 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001243{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001244 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001245 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001246
Antonio Quartullic018ad32013-06-04 12:11:39 +02001247 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001248 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001249 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001250
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001251 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001252 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1253 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001254
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001255 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001256 batadv_tt_global_free(bat_priv, tt_global_entry,
1257 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001258
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001259 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001261
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001262 /* if we are deleting a global entry due to a roam
1263 * event, there are two possibilities:
1264 * 1) the client roamed from node A to node B => if there
1265 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001266 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267 * wait for node B to claim it. In case of timeout
1268 * the entry is purged.
1269 *
1270 * If there are other originators left, we directly delete
1271 * the originator.
1272 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001273 * the global entry, since it is useless now.
1274 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001275 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001276 tt_global_entry->common.addr,
1277 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001278 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001279 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001280 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001281 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001282 } else
1283 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001284 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1285 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001286
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001287
Antonio Quartullicc47f662011-04-27 14:27:57 +02001288out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001289 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001290 batadv_tt_global_entry_free_ref(tt_global_entry);
1291 if (local_entry)
1292 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001293}
1294
Sven Eckelmann56303d32012-06-05 22:31:31 +02001295void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1296 struct batadv_orig_node *orig_node,
1297 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001298{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001299 struct batadv_tt_global_entry *tt_global;
1300 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001301 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001302 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001303 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001304 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001305 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001306
Simon Wunderlich6e801492011-10-19 10:28:26 +02001307 if (!hash)
1308 return;
1309
Antonio Quartullia73105b2011-04-27 14:27:44 +02001310 for (i = 0; i < hash->size; i++) {
1311 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001312 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001313
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001314 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001315 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001316 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001317 tt_global = container_of(tt_common_entry,
1318 struct batadv_tt_global_entry,
1319 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001320
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001322 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001323
Sven Eckelmann56303d32012-06-05 22:31:31 +02001324 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001325 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001326 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001327 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001328 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001329 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001330 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001332 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001333 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001334 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001335}
1336
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001337static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1338 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001339{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001340 bool purge = false;
1341 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1342 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001343
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001344 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1345 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1346 purge = true;
1347 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001348 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001349
1350 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1351 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1352 purge = true;
1353 *msg = "Temporary client timeout\n";
1354 }
1355
1356 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001357}
1358
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001359static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001360{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001361 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001362 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001363 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001364 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001365 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001366 char *msg = NULL;
1367 struct batadv_tt_common_entry *tt_common;
1368 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001369
Antonio Quartullicc47f662011-04-27 14:27:57 +02001370 for (i = 0; i < hash->size; i++) {
1371 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001372 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001373
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001374 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001375 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001376 hash_entry) {
1377 tt_global = container_of(tt_common,
1378 struct batadv_tt_global_entry,
1379 common);
1380
1381 if (!batadv_tt_global_to_purge(tt_global, &msg))
1382 continue;
1383
1384 batadv_dbg(BATADV_DBG_TT, bat_priv,
1385 "Deleting global tt entry (%pM): %s\n",
1386 tt_global->common.addr, msg);
1387
Sasha Levinb67bfe02013-02-27 17:06:00 -08001388 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001389
1390 batadv_tt_global_entry_free_ref(tt_global);
1391 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001392 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001393 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001394}
1395
Sven Eckelmann56303d32012-06-05 22:31:31 +02001396static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001397{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001398 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001399 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001400 struct batadv_tt_common_entry *tt_common_entry;
1401 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001402 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001403 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001404 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001405
Sven Eckelmann807736f2012-07-15 22:26:51 +02001406 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001407 return;
1408
Sven Eckelmann807736f2012-07-15 22:26:51 +02001409 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001410
1411 for (i = 0; i < hash->size; i++) {
1412 head = &hash->table[i];
1413 list_lock = &hash->list_locks[i];
1414
1415 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001416 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001417 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001418 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001419 tt_global = container_of(tt_common_entry,
1420 struct batadv_tt_global_entry,
1421 common);
1422 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001423 }
1424 spin_unlock_bh(list_lock);
1425 }
1426
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001427 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001428
Sven Eckelmann807736f2012-07-15 22:26:51 +02001429 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001430}
1431
Sven Eckelmann56303d32012-06-05 22:31:31 +02001432static bool
1433_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1434 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001435{
1436 bool ret = false;
1437
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001438 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1439 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001440 ret = true;
1441
1442 return ret;
1443}
1444
Antonio Quartullic018ad32013-06-04 12:11:39 +02001445/**
1446 * batadv_transtable_search - get the mesh destination for a given client
1447 * @bat_priv: the bat priv with all the soft interface information
1448 * @src: mac address of the source client
1449 * @addr: mac address of the destination client
1450 * @vid: VLAN identifier
1451 *
1452 * Returns a pointer to the originator that was selected as destination in the
1453 * mesh for contacting the client 'addr', NULL otherwise.
1454 * In case of multiple originators serving the same client, the function returns
1455 * the best one (best in terms of metric towards the destination node).
1456 *
1457 * If the two clients are AP isolated the function returns NULL.
1458 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001459struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1460 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001461 const uint8_t *addr,
1462 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001463{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001464 struct batadv_tt_local_entry *tt_local_entry = NULL;
1465 struct batadv_tt_global_entry *tt_global_entry = NULL;
1466 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001467 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001468
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001469 if (src && atomic_read(&bat_priv->ap_isolation)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001470 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001471 if (!tt_local_entry ||
1472 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001473 goto out;
1474 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001475
Antonio Quartullic018ad32013-06-04 12:11:39 +02001476 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001477 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001478 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001479
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001480 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001481 * isolation
1482 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001483 if (tt_local_entry &&
1484 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001485 goto out;
1486
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001487 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001488 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001489 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001490 if (best_entry)
1491 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001492 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1493 orig_node = NULL;
1494 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001495
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001496out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001497 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001498 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001499 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001500 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001501
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001502 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001503}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504
Antonio Quartulliced72932013-04-24 16:37:51 +02001505/**
1506 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1507 * to the given orig_node
1508 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001509 * @orig_node: originator for which the CRC should be computed
1510 *
1511 * This function computes the checksum for the global table corresponding to a
1512 * specific originator. In particular, the checksum is computed as follows: For
1513 * each client connected to the originator the CRC32C of the MAC address and the
1514 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1515 * together.
1516 *
1517 * The idea behind is that CRC32C should be used as much as possible in order to
1518 * produce a unique hash of the table, but since the order which is used to feed
1519 * the CRC32C function affects the result and since every node in the network
1520 * probably sorts the clients differently, the hash function cannot be directly
1521 * computed over the entire table. Hence the CRC32C is used only on
1522 * the single client entry, while all the results are then xor'ed together
1523 * because the XOR operation can combine them all while trying to reduce the
1524 * noise as much as possible.
1525 *
1526 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001527 */
1528static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001529 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001531 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001532 struct batadv_tt_common_entry *tt_common;
1533 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001534 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001535 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001536
1537 for (i = 0; i < hash->size; i++) {
1538 head = &hash->table[i];
1539
1540 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001541 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001542 tt_global = container_of(tt_common,
1543 struct batadv_tt_global_entry,
1544 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001545 /* Roaming clients are in the global table for
1546 * consistency only. They don't have to be
1547 * taken into account while computing the
1548 * global crc
1549 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001550 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001551 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001552 /* Temporary clients have not been announced yet, so
1553 * they have to be skipped while computing the global
1554 * crc
1555 */
1556 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1557 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001558
1559 /* find out if this global entry is announced by this
1560 * originator
1561 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001562 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001563 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001564 continue;
1565
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001566 crc_tmp = crc32c(0, &tt_common->vid,
1567 sizeof(tt_common->vid));
1568 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569 }
1570 rcu_read_unlock();
1571 }
1572
Antonio Quartulliced72932013-04-24 16:37:51 +02001573 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574}
1575
Antonio Quartulliced72932013-04-24 16:37:51 +02001576/**
1577 * batadv_tt_local_crc - calculates the checksum of the local table
1578 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001579 *
1580 * For details about the computation, please refer to the documentation for
1581 * batadv_tt_global_crc().
1582 *
1583 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02001584 */
1585static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001587 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001588 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001589 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001590 uint32_t i, crc_tmp, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001591
1592 for (i = 0; i < hash->size; i++) {
1593 head = &hash->table[i];
1594
1595 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001596 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001597 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001598 * account while computing the CRC
1599 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001600 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001601 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001602
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001603 crc_tmp = crc32c(0, &tt_common->vid,
1604 sizeof(tt_common->vid));
1605 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607 rcu_read_unlock();
1608 }
1609
Antonio Quartulliced72932013-04-24 16:37:51 +02001610 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611}
1612
Sven Eckelmann56303d32012-06-05 22:31:31 +02001613static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001615 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616
Sven Eckelmann807736f2012-07-15 22:26:51 +02001617 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001618
Sven Eckelmann807736f2012-07-15 22:26:51 +02001619 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620 list_del(&node->list);
1621 kfree(node);
1622 }
1623
Sven Eckelmann807736f2012-07-15 22:26:51 +02001624 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001625}
1626
Sven Eckelmann56303d32012-06-05 22:31:31 +02001627static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1628 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02001629 const void *tt_buff,
1630 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001632 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001633 * last OGM (the OGM could carry no changes)
1634 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 spin_lock_bh(&orig_node->tt_buff_lock);
1636 if (tt_buff_len > 0) {
1637 kfree(orig_node->tt_buff);
1638 orig_node->tt_buff_len = 0;
1639 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1640 if (orig_node->tt_buff) {
1641 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1642 orig_node->tt_buff_len = tt_buff_len;
1643 }
1644 }
1645 spin_unlock_bh(&orig_node->tt_buff_lock);
1646}
1647
Sven Eckelmann56303d32012-06-05 22:31:31 +02001648static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001650 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651
Sven Eckelmann807736f2012-07-15 22:26:51 +02001652 spin_lock_bh(&bat_priv->tt.req_list_lock);
1653 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001654 if (batadv_has_timed_out(node->issued_at,
1655 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656 list_del(&node->list);
1657 kfree(node);
1658 }
1659 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001660 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001661}
1662
1663/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001664 * has already been issued for this orig_node, NULL otherwise
1665 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001666static struct batadv_tt_req_node *
1667batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1668 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001670 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671
Sven Eckelmann807736f2012-07-15 22:26:51 +02001672 spin_lock_bh(&bat_priv->tt.req_list_lock);
1673 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001674 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1675 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001676 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677 goto unlock;
1678 }
1679
1680 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1681 if (!tt_req_node)
1682 goto unlock;
1683
1684 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1685 tt_req_node->issued_at = jiffies;
1686
Sven Eckelmann807736f2012-07-15 22:26:51 +02001687 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001689 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001690 return tt_req_node;
1691}
1692
Marek Lindner335fbe02013-04-23 21:40:02 +08001693/**
1694 * batadv_tt_local_valid - verify that given tt entry is a valid one
1695 * @entry_ptr: to be checked local tt entry
1696 * @data_ptr: not used but definition required to satisfy the callback prototype
1697 *
1698 * Returns 1 if the entry is a valid, 0 otherwise.
1699 */
1700static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001701{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001702 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001703
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001704 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001705 return 0;
1706 return 1;
1707}
1708
Sven Eckelmanna5130882012-05-16 20:23:16 +02001709static int batadv_tt_global_valid(const void *entry_ptr,
1710 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001711{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001712 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1713 const struct batadv_tt_global_entry *tt_global_entry;
1714 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001716 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1717 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001718 return 0;
1719
Sven Eckelmann56303d32012-06-05 22:31:31 +02001720 tt_global_entry = container_of(tt_common_entry,
1721 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001722 common);
1723
Sven Eckelmanna5130882012-05-16 20:23:16 +02001724 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001725}
1726
Marek Lindner335fbe02013-04-23 21:40:02 +08001727/**
1728 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1729 * tt entries from the specified tt hash
1730 * @bat_priv: the bat priv with all the soft interface information
1731 * @hash: hash table containing the tt entries
1732 * @tt_len: expected tvlv tt data buffer length in number of bytes
1733 * @valid_cb: function to filter tt change entries
1734 * @cb_data: data passed to the filter function as argument
1735 *
1736 * Returns pointer to allocated tvlv tt data buffer if operation was
1737 * successful or NULL otherwise.
1738 */
1739static struct batadv_tvlv_tt_data *
1740batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1741 struct batadv_hashtable *hash, uint16_t tt_len,
1742 int (*valid_cb)(const void *, const void *),
1743 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001745 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001746 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1747 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001748 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001749 uint16_t tt_tot, tt_num_entries = 0;
1750 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001751 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001752
Marek Lindner335fbe02013-04-23 21:40:02 +08001753 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1754 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1755 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001756 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757
Antonio Quartulli298e6e62013-05-28 13:14:27 +02001758 tt_tot = batadv_tt_entries(tt_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08001759
1760 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1761 GFP_ATOMIC);
1762 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 goto out;
1764
Marek Lindner335fbe02013-04-23 21:40:02 +08001765 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001766
1767 rcu_read_lock();
1768 for (i = 0; i < hash->size; i++) {
1769 head = &hash->table[i];
1770
Sasha Levinb67bfe02013-02-27 17:06:00 -08001771 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001773 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774 break;
1775
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001776 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001777 continue;
1778
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001779 memcpy(tt_change->addr, tt_common_entry->addr,
1780 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001781 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001782 tt_change->vid = htons(tt_common_entry->vid);
Marek Lindner335fbe02013-04-23 21:40:02 +08001783 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784
Marek Lindner335fbe02013-04-23 21:40:02 +08001785 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786 tt_change++;
1787 }
1788 }
1789 rcu_read_unlock();
1790
1791out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001792 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793}
1794
Antonio Quartulliced72932013-04-24 16:37:51 +02001795/**
1796 * batadv_send_tt_request - send a TT Request message to a given node
1797 * @bat_priv: the bat priv with all the soft interface information
1798 * @dst_orig_node: the destination of the message
1799 * @ttvn: the version number that the source of the message is looking for
1800 * @tt_crc: the CRC associated with the version number
1801 * @full_table: ask for the entire translation table if true, while only for the
1802 * last TT diff otherwise
1803 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001804static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1805 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001806 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001807 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808{
Marek Lindner335fbe02013-04-23 21:40:02 +08001809 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001810 struct batadv_hard_iface *primary_if;
1811 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001812 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001814 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 if (!primary_if)
1816 goto out;
1817
1818 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001819 * reply from the same orig_node yet
1820 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001821 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001822 if (!tt_req_node)
1823 goto out;
1824
Marek Lindner335fbe02013-04-23 21:40:02 +08001825 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1826 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827 goto out;
1828
Marek Lindner335fbe02013-04-23 21:40:02 +08001829 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1830 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001831 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832
1833 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001834 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001835
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001836 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001837 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001838
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001839 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001840 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1841 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1842 tvlv_tt_data, sizeof(*tvlv_tt_data));
1843 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844
1845out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001846 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001847 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001848 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001849 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001850 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001851 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852 kfree(tt_req_node);
1853 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001854 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 return ret;
1856}
1857
Marek Lindner335fbe02013-04-23 21:40:02 +08001858/**
1859 * batadv_send_other_tt_response - send reply to tt request concerning another
1860 * node's translation table
1861 * @bat_priv: the bat priv with all the soft interface information
1862 * @tt_data: tt data containing the tt request information
1863 * @req_src: mac address of tt request sender
1864 * @req_dst: mac address of tt request recipient
1865 *
1866 * Returns true if tt request reply was sent, false otherwise.
1867 */
1868static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1869 struct batadv_tvlv_tt_data *tt_data,
1870 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001871{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001872 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001873 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001874 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1875 uint8_t orig_ttvn, req_ttvn;
1876 uint16_t tt_len;
1877 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001878
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001879 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001880 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001881 req_src, tt_data->ttvn, req_dst,
1882 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883
1884 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001885 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001886 if (!req_dst_orig_node)
1887 goto out;
1888
Marek Lindner335fbe02013-04-23 21:40:02 +08001889 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001890 if (!res_dst_orig_node)
1891 goto out;
1892
Antonio Quartullia73105b2011-04-27 14:27:44 +02001893 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001894 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895
Marek Lindner335fbe02013-04-23 21:40:02 +08001896 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001897 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001898 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899 goto out;
1900
Antonio Quartulli015758d2011-07-09 17:52:13 +02001901 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001902 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903 !req_dst_orig_node->tt_buff)
1904 full_table = true;
1905 else
1906 full_table = false;
1907
Marek Lindner335fbe02013-04-23 21:40:02 +08001908 /* TT fragmentation hasn't been implemented yet, so send as many
1909 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001910 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001911 if (!full_table) {
1912 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1913 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914
Marek Lindner335fbe02013-04-23 21:40:02 +08001915 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1916 GFP_ATOMIC);
1917 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918 goto unlock;
1919
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001921 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001923 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1924 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001925 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001926 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927
Marek Lindner335fbe02013-04-23 21:40:02 +08001928 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1929 bat_priv->tt.global_hash,
1930 tt_len,
1931 batadv_tt_global_valid,
1932 req_dst_orig_node);
1933 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001935 }
1936
Marek Lindner335fbe02013-04-23 21:40:02 +08001937 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1938 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939
1940 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001941 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001943 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001944 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1945 res_dst_orig_node->orig, req_dst_orig_node->orig,
1946 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001947
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001948 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001949
Marek Lindner335fbe02013-04-23 21:40:02 +08001950 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1951 req_src, BATADV_TVLV_TT, 1,
1952 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001953
Marek Lindner335fbe02013-04-23 21:40:02 +08001954 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955 goto out;
1956
1957unlock:
1958 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1959
1960out:
1961 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001962 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001964 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001965 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001966 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967}
Sven Eckelmann96412692012-06-05 22:31:30 +02001968
Marek Lindner335fbe02013-04-23 21:40:02 +08001969/**
1970 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1971 * translation table
1972 * @bat_priv: the bat priv with all the soft interface information
1973 * @tt_data: tt data containing the tt request information
1974 * @req_src: mac address of tt request sender
1975 *
1976 * Returns true if tt request reply was sent, false otherwise.
1977 */
1978static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1979 struct batadv_tvlv_tt_data *tt_data,
1980 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001981{
Marek Lindner335fbe02013-04-23 21:40:02 +08001982 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02001983 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001984 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001985 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08001987 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001988
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001989 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001990 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001991 req_src, tt_data->ttvn,
1992 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001993
1994
Sven Eckelmann807736f2012-07-15 22:26:51 +02001995 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001996 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001997
Marek Lindner335fbe02013-04-23 21:40:02 +08001998 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001999 if (!orig_node)
2000 goto out;
2001
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002002 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003 if (!primary_if)
2004 goto out;
2005
2006 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002007 * is too big send the whole local translation table
2008 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002009 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002010 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011 full_table = true;
2012 else
2013 full_table = false;
2014
Marek Lindner335fbe02013-04-23 21:40:02 +08002015 /* TT fragmentation hasn't been implemented yet, so send as many
2016 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002017 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002018 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002019 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2020 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021
Marek Lindner335fbe02013-04-23 21:40:02 +08002022 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
2023 GFP_ATOMIC);
2024 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025 goto unlock;
2026
Marek Lindner335fbe02013-04-23 21:40:02 +08002027 /* Copy the last orig_node's OGM buffer */
2028 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002029 bat_priv->tt.last_changeset_len);
2030 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002032 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08002033 tt_len = batadv_tt_len(tt_len);
2034 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002035
Marek Lindner335fbe02013-04-23 21:40:02 +08002036 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
2037 bat_priv->tt.local_hash,
2038 tt_len,
2039 batadv_tt_local_valid,
2040 NULL);
2041 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043 }
2044
Marek Lindner335fbe02013-04-23 21:40:02 +08002045 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2046 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002047
2048 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002049 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002051 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002052 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2053 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002054
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002055 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002056
Marek Lindner335fbe02013-04-23 21:40:02 +08002057 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2058 req_src, BATADV_TVLV_TT, 1,
2059 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
2060
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061 goto out;
2062
2063unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002064 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002065out:
2066 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002067 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002068 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002069 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002070 kfree(tvlv_tt_data);
2071 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072 return true;
2073}
2074
Marek Lindner335fbe02013-04-23 21:40:02 +08002075/**
2076 * batadv_send_tt_response - send reply to tt request
2077 * @bat_priv: the bat priv with all the soft interface information
2078 * @tt_data: tt data containing the tt request information
2079 * @req_src: mac address of tt request sender
2080 * @req_dst: mac address of tt request recipient
2081 *
2082 * Returns true if tt request reply was sent, false otherwise.
2083 */
2084static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2085 struct batadv_tvlv_tt_data *tt_data,
2086 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087{
Marek Lindner335fbe02013-04-23 21:40:02 +08002088 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002089 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08002090 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002091 return true;
2092
Marek Lindner335fbe02013-04-23 21:40:02 +08002093 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002094 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002095 return batadv_send_other_tt_response(bat_priv, tt_data,
2096 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002097 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002098}
2099
Sven Eckelmann56303d32012-06-05 22:31:31 +02002100static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2101 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002102 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002103 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104{
2105 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002106 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002107
2108 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002109 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2110 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002111 batadv_tt_global_del(bat_priv, orig_node,
2112 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002113 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002114 "tt removed by changes",
2115 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002116 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002117 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002118 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002119 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002120 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121 /* In case of problem while storing a
2122 * global_entry, we stop the updating
2123 * procedure without committing the
2124 * ttvn change. This will avoid to send
2125 * corrupted data on tt_request
2126 */
2127 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002128 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002129 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002130 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002131}
2132
Sven Eckelmann56303d32012-06-05 22:31:31 +02002133static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002134 struct batadv_tvlv_tt_data *tt_data,
2135 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002136{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002137 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002138
Marek Lindner335fbe02013-04-23 21:40:02 +08002139 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002140 if (!orig_node)
2141 goto out;
2142
2143 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002144 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002145
Sven Eckelmanna5130882012-05-16 20:23:16 +02002146 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002147 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2148 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002149
2150 spin_lock_bh(&orig_node->tt_buff_lock);
2151 kfree(orig_node->tt_buff);
2152 orig_node->tt_buff_len = 0;
2153 orig_node->tt_buff = NULL;
2154 spin_unlock_bh(&orig_node->tt_buff_lock);
2155
Marek Lindner335fbe02013-04-23 21:40:02 +08002156 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002157
2158out:
2159 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002160 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002161}
2162
Sven Eckelmann56303d32012-06-05 22:31:31 +02002163static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2164 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002165 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002166 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002168 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2169 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002171 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2172 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002173 atomic_set(&orig_node->last_ttvn, ttvn);
2174}
2175
Antonio Quartullic018ad32013-06-04 12:11:39 +02002176/**
2177 * batadv_is_my_client - check if a client is served by the local node
2178 * @bat_priv: the bat priv with all the soft interface information
2179 * @addr: the mac adress of the client to check
2180 * @vid: VLAN identifier
2181 *
2182 * Returns true if the client is served by this node, false otherwise.
2183 */
2184bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2185 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002186{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002187 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002188 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002189
Antonio Quartullic018ad32013-06-04 12:11:39 +02002190 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002191 if (!tt_local_entry)
2192 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002193 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002194 * consistency purpose)
2195 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002196 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2197 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002198 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002199 ret = true;
2200out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002201 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002202 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002203 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002204}
2205
Marek Lindner335fbe02013-04-23 21:40:02 +08002206/**
2207 * batadv_handle_tt_response - process incoming tt reply
2208 * @bat_priv: the bat priv with all the soft interface information
2209 * @tt_data: tt data containing the tt request information
2210 * @resp_src: mac address of tt reply sender
2211 * @num_entries: number of tt change entries appended to the tt data
2212 */
2213static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2214 struct batadv_tvlv_tt_data *tt_data,
2215 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002216{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002217 struct batadv_tt_req_node *node, *safe;
2218 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002219 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002220
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002221 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002222 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002223 resp_src, tt_data->ttvn, num_entries,
2224 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002225
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002226 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002227 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002228 goto out;
2229
Marek Lindner335fbe02013-04-23 21:40:02 +08002230 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231 if (!orig_node)
2232 goto out;
2233
Marek Lindner335fbe02013-04-23 21:40:02 +08002234 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2235 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002236 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002237 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2238 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2239 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002240 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002241
2242 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002243 spin_lock_bh(&bat_priv->tt.req_list_lock);
2244 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002245 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002246 continue;
2247 list_del(&node->list);
2248 kfree(node);
2249 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002250 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002251
2252 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002253 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002254out:
2255 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002256 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002257}
2258
Sven Eckelmann56303d32012-06-05 22:31:31 +02002259static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002260{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002262
Sven Eckelmann807736f2012-07-15 22:26:51 +02002263 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002264
Sven Eckelmann807736f2012-07-15 22:26:51 +02002265 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002266 list_del(&node->list);
2267 kfree(node);
2268 }
2269
Sven Eckelmann807736f2012-07-15 22:26:51 +02002270 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002271}
2272
Sven Eckelmann56303d32012-06-05 22:31:31 +02002273static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002274{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002275 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002276
Sven Eckelmann807736f2012-07-15 22:26:51 +02002277 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2278 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002279 if (!batadv_has_timed_out(node->first_time,
2280 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002281 continue;
2282
2283 list_del(&node->list);
2284 kfree(node);
2285 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002286 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002287}
2288
2289/* This function checks whether the client already reached the
2290 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2291 * will not be sent.
2292 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002293 * returns true if the ROAMING_ADV can be sent, false otherwise
2294 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002295static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002296 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002297{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002298 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002299 bool ret = false;
2300
Sven Eckelmann807736f2012-07-15 22:26:51 +02002301 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002302 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002303 * reply from the same orig_node yet
2304 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002305 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002306 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002307 continue;
2308
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002309 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002310 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002311 continue;
2312
Sven Eckelmann3e348192012-05-16 20:23:22 +02002313 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002314 /* Sorry, you roamed too many times! */
2315 goto unlock;
2316 ret = true;
2317 break;
2318 }
2319
2320 if (!ret) {
2321 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2322 if (!tt_roam_node)
2323 goto unlock;
2324
2325 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002326 atomic_set(&tt_roam_node->counter,
2327 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002328 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2329
Sven Eckelmann807736f2012-07-15 22:26:51 +02002330 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002331 ret = true;
2332 }
2333
2334unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002335 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002336 return ret;
2337}
2338
Antonio Quartullic018ad32013-06-04 12:11:39 +02002339/**
2340 * batadv_send_roam_adv - send a roaming advertisement message
2341 * @bat_priv: the bat priv with all the soft interface information
2342 * @client: mac address of the roaming client
2343 * @vid: VLAN identifier
2344 * @orig_node: message destination
2345 *
2346 * Send a ROAMING_ADV message to the node which was previously serving this
2347 * client. This is done to inform the node that from now on all traffic destined
2348 * for this particular roamed client has to be forwarded to the sender of the
2349 * roaming message.
2350 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002351static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002352 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002353 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002354{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002355 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002356 struct batadv_tvlv_roam_adv tvlv_roam;
2357
2358 primary_if = batadv_primary_if_get_selected(bat_priv);
2359 if (!primary_if)
2360 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002361
2362 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002363 * already roamed to us too many times
2364 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002365 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002366 goto out;
2367
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002368 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002369 "Sending ROAMING_ADV to %pM (client %pM)\n",
2370 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002371
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002372 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002373
Marek Lindner122edaa2013-04-23 21:40:03 +08002374 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002375 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002376
2377 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2378 orig_node->orig, BATADV_TVLV_ROAM, 1,
2379 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002380
2381out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002382 if (primary_if)
2383 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384}
2385
Sven Eckelmanna5130882012-05-16 20:23:16 +02002386static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002387{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002388 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002389 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002390 struct batadv_priv *bat_priv;
2391
2392 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002393 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2394 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002395
Sven Eckelmanna5130882012-05-16 20:23:16 +02002396 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002397 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002398 batadv_tt_req_purge(bat_priv);
2399 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002400
Antonio Quartulli72414442012-12-25 13:14:37 +01002401 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2402 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002403}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002404
Sven Eckelmann56303d32012-06-05 22:31:31 +02002405void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002406{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002407 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2408 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2409
Sven Eckelmann807736f2012-07-15 22:26:51 +02002410 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002411
Sven Eckelmanna5130882012-05-16 20:23:16 +02002412 batadv_tt_local_table_free(bat_priv);
2413 batadv_tt_global_table_free(bat_priv);
2414 batadv_tt_req_list_free(bat_priv);
2415 batadv_tt_changes_list_free(bat_priv);
2416 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002417
Sven Eckelmann807736f2012-07-15 22:26:51 +02002418 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002419}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002420
Antonio Quartulli697f2532011-11-07 16:47:01 +01002421/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002422 * in the given hash table and returns the number of modified entries
2423 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002424static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2425 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002426{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002427 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002428 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002429 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002430 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002431
2432 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002433 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002434
2435 for (i = 0; i < hash->size; i++) {
2436 head = &hash->table[i];
2437
2438 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002439 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002440 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002441 if (enable) {
2442 if ((tt_common_entry->flags & flags) == flags)
2443 continue;
2444 tt_common_entry->flags |= flags;
2445 } else {
2446 if (!(tt_common_entry->flags & flags))
2447 continue;
2448 tt_common_entry->flags &= ~flags;
2449 }
2450 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002451 }
2452 rcu_read_unlock();
2453 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002454out:
2455 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002456}
2457
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002458/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002459static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002460{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002461 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002462 struct batadv_tt_common_entry *tt_common;
2463 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002464 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002465 struct hlist_head *head;
2466 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002467 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002468
2469 if (!hash)
2470 return;
2471
2472 for (i = 0; i < hash->size; i++) {
2473 head = &hash->table[i];
2474 list_lock = &hash->list_locks[i];
2475
2476 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002477 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002478 hash_entry) {
2479 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002480 continue;
2481
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002482 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002483 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002484 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002485
Sven Eckelmann807736f2012-07-15 22:26:51 +02002486 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002487 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002488 tt_local = container_of(tt_common,
2489 struct batadv_tt_local_entry,
2490 common);
2491 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002492 }
2493 spin_unlock_bh(list_lock);
2494 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002495}
2496
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002497/**
2498 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2499 * have been queued in the time since the last commit
2500 * @bat_priv: the bat priv with all the soft interface information
2501 */
2502void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002503{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002504 uint16_t changed_num = 0;
2505
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002506 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2507 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2508 batadv_tt_tvlv_container_update(bat_priv);
2509 return;
2510 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002511
Sven Eckelmann807736f2012-07-15 22:26:51 +02002512 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002513 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002514
2515 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002516 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002517 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002518 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002519
2520 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002521 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002522 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002523 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002524 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002525
2526 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002527 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002528 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002529}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002530
Sven Eckelmann56303d32012-06-05 22:31:31 +02002531bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002532 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002533{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002534 struct batadv_tt_local_entry *tt_local_entry = NULL;
2535 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002536 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002537
2538 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002539 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002540
Antonio Quartullic018ad32013-06-04 12:11:39 +02002541 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst,
2542 BATADV_NO_FLAGS);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002543 if (!tt_local_entry)
2544 goto out;
2545
Antonio Quartullic018ad32013-06-04 12:11:39 +02002546 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src,
2547 BATADV_NO_FLAGS);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002548 if (!tt_global_entry)
2549 goto out;
2550
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002551 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002552 goto out;
2553
Marek Lindner5870adc2012-06-20 17:16:05 +02002554 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002555
2556out:
2557 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002558 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002559 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002560 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002561 return ret;
2562}
Marek Lindnera943cac2011-07-30 13:10:18 +02002563
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002564/**
2565 * batadv_tt_update_orig - update global translation table with new tt
2566 * information received via ogms
2567 * @bat_priv: the bat priv with all the soft interface information
2568 * @orig: the orig_node of the ogm
2569 * @tt_buff: buffer holding the tt information
2570 * @tt_num_changes: number of tt changes inside the tt buffer
2571 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002572 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002573 */
2574static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2575 struct batadv_orig_node *orig_node,
2576 const unsigned char *tt_buff,
2577 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002578 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002579{
2580 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2581 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002582 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002583
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002584 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002585 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002586 return;
2587
Antonio Quartulli17071572011-11-07 16:36:40 +01002588 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002589 * increased by one -> we can apply the attached changes
2590 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002591 if ((!orig_node->tt_initialised && ttvn == 1) ||
2592 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002593 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002594 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2595 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002596 * In this case send a tt request
2597 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002598 if (!tt_num_changes) {
2599 full_table = false;
2600 goto request_table;
2601 }
2602
Marek Lindner335fbe02013-04-23 21:40:02 +08002603 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002604 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002605 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002606
2607 /* Even if we received the precomputed crc with the OGM, we
2608 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002609 * in the global table
2610 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002611 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002612
2613 /* The ttvn alone is not enough to guarantee consistency
2614 * because a single value could represent different states
2615 * (due to the wrap around). Thus a node has to check whether
2616 * the resulting table (after applying the changes) is still
2617 * consistent or not. E.g. a node could disconnect while its
2618 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2619 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002620 * inconsistency
2621 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002622 if (orig_node->tt_crc != tt_crc)
2623 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002624 } else {
2625 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002626 * in sync anymore -> request fresh tt data
2627 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002628 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2629 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002630request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002631 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002632 "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 +02002633 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2634 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002635 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2636 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002637 return;
2638 }
2639 }
2640}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002641
Antonio Quartullic018ad32013-06-04 12:11:39 +02002642/**
2643 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
2644 * @bat_priv: the bat priv with all the soft interface information
2645 * @addr: the mac address of the client to check
2646 * @vid: VLAN identifier
2647 *
2648 * Returns true if we know that the client has moved from its old originator
2649 * to another one. This entry is still kept for consistency purposes and will be
2650 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002651 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002652bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002653 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002654{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002655 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002656 bool ret = false;
2657
Antonio Quartullic018ad32013-06-04 12:11:39 +02002658 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002659 if (!tt_global_entry)
2660 goto out;
2661
Antonio Quartullic1d07432013-01-15 22:17:19 +10002662 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002663 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002664out:
2665 return ret;
2666}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002667
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002668/**
2669 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2670 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02002671 * @addr: the mac address of the local client to query
2672 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002673 *
2674 * Returns true if the local client is known to be roaming (it is not served by
2675 * this node anymore) or not. If yes, the client is still present in the table
2676 * to keep the latter consistent with the node TTVN
2677 */
2678bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002679 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002680{
2681 struct batadv_tt_local_entry *tt_local_entry;
2682 bool ret = false;
2683
Antonio Quartullic018ad32013-06-04 12:11:39 +02002684 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002685 if (!tt_local_entry)
2686 goto out;
2687
2688 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2689 batadv_tt_local_entry_free_ref(tt_local_entry);
2690out:
2691 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002692}
2693
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002694bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2695 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002696 const unsigned char *addr,
2697 unsigned short vlan)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002698{
2699 bool ret = false;
2700
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002701 /* if the originator is a backbone node (meaning it belongs to the same
2702 * LAN of this node) the temporary client must not be added because to
2703 * reach such destination the node must use the LAN instead of the mesh
2704 */
2705 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2706 goto out;
2707
Antonio Quartullic018ad32013-06-04 12:11:39 +02002708 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vlan,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002709 BATADV_TT_CLIENT_TEMP,
2710 atomic_read(&orig_node->last_ttvn)))
2711 goto out;
2712
2713 batadv_dbg(BATADV_DBG_TT, bat_priv,
2714 "Added temporary global client (addr: %pM orig: %pM)\n",
2715 addr, orig_node->orig);
2716 ret = true;
2717out:
2718 return ret;
2719}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002720
2721/**
2722 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2723 * @bat_priv: the bat priv with all the soft interface information
2724 * @orig: the orig_node of the ogm
2725 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2726 * @tvlv_value: tvlv buffer containing the gateway data
2727 * @tvlv_value_len: tvlv buffer length
2728 */
2729static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2730 struct batadv_orig_node *orig,
2731 uint8_t flags,
2732 void *tvlv_value,
2733 uint16_t tvlv_value_len)
2734{
2735 struct batadv_tvlv_tt_data *tt_data;
2736 uint16_t num_entries;
2737
2738 if (tvlv_value_len < sizeof(*tt_data))
2739 return;
2740
2741 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2742 tvlv_value_len -= sizeof(*tt_data);
2743
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002744 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002745
2746 batadv_tt_update_orig(bat_priv, orig,
2747 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002748 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002749}
2750
2751/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002752 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2753 * container
2754 * @bat_priv: the bat priv with all the soft interface information
2755 * @src: mac address of tt tvlv sender
2756 * @dst: mac address of tt tvlv recipient
2757 * @tvlv_value: tvlv buffer containing the tt data
2758 * @tvlv_value_len: tvlv buffer length
2759 *
2760 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2761 * otherwise.
2762 */
2763static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2764 uint8_t *src, uint8_t *dst,
2765 void *tvlv_value,
2766 uint16_t tvlv_value_len)
2767{
2768 struct batadv_tvlv_tt_data *tt_data;
2769 uint16_t num_entries;
2770 char tt_flag;
2771 bool ret;
2772
2773 if (tvlv_value_len < sizeof(*tt_data))
2774 return NET_RX_SUCCESS;
2775
2776 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2777 tvlv_value_len -= sizeof(*tt_data);
2778
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002779 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002780
2781 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2782 case BATADV_TT_REQUEST:
2783 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2784
2785 /* If this node cannot provide a TT response the tt_request is
2786 * forwarded
2787 */
2788 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2789 if (!ret) {
2790 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2791 tt_flag = 'F';
2792 else
2793 tt_flag = '.';
2794
2795 batadv_dbg(BATADV_DBG_TT, bat_priv,
2796 "Routing TT_REQUEST to %pM [%c]\n",
2797 dst, tt_flag);
2798 /* tvlv API will re-route the packet */
2799 return NET_RX_DROP;
2800 }
2801 break;
2802 case BATADV_TT_RESPONSE:
2803 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2804
2805 if (batadv_is_my_mac(bat_priv, dst)) {
2806 batadv_handle_tt_response(bat_priv, tt_data,
2807 src, num_entries);
2808 return NET_RX_SUCCESS;
2809 }
2810
2811 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2812 tt_flag = 'F';
2813 else
2814 tt_flag = '.';
2815
2816 batadv_dbg(BATADV_DBG_TT, bat_priv,
2817 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2818
2819 /* tvlv API will re-route the packet */
2820 return NET_RX_DROP;
2821 }
2822
2823 return NET_RX_SUCCESS;
2824}
2825
2826/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002827 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2828 * @bat_priv: the bat priv with all the soft interface information
2829 * @src: mac address of tt tvlv sender
2830 * @dst: mac address of tt tvlv recipient
2831 * @tvlv_value: tvlv buffer containing the tt data
2832 * @tvlv_value_len: tvlv buffer length
2833 *
2834 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2835 * otherwise.
2836 */
2837static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2838 uint8_t *src, uint8_t *dst,
2839 void *tvlv_value,
2840 uint16_t tvlv_value_len)
2841{
2842 struct batadv_tvlv_roam_adv *roaming_adv;
2843 struct batadv_orig_node *orig_node = NULL;
2844
2845 /* If this node is not the intended recipient of the
2846 * roaming advertisement the packet is forwarded
2847 * (the tvlv API will re-route the packet).
2848 */
2849 if (!batadv_is_my_mac(bat_priv, dst))
2850 return NET_RX_DROP;
2851
2852 /* check if it is a backbone gateway. we don't accept
2853 * roaming advertisement from it, as it has the same
2854 * entries as we have.
2855 */
2856 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2857 goto out;
2858
2859 if (tvlv_value_len < sizeof(*roaming_adv))
2860 goto out;
2861
2862 orig_node = batadv_orig_hash_find(bat_priv, src);
2863 if (!orig_node)
2864 goto out;
2865
2866 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2867 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2868
2869 batadv_dbg(BATADV_DBG_TT, bat_priv,
2870 "Received ROAMING_ADV from %pM (client %pM)\n",
2871 src, roaming_adv->client);
2872
2873 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002874 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08002875 atomic_read(&orig_node->last_ttvn) + 1);
2876
2877out:
2878 if (orig_node)
2879 batadv_orig_node_free_ref(orig_node);
2880 return NET_RX_SUCCESS;
2881}
2882
2883/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002884 * batadv_tt_init - initialise the translation table internals
2885 * @bat_priv: the bat priv with all the soft interface information
2886 *
2887 * Return 0 on success or negative error number in case of failure.
2888 */
2889int batadv_tt_init(struct batadv_priv *bat_priv)
2890{
2891 int ret;
2892
2893 ret = batadv_tt_local_init(bat_priv);
2894 if (ret < 0)
2895 return ret;
2896
2897 ret = batadv_tt_global_init(bat_priv);
2898 if (ret < 0)
2899 return ret;
2900
2901 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002902 batadv_tt_tvlv_unicast_handler_v1,
2903 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002904
Marek Lindner122edaa2013-04-23 21:40:03 +08002905 batadv_tvlv_handler_register(bat_priv, NULL,
2906 batadv_roam_tvlv_unicast_handler_v1,
2907 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2908
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002909 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2910 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2911 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2912
2913 return 1;
2914}