blob: 58636a77de790df192f373e4771e5d50a2b61af7 [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
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200235/**
236 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
237 * @tt_len: available space
238 *
239 * Returns the number of entries.
240 */
241static uint16_t batadv_tt_entries(uint16_t tt_len)
242{
243 return tt_len / batadv_tt_len(1);
244}
245
Sven Eckelmann56303d32012-06-05 22:31:31 +0200246static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200248 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200249 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Sven Eckelmann807736f2012-07-15 22:26:51 +0200251 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252
Sven Eckelmann807736f2012-07-15 22:26:51 +0200253 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200254 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255
Antonio Quartullidec05072012-11-10 11:00:32 +0100256 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
257 &batadv_tt_local_hash_lock_class_key);
258
Sven Eckelmann5346c352012-05-05 13:27:28 +0200259 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260}
261
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200262static void batadv_tt_global_free(struct batadv_priv *bat_priv,
263 struct batadv_tt_global_entry *tt_global,
264 const char *message)
265{
266 batadv_dbg(BATADV_DBG_TT, bat_priv,
267 "Deleting global tt entry %pM: %s\n",
268 tt_global->common.addr, message);
269
270 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
271 batadv_choose_orig, tt_global->common.addr);
272 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200273}
274
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200275void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
276 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200278 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200279 struct batadv_tt_local_entry *tt_local;
280 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200281 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200282 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100283 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200284 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Antonio Quartulli47c94652012-09-23 22:38:35 +0200286 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200287 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
Antonio Quartulli47c94652012-09-23 22:38:35 +0200289 if (tt_local) {
290 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200291 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
292 batadv_dbg(BATADV_DBG_TT, bat_priv,
293 "Re-adding pending client %pM\n", addr);
294 /* whatever the reason why the PENDING flag was set,
295 * this is a client which was enqueued to be removed in
296 * this orig_interval. Since it popped up again, the
297 * flag can be reset like it was never enqueued
298 */
299 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
300 goto add_event;
301 }
302
303 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
304 batadv_dbg(BATADV_DBG_TT, bat_priv,
305 "Roaming client %pM came back to its original location\n",
306 addr);
307 /* the ROAM flag is set because this client roamed away
308 * and the node got a roaming_advertisement message. Now
309 * that the client popped up again at its original
310 * location such flag can be unset
311 */
312 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
313 roamed_back = true;
314 }
315 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 }
317
Antonio Quartulli47c94652012-09-23 22:38:35 +0200318 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
319 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200320 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200321
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200322 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200323 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200324 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Antonio Quartulli47c94652012-09-23 22:38:35 +0200326 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100327 /* The local entry has to be marked as NEW to avoid to send it in
328 * a full table response going out before the next ttvn increment
329 * (consistency check)
330 */
331 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200332 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200333 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
334 atomic_set(&tt_local->common.refcount, 2);
335 tt_local->last_seen = jiffies;
336 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
338 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200339 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200340 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
Sven Eckelmann807736f2012-07-15 22:26:51 +0200342 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200343 batadv_choose_orig, &tt_local->common,
344 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100345
346 if (unlikely(hash_added != 0)) {
347 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200348 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100349 goto out;
350 }
351
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200352add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200353 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200354
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200355check_roaming:
356 /* Check whether it is a roaming, but don't do anything if the roaming
357 * process has already been handled
358 */
359 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200360 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200361 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200362 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800363 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200364 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200365 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200366 }
367 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200368 if (roamed_back) {
369 batadv_tt_global_free(bat_priv, tt_global,
370 "Roaming canceled");
371 tt_global = NULL;
372 } else {
373 /* The global entry has to be marked as ROAMING and
374 * has to be kept for consistency purpose
375 */
376 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
377 tt_global->roam_at = jiffies;
378 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200379 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200380
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200381out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200382 if (tt_local)
383 batadv_tt_local_entry_free_ref(tt_local);
384 if (tt_global)
385 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386}
387
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800388/**
389 * batadv_tt_tvlv_container_update - update the translation table tvlv container
390 * after local tt changes have been committed
391 * @bat_priv: the bat priv with all the soft interface information
392 */
393static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800395 struct batadv_tt_change_node *entry, *safe;
396 struct batadv_tvlv_tt_data *tt_data;
397 struct batadv_tvlv_tt_change *tt_change;
398 int tt_diff_len = 0, tt_change_len = 0;
399 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800401 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800402
403 /* if we have too many changes for one packet don't send any
404 * and wait for the tt table request which will be fragmented
405 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800406 if (tt_diff_len > bat_priv->soft_iface->mtu)
407 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800408
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800409 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
410 if (!tt_data)
411 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800412
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800413 tt_data->flags = BATADV_TT_OGM_DIFF;
414 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
Antonio Quartulliced72932013-04-24 16:37:51 +0200415 tt_data->crc = htonl(bat_priv->tt.local_crc);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800416
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800417 if (tt_diff_len == 0)
418 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800419
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200420 tt_diff_entries_num = batadv_tt_entries(tt_diff_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
Sven Eckelmann807736f2012-07-15 22:26:51 +0200422 spin_lock_bh(&bat_priv->tt.changes_list_lock);
423 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800425 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
426
Sven Eckelmann807736f2012-07-15 22:26:51 +0200427 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100428 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800429 if (tt_diff_entries_count < tt_diff_entries_num) {
430 memcpy(tt_change + tt_diff_entries_count,
431 &entry->change,
432 sizeof(struct batadv_tvlv_tt_change));
433 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200435 list_del(&entry->list);
436 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200438 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200441 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
442 kfree(bat_priv->tt.last_changeset);
443 bat_priv->tt.last_changeset_len = 0;
444 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800445 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800446 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800447 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800448 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200449 * instead of providing the diff
450 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800451 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200452 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800453 memcpy(bat_priv->tt.last_changeset,
454 tt_change, tt_change_len);
455 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 }
457 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200458 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800460container_register:
461 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
462 sizeof(*tt_data) + tt_change_len);
463 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464}
465
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200466int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467{
468 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200470 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200471 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100472 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200473 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200475 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100476 int last_seen_secs;
477 int last_seen_msecs;
478 unsigned long last_seen_jiffies;
479 bool no_purge;
480 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481
Marek Lindner30da63a2012-08-03 17:15:46 +0200482 primary_if = batadv_seq_print_text_primary_if_get(seq);
483 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200484 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100486 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200487 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100488 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
489 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100490 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
491 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 for (i = 0; i < hash->size; i++) {
494 head = &hash->table[i];
495
Marek Lindner7aadf882011-02-18 12:28:09 +0000496 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800497 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000498 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100499 tt_local = container_of(tt_common_entry,
500 struct batadv_tt_local_entry,
501 common);
502 last_seen_jiffies = jiffies - tt_local->last_seen;
503 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
504 last_seen_secs = last_seen_msecs / 1000;
505 last_seen_msecs = last_seen_msecs % 1000;
506
507 no_purge = tt_common_entry->flags & np_flag;
508
509 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100510 tt_common_entry->addr,
511 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100513 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100514 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200515 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100516 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200517 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100518 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100519 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100520 no_purge ? 0 : last_seen_secs,
521 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000523 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200525out:
526 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200527 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200528 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529}
530
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531static void
532batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
533 struct batadv_tt_local_entry *tt_local_entry,
534 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200536 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537
Antonio Quartulli015758d2011-07-09 17:52:13 +0200538 /* The local client has to be marked as "pending to be removed" but has
539 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200540 * response issued before the net ttvn increment (consistency check)
541 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200542 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100543
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200544 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200545 "Local tt entry (%pM) pending to be removed: %s\n",
546 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547}
548
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200549/**
550 * batadv_tt_local_remove - logically remove an entry from the local table
551 * @bat_priv: the bat priv with all the soft interface information
552 * @addr: the MAC address of the client to remove
553 * @message: message to append to the log on deletion
554 * @roaming: true if the deletion is due to a roaming event
555 *
556 * Returns the flags assigned to the local entry before being deleted
557 */
558uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
559 const uint8_t *addr, const char *message,
560 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200562 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200563 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
Sven Eckelmanna5130882012-05-16 20:23:16 +0200565 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200566 if (!tt_local_entry)
567 goto out;
568
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200569 curr_flags = tt_local_entry->common.flags;
570
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200571 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200572 /* if this global entry addition is due to a roaming, the node has to
573 * mark the local entry as "roamed" in order to correctly reroute
574 * packets later
575 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200576 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200577 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200578 /* mark the local client as ROAMed */
579 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
580 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200581
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200582 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
583 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
584 message);
585 goto out;
586 }
587 /* if this client has been added right now, it is possible to
588 * immediately purge it
589 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200590 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200591 hlist_del_rcu(&tt_local_entry->common.hash_entry);
592 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200593
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200594out:
595 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200596 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200597
598 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599}
600
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200602 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200604 struct batadv_tt_local_entry *tt_local_entry;
605 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800606 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200607
Sasha Levinb67bfe02013-02-27 17:06:00 -0800608 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200609 hash_entry) {
610 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200611 struct batadv_tt_local_entry,
612 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200613 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
614 continue;
615
616 /* entry already marked for deletion */
617 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
618 continue;
619
620 if (!batadv_has_timed_out(tt_local_entry->last_seen,
621 BATADV_TT_LOCAL_TIMEOUT))
622 continue;
623
624 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
625 BATADV_TT_CLIENT_DEL, "timed out");
626 }
627}
628
Sven Eckelmann56303d32012-06-05 22:31:31 +0200629static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200630{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200631 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200633 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200634 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 for (i = 0; i < hash->size; i++) {
637 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200641 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200642 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644}
645
Sven Eckelmann56303d32012-06-05 22:31:31 +0200646static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200648 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200649 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200650 struct batadv_tt_common_entry *tt_common_entry;
651 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800652 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200653 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200654 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200655
Sven Eckelmann807736f2012-07-15 22:26:51 +0200656 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 return;
658
Sven Eckelmann807736f2012-07-15 22:26:51 +0200659 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200660
661 for (i = 0; i < hash->size; i++) {
662 head = &hash->table[i];
663 list_lock = &hash->list_locks[i];
664
665 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800666 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200667 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800668 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200669 tt_local = container_of(tt_common_entry,
670 struct batadv_tt_local_entry,
671 common);
672 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200673 }
674 spin_unlock_bh(list_lock);
675 }
676
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200677 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200678
Sven Eckelmann807736f2012-07-15 22:26:51 +0200679 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680}
681
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200684 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200685 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686
Sven Eckelmann807736f2012-07-15 22:26:51 +0200687 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688
Sven Eckelmann807736f2012-07-15 22:26:51 +0200689 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200690 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691
Antonio Quartullidec05072012-11-10 11:00:32 +0100692 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
693 &batadv_tt_global_hash_lock_class_key);
694
Sven Eckelmann5346c352012-05-05 13:27:28 +0200695 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696}
697
Sven Eckelmann56303d32012-06-05 22:31:31 +0200698static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200699{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200700 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701
Sven Eckelmann807736f2012-07-15 22:26:51 +0200702 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703
Sven Eckelmann807736f2012-07-15 22:26:51 +0200704 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705 list) {
706 list_del(&entry->list);
707 kfree(entry);
708 }
709
Sven Eckelmann807736f2012-07-15 22:26:51 +0200710 atomic_set(&bat_priv->tt.local_changes, 0);
711 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200712}
713
Antonio Quartullid657e622012-07-01 14:09:12 +0200714/* retrieves the orig_tt_list_entry belonging to orig_node from the
715 * batadv_tt_global_entry list
716 *
717 * returns it with an increased refcounter, NULL if not found
718 */
719static struct batadv_tt_orig_list_entry *
720batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
721 const struct batadv_orig_node *orig_node)
722{
723 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
724 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200725
726 rcu_read_lock();
727 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800728 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200729 if (tmp_orig_entry->orig_node != orig_node)
730 continue;
731 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
732 continue;
733
734 orig_entry = tmp_orig_entry;
735 break;
736 }
737 rcu_read_unlock();
738
739 return orig_entry;
740}
741
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200742/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200743 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200744 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200745static bool
746batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
747 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200748{
Antonio Quartullid657e622012-07-01 14:09:12 +0200749 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200750 bool found = false;
751
Antonio Quartullid657e622012-07-01 14:09:12 +0200752 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
753 if (orig_entry) {
754 found = true;
755 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200756 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200757
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200758 return found;
759}
760
Sven Eckelmanna5130882012-05-16 20:23:16 +0200761static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200762batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200763 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200764{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200765 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200766
Antonio Quartullid657e622012-07-01 14:09:12 +0200767 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200768 if (orig_entry) {
769 /* refresh the ttvn: the current value could be a bogus one that
770 * was added during a "temporary client detection"
771 */
772 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200773 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200774 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200775
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200776 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
777 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200778 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779
780 INIT_HLIST_NODE(&orig_entry->list);
781 atomic_inc(&orig_node->refcount);
782 atomic_inc(&orig_node->tt_size);
783 orig_entry->orig_node = orig_node;
784 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200785 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200786
Antonio Quartullid657e622012-07-01 14:09:12 +0200787 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200788 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200789 &tt_global->orig_list);
790 spin_unlock_bh(&tt_global->list_lock);
791out:
792 if (orig_entry)
793 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200794}
795
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200796/**
797 * batadv_tt_global_add - add a new TT global entry or update an existing one
798 * @bat_priv: the bat priv with all the soft interface information
799 * @orig_node: the originator announcing the client
800 * @tt_addr: the mac address of the non-mesh client
801 * @flags: TT flags that have to be set for this non-mesh client
802 * @ttvn: the tt version number ever announcing this non-mesh client
803 *
804 * Add a new TT global entry for the given originator. If the entry already
805 * exists add a new reference to the given originator (a global entry can have
806 * references to multiple originators) and adjust the flags attribute to reflect
807 * the function argument.
808 * If a TT local entry exists for this non-mesh client remove it.
809 *
810 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200811 *
812 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200813 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200814static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
815 struct batadv_orig_node *orig_node,
816 const unsigned char *tt_addr, uint16_t flags,
817 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000818{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200819 struct batadv_tt_global_entry *tt_global_entry;
820 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200821 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100822 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200823 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200824 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825
Sven Eckelmanna5130882012-05-16 20:23:16 +0200826 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200827 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
828
829 /* if the node already has a local client for this entry, it has to wait
830 * for a roaming advertisement instead of manually messing up the global
831 * table
832 */
833 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
834 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
835 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836
Antonio Quartullia73105b2011-04-27 14:27:44 +0200837 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200838 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200839 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200840 goto out;
841
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200842 common = &tt_global_entry->common;
843 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200844
Antonio Quartullid4f44692012-05-25 00:00:54 +0200845 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200846 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200847 /* node must store current time in case of roaming. This is
848 * needed to purge this entry out on timeout (if nobody claims
849 * it)
850 */
851 if (flags & BATADV_TT_CLIENT_ROAM)
852 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200853 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200854 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200855
856 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
857 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200858
Sven Eckelmann807736f2012-07-15 22:26:51 +0200859 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200860 batadv_compare_tt,
861 batadv_choose_orig, common,
862 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100863
864 if (unlikely(hash_added != 0)) {
865 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200866 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100867 goto out_remove;
868 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200869 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200870 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200871 /* If there is already a global entry, we can use this one for
872 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200873 * But if we are trying to add a temporary client then here are
874 * two options at this point:
875 * 1) the global client is not a temporary client: the global
876 * client has to be left as it is, temporary information
877 * should never override any already known client state
878 * 2) the global client is a temporary client: purge the
879 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200880 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200881 if (flags & BATADV_TT_CLIENT_TEMP) {
882 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
883 goto out;
884 if (batadv_tt_global_entry_has_orig(tt_global_entry,
885 orig_node))
886 goto out_remove;
887 batadv_tt_global_del_orig_list(tt_global_entry);
888 goto add_orig_entry;
889 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200890
891 /* if the client was temporary added before receiving the first
892 * OGM announcing it, we have to clear the TEMP flag
893 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200894 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200895
Antonio Quartullie9c001362012-11-07 15:05:33 +0100896 /* the change can carry possible "attribute" flags like the
897 * TT_CLIENT_WIFI, therefore they have to be copied in the
898 * client entry
899 */
900 tt_global_entry->common.flags |= flags;
901
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200902 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
903 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200904 * delete + roaming change for this originator.
905 *
906 * We should first delete the old originator before adding the
907 * new one.
908 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200909 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200910 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200911 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200912 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913 }
914 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200915add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200916 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200917 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200918
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200919 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200920 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200921 common->addr, orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +0200922 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200923
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100924out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200925
Antonio Quartullia73105b2011-04-27 14:27:44 +0200926 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200927 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
928 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +1000929 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200930 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
931
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200932 if (!(flags & BATADV_TT_CLIENT_ROAM))
933 /* this is a normal global add. Therefore the client is not in a
934 * roaming state anymore.
935 */
936 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
937
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200938out:
939 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200940 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200941 if (tt_local_entry)
942 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200943 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000944}
945
Sven Eckelmann981d8902012-10-07 13:34:15 +0200946/* batadv_transtable_best_orig - Get best originator list entry from tt entry
947 * @tt_global_entry: global translation table entry to be analyzed
948 *
949 * This functon assumes the caller holds rcu_read_lock().
950 * Returns best originator list entry or NULL on errors.
951 */
952static struct batadv_tt_orig_list_entry *
953batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
954{
955 struct batadv_neigh_node *router = NULL;
956 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200957 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
958 int best_tq = 0;
959
960 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800961 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200962 router = batadv_orig_node_get_router(orig_entry->orig_node);
963 if (!router)
964 continue;
965
966 if (router->tq_avg > best_tq) {
967 best_entry = orig_entry;
968 best_tq = router->tq_avg;
969 }
970
971 batadv_neigh_node_free_ref(router);
972 }
973
974 return best_entry;
975}
976
977/* batadv_tt_global_print_entry - print all orig nodes who announce the address
978 * for this global entry
979 * @tt_global_entry: global translation table entry to be printed
980 * @seq: debugfs table seq_file struct
981 *
982 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200983 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200984static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200985batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200986 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200987{
988 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200989 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200990 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200991 uint16_t flags;
992 uint8_t last_ttvn;
993
994 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200995 flags = tt_common_entry->flags;
996
997 best_entry = batadv_transtable_best_orig(tt_global_entry);
998 if (best_entry) {
999 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001000 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +02001001 " %c %pM (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001002 '*', tt_global_entry->common.addr,
1003 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001004 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001005 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1006 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1007 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1008 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001009
1010 head = &tt_global_entry->orig_list;
1011
Sasha Levinb67bfe02013-02-27 17:06:00 -08001012 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001013 if (best_entry == orig_entry)
1014 continue;
1015
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001016 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001017 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1018 '+', tt_global_entry->common.addr,
1019 orig_entry->ttvn, orig_entry->orig_node->orig,
1020 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001021 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001022 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1023 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001024 }
1025}
1026
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001027int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028{
1029 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001030 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001031 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001032 struct batadv_tt_common_entry *tt_common_entry;
1033 struct batadv_tt_global_entry *tt_global;
1034 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001036 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Marek Lindner30da63a2012-08-03 17:15:46 +02001038 primary_if = batadv_seq_print_text_primary_if_get(seq);
1039 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001040 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001042 seq_printf(seq,
1043 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044 net_dev->name);
Antonio Quartulliced72932013-04-24 16:37:51 +02001045 seq_printf(seq, " %-13s %s %-15s %s (%-10s) %s\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001046 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1047 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049 for (i = 0; i < hash->size; i++) {
1050 head = &hash->table[i];
1051
Marek Lindner7aadf882011-02-18 12:28:09 +00001052 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001053 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001054 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001055 tt_global = container_of(tt_common_entry,
1056 struct batadv_tt_global_entry,
1057 common);
1058 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001060 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001062out:
1063 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001064 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001065 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001066}
1067
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001068/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001069static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001070batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001074 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001075
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076 spin_lock_bh(&tt_global_entry->list_lock);
1077 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001078 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1079 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001080 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001081 }
1082 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001083}
1084
Sven Eckelmanna5130882012-05-16 20:23:16 +02001085static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001086batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1087 struct batadv_tt_global_entry *tt_global_entry,
1088 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001089 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001090{
1091 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001093 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094
1095 spin_lock_bh(&tt_global_entry->list_lock);
1096 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001097 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001098 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001099 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001100 "Deleting %pM from global tt entry %pM: %s\n",
1101 orig_node->orig,
1102 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001103 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001104 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001105 }
1106 }
1107 spin_unlock_bh(&tt_global_entry->list_lock);
1108}
1109
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001110/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001111 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1112 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001113 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001114static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001115batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1116 struct batadv_tt_global_entry *tt_global_entry,
1117 struct batadv_orig_node *orig_node,
1118 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001119{
1120 bool last_entry = true;
1121 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001122 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001123
1124 /* no local entry exists, case 1:
1125 * Check if this is the last one or if other entries exist.
1126 */
1127
1128 rcu_read_lock();
1129 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001130 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001131 if (orig_entry->orig_node != orig_node) {
1132 last_entry = false;
1133 break;
1134 }
1135 }
1136 rcu_read_unlock();
1137
1138 if (last_entry) {
1139 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001140 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001141 tt_global_entry->roam_at = jiffies;
1142 } else
1143 /* there is another entry, we can simply delete this
1144 * one and can still use the other one.
1145 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001146 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1147 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001148}
1149
1150
1151
Sven Eckelmann56303d32012-06-05 22:31:31 +02001152static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1153 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001154 const unsigned char *addr,
1155 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001157 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001158 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159
Sven Eckelmanna5130882012-05-16 20:23:16 +02001160 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001161 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001162 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001165 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1166 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001167
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001168 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001169 batadv_tt_global_free(bat_priv, tt_global_entry,
1170 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001171
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001172 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001174
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001175 /* if we are deleting a global entry due to a roam
1176 * event, there are two possibilities:
1177 * 1) the client roamed from node A to node B => if there
1178 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001179 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001180 * wait for node B to claim it. In case of timeout
1181 * the entry is purged.
1182 *
1183 * If there are other originators left, we directly delete
1184 * the originator.
1185 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001186 * the global entry, since it is useless now.
1187 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001188 local_entry = batadv_tt_local_hash_find(bat_priv,
1189 tt_global_entry->common.addr);
1190 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001191 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001192 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001193 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001194 } else
1195 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001196 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1197 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001198
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001199
Antonio Quartullicc47f662011-04-27 14:27:57 +02001200out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001201 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001202 batadv_tt_global_entry_free_ref(tt_global_entry);
1203 if (local_entry)
1204 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205}
1206
Sven Eckelmann56303d32012-06-05 22:31:31 +02001207void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1208 struct batadv_orig_node *orig_node,
1209 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001210{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001211 struct batadv_tt_global_entry *tt_global;
1212 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001213 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001214 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001215 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001216 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001217 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001218
Simon Wunderlich6e801492011-10-19 10:28:26 +02001219 if (!hash)
1220 return;
1221
Antonio Quartullia73105b2011-04-27 14:27:44 +02001222 for (i = 0; i < hash->size; i++) {
1223 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001224 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001225
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001226 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001227 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001228 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001229 tt_global = container_of(tt_common_entry,
1230 struct batadv_tt_global_entry,
1231 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001232
Sven Eckelmann56303d32012-06-05 22:31:31 +02001233 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001234 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001235
Sven Eckelmann56303d32012-06-05 22:31:31 +02001236 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001237 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001238 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001239 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001240 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001241 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001242 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001243 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001244 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001245 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001246 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001247}
1248
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001249static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1250 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001251{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001252 bool purge = false;
1253 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1254 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001255
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001256 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1257 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1258 purge = true;
1259 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001260 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001261
1262 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1263 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1264 purge = true;
1265 *msg = "Temporary client timeout\n";
1266 }
1267
1268 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001269}
1270
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001271static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001272{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001273 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001274 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001275 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001276 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001277 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001278 char *msg = NULL;
1279 struct batadv_tt_common_entry *tt_common;
1280 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001281
Antonio Quartullicc47f662011-04-27 14:27:57 +02001282 for (i = 0; i < hash->size; i++) {
1283 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001284 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001285
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001286 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001287 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001288 hash_entry) {
1289 tt_global = container_of(tt_common,
1290 struct batadv_tt_global_entry,
1291 common);
1292
1293 if (!batadv_tt_global_to_purge(tt_global, &msg))
1294 continue;
1295
1296 batadv_dbg(BATADV_DBG_TT, bat_priv,
1297 "Deleting global tt entry (%pM): %s\n",
1298 tt_global->common.addr, msg);
1299
Sasha Levinb67bfe02013-02-27 17:06:00 -08001300 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001301
1302 batadv_tt_global_entry_free_ref(tt_global);
1303 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001305 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001306}
1307
Sven Eckelmann56303d32012-06-05 22:31:31 +02001308static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001309{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001310 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001311 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001312 struct batadv_tt_common_entry *tt_common_entry;
1313 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001314 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001315 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001316 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001317
Sven Eckelmann807736f2012-07-15 22:26:51 +02001318 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001319 return;
1320
Sven Eckelmann807736f2012-07-15 22:26:51 +02001321 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001322
1323 for (i = 0; i < hash->size; i++) {
1324 head = &hash->table[i];
1325 list_lock = &hash->list_locks[i];
1326
1327 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001328 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001329 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001330 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001331 tt_global = container_of(tt_common_entry,
1332 struct batadv_tt_global_entry,
1333 common);
1334 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001335 }
1336 spin_unlock_bh(list_lock);
1337 }
1338
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001339 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001340
Sven Eckelmann807736f2012-07-15 22:26:51 +02001341 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001342}
1343
Sven Eckelmann56303d32012-06-05 22:31:31 +02001344static bool
1345_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1346 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001347{
1348 bool ret = false;
1349
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001350 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1351 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001352 ret = true;
1353
1354 return ret;
1355}
1356
Sven Eckelmann56303d32012-06-05 22:31:31 +02001357struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1358 const uint8_t *src,
1359 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001360{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001361 struct batadv_tt_local_entry *tt_local_entry = NULL;
1362 struct batadv_tt_global_entry *tt_global_entry = NULL;
1363 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001364 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001366 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001367 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001368 if (!tt_local_entry ||
1369 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001370 goto out;
1371 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001372
Sven Eckelmanna5130882012-05-16 20:23:16 +02001373 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001374 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001375 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001376
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001377 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001378 * isolation
1379 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001380 if (tt_local_entry &&
1381 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001382 goto out;
1383
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001384 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001385 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001386 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001387 if (best_entry)
1388 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001389 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1390 orig_node = NULL;
1391 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001392
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001393out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001394 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001395 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001396 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001397 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001398
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001399 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001400}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401
Antonio Quartulliced72932013-04-24 16:37:51 +02001402/**
1403 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1404 * to the given orig_node
1405 * @bat_priv: the bat priv with all the soft interface information
1406 */
1407static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001408 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001409{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001410 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001411 struct batadv_tt_common_entry *tt_common;
1412 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001413 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001414 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415
1416 for (i = 0; i < hash->size; i++) {
1417 head = &hash->table[i];
1418
1419 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001420 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001421 tt_global = container_of(tt_common,
1422 struct batadv_tt_global_entry,
1423 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001424 /* Roaming clients are in the global table for
1425 * consistency only. They don't have to be
1426 * taken into account while computing the
1427 * global crc
1428 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001429 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001430 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001431 /* Temporary clients have not been announced yet, so
1432 * they have to be skipped while computing the global
1433 * crc
1434 */
1435 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1436 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001437
1438 /* find out if this global entry is announced by this
1439 * originator
1440 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001441 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001442 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001443 continue;
1444
Antonio Quartulliced72932013-04-24 16:37:51 +02001445 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446 }
1447 rcu_read_unlock();
1448 }
1449
Antonio Quartulliced72932013-04-24 16:37:51 +02001450 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001451}
1452
Antonio Quartulliced72932013-04-24 16:37:51 +02001453/**
1454 * batadv_tt_local_crc - calculates the checksum of the local table
1455 * @bat_priv: the bat priv with all the soft interface information
1456 */
1457static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001458{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001459 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001460 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001461 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001462 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463
1464 for (i = 0; i < hash->size; i++) {
1465 head = &hash->table[i];
1466
1467 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001468 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001469 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001470 * account while computing the CRC
1471 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001472 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001473 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001474
1475 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477 rcu_read_unlock();
1478 }
1479
Antonio Quartulliced72932013-04-24 16:37:51 +02001480 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481}
1482
Sven Eckelmann56303d32012-06-05 22:31:31 +02001483static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001485 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486
Sven Eckelmann807736f2012-07-15 22:26:51 +02001487 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001488
Sven Eckelmann807736f2012-07-15 22:26:51 +02001489 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490 list_del(&node->list);
1491 kfree(node);
1492 }
1493
Sven Eckelmann807736f2012-07-15 22:26:51 +02001494 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495}
1496
Sven Eckelmann56303d32012-06-05 22:31:31 +02001497static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1498 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001499 const unsigned char *tt_buff,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001500 uint16_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001501{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001502 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503
1504 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001505 * last OGM (the OGM could carry no changes)
1506 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507 spin_lock_bh(&orig_node->tt_buff_lock);
1508 if (tt_buff_len > 0) {
1509 kfree(orig_node->tt_buff);
1510 orig_node->tt_buff_len = 0;
1511 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1512 if (orig_node->tt_buff) {
1513 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1514 orig_node->tt_buff_len = tt_buff_len;
1515 }
1516 }
1517 spin_unlock_bh(&orig_node->tt_buff_lock);
1518}
1519
Sven Eckelmann56303d32012-06-05 22:31:31 +02001520static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001522 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001523
Sven Eckelmann807736f2012-07-15 22:26:51 +02001524 spin_lock_bh(&bat_priv->tt.req_list_lock);
1525 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001526 if (batadv_has_timed_out(node->issued_at,
1527 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528 list_del(&node->list);
1529 kfree(node);
1530 }
1531 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001532 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001533}
1534
1535/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001536 * has already been issued for this orig_node, NULL otherwise
1537 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001538static struct batadv_tt_req_node *
1539batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1540 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001541{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001542 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001543
Sven Eckelmann807736f2012-07-15 22:26:51 +02001544 spin_lock_bh(&bat_priv->tt.req_list_lock);
1545 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001546 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1547 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001548 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001549 goto unlock;
1550 }
1551
1552 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1553 if (!tt_req_node)
1554 goto unlock;
1555
1556 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1557 tt_req_node->issued_at = jiffies;
1558
Sven Eckelmann807736f2012-07-15 22:26:51 +02001559 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001560unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001561 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562 return tt_req_node;
1563}
1564
Marek Lindner335fbe02013-04-23 21:40:02 +08001565/**
1566 * batadv_tt_local_valid - verify that given tt entry is a valid one
1567 * @entry_ptr: to be checked local tt entry
1568 * @data_ptr: not used but definition required to satisfy the callback prototype
1569 *
1570 * Returns 1 if the entry is a valid, 0 otherwise.
1571 */
1572static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001573{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001574 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001575
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001576 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001577 return 0;
1578 return 1;
1579}
1580
Sven Eckelmanna5130882012-05-16 20:23:16 +02001581static int batadv_tt_global_valid(const void *entry_ptr,
1582 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001583{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001584 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1585 const struct batadv_tt_global_entry *tt_global_entry;
1586 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001587
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001588 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1589 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001590 return 0;
1591
Sven Eckelmann56303d32012-06-05 22:31:31 +02001592 tt_global_entry = container_of(tt_common_entry,
1593 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001594 common);
1595
Sven Eckelmanna5130882012-05-16 20:23:16 +02001596 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001597}
1598
Marek Lindner335fbe02013-04-23 21:40:02 +08001599/**
1600 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1601 * tt entries from the specified tt hash
1602 * @bat_priv: the bat priv with all the soft interface information
1603 * @hash: hash table containing the tt entries
1604 * @tt_len: expected tvlv tt data buffer length in number of bytes
1605 * @valid_cb: function to filter tt change entries
1606 * @cb_data: data passed to the filter function as argument
1607 *
1608 * Returns pointer to allocated tvlv tt data buffer if operation was
1609 * successful or NULL otherwise.
1610 */
1611static struct batadv_tvlv_tt_data *
1612batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1613 struct batadv_hashtable *hash, uint16_t tt_len,
1614 int (*valid_cb)(const void *, const void *),
1615 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001617 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001618 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1619 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001621 uint16_t tt_tot, tt_num_entries = 0;
1622 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001623 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624
Marek Lindner335fbe02013-04-23 21:40:02 +08001625 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1626 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1627 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001629
Antonio Quartulli298e6e62013-05-28 13:14:27 +02001630 tt_tot = batadv_tt_entries(tt_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08001631
1632 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1633 GFP_ATOMIC);
1634 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 goto out;
1636
Marek Lindner335fbe02013-04-23 21:40:02 +08001637 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001638
1639 rcu_read_lock();
1640 for (i = 0; i < hash->size; i++) {
1641 head = &hash->table[i];
1642
Sasha Levinb67bfe02013-02-27 17:06:00 -08001643 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001645 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001646 break;
1647
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001648 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649 continue;
1650
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001651 memcpy(tt_change->addr, tt_common_entry->addr,
1652 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001653 tt_change->flags = tt_common_entry->flags;
Marek Lindner335fbe02013-04-23 21:40:02 +08001654 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001655
Marek Lindner335fbe02013-04-23 21:40:02 +08001656 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001657 tt_change++;
1658 }
1659 }
1660 rcu_read_unlock();
1661
1662out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001663 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001664}
1665
Antonio Quartulliced72932013-04-24 16:37:51 +02001666/**
1667 * batadv_send_tt_request - send a TT Request message to a given node
1668 * @bat_priv: the bat priv with all the soft interface information
1669 * @dst_orig_node: the destination of the message
1670 * @ttvn: the version number that the source of the message is looking for
1671 * @tt_crc: the CRC associated with the version number
1672 * @full_table: ask for the entire translation table if true, while only for the
1673 * last TT diff otherwise
1674 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001675static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1676 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001677 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001678 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679{
Marek Lindner335fbe02013-04-23 21:40:02 +08001680 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001681 struct batadv_hard_iface *primary_if;
1682 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001683 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001685 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 if (!primary_if)
1687 goto out;
1688
1689 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001690 * reply from the same orig_node yet
1691 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001692 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001693 if (!tt_req_node)
1694 goto out;
1695
Marek Lindner335fbe02013-04-23 21:40:02 +08001696 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1697 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698 goto out;
1699
Marek Lindner335fbe02013-04-23 21:40:02 +08001700 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1701 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001702 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
1704 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001705 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001707 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001708 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001709
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001710 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001711 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1712 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1713 tvlv_tt_data, sizeof(*tvlv_tt_data));
1714 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715
1716out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001718 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001720 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001722 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001723 kfree(tt_req_node);
1724 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001725 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726 return ret;
1727}
1728
Marek Lindner335fbe02013-04-23 21:40:02 +08001729/**
1730 * batadv_send_other_tt_response - send reply to tt request concerning another
1731 * node's translation table
1732 * @bat_priv: the bat priv with all the soft interface information
1733 * @tt_data: tt data containing the tt request information
1734 * @req_src: mac address of tt request sender
1735 * @req_dst: mac address of tt request recipient
1736 *
1737 * Returns true if tt request reply was sent, false otherwise.
1738 */
1739static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1740 struct batadv_tvlv_tt_data *tt_data,
1741 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001743 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001744 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001745 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1746 uint8_t orig_ttvn, req_ttvn;
1747 uint16_t tt_len;
1748 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001749
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001750 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001751 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001752 req_src, tt_data->ttvn, req_dst,
1753 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754
1755 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001756 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 if (!req_dst_orig_node)
1758 goto out;
1759
Marek Lindner335fbe02013-04-23 21:40:02 +08001760 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761 if (!res_dst_orig_node)
1762 goto out;
1763
Antonio Quartullia73105b2011-04-27 14:27:44 +02001764 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001765 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001766
Marek Lindner335fbe02013-04-23 21:40:02 +08001767 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001768 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001769 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001770 goto out;
1771
Antonio Quartulli015758d2011-07-09 17:52:13 +02001772 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001773 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774 !req_dst_orig_node->tt_buff)
1775 full_table = true;
1776 else
1777 full_table = false;
1778
Marek Lindner335fbe02013-04-23 21:40:02 +08001779 /* TT fragmentation hasn't been implemented yet, so send as many
1780 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001781 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782 if (!full_table) {
1783 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1784 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785
Marek Lindner335fbe02013-04-23 21:40:02 +08001786 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1787 GFP_ATOMIC);
1788 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 goto unlock;
1790
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001792 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1795 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001796 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001797 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001798
Marek Lindner335fbe02013-04-23 21:40:02 +08001799 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1800 bat_priv->tt.global_hash,
1801 tt_len,
1802 batadv_tt_global_valid,
1803 req_dst_orig_node);
1804 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001806 }
1807
Marek Lindner335fbe02013-04-23 21:40:02 +08001808 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1809 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810
1811 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001812 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001814 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001815 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1816 res_dst_orig_node->orig, req_dst_orig_node->orig,
1817 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001818
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001819 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001820
Marek Lindner335fbe02013-04-23 21:40:02 +08001821 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1822 req_src, BATADV_TVLV_TT, 1,
1823 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001824
Marek Lindner335fbe02013-04-23 21:40:02 +08001825 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001826 goto out;
1827
1828unlock:
1829 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1830
1831out:
1832 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001833 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001834 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001835 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001836 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001837 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001838}
Sven Eckelmann96412692012-06-05 22:31:30 +02001839
Marek Lindner335fbe02013-04-23 21:40:02 +08001840/**
1841 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1842 * translation table
1843 * @bat_priv: the bat priv with all the soft interface information
1844 * @tt_data: tt data containing the tt request information
1845 * @req_src: mac address of tt request sender
1846 *
1847 * Returns true if tt request reply was sent, false otherwise.
1848 */
1849static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1850 struct batadv_tvlv_tt_data *tt_data,
1851 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852{
Marek Lindner335fbe02013-04-23 21:40:02 +08001853 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02001854 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001855 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001856 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001857 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08001858 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001860 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001861 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001862 req_src, tt_data->ttvn,
1863 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864
1865
Sven Eckelmann807736f2012-07-15 22:26:51 +02001866 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001867 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868
Marek Lindner335fbe02013-04-23 21:40:02 +08001869 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001870 if (!orig_node)
1871 goto out;
1872
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001873 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874 if (!primary_if)
1875 goto out;
1876
1877 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001878 * is too big send the whole local translation table
1879 */
Marek Lindner335fbe02013-04-23 21:40:02 +08001880 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001881 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001882 full_table = true;
1883 else
1884 full_table = false;
1885
Marek Lindner335fbe02013-04-23 21:40:02 +08001886 /* TT fragmentation hasn't been implemented yet, so send as many
1887 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001888 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001889 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001890 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1891 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892
Marek Lindner335fbe02013-04-23 21:40:02 +08001893 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1894 GFP_ATOMIC);
1895 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001896 goto unlock;
1897
Marek Lindner335fbe02013-04-23 21:40:02 +08001898 /* Copy the last orig_node's OGM buffer */
1899 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001900 bat_priv->tt.last_changeset_len);
1901 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001903 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08001904 tt_len = batadv_tt_len(tt_len);
1905 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001906
Marek Lindner335fbe02013-04-23 21:40:02 +08001907 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1908 bat_priv->tt.local_hash,
1909 tt_len,
1910 batadv_tt_local_valid,
1911 NULL);
1912 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001913 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 }
1915
Marek Lindner335fbe02013-04-23 21:40:02 +08001916 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1917 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918
1919 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001920 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001921
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001922 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001923 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1924 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001926 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001927
Marek Lindner335fbe02013-04-23 21:40:02 +08001928 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1929 req_src, BATADV_TVLV_TT, 1,
1930 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1931
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 goto out;
1933
1934unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001935 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001936out:
1937 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001938 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001940 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08001941 kfree(tvlv_tt_data);
1942 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001943 return true;
1944}
1945
Marek Lindner335fbe02013-04-23 21:40:02 +08001946/**
1947 * batadv_send_tt_response - send reply to tt request
1948 * @bat_priv: the bat priv with all the soft interface information
1949 * @tt_data: tt data containing the tt request information
1950 * @req_src: mac address of tt request sender
1951 * @req_dst: mac address of tt request recipient
1952 *
1953 * Returns true if tt request reply was sent, false otherwise.
1954 */
1955static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1956 struct batadv_tvlv_tt_data *tt_data,
1957 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958{
Marek Lindner335fbe02013-04-23 21:40:02 +08001959 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001960 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08001961 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001962 return true;
1963
Marek Lindner335fbe02013-04-23 21:40:02 +08001964 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001965 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08001966 return batadv_send_other_tt_response(bat_priv, tt_data,
1967 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001968 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001969}
1970
Sven Eckelmann56303d32012-06-05 22:31:31 +02001971static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1972 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08001973 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001974 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001975{
1976 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001977 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001978
1979 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001980 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1981 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001982 batadv_tt_global_del(bat_priv, orig_node,
1983 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001984 "tt removed by changes",
1985 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001986 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001987 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001988 (tt_change + i)->addr,
1989 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001990 /* In case of problem while storing a
1991 * global_entry, we stop the updating
1992 * procedure without committing the
1993 * ttvn change. This will avoid to send
1994 * corrupted data on tt_request
1995 */
1996 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001997 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001999 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002000}
2001
Sven Eckelmann56303d32012-06-05 22:31:31 +02002002static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002003 struct batadv_tvlv_tt_data *tt_data,
2004 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002005{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002006 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007
Marek Lindner335fbe02013-04-23 21:40:02 +08002008 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009 if (!orig_node)
2010 goto out;
2011
2012 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002013 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014
Sven Eckelmanna5130882012-05-16 20:23:16 +02002015 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002016 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2017 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002018
2019 spin_lock_bh(&orig_node->tt_buff_lock);
2020 kfree(orig_node->tt_buff);
2021 orig_node->tt_buff_len = 0;
2022 orig_node->tt_buff = NULL;
2023 spin_unlock_bh(&orig_node->tt_buff_lock);
2024
Marek Lindner335fbe02013-04-23 21:40:02 +08002025 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
2027out:
2028 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002029 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002030}
2031
Sven Eckelmann56303d32012-06-05 22:31:31 +02002032static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2033 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002034 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002035 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002036{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002037 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2038 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002039
Sven Eckelmanna5130882012-05-16 20:23:16 +02002040 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2041 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042 atomic_set(&orig_node->last_ttvn, ttvn);
2043}
2044
Sven Eckelmann56303d32012-06-05 22:31:31 +02002045bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002046{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002047 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002048 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002049
Sven Eckelmanna5130882012-05-16 20:23:16 +02002050 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002051 if (!tt_local_entry)
2052 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002053 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002054 * consistency purpose)
2055 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002056 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2057 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002058 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002059 ret = true;
2060out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002062 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002063 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002064}
2065
Marek Lindner335fbe02013-04-23 21:40:02 +08002066/**
2067 * batadv_handle_tt_response - process incoming tt reply
2068 * @bat_priv: the bat priv with all the soft interface information
2069 * @tt_data: tt data containing the tt request information
2070 * @resp_src: mac address of tt reply sender
2071 * @num_entries: number of tt change entries appended to the tt data
2072 */
2073static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2074 struct batadv_tvlv_tt_data *tt_data,
2075 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002076{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002077 struct batadv_tt_req_node *node, *safe;
2078 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002079 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002081 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002082 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002083 resp_src, tt_data->ttvn, num_entries,
2084 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002085
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002086 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002087 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002088 goto out;
2089
Marek Lindner335fbe02013-04-23 21:40:02 +08002090 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091 if (!orig_node)
2092 goto out;
2093
Marek Lindner335fbe02013-04-23 21:40:02 +08002094 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2095 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002096 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002097 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2098 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2099 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002100 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101
2102 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002103 spin_lock_bh(&bat_priv->tt.req_list_lock);
2104 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002105 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106 continue;
2107 list_del(&node->list);
2108 kfree(node);
2109 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002110 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111
2112 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002113 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002114out:
2115 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002116 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002117}
2118
Sven Eckelmann56303d32012-06-05 22:31:31 +02002119static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002120{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002121 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122
Sven Eckelmann807736f2012-07-15 22:26:51 +02002123 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124
Sven Eckelmann807736f2012-07-15 22:26:51 +02002125 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002126 list_del(&node->list);
2127 kfree(node);
2128 }
2129
Sven Eckelmann807736f2012-07-15 22:26:51 +02002130 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002131}
2132
Sven Eckelmann56303d32012-06-05 22:31:31 +02002133static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002134{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002135 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002136
Sven Eckelmann807736f2012-07-15 22:26:51 +02002137 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2138 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002139 if (!batadv_has_timed_out(node->first_time,
2140 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002141 continue;
2142
2143 list_del(&node->list);
2144 kfree(node);
2145 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002146 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002147}
2148
2149/* This function checks whether the client already reached the
2150 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2151 * will not be sent.
2152 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002153 * returns true if the ROAMING_ADV can be sent, false otherwise
2154 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002155static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002156 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002157{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002158 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002159 bool ret = false;
2160
Sven Eckelmann807736f2012-07-15 22:26:51 +02002161 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002162 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002163 * reply from the same orig_node yet
2164 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002165 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002166 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002167 continue;
2168
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002169 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002170 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002171 continue;
2172
Sven Eckelmann3e348192012-05-16 20:23:22 +02002173 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002174 /* Sorry, you roamed too many times! */
2175 goto unlock;
2176 ret = true;
2177 break;
2178 }
2179
2180 if (!ret) {
2181 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2182 if (!tt_roam_node)
2183 goto unlock;
2184
2185 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002186 atomic_set(&tt_roam_node->counter,
2187 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002188 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2189
Sven Eckelmann807736f2012-07-15 22:26:51 +02002190 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002191 ret = true;
2192 }
2193
2194unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002195 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002196 return ret;
2197}
2198
Sven Eckelmann56303d32012-06-05 22:31:31 +02002199static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2200 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002201{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002202 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002203 struct batadv_tvlv_roam_adv tvlv_roam;
2204
2205 primary_if = batadv_primary_if_get_selected(bat_priv);
2206 if (!primary_if)
2207 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208
2209 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002210 * already roamed to us too many times
2211 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002212 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002213 goto out;
2214
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002215 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002216 "Sending ROAMING_ADV to %pM (client %pM)\n",
2217 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002218
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002219 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002220
Marek Lindner122edaa2013-04-23 21:40:03 +08002221 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2222 tvlv_roam.reserved = 0;
2223
2224 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2225 orig_node->orig, BATADV_TVLV_ROAM, 1,
2226 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002227
2228out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002229 if (primary_if)
2230 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231}
2232
Sven Eckelmanna5130882012-05-16 20:23:16 +02002233static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002234{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002235 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002236 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002237 struct batadv_priv *bat_priv;
2238
2239 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002240 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2241 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002242
Sven Eckelmanna5130882012-05-16 20:23:16 +02002243 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002244 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002245 batadv_tt_req_purge(bat_priv);
2246 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002247
Antonio Quartulli72414442012-12-25 13:14:37 +01002248 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2249 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002250}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002251
Sven Eckelmann56303d32012-06-05 22:31:31 +02002252void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002253{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002254 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2255 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2256
Sven Eckelmann807736f2012-07-15 22:26:51 +02002257 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002258
Sven Eckelmanna5130882012-05-16 20:23:16 +02002259 batadv_tt_local_table_free(bat_priv);
2260 batadv_tt_global_table_free(bat_priv);
2261 batadv_tt_req_list_free(bat_priv);
2262 batadv_tt_changes_list_free(bat_priv);
2263 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002264
Sven Eckelmann807736f2012-07-15 22:26:51 +02002265 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002266}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002267
Antonio Quartulli697f2532011-11-07 16:47:01 +01002268/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002269 * in the given hash table and returns the number of modified entries
2270 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002271static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2272 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002273{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002274 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002275 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002276 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002277 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002278
2279 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002280 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002281
2282 for (i = 0; i < hash->size; i++) {
2283 head = &hash->table[i];
2284
2285 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002286 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002287 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002288 if (enable) {
2289 if ((tt_common_entry->flags & flags) == flags)
2290 continue;
2291 tt_common_entry->flags |= flags;
2292 } else {
2293 if (!(tt_common_entry->flags & flags))
2294 continue;
2295 tt_common_entry->flags &= ~flags;
2296 }
2297 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002298 }
2299 rcu_read_unlock();
2300 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002301out:
2302 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002303}
2304
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002305/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002306static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002307{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002308 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002309 struct batadv_tt_common_entry *tt_common;
2310 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002311 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002312 struct hlist_head *head;
2313 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002314 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002315
2316 if (!hash)
2317 return;
2318
2319 for (i = 0; i < hash->size; i++) {
2320 head = &hash->table[i];
2321 list_lock = &hash->list_locks[i];
2322
2323 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002324 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002325 hash_entry) {
2326 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002327 continue;
2328
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002329 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002330 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002331 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002332
Sven Eckelmann807736f2012-07-15 22:26:51 +02002333 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002334 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002335 tt_local = container_of(tt_common,
2336 struct batadv_tt_local_entry,
2337 common);
2338 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002339 }
2340 spin_unlock_bh(list_lock);
2341 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002342}
2343
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002344/**
2345 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2346 * have been queued in the time since the last commit
2347 * @bat_priv: the bat priv with all the soft interface information
2348 */
2349void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002350{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002351 uint16_t changed_num = 0;
2352
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002353 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2354 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2355 batadv_tt_tvlv_container_update(bat_priv);
2356 return;
2357 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002358
Sven Eckelmann807736f2012-07-15 22:26:51 +02002359 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002360 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002361
2362 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002363 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002364 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002365 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002366
2367 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002368 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002369 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002370 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002371 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002372
2373 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002374 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002375 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002376}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002377
Sven Eckelmann56303d32012-06-05 22:31:31 +02002378bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002379 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002380{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002381 struct batadv_tt_local_entry *tt_local_entry = NULL;
2382 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002383 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002384
2385 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002386 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002387
Sven Eckelmanna5130882012-05-16 20:23:16 +02002388 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002389 if (!tt_local_entry)
2390 goto out;
2391
Sven Eckelmanna5130882012-05-16 20:23:16 +02002392 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002393 if (!tt_global_entry)
2394 goto out;
2395
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002396 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002397 goto out;
2398
Marek Lindner5870adc2012-06-20 17:16:05 +02002399 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002400
2401out:
2402 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002403 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002404 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002405 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002406 return ret;
2407}
Marek Lindnera943cac2011-07-30 13:10:18 +02002408
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002409/**
2410 * batadv_tt_update_orig - update global translation table with new tt
2411 * information received via ogms
2412 * @bat_priv: the bat priv with all the soft interface information
2413 * @orig: the orig_node of the ogm
2414 * @tt_buff: buffer holding the tt information
2415 * @tt_num_changes: number of tt changes inside the tt buffer
2416 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002417 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002418 */
2419static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2420 struct batadv_orig_node *orig_node,
2421 const unsigned char *tt_buff,
2422 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002423 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002424{
2425 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2426 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002427 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002428
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002429 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002430 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002431 return;
2432
Antonio Quartulli17071572011-11-07 16:36:40 +01002433 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002434 * increased by one -> we can apply the attached changes
2435 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002436 if ((!orig_node->tt_initialised && ttvn == 1) ||
2437 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002438 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002439 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2440 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002441 * In this case send a tt request
2442 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002443 if (!tt_num_changes) {
2444 full_table = false;
2445 goto request_table;
2446 }
2447
Marek Lindner335fbe02013-04-23 21:40:02 +08002448 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002449 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002450 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002451
2452 /* Even if we received the precomputed crc with the OGM, we
2453 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002454 * in the global table
2455 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002456 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002457
2458 /* The ttvn alone is not enough to guarantee consistency
2459 * because a single value could represent different states
2460 * (due to the wrap around). Thus a node has to check whether
2461 * the resulting table (after applying the changes) is still
2462 * consistent or not. E.g. a node could disconnect while its
2463 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2464 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002465 * inconsistency
2466 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002467 if (orig_node->tt_crc != tt_crc)
2468 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002469 } else {
2470 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002471 * in sync anymore -> request fresh tt data
2472 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002473 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2474 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002475request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002476 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002477 "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 +02002478 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2479 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002480 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2481 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002482 return;
2483 }
2484 }
2485}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002486
2487/* returns true whether we know that the client has moved from its old
2488 * originator to another one. This entry is kept is still kept for consistency
2489 * purposes
2490 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002491bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002492 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002493{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002494 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002495 bool ret = false;
2496
Sven Eckelmanna5130882012-05-16 20:23:16 +02002497 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002498 if (!tt_global_entry)
2499 goto out;
2500
Antonio Quartullic1d07432013-01-15 22:17:19 +10002501 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002502 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002503out:
2504 return ret;
2505}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002506
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002507/**
2508 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2509 * @bat_priv: the bat priv with all the soft interface information
2510 * @addr: the MAC address of the local client to query
2511 *
2512 * Returns true if the local client is known to be roaming (it is not served by
2513 * this node anymore) or not. If yes, the client is still present in the table
2514 * to keep the latter consistent with the node TTVN
2515 */
2516bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2517 uint8_t *addr)
2518{
2519 struct batadv_tt_local_entry *tt_local_entry;
2520 bool ret = false;
2521
2522 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2523 if (!tt_local_entry)
2524 goto out;
2525
2526 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2527 batadv_tt_local_entry_free_ref(tt_local_entry);
2528out:
2529 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002530}
2531
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002532bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2533 struct batadv_orig_node *orig_node,
2534 const unsigned char *addr)
2535{
2536 bool ret = false;
2537
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002538 /* if the originator is a backbone node (meaning it belongs to the same
2539 * LAN of this node) the temporary client must not be added because to
2540 * reach such destination the node must use the LAN instead of the mesh
2541 */
2542 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2543 goto out;
2544
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002545 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2546 BATADV_TT_CLIENT_TEMP,
2547 atomic_read(&orig_node->last_ttvn)))
2548 goto out;
2549
2550 batadv_dbg(BATADV_DBG_TT, bat_priv,
2551 "Added temporary global client (addr: %pM orig: %pM)\n",
2552 addr, orig_node->orig);
2553 ret = true;
2554out:
2555 return ret;
2556}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002557
2558/**
2559 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2560 * @bat_priv: the bat priv with all the soft interface information
2561 * @orig: the orig_node of the ogm
2562 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2563 * @tvlv_value: tvlv buffer containing the gateway data
2564 * @tvlv_value_len: tvlv buffer length
2565 */
2566static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2567 struct batadv_orig_node *orig,
2568 uint8_t flags,
2569 void *tvlv_value,
2570 uint16_t tvlv_value_len)
2571{
2572 struct batadv_tvlv_tt_data *tt_data;
2573 uint16_t num_entries;
2574
2575 if (tvlv_value_len < sizeof(*tt_data))
2576 return;
2577
2578 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2579 tvlv_value_len -= sizeof(*tt_data);
2580
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002581 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002582
2583 batadv_tt_update_orig(bat_priv, orig,
2584 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002585 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002586}
2587
2588/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002589 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2590 * container
2591 * @bat_priv: the bat priv with all the soft interface information
2592 * @src: mac address of tt tvlv sender
2593 * @dst: mac address of tt tvlv recipient
2594 * @tvlv_value: tvlv buffer containing the tt data
2595 * @tvlv_value_len: tvlv buffer length
2596 *
2597 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2598 * otherwise.
2599 */
2600static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2601 uint8_t *src, uint8_t *dst,
2602 void *tvlv_value,
2603 uint16_t tvlv_value_len)
2604{
2605 struct batadv_tvlv_tt_data *tt_data;
2606 uint16_t num_entries;
2607 char tt_flag;
2608 bool ret;
2609
2610 if (tvlv_value_len < sizeof(*tt_data))
2611 return NET_RX_SUCCESS;
2612
2613 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2614 tvlv_value_len -= sizeof(*tt_data);
2615
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002616 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002617
2618 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2619 case BATADV_TT_REQUEST:
2620 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2621
2622 /* If this node cannot provide a TT response the tt_request is
2623 * forwarded
2624 */
2625 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2626 if (!ret) {
2627 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2628 tt_flag = 'F';
2629 else
2630 tt_flag = '.';
2631
2632 batadv_dbg(BATADV_DBG_TT, bat_priv,
2633 "Routing TT_REQUEST to %pM [%c]\n",
2634 dst, tt_flag);
2635 /* tvlv API will re-route the packet */
2636 return NET_RX_DROP;
2637 }
2638 break;
2639 case BATADV_TT_RESPONSE:
2640 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2641
2642 if (batadv_is_my_mac(bat_priv, dst)) {
2643 batadv_handle_tt_response(bat_priv, tt_data,
2644 src, num_entries);
2645 return NET_RX_SUCCESS;
2646 }
2647
2648 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2649 tt_flag = 'F';
2650 else
2651 tt_flag = '.';
2652
2653 batadv_dbg(BATADV_DBG_TT, bat_priv,
2654 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2655
2656 /* tvlv API will re-route the packet */
2657 return NET_RX_DROP;
2658 }
2659
2660 return NET_RX_SUCCESS;
2661}
2662
2663/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002664 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2665 * @bat_priv: the bat priv with all the soft interface information
2666 * @src: mac address of tt tvlv sender
2667 * @dst: mac address of tt tvlv recipient
2668 * @tvlv_value: tvlv buffer containing the tt data
2669 * @tvlv_value_len: tvlv buffer length
2670 *
2671 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2672 * otherwise.
2673 */
2674static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2675 uint8_t *src, uint8_t *dst,
2676 void *tvlv_value,
2677 uint16_t tvlv_value_len)
2678{
2679 struct batadv_tvlv_roam_adv *roaming_adv;
2680 struct batadv_orig_node *orig_node = NULL;
2681
2682 /* If this node is not the intended recipient of the
2683 * roaming advertisement the packet is forwarded
2684 * (the tvlv API will re-route the packet).
2685 */
2686 if (!batadv_is_my_mac(bat_priv, dst))
2687 return NET_RX_DROP;
2688
2689 /* check if it is a backbone gateway. we don't accept
2690 * roaming advertisement from it, as it has the same
2691 * entries as we have.
2692 */
2693 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2694 goto out;
2695
2696 if (tvlv_value_len < sizeof(*roaming_adv))
2697 goto out;
2698
2699 orig_node = batadv_orig_hash_find(bat_priv, src);
2700 if (!orig_node)
2701 goto out;
2702
2703 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2704 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2705
2706 batadv_dbg(BATADV_DBG_TT, bat_priv,
2707 "Received ROAMING_ADV from %pM (client %pM)\n",
2708 src, roaming_adv->client);
2709
2710 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
2711 BATADV_TT_CLIENT_ROAM,
2712 atomic_read(&orig_node->last_ttvn) + 1);
2713
2714out:
2715 if (orig_node)
2716 batadv_orig_node_free_ref(orig_node);
2717 return NET_RX_SUCCESS;
2718}
2719
2720/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002721 * batadv_tt_init - initialise the translation table internals
2722 * @bat_priv: the bat priv with all the soft interface information
2723 *
2724 * Return 0 on success or negative error number in case of failure.
2725 */
2726int batadv_tt_init(struct batadv_priv *bat_priv)
2727{
2728 int ret;
2729
2730 ret = batadv_tt_local_init(bat_priv);
2731 if (ret < 0)
2732 return ret;
2733
2734 ret = batadv_tt_global_init(bat_priv);
2735 if (ret < 0)
2736 return ret;
2737
2738 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002739 batadv_tt_tvlv_unicast_handler_v1,
2740 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002741
Marek Lindner122edaa2013-04-23 21:40:03 +08002742 batadv_tvlv_handler_register(bat_priv, NULL,
2743 batadv_roam_tvlv_unicast_handler_v1,
2744 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2745
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002746 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2747 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2748 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2749
2750 return 1;
2751}