blob: 34fa6cc381c913ba1b5f3a0f5f4edc76b9109e4f [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,
37 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020038static void batadv_tt_purge(struct work_struct *work);
39static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020040batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020041static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42 struct batadv_orig_node *orig_node,
43 const unsigned char *addr,
44 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Marek Lindner7aadf882011-02-18 12:28:09 +000046/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020047static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000048{
Sven Eckelmann56303d32012-06-05 22:31:31 +020049 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000051
52 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
53}
54
Sven Eckelmann56303d32012-06-05 22:31:31 +020055static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020056batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000057{
Marek Lindner7aadf882011-02-18 12:28:09 +000058 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +020059 struct batadv_tt_common_entry *tt_common_entry;
60 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020061 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000062
63 if (!hash)
64 return NULL;
65
Sven Eckelmannda641192012-05-12 13:48:56 +020066 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000067 head = &hash->table[index];
68
69 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -080070 hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020071 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000072 continue;
73
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020075 continue;
76
Antonio Quartulli48100ba2011-10-30 12:17:33 +010077 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000078 break;
79 }
80 rcu_read_unlock();
81
Antonio Quartulli48100ba2011-10-30 12:17:33 +010082 return tt_common_entry_tmp;
83}
84
Sven Eckelmann56303d32012-06-05 22:31:31 +020085static struct batadv_tt_local_entry *
86batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010087{
Sven Eckelmann56303d32012-06-05 22:31:31 +020088 struct batadv_tt_common_entry *tt_common_entry;
89 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010090
Sven Eckelmann807736f2012-07-15 22:26:51 +020091 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010092 if (tt_common_entry)
93 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020094 struct batadv_tt_local_entry,
95 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000097}
98
Sven Eckelmann56303d32012-06-05 22:31:31 +020099static struct batadv_tt_global_entry *
100batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000101{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200102 struct batadv_tt_common_entry *tt_common_entry;
103 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000104
Sven Eckelmann807736f2012-07-15 22:26:51 +0200105 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100106 if (tt_common_entry)
107 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200108 struct batadv_tt_global_entry,
109 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000111}
112
Sven Eckelmanna5130882012-05-16 20:23:16 +0200113static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100116 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
117 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200118}
119
Antonio Quartulli21026052013-05-07 00:29:22 +0200120/**
121 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
122 * tt_global_entry and possibly free it
123 * @tt_global_entry: the object to free
124 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200125static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200126batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200127{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200128 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200129 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200130 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200131 }
132}
133
Sven Eckelmanna5130882012-05-16 20:23:16 +0200134static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200135{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200136 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200137
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800139
140 /* We are in an rcu callback here, therefore we cannot use
141 * batadv_orig_node_free_ref() and its call_rcu():
142 * An rcu_barrier() wouldn't wait for that to finish
143 */
144 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200145 kfree(orig_entry);
146}
147
Sven Eckelmanna5130882012-05-16 20:23:16 +0200148static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200150{
Antonio Quartullid657e622012-07-01 14:09:12 +0200151 if (!atomic_dec_and_test(&orig_entry->refcount))
152 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000153 /* to avoid race conditions, immediately decrease the tt counter */
154 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200155 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200156}
157
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200158/**
159 * batadv_tt_local_event - store a local TT event (ADD/DEL)
160 * @bat_priv: the bat priv with all the soft interface information
161 * @tt_local_entry: the TT entry involved in the event
162 * @event_flags: flags to store in the event structure
163 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200165 struct batadv_tt_local_entry *tt_local_entry,
166 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200167{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200168 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200169 struct batadv_tt_common_entry *common = &tt_local_entry->common;
170 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200171 bool event_removed = false;
172 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200173
174 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800179 tt_change_node->change.reserved = 0;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200180 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200181
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200182 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200183
184 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200185 spin_lock_bh(&bat_priv->tt.changes_list_lock);
186 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200187 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200188 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200189 continue;
190
191 /* DEL+ADD in the same orig interval have no effect and can be
192 * removed to avoid silly behaviour on the receiver side. The
193 * other way around (ADD+DEL) can happen in case of roaming of
194 * a client still in the NEW state. Roaming of NEW clients is
195 * now possible due to automatically recognition of "temporary"
196 * clients
197 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200198 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200199 if (!del_op_requested && del_op_entry)
200 goto del;
201 if (del_op_requested && !del_op_entry)
202 goto del;
203 continue;
204del:
205 list_del(&entry->list);
206 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000207 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200208 event_removed = true;
209 goto unlock;
210 }
211
Antonio Quartullia73105b2011-04-27 14:27:44 +0200212 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200213 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200214
215unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200216 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200217
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200218 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200219 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200220 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200221 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200222}
223
Marek Lindner335fbe02013-04-23 21:40:02 +0800224/**
225 * batadv_tt_len - compute length in bytes of given number of tt changes
226 * @changes_num: number of tt changes
227 *
228 * Returns computed length in bytes.
229 */
230static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200231{
Marek Lindner335fbe02013-04-23 21:40:02 +0800232 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200233}
234
Sven Eckelmann56303d32012-06-05 22:31:31 +0200235static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200237 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239
Sven Eckelmann807736f2012-07-15 22:26:51 +0200240 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Sven Eckelmann807736f2012-07-15 22:26:51 +0200242 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200243 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Antonio Quartullidec05072012-11-10 11:00:32 +0100245 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
246 &batadv_tt_local_hash_lock_class_key);
247
Sven Eckelmann5346c352012-05-05 13:27:28 +0200248 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249}
250
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200251static void batadv_tt_global_free(struct batadv_priv *bat_priv,
252 struct batadv_tt_global_entry *tt_global,
253 const char *message)
254{
255 batadv_dbg(BATADV_DBG_TT, bat_priv,
256 "Deleting global tt entry %pM: %s\n",
257 tt_global->common.addr, message);
258
259 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
260 batadv_choose_orig, tt_global->common.addr);
261 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200262}
263
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200264void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
265 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200267 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200268 struct batadv_tt_local_entry *tt_local;
269 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200270 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200271 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100272 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200273 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Antonio Quartulli47c94652012-09-23 22:38:35 +0200275 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200276 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277
Antonio Quartulli47c94652012-09-23 22:38:35 +0200278 if (tt_local) {
279 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200280 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
281 batadv_dbg(BATADV_DBG_TT, bat_priv,
282 "Re-adding pending client %pM\n", addr);
283 /* whatever the reason why the PENDING flag was set,
284 * this is a client which was enqueued to be removed in
285 * this orig_interval. Since it popped up again, the
286 * flag can be reset like it was never enqueued
287 */
288 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
289 goto add_event;
290 }
291
292 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
293 batadv_dbg(BATADV_DBG_TT, bat_priv,
294 "Roaming client %pM came back to its original location\n",
295 addr);
296 /* the ROAM flag is set because this client roamed away
297 * and the node got a roaming_advertisement message. Now
298 * that the client popped up again at its original
299 * location such flag can be unset
300 */
301 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
302 roamed_back = true;
303 }
304 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 }
306
Antonio Quartulli47c94652012-09-23 22:38:35 +0200307 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
308 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200309 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200310
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200311 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200312 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200313 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
Antonio Quartulli47c94652012-09-23 22:38:35 +0200315 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100316 /* The local entry has to be marked as NEW to avoid to send it in
317 * a full table response going out before the next ttvn increment
318 * (consistency check)
319 */
320 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200321 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200322 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
323 atomic_set(&tt_local->common.refcount, 2);
324 tt_local->last_seen = jiffies;
325 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
327 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200328 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200329 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330
Sven Eckelmann807736f2012-07-15 22:26:51 +0200331 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200332 batadv_choose_orig, &tt_local->common,
333 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100334
335 if (unlikely(hash_added != 0)) {
336 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200337 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100338 goto out;
339 }
340
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200341add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200342 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200343
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200344check_roaming:
345 /* Check whether it is a roaming, but don't do anything if the roaming
346 * process has already been handled
347 */
348 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200349 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200350 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200351 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800352 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200353 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200354 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200355 }
356 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200357 if (roamed_back) {
358 batadv_tt_global_free(bat_priv, tt_global,
359 "Roaming canceled");
360 tt_global = NULL;
361 } else {
362 /* The global entry has to be marked as ROAMING and
363 * has to be kept for consistency purpose
364 */
365 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
366 tt_global->roam_at = jiffies;
367 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200368 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200369
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200370out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200371 if (tt_local)
372 batadv_tt_local_entry_free_ref(tt_local);
373 if (tt_global)
374 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375}
376
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800377/**
378 * batadv_tt_tvlv_container_update - update the translation table tvlv container
379 * after local tt changes have been committed
380 * @bat_priv: the bat priv with all the soft interface information
381 */
382static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800384 struct batadv_tt_change_node *entry, *safe;
385 struct batadv_tvlv_tt_data *tt_data;
386 struct batadv_tvlv_tt_change *tt_change;
387 int tt_diff_len = 0, tt_change_len = 0;
388 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800390 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800391
392 /* if we have too many changes for one packet don't send any
393 * and wait for the tt table request which will be fragmented
394 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800395 if (tt_diff_len > bat_priv->soft_iface->mtu)
396 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800397
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800398 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
399 if (!tt_data)
400 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800401
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800402 tt_data->flags = BATADV_TT_OGM_DIFF;
403 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
Antonio Quartulliced72932013-04-24 16:37:51 +0200404 tt_data->crc = htonl(bat_priv->tt.local_crc);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800405
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800406 if (tt_diff_len == 0)
407 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800408
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800409 tt_diff_entries_num = tt_diff_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Sven Eckelmann807736f2012-07-15 22:26:51 +0200411 spin_lock_bh(&bat_priv->tt.changes_list_lock);
412 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800414 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
415
Sven Eckelmann807736f2012-07-15 22:26:51 +0200416 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100417 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800418 if (tt_diff_entries_count < tt_diff_entries_num) {
419 memcpy(tt_change + tt_diff_entries_count,
420 &entry->change,
421 sizeof(struct batadv_tvlv_tt_change));
422 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200424 list_del(&entry->list);
425 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200427 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428
Antonio Quartullia73105b2011-04-27 14:27:44 +0200429 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200430 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
431 kfree(bat_priv->tt.last_changeset);
432 bat_priv->tt.last_changeset_len = 0;
433 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800434 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800435 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800436 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800437 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 * instead of providing the diff
439 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800440 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200441 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800442 memcpy(bat_priv->tt.last_changeset,
443 tt_change, tt_change_len);
444 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200445 }
446 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200447 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800449container_register:
450 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
451 sizeof(*tt_data) + tt_change_len);
452 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453}
454
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200455int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456{
457 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200458 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200459 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200460 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100461 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200464 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100465 int last_seen_secs;
466 int last_seen_msecs;
467 unsigned long last_seen_jiffies;
468 bool no_purge;
469 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470
Marek Lindner30da63a2012-08-03 17:15:46 +0200471 primary_if = batadv_seq_print_text_primary_if_get(seq);
472 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200473 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100475 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200476 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100477 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
478 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100479 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
480 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 for (i = 0; i < hash->size; i++) {
483 head = &hash->table[i];
484
Marek Lindner7aadf882011-02-18 12:28:09 +0000485 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800486 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000487 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100488 tt_local = container_of(tt_common_entry,
489 struct batadv_tt_local_entry,
490 common);
491 last_seen_jiffies = jiffies - tt_local->last_seen;
492 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
493 last_seen_secs = last_seen_msecs / 1000;
494 last_seen_msecs = last_seen_msecs % 1000;
495
496 no_purge = tt_common_entry->flags & np_flag;
497
498 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100499 tt_common_entry->addr,
500 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200501 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100502 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100503 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200504 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100505 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200506 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100507 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100508 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100509 no_purge ? 0 : last_seen_secs,
510 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000512 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200514out:
515 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200516 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200517 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518}
519
Sven Eckelmann56303d32012-06-05 22:31:31 +0200520static void
521batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
522 struct batadv_tt_local_entry *tt_local_entry,
523 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200525 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526
Antonio Quartulli015758d2011-07-09 17:52:13 +0200527 /* The local client has to be marked as "pending to be removed" but has
528 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200529 * response issued before the net ttvn increment (consistency check)
530 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200531 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100532
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200533 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200534 "Local tt entry (%pM) pending to be removed: %s\n",
535 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536}
537
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200538/**
539 * batadv_tt_local_remove - logically remove an entry from the local table
540 * @bat_priv: the bat priv with all the soft interface information
541 * @addr: the MAC address of the client to remove
542 * @message: message to append to the log on deletion
543 * @roaming: true if the deletion is due to a roaming event
544 *
545 * Returns the flags assigned to the local entry before being deleted
546 */
547uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
548 const uint8_t *addr, const char *message,
549 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200551 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200552 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553
Sven Eckelmanna5130882012-05-16 20:23:16 +0200554 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200555 if (!tt_local_entry)
556 goto out;
557
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200558 curr_flags = tt_local_entry->common.flags;
559
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200560 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200561 /* if this global entry addition is due to a roaming, the node has to
562 * mark the local entry as "roamed" in order to correctly reroute
563 * packets later
564 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200565 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200566 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200567 /* mark the local client as ROAMed */
568 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
569 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200570
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200571 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
572 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
573 message);
574 goto out;
575 }
576 /* if this client has been added right now, it is possible to
577 * immediately purge it
578 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200579 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200580 hlist_del_rcu(&tt_local_entry->common.hash_entry);
581 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200582
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200583out:
584 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200585 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200586
587 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588}
589
Sven Eckelmann56303d32012-06-05 22:31:31 +0200590static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200591 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200593 struct batadv_tt_local_entry *tt_local_entry;
594 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800595 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200596
Sasha Levinb67bfe02013-02-27 17:06:00 -0800597 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200598 hash_entry) {
599 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200600 struct batadv_tt_local_entry,
601 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200602 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
603 continue;
604
605 /* entry already marked for deletion */
606 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
607 continue;
608
609 if (!batadv_has_timed_out(tt_local_entry->last_seen,
610 BATADV_TT_LOCAL_TIMEOUT))
611 continue;
612
613 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
614 BATADV_TT_CLIENT_DEL, "timed out");
615 }
616}
617
Sven Eckelmann56303d32012-06-05 22:31:31 +0200618static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200619{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200620 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200622 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200623 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 for (i = 0; i < hash->size; i++) {
626 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200627 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200629 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200630 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200631 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633}
634
Sven Eckelmann56303d32012-06-05 22:31:31 +0200635static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200637 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200639 struct batadv_tt_common_entry *tt_common_entry;
640 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800641 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200642 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200643 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200644
Sven Eckelmann807736f2012-07-15 22:26:51 +0200645 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646 return;
647
Sven Eckelmann807736f2012-07-15 22:26:51 +0200648 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200649
650 for (i = 0; i < hash->size; i++) {
651 head = &hash->table[i];
652 list_lock = &hash->list_locks[i];
653
654 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800655 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800657 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200658 tt_local = container_of(tt_common_entry,
659 struct batadv_tt_local_entry,
660 common);
661 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662 }
663 spin_unlock_bh(list_lock);
664 }
665
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200666 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200667
Sven Eckelmann807736f2012-07-15 22:26:51 +0200668 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669}
670
Sven Eckelmann56303d32012-06-05 22:31:31 +0200671static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200673 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200674 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675
Sven Eckelmann807736f2012-07-15 22:26:51 +0200676 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677
Sven Eckelmann807736f2012-07-15 22:26:51 +0200678 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200679 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
Antonio Quartullidec05072012-11-10 11:00:32 +0100681 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
682 &batadv_tt_global_hash_lock_class_key);
683
Sven Eckelmann5346c352012-05-05 13:27:28 +0200684 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685}
686
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200688{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200690
Sven Eckelmann807736f2012-07-15 22:26:51 +0200691 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200692
Sven Eckelmann807736f2012-07-15 22:26:51 +0200693 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694 list) {
695 list_del(&entry->list);
696 kfree(entry);
697 }
698
Sven Eckelmann807736f2012-07-15 22:26:51 +0200699 atomic_set(&bat_priv->tt.local_changes, 0);
700 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701}
702
Antonio Quartullid657e622012-07-01 14:09:12 +0200703/* retrieves the orig_tt_list_entry belonging to orig_node from the
704 * batadv_tt_global_entry list
705 *
706 * returns it with an increased refcounter, NULL if not found
707 */
708static struct batadv_tt_orig_list_entry *
709batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
710 const struct batadv_orig_node *orig_node)
711{
712 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
713 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200714
715 rcu_read_lock();
716 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800717 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200718 if (tmp_orig_entry->orig_node != orig_node)
719 continue;
720 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
721 continue;
722
723 orig_entry = tmp_orig_entry;
724 break;
725 }
726 rcu_read_unlock();
727
728 return orig_entry;
729}
730
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200731/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200732 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200733 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200734static bool
735batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
736 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200737{
Antonio Quartullid657e622012-07-01 14:09:12 +0200738 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200739 bool found = false;
740
Antonio Quartullid657e622012-07-01 14:09:12 +0200741 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
742 if (orig_entry) {
743 found = true;
744 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200745 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200746
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747 return found;
748}
749
Sven Eckelmanna5130882012-05-16 20:23:16 +0200750static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200751batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200752 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200754 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200755
Antonio Quartullid657e622012-07-01 14:09:12 +0200756 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200757 if (orig_entry) {
758 /* refresh the ttvn: the current value could be a bogus one that
759 * was added during a "temporary client detection"
760 */
761 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200762 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200763 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200764
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200765 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
766 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200767 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200768
769 INIT_HLIST_NODE(&orig_entry->list);
770 atomic_inc(&orig_node->refcount);
771 atomic_inc(&orig_node->tt_size);
772 orig_entry->orig_node = orig_node;
773 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200774 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200775
Antonio Quartullid657e622012-07-01 14:09:12 +0200776 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200777 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200778 &tt_global->orig_list);
779 spin_unlock_bh(&tt_global->list_lock);
780out:
781 if (orig_entry)
782 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200783}
784
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200785/**
786 * batadv_tt_global_add - add a new TT global entry or update an existing one
787 * @bat_priv: the bat priv with all the soft interface information
788 * @orig_node: the originator announcing the client
789 * @tt_addr: the mac address of the non-mesh client
790 * @flags: TT flags that have to be set for this non-mesh client
791 * @ttvn: the tt version number ever announcing this non-mesh client
792 *
793 * Add a new TT global entry for the given originator. If the entry already
794 * exists add a new reference to the given originator (a global entry can have
795 * references to multiple originators) and adjust the flags attribute to reflect
796 * the function argument.
797 * If a TT local entry exists for this non-mesh client remove it.
798 *
799 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200800 *
801 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200802 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200803static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
804 struct batadv_orig_node *orig_node,
805 const unsigned char *tt_addr, uint16_t flags,
806 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200808 struct batadv_tt_global_entry *tt_global_entry;
809 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200810 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100811 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200812 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200813 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814
Sven Eckelmanna5130882012-05-16 20:23:16 +0200815 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200816 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
817
818 /* if the node already has a local client for this entry, it has to wait
819 * for a roaming advertisement instead of manually messing up the global
820 * table
821 */
822 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
823 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
824 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825
Antonio Quartullia73105b2011-04-27 14:27:44 +0200826 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200827 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200828 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200829 goto out;
830
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200831 common = &tt_global_entry->common;
832 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200833
Antonio Quartullid4f44692012-05-25 00:00:54 +0200834 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200835 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200836 /* node must store current time in case of roaming. This is
837 * needed to purge this entry out on timeout (if nobody claims
838 * it)
839 */
840 if (flags & BATADV_TT_CLIENT_ROAM)
841 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200842 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200843 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200844
845 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
846 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200847
Sven Eckelmann807736f2012-07-15 22:26:51 +0200848 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200849 batadv_compare_tt,
850 batadv_choose_orig, common,
851 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100852
853 if (unlikely(hash_added != 0)) {
854 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200855 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100856 goto out_remove;
857 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200858 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200859 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200860 /* If there is already a global entry, we can use this one for
861 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200862 * But if we are trying to add a temporary client then here are
863 * two options at this point:
864 * 1) the global client is not a temporary client: the global
865 * client has to be left as it is, temporary information
866 * should never override any already known client state
867 * 2) the global client is a temporary client: purge the
868 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200869 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200870 if (flags & BATADV_TT_CLIENT_TEMP) {
871 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
872 goto out;
873 if (batadv_tt_global_entry_has_orig(tt_global_entry,
874 orig_node))
875 goto out_remove;
876 batadv_tt_global_del_orig_list(tt_global_entry);
877 goto add_orig_entry;
878 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200879
880 /* if the client was temporary added before receiving the first
881 * OGM announcing it, we have to clear the TEMP flag
882 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200883 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200884
Antonio Quartullie9c001362012-11-07 15:05:33 +0100885 /* the change can carry possible "attribute" flags like the
886 * TT_CLIENT_WIFI, therefore they have to be copied in the
887 * client entry
888 */
889 tt_global_entry->common.flags |= flags;
890
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200891 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
892 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200893 * delete + roaming change for this originator.
894 *
895 * We should first delete the old originator before adding the
896 * new one.
897 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200898 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200899 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200900 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200901 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902 }
903 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200904add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200905 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200906 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200908 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200909 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200910 common->addr, orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200911 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200912
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100913out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200914
Antonio Quartullia73105b2011-04-27 14:27:44 +0200915 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200916 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
917 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +1000918 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200919 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
920
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200921 if (!(flags & BATADV_TT_CLIENT_ROAM))
922 /* this is a normal global add. Therefore the client is not in a
923 * roaming state anymore.
924 */
925 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
926
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200927out:
928 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200929 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200930 if (tt_local_entry)
931 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200932 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000933}
934
Sven Eckelmann981d8902012-10-07 13:34:15 +0200935/* batadv_transtable_best_orig - Get best originator list entry from tt entry
936 * @tt_global_entry: global translation table entry to be analyzed
937 *
938 * This functon assumes the caller holds rcu_read_lock().
939 * Returns best originator list entry or NULL on errors.
940 */
941static struct batadv_tt_orig_list_entry *
942batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
943{
944 struct batadv_neigh_node *router = NULL;
945 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200946 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
947 int best_tq = 0;
948
949 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800950 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200951 router = batadv_orig_node_get_router(orig_entry->orig_node);
952 if (!router)
953 continue;
954
955 if (router->tq_avg > best_tq) {
956 best_entry = orig_entry;
957 best_tq = router->tq_avg;
958 }
959
960 batadv_neigh_node_free_ref(router);
961 }
962
963 return best_entry;
964}
965
966/* batadv_tt_global_print_entry - print all orig nodes who announce the address
967 * for this global entry
968 * @tt_global_entry: global translation table entry to be printed
969 * @seq: debugfs table seq_file struct
970 *
971 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200972 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200975 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200976{
977 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200978 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200979 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200980 uint16_t flags;
981 uint8_t last_ttvn;
982
983 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200984 flags = tt_common_entry->flags;
985
986 best_entry = batadv_transtable_best_orig(tt_global_entry);
987 if (best_entry) {
988 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100989 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200990 " %c %pM (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200991 '*', tt_global_entry->common.addr,
992 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100993 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200994 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
995 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
996 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
997 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200998
999 head = &tt_global_entry->orig_list;
1000
Sasha Levinb67bfe02013-02-27 17:06:00 -08001001 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001002 if (best_entry == orig_entry)
1003 continue;
1004
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001005 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001006 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1007 '+', tt_global_entry->common.addr,
1008 orig_entry->ttvn, orig_entry->orig_node->orig,
1009 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001010 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001011 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1012 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001013 }
1014}
1015
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001016int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017{
1018 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001019 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001020 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001021 struct batadv_tt_common_entry *tt_common_entry;
1022 struct batadv_tt_global_entry *tt_global;
1023 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001025 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026
Marek Lindner30da63a2012-08-03 17:15:46 +02001027 primary_if = batadv_seq_print_text_primary_if_get(seq);
1028 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001029 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001031 seq_printf(seq,
1032 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 net_dev->name);
Antonio Quartulliced72932013-04-24 16:37:51 +02001034 seq_printf(seq, " %-13s %s %-15s %s (%-10s) %s\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001035 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1036 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001038 for (i = 0; i < hash->size; i++) {
1039 head = &hash->table[i];
1040
Marek Lindner7aadf882011-02-18 12:28:09 +00001041 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001042 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001043 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001044 tt_global = container_of(tt_common_entry,
1045 struct batadv_tt_global_entry,
1046 common);
1047 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001049 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001051out:
1052 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001053 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001054 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001055}
1056
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001057/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001058static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001059batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001061 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001062 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001063 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001064
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001065 spin_lock_bh(&tt_global_entry->list_lock);
1066 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001067 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1068 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001069 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070 }
1071 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072}
1073
Sven Eckelmanna5130882012-05-16 20:23:16 +02001074static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1076 struct batadv_tt_global_entry *tt_global_entry,
1077 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001078 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001079{
1080 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001081 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001082 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001083
1084 spin_lock_bh(&tt_global_entry->list_lock);
1085 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001086 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001087 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001088 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001089 "Deleting %pM from global tt entry %pM: %s\n",
1090 orig_node->orig,
1091 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001093 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094 }
1095 }
1096 spin_unlock_bh(&tt_global_entry->list_lock);
1097}
1098
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001099/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001100 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1101 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001102 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001103static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001104batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1105 struct batadv_tt_global_entry *tt_global_entry,
1106 struct batadv_orig_node *orig_node,
1107 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001108{
1109 bool last_entry = true;
1110 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001112
1113 /* no local entry exists, case 1:
1114 * Check if this is the last one or if other entries exist.
1115 */
1116
1117 rcu_read_lock();
1118 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001119 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001120 if (orig_entry->orig_node != orig_node) {
1121 last_entry = false;
1122 break;
1123 }
1124 }
1125 rcu_read_unlock();
1126
1127 if (last_entry) {
1128 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001129 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001130 tt_global_entry->roam_at = jiffies;
1131 } else
1132 /* there is another entry, we can simply delete this
1133 * one and can still use the other one.
1134 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001135 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1136 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001137}
1138
1139
1140
Sven Eckelmann56303d32012-06-05 22:31:31 +02001141static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1142 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001143 const unsigned char *addr,
1144 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001146 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001147 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148
Sven Eckelmanna5130882012-05-16 20:23:16 +02001149 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001150 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001151 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001152
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001153 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001154 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1155 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001156
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001157 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001158 batadv_tt_global_free(bat_priv, tt_global_entry,
1159 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001160
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001161 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001162 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001163
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164 /* if we are deleting a global entry due to a roam
1165 * event, there are two possibilities:
1166 * 1) the client roamed from node A to node B => if there
1167 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001168 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001169 * wait for node B to claim it. In case of timeout
1170 * the entry is purged.
1171 *
1172 * If there are other originators left, we directly delete
1173 * the originator.
1174 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001175 * the global entry, since it is useless now.
1176 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001177 local_entry = batadv_tt_local_hash_find(bat_priv,
1178 tt_global_entry->common.addr);
1179 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001180 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001181 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001182 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001183 } else
1184 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001185 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1186 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001187
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001188
Antonio Quartullicc47f662011-04-27 14:27:57 +02001189out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001190 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001191 batadv_tt_global_entry_free_ref(tt_global_entry);
1192 if (local_entry)
1193 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194}
1195
Sven Eckelmann56303d32012-06-05 22:31:31 +02001196void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1197 struct batadv_orig_node *orig_node,
1198 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001199{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001200 struct batadv_tt_global_entry *tt_global;
1201 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001202 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001203 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001204 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001206 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001207
Simon Wunderlich6e801492011-10-19 10:28:26 +02001208 if (!hash)
1209 return;
1210
Antonio Quartullia73105b2011-04-27 14:27:44 +02001211 for (i = 0; i < hash->size; i++) {
1212 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001213 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001214
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001215 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001216 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001217 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001218 tt_global = container_of(tt_common_entry,
1219 struct batadv_tt_global_entry,
1220 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001221
Sven Eckelmann56303d32012-06-05 22:31:31 +02001222 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001223 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001224
Sven Eckelmann56303d32012-06-05 22:31:31 +02001225 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001226 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001227 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001228 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001230 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001231 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001233 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001234 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001235 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001236}
1237
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001238static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1239 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001240{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001241 bool purge = false;
1242 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1243 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001244
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001245 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1246 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1247 purge = true;
1248 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001249 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001250
1251 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1252 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1253 purge = true;
1254 *msg = "Temporary client timeout\n";
1255 }
1256
1257 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001258}
1259
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001260static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001261{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001262 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001263 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001264 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001265 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001266 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001267 char *msg = NULL;
1268 struct batadv_tt_common_entry *tt_common;
1269 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001270
Antonio Quartullicc47f662011-04-27 14:27:57 +02001271 for (i = 0; i < hash->size; i++) {
1272 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001273 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001274
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001275 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001276 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001277 hash_entry) {
1278 tt_global = container_of(tt_common,
1279 struct batadv_tt_global_entry,
1280 common);
1281
1282 if (!batadv_tt_global_to_purge(tt_global, &msg))
1283 continue;
1284
1285 batadv_dbg(BATADV_DBG_TT, bat_priv,
1286 "Deleting global tt entry (%pM): %s\n",
1287 tt_global->common.addr, msg);
1288
Sasha Levinb67bfe02013-02-27 17:06:00 -08001289 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001290
1291 batadv_tt_global_entry_free_ref(tt_global);
1292 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001293 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001294 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001295}
1296
Sven Eckelmann56303d32012-06-05 22:31:31 +02001297static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001298{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001299 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001300 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001301 struct batadv_tt_common_entry *tt_common_entry;
1302 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001303 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001305 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001306
Sven Eckelmann807736f2012-07-15 22:26:51 +02001307 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001308 return;
1309
Sven Eckelmann807736f2012-07-15 22:26:51 +02001310 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001311
1312 for (i = 0; i < hash->size; i++) {
1313 head = &hash->table[i];
1314 list_lock = &hash->list_locks[i];
1315
1316 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001318 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001319 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001320 tt_global = container_of(tt_common_entry,
1321 struct batadv_tt_global_entry,
1322 common);
1323 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001324 }
1325 spin_unlock_bh(list_lock);
1326 }
1327
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001328 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001329
Sven Eckelmann807736f2012-07-15 22:26:51 +02001330 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001331}
1332
Sven Eckelmann56303d32012-06-05 22:31:31 +02001333static bool
1334_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1335 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001336{
1337 bool ret = false;
1338
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001339 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1340 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001341 ret = true;
1342
1343 return ret;
1344}
1345
Sven Eckelmann56303d32012-06-05 22:31:31 +02001346struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1347 const uint8_t *src,
1348 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001349{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001350 struct batadv_tt_local_entry *tt_local_entry = NULL;
1351 struct batadv_tt_global_entry *tt_global_entry = NULL;
1352 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001353 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001354
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001355 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001356 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001357 if (!tt_local_entry ||
1358 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001359 goto out;
1360 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001361
Sven Eckelmanna5130882012-05-16 20:23:16 +02001362 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001363 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001364 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001366 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001367 * isolation
1368 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001369 if (tt_local_entry &&
1370 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001371 goto out;
1372
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001373 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001374 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001375 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001376 if (best_entry)
1377 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001378 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1379 orig_node = NULL;
1380 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001381
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001382out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001383 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001384 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001385 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001386 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001387
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001388 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001389}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390
Antonio Quartulliced72932013-04-24 16:37:51 +02001391/**
1392 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1393 * to the given orig_node
1394 * @bat_priv: the bat priv with all the soft interface information
1395 */
1396static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001397 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001399 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001400 struct batadv_tt_common_entry *tt_common;
1401 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001402 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001403 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404
1405 for (i = 0; i < hash->size; i++) {
1406 head = &hash->table[i];
1407
1408 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001409 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001410 tt_global = container_of(tt_common,
1411 struct batadv_tt_global_entry,
1412 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001413 /* Roaming clients are in the global table for
1414 * consistency only. They don't have to be
1415 * taken into account while computing the
1416 * global crc
1417 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001418 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001420 /* Temporary clients have not been announced yet, so
1421 * they have to be skipped while computing the global
1422 * crc
1423 */
1424 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1425 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001426
1427 /* find out if this global entry is announced by this
1428 * originator
1429 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001430 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001431 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001432 continue;
1433
Antonio Quartulliced72932013-04-24 16:37:51 +02001434 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435 }
1436 rcu_read_unlock();
1437 }
1438
Antonio Quartulliced72932013-04-24 16:37:51 +02001439 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001440}
1441
Antonio Quartulliced72932013-04-24 16:37:51 +02001442/**
1443 * batadv_tt_local_crc - calculates the checksum of the local table
1444 * @bat_priv: the bat priv with all the soft interface information
1445 */
1446static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001447{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001448 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001449 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001450 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001451 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452
1453 for (i = 0; i < hash->size; i++) {
1454 head = &hash->table[i];
1455
1456 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001457 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001458 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001459 * account while computing the CRC
1460 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001461 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001462 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001463
1464 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001465 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001466 rcu_read_unlock();
1467 }
1468
Antonio Quartulliced72932013-04-24 16:37:51 +02001469 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470}
1471
Sven Eckelmann56303d32012-06-05 22:31:31 +02001472static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001474 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475
Sven Eckelmann807736f2012-07-15 22:26:51 +02001476 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477
Sven Eckelmann807736f2012-07-15 22:26:51 +02001478 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001479 list_del(&node->list);
1480 kfree(node);
1481 }
1482
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484}
1485
Sven Eckelmann56303d32012-06-05 22:31:31 +02001486static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1487 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001488 const unsigned char *tt_buff,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001489 uint16_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001491 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001492
1493 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001494 * last OGM (the OGM could carry no changes)
1495 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496 spin_lock_bh(&orig_node->tt_buff_lock);
1497 if (tt_buff_len > 0) {
1498 kfree(orig_node->tt_buff);
1499 orig_node->tt_buff_len = 0;
1500 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1501 if (orig_node->tt_buff) {
1502 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1503 orig_node->tt_buff_len = tt_buff_len;
1504 }
1505 }
1506 spin_unlock_bh(&orig_node->tt_buff_lock);
1507}
1508
Sven Eckelmann56303d32012-06-05 22:31:31 +02001509static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001511 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001512
Sven Eckelmann807736f2012-07-15 22:26:51 +02001513 spin_lock_bh(&bat_priv->tt.req_list_lock);
1514 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001515 if (batadv_has_timed_out(node->issued_at,
1516 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001517 list_del(&node->list);
1518 kfree(node);
1519 }
1520 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001521 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001522}
1523
1524/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001525 * has already been issued for this orig_node, NULL otherwise
1526 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001527static struct batadv_tt_req_node *
1528batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1529 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001531 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001532
Sven Eckelmann807736f2012-07-15 22:26:51 +02001533 spin_lock_bh(&bat_priv->tt.req_list_lock);
1534 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001535 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1536 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001537 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001538 goto unlock;
1539 }
1540
1541 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1542 if (!tt_req_node)
1543 goto unlock;
1544
1545 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1546 tt_req_node->issued_at = jiffies;
1547
Sven Eckelmann807736f2012-07-15 22:26:51 +02001548 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001549unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001550 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001551 return tt_req_node;
1552}
1553
Marek Lindner335fbe02013-04-23 21:40:02 +08001554/**
1555 * batadv_tt_local_valid - verify that given tt entry is a valid one
1556 * @entry_ptr: to be checked local tt entry
1557 * @data_ptr: not used but definition required to satisfy the callback prototype
1558 *
1559 * Returns 1 if the entry is a valid, 0 otherwise.
1560 */
1561static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001562{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001563 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001564
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001565 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001566 return 0;
1567 return 1;
1568}
1569
Sven Eckelmanna5130882012-05-16 20:23:16 +02001570static int batadv_tt_global_valid(const void *entry_ptr,
1571 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001572{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001573 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1574 const struct batadv_tt_global_entry *tt_global_entry;
1575 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001577 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1578 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001579 return 0;
1580
Sven Eckelmann56303d32012-06-05 22:31:31 +02001581 tt_global_entry = container_of(tt_common_entry,
1582 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001583 common);
1584
Sven Eckelmanna5130882012-05-16 20:23:16 +02001585 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586}
1587
Marek Lindner335fbe02013-04-23 21:40:02 +08001588/**
1589 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1590 * tt entries from the specified tt hash
1591 * @bat_priv: the bat priv with all the soft interface information
1592 * @hash: hash table containing the tt entries
1593 * @tt_len: expected tvlv tt data buffer length in number of bytes
1594 * @valid_cb: function to filter tt change entries
1595 * @cb_data: data passed to the filter function as argument
1596 *
1597 * Returns pointer to allocated tvlv tt data buffer if operation was
1598 * successful or NULL otherwise.
1599 */
1600static struct batadv_tvlv_tt_data *
1601batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1602 struct batadv_hashtable *hash, uint16_t tt_len,
1603 int (*valid_cb)(const void *, const void *),
1604 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001605{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001606 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001607 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1608 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001609 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001610 uint16_t tt_tot, tt_num_entries = 0;
1611 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001612 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613
Marek Lindner335fbe02013-04-23 21:40:02 +08001614 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1615 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1616 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001618
Marek Lindner335fbe02013-04-23 21:40:02 +08001619 tt_tot = tt_len / sizeof(struct batadv_tvlv_tt_change);
1620
1621 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1622 GFP_ATOMIC);
1623 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 goto out;
1625
Marek Lindner335fbe02013-04-23 21:40:02 +08001626 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001627
1628 rcu_read_lock();
1629 for (i = 0; i < hash->size; i++) {
1630 head = &hash->table[i];
1631
Sasha Levinb67bfe02013-02-27 17:06:00 -08001632 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001633 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001634 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 break;
1636
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001637 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001638 continue;
1639
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001640 memcpy(tt_change->addr, tt_common_entry->addr,
1641 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001642 tt_change->flags = tt_common_entry->flags;
Marek Lindner335fbe02013-04-23 21:40:02 +08001643 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644
Marek Lindner335fbe02013-04-23 21:40:02 +08001645 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001646 tt_change++;
1647 }
1648 }
1649 rcu_read_unlock();
1650
1651out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001652 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653}
1654
Antonio Quartulliced72932013-04-24 16:37:51 +02001655/**
1656 * batadv_send_tt_request - send a TT Request message to a given node
1657 * @bat_priv: the bat priv with all the soft interface information
1658 * @dst_orig_node: the destination of the message
1659 * @ttvn: the version number that the source of the message is looking for
1660 * @tt_crc: the CRC associated with the version number
1661 * @full_table: ask for the entire translation table if true, while only for the
1662 * last TT diff otherwise
1663 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001664static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1665 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001666 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001667 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668{
Marek Lindner335fbe02013-04-23 21:40:02 +08001669 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001670 struct batadv_hard_iface *primary_if;
1671 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001672 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001674 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001675 if (!primary_if)
1676 goto out;
1677
1678 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001679 * reply from the same orig_node yet
1680 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001681 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682 if (!tt_req_node)
1683 goto out;
1684
Marek Lindner335fbe02013-04-23 21:40:02 +08001685 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1686 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001687 goto out;
1688
Marek Lindner335fbe02013-04-23 21:40:02 +08001689 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1690 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001691 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692
1693 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001694 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001695
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001696 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001697 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001699 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001700 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1701 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1702 tvlv_tt_data, sizeof(*tvlv_tt_data));
1703 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704
1705out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001707 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001709 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001710 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001711 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712 kfree(tt_req_node);
1713 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001714 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 return ret;
1716}
1717
Marek Lindner335fbe02013-04-23 21:40:02 +08001718/**
1719 * batadv_send_other_tt_response - send reply to tt request concerning another
1720 * node's translation table
1721 * @bat_priv: the bat priv with all the soft interface information
1722 * @tt_data: tt data containing the tt request information
1723 * @req_src: mac address of tt request sender
1724 * @req_dst: mac address of tt request recipient
1725 *
1726 * Returns true if tt request reply was sent, false otherwise.
1727 */
1728static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1729 struct batadv_tvlv_tt_data *tt_data,
1730 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001731{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001732 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001733 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001734 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1735 uint8_t orig_ttvn, req_ttvn;
1736 uint16_t tt_len;
1737 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001738
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001739 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001740 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001741 req_src, tt_data->ttvn, req_dst,
1742 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743
1744 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001745 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746 if (!req_dst_orig_node)
1747 goto out;
1748
Marek Lindner335fbe02013-04-23 21:40:02 +08001749 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750 if (!res_dst_orig_node)
1751 goto out;
1752
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001754 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755
Marek Lindner335fbe02013-04-23 21:40:02 +08001756 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001758 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 goto out;
1760
Antonio Quartulli015758d2011-07-09 17:52:13 +02001761 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001762 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 !req_dst_orig_node->tt_buff)
1764 full_table = true;
1765 else
1766 full_table = false;
1767
Marek Lindner335fbe02013-04-23 21:40:02 +08001768 /* TT fragmentation hasn't been implemented yet, so send as many
1769 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001770 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 if (!full_table) {
1772 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1773 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774
Marek Lindner335fbe02013-04-23 21:40:02 +08001775 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1776 GFP_ATOMIC);
1777 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001778 goto unlock;
1779
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001781 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1784 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001785 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001786 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001787
Marek Lindner335fbe02013-04-23 21:40:02 +08001788 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1789 bat_priv->tt.global_hash,
1790 tt_len,
1791 batadv_tt_global_valid,
1792 req_dst_orig_node);
1793 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795 }
1796
Marek Lindner335fbe02013-04-23 21:40:02 +08001797 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1798 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001799
1800 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001801 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001802
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001803 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001804 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1805 res_dst_orig_node->orig, req_dst_orig_node->orig,
1806 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001807
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001808 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001809
Marek Lindner335fbe02013-04-23 21:40:02 +08001810 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1811 req_src, BATADV_TVLV_TT, 1,
1812 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001813
Marek Lindner335fbe02013-04-23 21:40:02 +08001814 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 goto out;
1816
1817unlock:
1818 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1819
1820out:
1821 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001822 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001824 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001825 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001826 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827}
Sven Eckelmann96412692012-06-05 22:31:30 +02001828
Marek Lindner335fbe02013-04-23 21:40:02 +08001829/**
1830 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1831 * translation table
1832 * @bat_priv: the bat priv with all the soft interface information
1833 * @tt_data: tt data containing the tt request information
1834 * @req_src: mac address of tt request sender
1835 *
1836 * Returns true if tt request reply was sent, false otherwise.
1837 */
1838static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1839 struct batadv_tvlv_tt_data *tt_data,
1840 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841{
Marek Lindner335fbe02013-04-23 21:40:02 +08001842 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02001843 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001844 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001845 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001846 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08001847 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001848
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001849 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001850 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001851 req_src, tt_data->ttvn,
1852 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853
1854
Sven Eckelmann807736f2012-07-15 22:26:51 +02001855 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001856 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001857
Marek Lindner335fbe02013-04-23 21:40:02 +08001858 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859 if (!orig_node)
1860 goto out;
1861
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001862 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863 if (!primary_if)
1864 goto out;
1865
1866 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001867 * is too big send the whole local translation table
1868 */
Marek Lindner335fbe02013-04-23 21:40:02 +08001869 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001870 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001871 full_table = true;
1872 else
1873 full_table = false;
1874
Marek Lindner335fbe02013-04-23 21:40:02 +08001875 /* TT fragmentation hasn't been implemented yet, so send as many
1876 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001877 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001878 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001879 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1880 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881
Marek Lindner335fbe02013-04-23 21:40:02 +08001882 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1883 GFP_ATOMIC);
1884 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001885 goto unlock;
1886
Marek Lindner335fbe02013-04-23 21:40:02 +08001887 /* Copy the last orig_node's OGM buffer */
1888 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001889 bat_priv->tt.last_changeset_len);
1890 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001892 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08001893 tt_len = batadv_tt_len(tt_len);
1894 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895
Marek Lindner335fbe02013-04-23 21:40:02 +08001896 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1897 bat_priv->tt.local_hash,
1898 tt_len,
1899 batadv_tt_local_valid,
1900 NULL);
1901 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903 }
1904
Marek Lindner335fbe02013-04-23 21:40:02 +08001905 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1906 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001907
1908 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001909 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001910
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001911 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001912 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1913 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001915 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001916
Marek Lindner335fbe02013-04-23 21:40:02 +08001917 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1918 req_src, BATADV_TVLV_TT, 1,
1919 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1920
Antonio Quartullia73105b2011-04-27 14:27:44 +02001921 goto out;
1922
1923unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001924 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925out:
1926 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001927 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001929 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08001930 kfree(tvlv_tt_data);
1931 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 return true;
1933}
1934
Marek Lindner335fbe02013-04-23 21:40:02 +08001935/**
1936 * batadv_send_tt_response - send reply to tt request
1937 * @bat_priv: the bat priv with all the soft interface information
1938 * @tt_data: tt data containing the tt request information
1939 * @req_src: mac address of tt request sender
1940 * @req_dst: mac address of tt request recipient
1941 *
1942 * Returns true if tt request reply was sent, false otherwise.
1943 */
1944static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1945 struct batadv_tvlv_tt_data *tt_data,
1946 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001947{
Marek Lindner335fbe02013-04-23 21:40:02 +08001948 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001949 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08001950 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001951 return true;
1952
Marek Lindner335fbe02013-04-23 21:40:02 +08001953 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001954 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08001955 return batadv_send_other_tt_response(bat_priv, tt_data,
1956 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001957 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958}
1959
Sven Eckelmann56303d32012-06-05 22:31:31 +02001960static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1961 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08001962 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001963 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964{
1965 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001966 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967
1968 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001969 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1970 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001971 batadv_tt_global_del(bat_priv, orig_node,
1972 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001973 "tt removed by changes",
1974 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001975 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001976 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001977 (tt_change + i)->addr,
1978 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001979 /* In case of problem while storing a
1980 * global_entry, we stop the updating
1981 * procedure without committing the
1982 * ttvn change. This will avoid to send
1983 * corrupted data on tt_request
1984 */
1985 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001986 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001987 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001988 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001989}
1990
Sven Eckelmann56303d32012-06-05 22:31:31 +02001991static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001992 struct batadv_tvlv_tt_data *tt_data,
1993 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001994{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001995 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996
Marek Lindner335fbe02013-04-23 21:40:02 +08001997 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998 if (!orig_node)
1999 goto out;
2000
2001 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002002 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003
Sven Eckelmanna5130882012-05-16 20:23:16 +02002004 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002005 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2006 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007
2008 spin_lock_bh(&orig_node->tt_buff_lock);
2009 kfree(orig_node->tt_buff);
2010 orig_node->tt_buff_len = 0;
2011 orig_node->tt_buff = NULL;
2012 spin_unlock_bh(&orig_node->tt_buff_lock);
2013
Marek Lindner335fbe02013-04-23 21:40:02 +08002014 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002015
2016out:
2017 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002018 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019}
2020
Sven Eckelmann56303d32012-06-05 22:31:31 +02002021static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2022 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002023 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002024 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002026 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2027 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028
Sven Eckelmanna5130882012-05-16 20:23:16 +02002029 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2030 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031 atomic_set(&orig_node->last_ttvn, ttvn);
2032}
2033
Sven Eckelmann56303d32012-06-05 22:31:31 +02002034bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002035{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002036 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002037 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002038
Sven Eckelmanna5130882012-05-16 20:23:16 +02002039 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002040 if (!tt_local_entry)
2041 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002042 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002043 * consistency purpose)
2044 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002045 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2046 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002047 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002048 ret = true;
2049out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002051 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002052 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002053}
2054
Marek Lindner335fbe02013-04-23 21:40:02 +08002055/**
2056 * batadv_handle_tt_response - process incoming tt reply
2057 * @bat_priv: the bat priv with all the soft interface information
2058 * @tt_data: tt data containing the tt request information
2059 * @resp_src: mac address of tt reply sender
2060 * @num_entries: number of tt change entries appended to the tt data
2061 */
2062static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2063 struct batadv_tvlv_tt_data *tt_data,
2064 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002065{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002066 struct batadv_tt_req_node *node, *safe;
2067 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002068 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002069
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002070 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002071 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002072 resp_src, tt_data->ttvn, num_entries,
2073 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002074
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002075 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002076 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002077 goto out;
2078
Marek Lindner335fbe02013-04-23 21:40:02 +08002079 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080 if (!orig_node)
2081 goto out;
2082
Marek Lindner335fbe02013-04-23 21:40:02 +08002083 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2084 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002085 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002086 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2087 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2088 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002089 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002090
2091 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002092 spin_lock_bh(&bat_priv->tt.req_list_lock);
2093 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002094 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002095 continue;
2096 list_del(&node->list);
2097 kfree(node);
2098 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002099 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002100
2101 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002102 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002103out:
2104 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002105 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106}
2107
Sven Eckelmann56303d32012-06-05 22:31:31 +02002108static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002109{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002110 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111
Sven Eckelmann807736f2012-07-15 22:26:51 +02002112 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002113
Sven Eckelmann807736f2012-07-15 22:26:51 +02002114 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002115 list_del(&node->list);
2116 kfree(node);
2117 }
2118
Sven Eckelmann807736f2012-07-15 22:26:51 +02002119 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002120}
2121
Sven Eckelmann56303d32012-06-05 22:31:31 +02002122static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002123{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002124 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002125
Sven Eckelmann807736f2012-07-15 22:26:51 +02002126 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2127 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002128 if (!batadv_has_timed_out(node->first_time,
2129 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002130 continue;
2131
2132 list_del(&node->list);
2133 kfree(node);
2134 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002135 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002136}
2137
2138/* This function checks whether the client already reached the
2139 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2140 * will not be sent.
2141 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002142 * returns true if the ROAMING_ADV can be sent, false otherwise
2143 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002144static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002145 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002146{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002147 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002148 bool ret = false;
2149
Sven Eckelmann807736f2012-07-15 22:26:51 +02002150 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002151 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002152 * reply from the same orig_node yet
2153 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002154 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002155 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002156 continue;
2157
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002158 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002159 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002160 continue;
2161
Sven Eckelmann3e348192012-05-16 20:23:22 +02002162 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002163 /* Sorry, you roamed too many times! */
2164 goto unlock;
2165 ret = true;
2166 break;
2167 }
2168
2169 if (!ret) {
2170 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2171 if (!tt_roam_node)
2172 goto unlock;
2173
2174 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002175 atomic_set(&tt_roam_node->counter,
2176 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002177 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2178
Sven Eckelmann807736f2012-07-15 22:26:51 +02002179 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002180 ret = true;
2181 }
2182
2183unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002184 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002185 return ret;
2186}
2187
Sven Eckelmann56303d32012-06-05 22:31:31 +02002188static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2189 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002190{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002191 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002192 struct batadv_tvlv_roam_adv tvlv_roam;
2193
2194 primary_if = batadv_primary_if_get_selected(bat_priv);
2195 if (!primary_if)
2196 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002197
2198 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002199 * already roamed to us too many times
2200 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002201 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002202 goto out;
2203
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002204 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002205 "Sending ROAMING_ADV to %pM (client %pM)\n",
2206 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002207
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002208 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002209
Marek Lindner122edaa2013-04-23 21:40:03 +08002210 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2211 tvlv_roam.reserved = 0;
2212
2213 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2214 orig_node->orig, BATADV_TVLV_ROAM, 1,
2215 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002216
2217out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002218 if (primary_if)
2219 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002220}
2221
Sven Eckelmanna5130882012-05-16 20:23:16 +02002222static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002223{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002224 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002225 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002226 struct batadv_priv *bat_priv;
2227
2228 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002229 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2230 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231
Sven Eckelmanna5130882012-05-16 20:23:16 +02002232 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002233 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002234 batadv_tt_req_purge(bat_priv);
2235 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002236
Antonio Quartulli72414442012-12-25 13:14:37 +01002237 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2238 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002239}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002240
Sven Eckelmann56303d32012-06-05 22:31:31 +02002241void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002242{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002243 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2244 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2245
Sven Eckelmann807736f2012-07-15 22:26:51 +02002246 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002247
Sven Eckelmanna5130882012-05-16 20:23:16 +02002248 batadv_tt_local_table_free(bat_priv);
2249 batadv_tt_global_table_free(bat_priv);
2250 batadv_tt_req_list_free(bat_priv);
2251 batadv_tt_changes_list_free(bat_priv);
2252 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002253
Sven Eckelmann807736f2012-07-15 22:26:51 +02002254 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002255}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002256
Antonio Quartulli697f2532011-11-07 16:47:01 +01002257/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002258 * in the given hash table and returns the number of modified entries
2259 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002260static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2261 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002262{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002263 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002264 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002265 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002266 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002267
2268 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002269 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002270
2271 for (i = 0; i < hash->size; i++) {
2272 head = &hash->table[i];
2273
2274 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002275 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002276 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002277 if (enable) {
2278 if ((tt_common_entry->flags & flags) == flags)
2279 continue;
2280 tt_common_entry->flags |= flags;
2281 } else {
2282 if (!(tt_common_entry->flags & flags))
2283 continue;
2284 tt_common_entry->flags &= ~flags;
2285 }
2286 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002287 }
2288 rcu_read_unlock();
2289 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002290out:
2291 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002292}
2293
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002294/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002295static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002296{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002297 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002298 struct batadv_tt_common_entry *tt_common;
2299 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002300 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002301 struct hlist_head *head;
2302 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002303 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002304
2305 if (!hash)
2306 return;
2307
2308 for (i = 0; i < hash->size; i++) {
2309 head = &hash->table[i];
2310 list_lock = &hash->list_locks[i];
2311
2312 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002313 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002314 hash_entry) {
2315 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002316 continue;
2317
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002318 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002319 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002320 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002321
Sven Eckelmann807736f2012-07-15 22:26:51 +02002322 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002323 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002324 tt_local = container_of(tt_common,
2325 struct batadv_tt_local_entry,
2326 common);
2327 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002328 }
2329 spin_unlock_bh(list_lock);
2330 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002331}
2332
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002333/**
2334 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2335 * have been queued in the time since the last commit
2336 * @bat_priv: the bat priv with all the soft interface information
2337 */
2338void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002339{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002340 uint16_t changed_num = 0;
2341
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002342 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2343 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2344 batadv_tt_tvlv_container_update(bat_priv);
2345 return;
2346 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002347
Sven Eckelmann807736f2012-07-15 22:26:51 +02002348 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002349 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002350
2351 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002352 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002353 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002354 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002355
2356 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002357 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002358 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002359 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002360 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002361
2362 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002363 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002364 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002365}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002366
Sven Eckelmann56303d32012-06-05 22:31:31 +02002367bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002368 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002369{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002370 struct batadv_tt_local_entry *tt_local_entry = NULL;
2371 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002372 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002373
2374 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002375 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002376
Sven Eckelmanna5130882012-05-16 20:23:16 +02002377 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002378 if (!tt_local_entry)
2379 goto out;
2380
Sven Eckelmanna5130882012-05-16 20:23:16 +02002381 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002382 if (!tt_global_entry)
2383 goto out;
2384
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002385 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002386 goto out;
2387
Marek Lindner5870adc2012-06-20 17:16:05 +02002388 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002389
2390out:
2391 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002392 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002393 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002394 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002395 return ret;
2396}
Marek Lindnera943cac2011-07-30 13:10:18 +02002397
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002398/**
2399 * batadv_tt_update_orig - update global translation table with new tt
2400 * information received via ogms
2401 * @bat_priv: the bat priv with all the soft interface information
2402 * @orig: the orig_node of the ogm
2403 * @tt_buff: buffer holding the tt information
2404 * @tt_num_changes: number of tt changes inside the tt buffer
2405 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002406 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002407 */
2408static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2409 struct batadv_orig_node *orig_node,
2410 const unsigned char *tt_buff,
2411 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002412 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002413{
2414 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2415 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002416 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002417
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002418 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002419 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002420 return;
2421
Antonio Quartulli17071572011-11-07 16:36:40 +01002422 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002423 * increased by one -> we can apply the attached changes
2424 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002425 if ((!orig_node->tt_initialised && ttvn == 1) ||
2426 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002427 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002428 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2429 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002430 * In this case send a tt request
2431 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002432 if (!tt_num_changes) {
2433 full_table = false;
2434 goto request_table;
2435 }
2436
Marek Lindner335fbe02013-04-23 21:40:02 +08002437 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002438 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002439 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002440
2441 /* Even if we received the precomputed crc with the OGM, we
2442 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002443 * in the global table
2444 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002445 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002446
2447 /* The ttvn alone is not enough to guarantee consistency
2448 * because a single value could represent different states
2449 * (due to the wrap around). Thus a node has to check whether
2450 * the resulting table (after applying the changes) is still
2451 * consistent or not. E.g. a node could disconnect while its
2452 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2453 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002454 * inconsistency
2455 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002456 if (orig_node->tt_crc != tt_crc)
2457 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002458 } else {
2459 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002460 * in sync anymore -> request fresh tt data
2461 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002462 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2463 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002464request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002465 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002466 "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 +02002467 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2468 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002469 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2470 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002471 return;
2472 }
2473 }
2474}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002475
2476/* returns true whether we know that the client has moved from its old
2477 * originator to another one. This entry is kept is still kept for consistency
2478 * purposes
2479 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002480bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002481 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002482{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002483 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002484 bool ret = false;
2485
Sven Eckelmanna5130882012-05-16 20:23:16 +02002486 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002487 if (!tt_global_entry)
2488 goto out;
2489
Antonio Quartullic1d07432013-01-15 22:17:19 +10002490 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002491 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002492out:
2493 return ret;
2494}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002495
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002496/**
2497 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2498 * @bat_priv: the bat priv with all the soft interface information
2499 * @addr: the MAC address of the local client to query
2500 *
2501 * Returns true if the local client is known to be roaming (it is not served by
2502 * this node anymore) or not. If yes, the client is still present in the table
2503 * to keep the latter consistent with the node TTVN
2504 */
2505bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2506 uint8_t *addr)
2507{
2508 struct batadv_tt_local_entry *tt_local_entry;
2509 bool ret = false;
2510
2511 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2512 if (!tt_local_entry)
2513 goto out;
2514
2515 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2516 batadv_tt_local_entry_free_ref(tt_local_entry);
2517out:
2518 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002519}
2520
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002521bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2522 struct batadv_orig_node *orig_node,
2523 const unsigned char *addr)
2524{
2525 bool ret = false;
2526
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002527 /* if the originator is a backbone node (meaning it belongs to the same
2528 * LAN of this node) the temporary client must not be added because to
2529 * reach such destination the node must use the LAN instead of the mesh
2530 */
2531 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2532 goto out;
2533
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002534 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2535 BATADV_TT_CLIENT_TEMP,
2536 atomic_read(&orig_node->last_ttvn)))
2537 goto out;
2538
2539 batadv_dbg(BATADV_DBG_TT, bat_priv,
2540 "Added temporary global client (addr: %pM orig: %pM)\n",
2541 addr, orig_node->orig);
2542 ret = true;
2543out:
2544 return ret;
2545}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002546
2547/**
2548 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2549 * @bat_priv: the bat priv with all the soft interface information
2550 * @orig: the orig_node of the ogm
2551 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2552 * @tvlv_value: tvlv buffer containing the gateway data
2553 * @tvlv_value_len: tvlv buffer length
2554 */
2555static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2556 struct batadv_orig_node *orig,
2557 uint8_t flags,
2558 void *tvlv_value,
2559 uint16_t tvlv_value_len)
2560{
2561 struct batadv_tvlv_tt_data *tt_data;
2562 uint16_t num_entries;
2563
2564 if (tvlv_value_len < sizeof(*tt_data))
2565 return;
2566
2567 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2568 tvlv_value_len -= sizeof(*tt_data);
2569
2570 num_entries = tvlv_value_len / batadv_tt_len(1);
2571
2572 batadv_tt_update_orig(bat_priv, orig,
2573 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002574 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002575}
2576
2577/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002578 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2579 * container
2580 * @bat_priv: the bat priv with all the soft interface information
2581 * @src: mac address of tt tvlv sender
2582 * @dst: mac address of tt tvlv recipient
2583 * @tvlv_value: tvlv buffer containing the tt data
2584 * @tvlv_value_len: tvlv buffer length
2585 *
2586 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2587 * otherwise.
2588 */
2589static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2590 uint8_t *src, uint8_t *dst,
2591 void *tvlv_value,
2592 uint16_t tvlv_value_len)
2593{
2594 struct batadv_tvlv_tt_data *tt_data;
2595 uint16_t num_entries;
2596 char tt_flag;
2597 bool ret;
2598
2599 if (tvlv_value_len < sizeof(*tt_data))
2600 return NET_RX_SUCCESS;
2601
2602 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2603 tvlv_value_len -= sizeof(*tt_data);
2604
2605 num_entries = tvlv_value_len / batadv_tt_len(1);
2606
2607 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2608 case BATADV_TT_REQUEST:
2609 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2610
2611 /* If this node cannot provide a TT response the tt_request is
2612 * forwarded
2613 */
2614 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2615 if (!ret) {
2616 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2617 tt_flag = 'F';
2618 else
2619 tt_flag = '.';
2620
2621 batadv_dbg(BATADV_DBG_TT, bat_priv,
2622 "Routing TT_REQUEST to %pM [%c]\n",
2623 dst, tt_flag);
2624 /* tvlv API will re-route the packet */
2625 return NET_RX_DROP;
2626 }
2627 break;
2628 case BATADV_TT_RESPONSE:
2629 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2630
2631 if (batadv_is_my_mac(bat_priv, dst)) {
2632 batadv_handle_tt_response(bat_priv, tt_data,
2633 src, num_entries);
2634 return NET_RX_SUCCESS;
2635 }
2636
2637 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2638 tt_flag = 'F';
2639 else
2640 tt_flag = '.';
2641
2642 batadv_dbg(BATADV_DBG_TT, bat_priv,
2643 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2644
2645 /* tvlv API will re-route the packet */
2646 return NET_RX_DROP;
2647 }
2648
2649 return NET_RX_SUCCESS;
2650}
2651
2652/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002653 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2654 * @bat_priv: the bat priv with all the soft interface information
2655 * @src: mac address of tt tvlv sender
2656 * @dst: mac address of tt tvlv recipient
2657 * @tvlv_value: tvlv buffer containing the tt data
2658 * @tvlv_value_len: tvlv buffer length
2659 *
2660 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2661 * otherwise.
2662 */
2663static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2664 uint8_t *src, uint8_t *dst,
2665 void *tvlv_value,
2666 uint16_t tvlv_value_len)
2667{
2668 struct batadv_tvlv_roam_adv *roaming_adv;
2669 struct batadv_orig_node *orig_node = NULL;
2670
2671 /* If this node is not the intended recipient of the
2672 * roaming advertisement the packet is forwarded
2673 * (the tvlv API will re-route the packet).
2674 */
2675 if (!batadv_is_my_mac(bat_priv, dst))
2676 return NET_RX_DROP;
2677
2678 /* check if it is a backbone gateway. we don't accept
2679 * roaming advertisement from it, as it has the same
2680 * entries as we have.
2681 */
2682 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2683 goto out;
2684
2685 if (tvlv_value_len < sizeof(*roaming_adv))
2686 goto out;
2687
2688 orig_node = batadv_orig_hash_find(bat_priv, src);
2689 if (!orig_node)
2690 goto out;
2691
2692 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2693 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2694
2695 batadv_dbg(BATADV_DBG_TT, bat_priv,
2696 "Received ROAMING_ADV from %pM (client %pM)\n",
2697 src, roaming_adv->client);
2698
2699 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
2700 BATADV_TT_CLIENT_ROAM,
2701 atomic_read(&orig_node->last_ttvn) + 1);
2702
2703out:
2704 if (orig_node)
2705 batadv_orig_node_free_ref(orig_node);
2706 return NET_RX_SUCCESS;
2707}
2708
2709/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002710 * batadv_tt_init - initialise the translation table internals
2711 * @bat_priv: the bat priv with all the soft interface information
2712 *
2713 * Return 0 on success or negative error number in case of failure.
2714 */
2715int batadv_tt_init(struct batadv_priv *bat_priv)
2716{
2717 int ret;
2718
2719 ret = batadv_tt_local_init(bat_priv);
2720 if (ret < 0)
2721 return ret;
2722
2723 ret = batadv_tt_global_init(bat_priv);
2724 if (ret < 0)
2725 return ret;
2726
2727 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002728 batadv_tt_tvlv_unicast_handler_v1,
2729 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002730
Marek Lindner122edaa2013-04-23 21:40:03 +08002731 batadv_tvlv_handler_register(bat_priv, NULL,
2732 batadv_roam_tvlv_unicast_handler_v1,
2733 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2734
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002735 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2736 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2737 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2738
2739 return 1;
2740}