blob: 5e89deeb9542979c72ba635fb1c18d301a9c1621 [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 Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
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
Sven Eckelmanna5130882012-05-16 20:23:16 +0200120static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200121{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200122 struct batadv_tt_common_entry *tt_common_entry;
123 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200124
Sven Eckelmann56303d32012-06-05 22:31:31 +0200125 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
126 tt_global_entry = container_of(tt_common_entry,
127 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200128
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129 kfree(tt_global_entry);
130}
131
Sven Eckelmanna5130882012-05-16 20:23:16 +0200132static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200133batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200135 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200136 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100137 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200138 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200139 }
140}
141
Sven Eckelmanna5130882012-05-16 20:23:16 +0200142static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200143{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200144 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200145
Sven Eckelmann56303d32012-06-05 22:31:31 +0200146 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200147 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200148 kfree(orig_entry);
149}
150
Sven Eckelmanna5130882012-05-16 20:23:16 +0200151static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200152batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153{
Antonio Quartullid657e622012-07-01 14:09:12 +0200154 if (!atomic_dec_and_test(&orig_entry->refcount))
155 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000156 /* to avoid race conditions, immediately decrease the tt counter */
157 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200158 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200159}
160
Sven Eckelmann56303d32012-06-05 22:31:31 +0200161static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200162 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200163{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200165 bool event_removed = false;
166 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200167
168 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
169
170 if (!tt_change_node)
171 return;
172
Antonio Quartulliff66c972011-06-30 01:14:00 +0200173 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200174 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
175
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200176 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200177
178 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200179 spin_lock_bh(&bat_priv->tt.changes_list_lock);
180 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200181 list) {
182 if (!batadv_compare_eth(entry->change.addr, addr))
183 continue;
184
185 /* DEL+ADD in the same orig interval have no effect and can be
186 * removed to avoid silly behaviour on the receiver side. The
187 * other way around (ADD+DEL) can happen in case of roaming of
188 * a client still in the NEW state. Roaming of NEW clients is
189 * now possible due to automatically recognition of "temporary"
190 * clients
191 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200192 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200193 if (!del_op_requested && del_op_entry)
194 goto del;
195 if (del_op_requested && !del_op_entry)
196 goto del;
197 continue;
198del:
199 list_del(&entry->list);
200 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000201 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200202 event_removed = true;
203 goto unlock;
204 }
205
Antonio Quartullia73105b2011-04-27 14:27:44 +0200206 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200207 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200208
209unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200210 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200212 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200213 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200214 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216}
217
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200218int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200219{
Sven Eckelmann96412692012-06-05 22:31:30 +0200220 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann56303d32012-06-05 22:31:31 +0200223static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200225 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200226 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Sven Eckelmann807736f2012-07-15 22:26:51 +0200228 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Antonio Quartullidec05072012-11-10 11:00:32 +0100233 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
234 &batadv_tt_local_hash_lock_class_key);
235
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237}
238
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200239static void batadv_tt_global_free(struct batadv_priv *bat_priv,
240 struct batadv_tt_global_entry *tt_global,
241 const char *message)
242{
243 batadv_dbg(BATADV_DBG_TT, bat_priv,
244 "Deleting global tt entry %pM: %s\n",
245 tt_global->common.addr, message);
246
247 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
248 batadv_choose_orig, tt_global->common.addr);
249 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200250}
251
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200252void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
253 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200255 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200256 struct batadv_tt_local_entry *tt_local;
257 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200258 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200259 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100260 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200261 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Antonio Quartulli47c94652012-09-23 22:38:35 +0200263 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200264 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
Antonio Quartulli47c94652012-09-23 22:38:35 +0200266 if (tt_local) {
267 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200268 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
269 batadv_dbg(BATADV_DBG_TT, bat_priv,
270 "Re-adding pending client %pM\n", addr);
271 /* whatever the reason why the PENDING flag was set,
272 * this is a client which was enqueued to be removed in
273 * this orig_interval. Since it popped up again, the
274 * flag can be reset like it was never enqueued
275 */
276 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
277 goto add_event;
278 }
279
280 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
281 batadv_dbg(BATADV_DBG_TT, bat_priv,
282 "Roaming client %pM came back to its original location\n",
283 addr);
284 /* the ROAM flag is set because this client roamed away
285 * and the node got a roaming_advertisement message. Now
286 * that the client popped up again at its original
287 * location such flag can be unset
288 */
289 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
290 roamed_back = true;
291 }
292 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 }
294
Antonio Quartulli47c94652012-09-23 22:38:35 +0200295 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
296 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200297 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200298
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200299 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200300 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200301 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartulli47c94652012-09-23 22:38:35 +0200303 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100304 /* The local entry has to be marked as NEW to avoid to send it in
305 * a full table response going out before the next ttvn increment
306 * (consistency check)
307 */
308 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200309 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200310 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311 atomic_set(&tt_local->common.refcount, 2);
312 tt_local->last_seen = jiffies;
313 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
315 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200316 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200317 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
Sven Eckelmann807736f2012-07-15 22:26:51 +0200319 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200320 batadv_choose_orig, &tt_local->common,
321 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100322
323 if (unlikely(hash_added != 0)) {
324 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200325 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100326 goto out;
327 }
328
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200329add_event:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200330 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200331
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200332check_roaming:
333 /* Check whether it is a roaming, but don't do anything if the roaming
334 * process has already been handled
335 */
336 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200337 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200338 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200339 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800340 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200341 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200342 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200343 }
344 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200345 if (roamed_back) {
346 batadv_tt_global_free(bat_priv, tt_global,
347 "Roaming canceled");
348 tt_global = NULL;
349 } else {
350 /* The global entry has to be marked as ROAMING and
351 * has to be kept for consistency purpose
352 */
353 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
354 tt_global->roam_at = jiffies;
355 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200356 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200357
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200358out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200359 if (tt_local)
360 batadv_tt_local_entry_free_ref(tt_local);
361 if (tt_global)
362 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363}
364
Sven Eckelmanna5130882012-05-16 20:23:16 +0200365static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
366 int *packet_buff_len,
367 int min_packet_len,
368 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800370 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800372 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
373
374 /* keep old buffer if kmalloc should fail */
375 if (new_buff) {
376 memcpy(new_buff, *packet_buff, min_packet_len);
377 kfree(*packet_buff);
378 *packet_buff = new_buff;
379 *packet_buff_len = new_packet_len;
380 }
381}
382
Sven Eckelmann56303d32012-06-05 22:31:31 +0200383static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200384 unsigned char **packet_buff,
385 int *packet_buff_len,
386 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800387{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800388 int req_len;
389
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800390 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200391 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800392
393 /* if we have too many changes for one packet don't send any
394 * and wait for the tt table request which will be fragmented
395 */
Marek Lindner736292c2013-01-12 19:19:06 +0800396 if (req_len > bat_priv->soft_iface->mtu)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800397 req_len = min_packet_len;
398
Sven Eckelmanna5130882012-05-16 20:23:16 +0200399 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
400 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800401}
402
Sven Eckelmann56303d32012-06-05 22:31:31 +0200403static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200404 unsigned char **packet_buff,
405 int *packet_buff_len,
406 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800407{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200408 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800409 int count = 0, tot_changes = 0, new_len;
410 unsigned char *tt_buff;
411
Sven Eckelmanna5130882012-05-16 20:23:16 +0200412 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
413 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800414
415 new_len = *packet_buff_len - min_packet_len;
416 tt_buff = *packet_buff + min_packet_len;
417
418 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200419 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
Sven Eckelmann807736f2012-07-15 22:26:51 +0200421 spin_lock_bh(&bat_priv->tt.changes_list_lock);
422 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Sven Eckelmann807736f2012-07-15 22:26:51 +0200424 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100425 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200426 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200427 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200428 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 count++;
430 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200431 list_del(&entry->list);
432 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200434 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200437 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
438 kfree(bat_priv->tt.last_changeset);
439 bat_priv->tt.last_changeset_len = 0;
440 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800441 /* check whether this new OGM has no changes due to size problems */
442 if (new_len > 0) {
443 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200444 * instead of providing the diff
445 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200446 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
447 if (bat_priv->tt.last_changeset) {
448 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
449 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200450 }
451 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200452 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453
Marek Lindner08ad76e2012-04-23 16:32:55 +0800454 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455}
456
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200457int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458{
459 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200460 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200461 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100463 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200464 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200466 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100467 int last_seen_secs;
468 int last_seen_msecs;
469 unsigned long last_seen_jiffies;
470 bool no_purge;
471 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472
Marek Lindner30da63a2012-08-03 17:15:46 +0200473 primary_if = batadv_seq_print_text_primary_if_get(seq);
474 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200475 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100477 seq_printf(seq,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100478 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
479 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
480 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100481 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
482 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484 for (i = 0; i < hash->size; i++) {
485 head = &hash->table[i];
486
Marek Lindner7aadf882011-02-18 12:28:09 +0000487 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800488 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000489 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100490 tt_local = container_of(tt_common_entry,
491 struct batadv_tt_local_entry,
492 common);
493 last_seen_jiffies = jiffies - tt_local->last_seen;
494 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
495 last_seen_secs = last_seen_msecs / 1000;
496 last_seen_msecs = last_seen_msecs % 1000;
497
498 no_purge = tt_common_entry->flags & np_flag;
499
500 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100501 tt_common_entry->addr,
502 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200503 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100504 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100505 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200506 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100507 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200508 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100509 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100510 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100511 no_purge ? 0 : last_seen_secs,
512 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000514 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200516out:
517 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200518 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200519 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520}
521
Sven Eckelmann56303d32012-06-05 22:31:31 +0200522static void
523batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
524 struct batadv_tt_local_entry *tt_local_entry,
525 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200527 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
528 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529
Antonio Quartulli015758d2011-07-09 17:52:13 +0200530 /* The local client has to be marked as "pending to be removed" but has
531 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200532 * response issued before the net ttvn increment (consistency check)
533 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200534 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100535
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200536 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200537 "Local tt entry (%pM) pending to be removed: %s\n",
538 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539}
540
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200541/**
542 * batadv_tt_local_remove - logically remove an entry from the local table
543 * @bat_priv: the bat priv with all the soft interface information
544 * @addr: the MAC address of the client to remove
545 * @message: message to append to the log on deletion
546 * @roaming: true if the deletion is due to a roaming event
547 *
548 * Returns the flags assigned to the local entry before being deleted
549 */
550uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
551 const uint8_t *addr, const char *message,
552 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200554 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200555 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556
Sven Eckelmanna5130882012-05-16 20:23:16 +0200557 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200558 if (!tt_local_entry)
559 goto out;
560
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200561 curr_flags = tt_local_entry->common.flags;
562
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200563 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200564 /* if this global entry addition is due to a roaming, the node has to
565 * mark the local entry as "roamed" in order to correctly reroute
566 * packets later
567 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200568 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200569 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200570 /* mark the local client as ROAMed */
571 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
572 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200573
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200574 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
575 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
576 message);
577 goto out;
578 }
579 /* if this client has been added right now, it is possible to
580 * immediately purge it
581 */
582 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
583 curr_flags | BATADV_TT_CLIENT_DEL);
584 hlist_del_rcu(&tt_local_entry->common.hash_entry);
585 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200586
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200587out:
588 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200589 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200590
591 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592}
593
Sven Eckelmann56303d32012-06-05 22:31:31 +0200594static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200595 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200597 struct batadv_tt_local_entry *tt_local_entry;
598 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800599 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200600
Sasha Levinb67bfe02013-02-27 17:06:00 -0800601 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200602 hash_entry) {
603 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200604 struct batadv_tt_local_entry,
605 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200606 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
607 continue;
608
609 /* entry already marked for deletion */
610 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
611 continue;
612
613 if (!batadv_has_timed_out(tt_local_entry->last_seen,
614 BATADV_TT_LOCAL_TIMEOUT))
615 continue;
616
617 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
618 BATADV_TT_CLIENT_DEL, "timed out");
619 }
620}
621
Sven Eckelmann56303d32012-06-05 22:31:31 +0200622static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200623{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200624 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200626 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200627 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 for (i = 0; i < hash->size; i++) {
630 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200631 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200633 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200634 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200635 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637}
638
Sven Eckelmann56303d32012-06-05 22:31:31 +0200639static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200641 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200642 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200643 struct batadv_tt_common_entry *tt_common_entry;
644 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800645 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200646 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200647 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200648
Sven Eckelmann807736f2012-07-15 22:26:51 +0200649 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 return;
651
Sven Eckelmann807736f2012-07-15 22:26:51 +0200652 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200653
654 for (i = 0; i < hash->size; i++) {
655 head = &hash->table[i];
656 list_lock = &hash->list_locks[i];
657
658 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800659 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200660 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800661 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200662 tt_local = container_of(tt_common_entry,
663 struct batadv_tt_local_entry,
664 common);
665 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200666 }
667 spin_unlock_bh(list_lock);
668 }
669
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200670 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200671
Sven Eckelmann807736f2012-07-15 22:26:51 +0200672 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673}
674
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200677 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200678 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679
Sven Eckelmann807736f2012-07-15 22:26:51 +0200680 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681
Sven Eckelmann807736f2012-07-15 22:26:51 +0200682 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200683 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684
Antonio Quartullidec05072012-11-10 11:00:32 +0100685 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
686 &batadv_tt_global_hash_lock_class_key);
687
Sven Eckelmann5346c352012-05-05 13:27:28 +0200688 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689}
690
Sven Eckelmann56303d32012-06-05 22:31:31 +0200691static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200692{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200693 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694
Sven Eckelmann807736f2012-07-15 22:26:51 +0200695 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696
Sven Eckelmann807736f2012-07-15 22:26:51 +0200697 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200698 list) {
699 list_del(&entry->list);
700 kfree(entry);
701 }
702
Sven Eckelmann807736f2012-07-15 22:26:51 +0200703 atomic_set(&bat_priv->tt.local_changes, 0);
704 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705}
706
Antonio Quartullid657e622012-07-01 14:09:12 +0200707/* retrieves the orig_tt_list_entry belonging to orig_node from the
708 * batadv_tt_global_entry list
709 *
710 * returns it with an increased refcounter, NULL if not found
711 */
712static struct batadv_tt_orig_list_entry *
713batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
714 const struct batadv_orig_node *orig_node)
715{
716 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
717 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200718
719 rcu_read_lock();
720 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800721 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200722 if (tmp_orig_entry->orig_node != orig_node)
723 continue;
724 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
725 continue;
726
727 orig_entry = tmp_orig_entry;
728 break;
729 }
730 rcu_read_unlock();
731
732 return orig_entry;
733}
734
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200735/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200736 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200737 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200738static bool
739batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
740 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741{
Antonio Quartullid657e622012-07-01 14:09:12 +0200742 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200743 bool found = false;
744
Antonio Quartullid657e622012-07-01 14:09:12 +0200745 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
746 if (orig_entry) {
747 found = true;
748 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200749 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200750
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200751 return found;
752}
753
Sven Eckelmanna5130882012-05-16 20:23:16 +0200754static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200755batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200756 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200757{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200758 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200759
Antonio Quartullid657e622012-07-01 14:09:12 +0200760 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200761 if (orig_entry) {
762 /* refresh the ttvn: the current value could be a bogus one that
763 * was added during a "temporary client detection"
764 */
765 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200766 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200767 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200768
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200769 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
770 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200771 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200772
773 INIT_HLIST_NODE(&orig_entry->list);
774 atomic_inc(&orig_node->refcount);
775 atomic_inc(&orig_node->tt_size);
776 orig_entry->orig_node = orig_node;
777 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200778 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779
Antonio Quartullid657e622012-07-01 14:09:12 +0200780 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200781 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200782 &tt_global->orig_list);
783 spin_unlock_bh(&tt_global->list_lock);
784out:
785 if (orig_entry)
786 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200787}
788
Antonio Quartullia73105b2011-04-27 14:27:44 +0200789/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200790int batadv_tt_global_add(struct batadv_priv *bat_priv,
791 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200792 const unsigned char *tt_addr, uint8_t flags,
793 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200795 struct batadv_tt_global_entry *tt_global_entry;
796 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200797 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100798 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200799 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200800 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801
Sven Eckelmanna5130882012-05-16 20:23:16 +0200802 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200803 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
804
805 /* if the node already has a local client for this entry, it has to wait
806 * for a roaming advertisement instead of manually messing up the global
807 * table
808 */
809 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
810 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
811 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812
Antonio Quartullia73105b2011-04-27 14:27:44 +0200813 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200814 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200815 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200816 goto out;
817
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200818 common = &tt_global_entry->common;
819 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200820
Antonio Quartullid4f44692012-05-25 00:00:54 +0200821 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200822 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200823 /* node must store current time in case of roaming. This is
824 * needed to purge this entry out on timeout (if nobody claims
825 * it)
826 */
827 if (flags & BATADV_TT_CLIENT_ROAM)
828 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200829 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200830 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200831
832 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
833 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200834
Sven Eckelmann807736f2012-07-15 22:26:51 +0200835 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200836 batadv_compare_tt,
837 batadv_choose_orig, common,
838 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100839
840 if (unlikely(hash_added != 0)) {
841 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200842 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100843 goto out_remove;
844 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200845 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200846 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200847 /* If there is already a global entry, we can use this one for
848 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200849 * But if we are trying to add a temporary client then here are
850 * two options at this point:
851 * 1) the global client is not a temporary client: the global
852 * client has to be left as it is, temporary information
853 * should never override any already known client state
854 * 2) the global client is a temporary client: purge the
855 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200856 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200857 if (flags & BATADV_TT_CLIENT_TEMP) {
858 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
859 goto out;
860 if (batadv_tt_global_entry_has_orig(tt_global_entry,
861 orig_node))
862 goto out_remove;
863 batadv_tt_global_del_orig_list(tt_global_entry);
864 goto add_orig_entry;
865 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200866
867 /* if the client was temporary added before receiving the first
868 * OGM announcing it, we have to clear the TEMP flag
869 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200870 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200871
Antonio Quartullie9c001362012-11-07 15:05:33 +0100872 /* the change can carry possible "attribute" flags like the
873 * TT_CLIENT_WIFI, therefore they have to be copied in the
874 * client entry
875 */
876 tt_global_entry->common.flags |= flags;
877
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200878 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
879 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200880 * delete + roaming change for this originator.
881 *
882 * We should first delete the old originator before adding the
883 * new one.
884 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200885 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200886 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200887 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200888 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889 }
890 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200891add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200892 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200893 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200895 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200896 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200897 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200898 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200899
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100900out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200901
Antonio Quartullia73105b2011-04-27 14:27:44 +0200902 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200903 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
904 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +1000905 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200906 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
907
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200908 if (!(flags & BATADV_TT_CLIENT_ROAM))
909 /* this is a normal global add. Therefore the client is not in a
910 * roaming state anymore.
911 */
912 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
913
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200914out:
915 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200916 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200917 if (tt_local_entry)
918 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200919 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920}
921
Sven Eckelmann981d8902012-10-07 13:34:15 +0200922/* batadv_transtable_best_orig - Get best originator list entry from tt entry
923 * @tt_global_entry: global translation table entry to be analyzed
924 *
925 * This functon assumes the caller holds rcu_read_lock().
926 * Returns best originator list entry or NULL on errors.
927 */
928static struct batadv_tt_orig_list_entry *
929batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
930{
931 struct batadv_neigh_node *router = NULL;
932 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200933 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
934 int best_tq = 0;
935
936 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800937 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200938 router = batadv_orig_node_get_router(orig_entry->orig_node);
939 if (!router)
940 continue;
941
942 if (router->tq_avg > best_tq) {
943 best_entry = orig_entry;
944 best_tq = router->tq_avg;
945 }
946
947 batadv_neigh_node_free_ref(router);
948 }
949
950 return best_entry;
951}
952
953/* batadv_tt_global_print_entry - print all orig nodes who announce the address
954 * for this global entry
955 * @tt_global_entry: global translation table entry to be printed
956 * @seq: debugfs table seq_file struct
957 *
958 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200959 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200960static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200961batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200962 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200963{
964 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200965 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200966 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200967 uint16_t flags;
968 uint8_t last_ttvn;
969
970 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200971 flags = tt_common_entry->flags;
972
973 best_entry = batadv_transtable_best_orig(tt_global_entry);
974 if (best_entry) {
975 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100976 seq_printf(seq,
977 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200978 '*', tt_global_entry->common.addr,
979 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100980 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200981 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
982 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
983 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
984 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200985
986 head = &tt_global_entry->orig_list;
987
Sasha Levinb67bfe02013-02-27 17:06:00 -0800988 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200989 if (best_entry == orig_entry)
990 continue;
991
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200992 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +0200993 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
994 '+', tt_global_entry->common.addr,
995 orig_entry->ttvn, orig_entry->orig_node->orig,
996 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200997 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200998 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
999 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001000 }
1001}
1002
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001003int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001004{
1005 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001006 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001007 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001008 struct batadv_tt_common_entry *tt_common_entry;
1009 struct batadv_tt_global_entry *tt_global;
1010 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001012 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013
Marek Lindner30da63a2012-08-03 17:15:46 +02001014 primary_if = batadv_seq_print_text_primary_if_get(seq);
1015 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001016 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001018 seq_printf(seq,
1019 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020 net_dev->name);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001021 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1022 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1023 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 for (i = 0; i < hash->size; i++) {
1026 head = &hash->table[i];
1027
Marek Lindner7aadf882011-02-18 12:28:09 +00001028 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001029 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001030 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001031 tt_global = container_of(tt_common_entry,
1032 struct batadv_tt_global_entry,
1033 common);
1034 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001036 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001038out:
1039 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001040 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001041 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001042}
1043
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001044/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001045static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001046batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001048 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001049 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001050 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001051
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001052 spin_lock_bh(&tt_global_entry->list_lock);
1053 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1055 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001056 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001057 }
1058 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001059}
1060
Sven Eckelmanna5130882012-05-16 20:23:16 +02001061static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1063 struct batadv_tt_global_entry *tt_global_entry,
1064 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001065 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001066{
1067 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001068 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070
1071 spin_lock_bh(&tt_global_entry->list_lock);
1072 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001074 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001075 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001076 "Deleting %pM from global tt entry %pM: %s\n",
1077 orig_node->orig,
1078 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001079 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 }
1083 spin_unlock_bh(&tt_global_entry->list_lock);
1084}
1085
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001087 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1088 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001089 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001090static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001091batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1092 struct batadv_tt_global_entry *tt_global_entry,
1093 struct batadv_orig_node *orig_node,
1094 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095{
1096 bool last_entry = true;
1097 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001098 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001099
1100 /* no local entry exists, case 1:
1101 * Check if this is the last one or if other entries exist.
1102 */
1103
1104 rcu_read_lock();
1105 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001106 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001107 if (orig_entry->orig_node != orig_node) {
1108 last_entry = false;
1109 break;
1110 }
1111 }
1112 rcu_read_unlock();
1113
1114 if (last_entry) {
1115 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001116 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001117 tt_global_entry->roam_at = jiffies;
1118 } else
1119 /* there is another entry, we can simply delete this
1120 * one and can still use the other one.
1121 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001122 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1123 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001124}
1125
1126
1127
Sven Eckelmann56303d32012-06-05 22:31:31 +02001128static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1129 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001130 const unsigned char *addr,
1131 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001132{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001133 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001134 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001135
Sven Eckelmanna5130882012-05-16 20:23:16 +02001136 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001137 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001138 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001139
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001140 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001141 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1142 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001143
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001144 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001145 batadv_tt_global_free(bat_priv, tt_global_entry,
1146 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001147
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001148 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001149 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001150
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151 /* if we are deleting a global entry due to a roam
1152 * event, there are two possibilities:
1153 * 1) the client roamed from node A to node B => if there
1154 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001155 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001156 * wait for node B to claim it. In case of timeout
1157 * the entry is purged.
1158 *
1159 * If there are other originators left, we directly delete
1160 * the originator.
1161 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001162 * the global entry, since it is useless now.
1163 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001164 local_entry = batadv_tt_local_hash_find(bat_priv,
1165 tt_global_entry->common.addr);
1166 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001167 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001168 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001169 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 } else
1171 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001172 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1173 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001174
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001175
Antonio Quartullicc47f662011-04-27 14:27:57 +02001176out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001177 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001178 batadv_tt_global_entry_free_ref(tt_global_entry);
1179 if (local_entry)
1180 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001181}
1182
Sven Eckelmann56303d32012-06-05 22:31:31 +02001183void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1184 struct batadv_orig_node *orig_node,
1185 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001186{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001187 struct batadv_tt_global_entry *tt_global;
1188 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001189 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001190 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001191 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001192 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001193 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194
Simon Wunderlich6e801492011-10-19 10:28:26 +02001195 if (!hash)
1196 return;
1197
Antonio Quartullia73105b2011-04-27 14:27:44 +02001198 for (i = 0; i < hash->size; i++) {
1199 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001200 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001201
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001202 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001203 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001204 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001205 tt_global = container_of(tt_common_entry,
1206 struct batadv_tt_global_entry,
1207 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001208
Sven Eckelmann56303d32012-06-05 22:31:31 +02001209 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001210 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001211
Sven Eckelmann56303d32012-06-05 22:31:31 +02001212 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001213 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001214 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001216 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001217 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001218 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001219 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001220 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001221 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001222 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001223}
1224
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001225static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1226 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001227{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001228 bool purge = false;
1229 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1230 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001231
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001232 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1233 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1234 purge = true;
1235 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001236 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001237
1238 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1239 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1240 purge = true;
1241 *msg = "Temporary client timeout\n";
1242 }
1243
1244 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001245}
1246
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001247static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001248{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001249 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001250 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001251 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001252 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001253 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001254 char *msg = NULL;
1255 struct batadv_tt_common_entry *tt_common;
1256 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001257
Antonio Quartullicc47f662011-04-27 14:27:57 +02001258 for (i = 0; i < hash->size; i++) {
1259 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001260 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001261
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001262 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001263 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001264 hash_entry) {
1265 tt_global = container_of(tt_common,
1266 struct batadv_tt_global_entry,
1267 common);
1268
1269 if (!batadv_tt_global_to_purge(tt_global, &msg))
1270 continue;
1271
1272 batadv_dbg(BATADV_DBG_TT, bat_priv,
1273 "Deleting global tt entry (%pM): %s\n",
1274 tt_global->common.addr, msg);
1275
Sasha Levinb67bfe02013-02-27 17:06:00 -08001276 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001277
1278 batadv_tt_global_entry_free_ref(tt_global);
1279 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001280 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001281 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001282}
1283
Sven Eckelmann56303d32012-06-05 22:31:31 +02001284static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001285{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001286 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001287 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001288 struct batadv_tt_common_entry *tt_common_entry;
1289 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001290 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001291 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001292 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001293
Sven Eckelmann807736f2012-07-15 22:26:51 +02001294 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001295 return;
1296
Sven Eckelmann807736f2012-07-15 22:26:51 +02001297 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001298
1299 for (i = 0; i < hash->size; i++) {
1300 head = &hash->table[i];
1301 list_lock = &hash->list_locks[i];
1302
1303 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001304 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001305 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001306 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001307 tt_global = container_of(tt_common_entry,
1308 struct batadv_tt_global_entry,
1309 common);
1310 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001311 }
1312 spin_unlock_bh(list_lock);
1313 }
1314
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001315 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001316
Sven Eckelmann807736f2012-07-15 22:26:51 +02001317 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001318}
1319
Sven Eckelmann56303d32012-06-05 22:31:31 +02001320static bool
1321_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1322 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001323{
1324 bool ret = false;
1325
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001326 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1327 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001328 ret = true;
1329
1330 return ret;
1331}
1332
Sven Eckelmann56303d32012-06-05 22:31:31 +02001333struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1334 const uint8_t *src,
1335 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001336{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001337 struct batadv_tt_local_entry *tt_local_entry = NULL;
1338 struct batadv_tt_global_entry *tt_global_entry = NULL;
1339 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001340 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001341
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001342 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001343 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001344 if (!tt_local_entry ||
1345 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001346 goto out;
1347 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001348
Sven Eckelmanna5130882012-05-16 20:23:16 +02001349 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001350 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001351 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001352
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001353 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001354 * isolation
1355 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001356 if (tt_local_entry &&
1357 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001358 goto out;
1359
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001360 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001361 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001362 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001363 if (best_entry)
1364 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001365 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1366 orig_node = NULL;
1367 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001368
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001369out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001370 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001371 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001372 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001373 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001374
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001375 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001376}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001377
1378/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1380 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001381{
1382 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001383 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001384 struct batadv_tt_common_entry *tt_common;
1385 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001386 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001387 uint32_t i;
1388 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001389
1390 for (i = 0; i < hash->size; i++) {
1391 head = &hash->table[i];
1392
1393 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001394 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001395 tt_global = container_of(tt_common,
1396 struct batadv_tt_global_entry,
1397 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001398 /* Roaming clients are in the global table for
1399 * consistency only. They don't have to be
1400 * taken into account while computing the
1401 * global crc
1402 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001403 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001404 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001405 /* Temporary clients have not been announced yet, so
1406 * they have to be skipped while computing the global
1407 * crc
1408 */
1409 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1410 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001411
1412 /* find out if this global entry is announced by this
1413 * originator
1414 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001415 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001416 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001417 continue;
1418
1419 total_one = 0;
1420 for (j = 0; j < ETH_ALEN; j++)
1421 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001422 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001423 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001424 }
1425 rcu_read_unlock();
1426 }
1427
1428 return total;
1429}
1430
1431/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001432static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433{
1434 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001435 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001436 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001438 uint32_t i;
1439 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001440
1441 for (i = 0; i < hash->size; i++) {
1442 head = &hash->table[i];
1443
1444 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001445 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001446 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001447 * account while computing the CRC
1448 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001449 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001450 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001451 total_one = 0;
1452 for (j = 0; j < ETH_ALEN; j++)
1453 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001454 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455 total ^= total_one;
1456 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457 rcu_read_unlock();
1458 }
1459
1460 return total;
1461}
1462
Sven Eckelmann56303d32012-06-05 22:31:31 +02001463static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001464{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001465 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001466
Sven Eckelmann807736f2012-07-15 22:26:51 +02001467 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468
Sven Eckelmann807736f2012-07-15 22:26:51 +02001469 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470 list_del(&node->list);
1471 kfree(node);
1472 }
1473
Sven Eckelmann807736f2012-07-15 22:26:51 +02001474 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475}
1476
Sven Eckelmann56303d32012-06-05 22:31:31 +02001477static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1478 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001479 const unsigned char *tt_buff,
1480 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001482 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483
1484 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001485 * last OGM (the OGM could carry no changes)
1486 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487 spin_lock_bh(&orig_node->tt_buff_lock);
1488 if (tt_buff_len > 0) {
1489 kfree(orig_node->tt_buff);
1490 orig_node->tt_buff_len = 0;
1491 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1492 if (orig_node->tt_buff) {
1493 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1494 orig_node->tt_buff_len = tt_buff_len;
1495 }
1496 }
1497 spin_unlock_bh(&orig_node->tt_buff_lock);
1498}
1499
Sven Eckelmann56303d32012-06-05 22:31:31 +02001500static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001501{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001502 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503
Sven Eckelmann807736f2012-07-15 22:26:51 +02001504 spin_lock_bh(&bat_priv->tt.req_list_lock);
1505 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001506 if (batadv_has_timed_out(node->issued_at,
1507 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001508 list_del(&node->list);
1509 kfree(node);
1510 }
1511 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001512 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001513}
1514
1515/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001516 * has already been issued for this orig_node, NULL otherwise
1517 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518static struct batadv_tt_req_node *
1519batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1520 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001522 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
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(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001526 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1527 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001528 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529 goto unlock;
1530 }
1531
1532 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1533 if (!tt_req_node)
1534 goto unlock;
1535
1536 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1537 tt_req_node->issued_at = jiffies;
1538
Sven Eckelmann807736f2012-07-15 22:26:51 +02001539 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001540unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001541 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542 return tt_req_node;
1543}
1544
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001545/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001546static int batadv_tt_local_valid_entry(const void *entry_ptr,
1547 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001548{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001549 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001550
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001551 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001552 return 0;
1553 return 1;
1554}
1555
Sven Eckelmanna5130882012-05-16 20:23:16 +02001556static int batadv_tt_global_valid(const void *entry_ptr,
1557 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001559 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1560 const struct batadv_tt_global_entry *tt_global_entry;
1561 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001563 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1564 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001565 return 0;
1566
Sven Eckelmann56303d32012-06-05 22:31:31 +02001567 tt_global_entry = container_of(tt_common_entry,
1568 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001569 common);
1570
Sven Eckelmanna5130882012-05-16 20:23:16 +02001571 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001572}
1573
Sven Eckelmanna5130882012-05-16 20:23:16 +02001574static struct sk_buff *
1575batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001576 struct batadv_hashtable *hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001577 struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001578 int (*valid_cb)(const void *, const void *),
1579 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001580{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001581 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001582 struct batadv_tt_query_packet *tt_response;
1583 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001584 struct hlist_head *head;
1585 struct sk_buff *skb = NULL;
1586 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001587 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001588 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001589 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001590
Marek Lindner736292c2013-01-12 19:19:06 +08001591 if (tt_query_size + tt_len > bat_priv->soft_iface->mtu) {
1592 tt_len = bat_priv->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001593 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001594 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001595 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596
Sven Eckelmann96412692012-06-05 22:31:30 +02001597 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001598 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599 if (!skb)
1600 goto out;
1601
Sven Eckelmann5b246572012-11-04 17:11:45 +01001602 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001603 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001604 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001605
Sven Eckelmann96412692012-06-05 22:31:30 +02001606 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607 tt_count = 0;
1608
1609 rcu_read_lock();
1610 for (i = 0; i < hash->size; i++) {
1611 head = &hash->table[i];
1612
Sasha Levinb67bfe02013-02-27 17:06:00 -08001613 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614 head, hash_entry) {
1615 if (tt_count == tt_tot)
1616 break;
1617
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001618 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619 continue;
1620
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001621 memcpy(tt_change->addr, tt_common_entry->addr,
1622 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001623 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624
1625 tt_count++;
1626 tt_change++;
1627 }
1628 }
1629 rcu_read_unlock();
1630
Antonio Quartulli9d852392011-10-17 14:25:13 +02001631 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001632 * copied
1633 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001634 tt_response->tt_data = htons(tt_count);
1635
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636out:
1637 return skb;
1638}
1639
Sven Eckelmann56303d32012-06-05 22:31:31 +02001640static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1641 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001642 uint8_t ttvn, uint16_t tt_crc,
1643 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644{
1645 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001646 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001647 struct batadv_hard_iface *primary_if;
1648 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001650 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001652 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653 if (!primary_if)
1654 goto out;
1655
1656 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001657 * reply from the same orig_node yet
1658 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001659 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001660 if (!tt_req_node)
1661 goto out;
1662
Sven Eckelmann5b246572012-11-04 17:11:45 +01001663 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001664 if (!skb)
1665 goto out;
1666
Sven Eckelmann5b246572012-11-04 17:11:45 +01001667 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668
Sven Eckelmann96412692012-06-05 22:31:30 +02001669 tt_req_len = sizeof(*tt_request);
1670 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001672 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001673 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001674 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1675 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001676 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001678 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001679 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001680
1681 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001682 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001683
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001684 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1685 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001687 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001688
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001689 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1690 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691
1692out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001693 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001694 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001695 if (ret)
1696 kfree_skb(skb);
1697 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001698 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001699 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001700 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701 kfree(tt_req_node);
1702 }
1703 return ret;
1704}
1705
Sven Eckelmann96412692012-06-05 22:31:30 +02001706static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001707batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001708 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001709{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001710 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001711 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712 uint8_t orig_ttvn, req_ttvn, ttvn;
1713 int ret = false;
1714 unsigned char *tt_buff;
1715 bool full_table;
1716 uint16_t tt_len, tt_tot;
1717 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001718 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001719 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001720 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001722 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001723 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1724 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001725 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726
1727 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001728 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001729 if (!req_dst_orig_node)
1730 goto out;
1731
Sven Eckelmannda641192012-05-12 13:48:56 +02001732 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001733 if (!res_dst_orig_node)
1734 goto out;
1735
Antonio Quartullia73105b2011-04-27 14:27:44 +02001736 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1737 req_ttvn = tt_request->ttvn;
1738
Antonio Quartulli015758d2011-07-09 17:52:13 +02001739 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001740 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001741 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742 goto out;
1743
Antonio Quartulli015758d2011-07-09 17:52:13 +02001744 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001745 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746 !req_dst_orig_node->tt_buff)
1747 full_table = true;
1748 else
1749 full_table = false;
1750
1751 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001752 * I'll send only one packet with as much TT entries as I can
1753 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754 if (!full_table) {
1755 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1756 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001757 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001758
Sven Eckelmann96412692012-06-05 22:31:30 +02001759 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001760 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761 if (!skb)
1762 goto unlock;
1763
Sven Eckelmann5b246572012-11-04 17:11:45 +01001764 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001765 packet_pos = skb_put(skb, len);
1766 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001767 tt_response->ttvn = req_ttvn;
1768 tt_response->tt_data = htons(tt_tot);
1769
Sven Eckelmann96412692012-06-05 22:31:30 +02001770 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 /* Copy the last orig_node's OGM buffer */
1772 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1773 req_dst_orig_node->tt_buff_len);
1774
1775 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1776 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001777 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1778 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001779 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1780
Sven Eckelmanna5130882012-05-16 20:23:16 +02001781 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001782 bat_priv->tt.global_hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001783 bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001784 batadv_tt_global_valid,
1785 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786 if (!skb)
1787 goto out;
1788
Sven Eckelmann96412692012-06-05 22:31:30 +02001789 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001790 }
1791
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001792 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001793 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001794 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1796 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001797 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001798
1799 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001800 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001802 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001803 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1804 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001806 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001807
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001808 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1809 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810 goto out;
1811
1812unlock:
1813 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1814
1815out:
1816 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001817 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001818 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001819 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820 if (!ret)
1821 kfree_skb(skb);
1822 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823}
Sven Eckelmann96412692012-06-05 22:31:30 +02001824
1825static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001826batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001827 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001829 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001830 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001831 uint8_t my_ttvn, req_ttvn, ttvn;
1832 int ret = false;
1833 unsigned char *tt_buff;
1834 bool full_table;
1835 uint16_t tt_len, tt_tot;
1836 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001837 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001838 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001839 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001841 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001842 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1843 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001844 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845
1846
Sven Eckelmann807736f2012-07-15 22:26:51 +02001847 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001848 req_ttvn = tt_request->ttvn;
1849
Sven Eckelmannda641192012-05-12 13:48:56 +02001850 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851 if (!orig_node)
1852 goto out;
1853
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001854 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 if (!primary_if)
1856 goto out;
1857
1858 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001859 * is too big send the whole local translation table
1860 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001861 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001862 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863 full_table = true;
1864 else
1865 full_table = false;
1866
1867 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001868 * I'll send only one packet with as much TT entries as I can
1869 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001870 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001871 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1872 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001873 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874
Sven Eckelmann96412692012-06-05 22:31:30 +02001875 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001876 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001877 if (!skb)
1878 goto unlock;
1879
Sven Eckelmann5b246572012-11-04 17:11:45 +01001880 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001881 packet_pos = skb_put(skb, len);
1882 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 tt_response->ttvn = req_ttvn;
1884 tt_response->tt_data = htons(tt_tot);
1885
Sven Eckelmann96412692012-06-05 22:31:30 +02001886 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001887 memcpy(tt_buff, bat_priv->tt.last_changeset,
1888 bat_priv->tt.last_changeset_len);
1889 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001890 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001891 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001892 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001893 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001894
Sven Eckelmanna5130882012-05-16 20:23:16 +02001895 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001896 bat_priv->tt.local_hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001897 bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001898 batadv_tt_local_valid_entry,
1899 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001900 if (!skb)
1901 goto out;
1902
Sven Eckelmann96412692012-06-05 22:31:30 +02001903 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001904 }
1905
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001906 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001907 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001908 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1910 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001911 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001912
1913 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001914 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001916 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001917 "Sending TT_RESPONSE to %pM [%c]\n",
1918 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001919 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001921 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001922
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001923 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1924 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925 goto out;
1926
1927unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001928 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001929out:
1930 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001931 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001933 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934 if (!ret)
1935 kfree_skb(skb);
1936 /* This packet was for me, so it doesn't need to be re-routed */
1937 return true;
1938}
1939
Sven Eckelmann56303d32012-06-05 22:31:31 +02001940bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001941 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942{
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001943 if (batadv_is_my_mac(bat_priv, tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001944 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001945 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001946 return true;
1947
Sven Eckelmanna5130882012-05-16 20:23:16 +02001948 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001949 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001950 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001951 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952}
1953
Sven Eckelmann56303d32012-06-05 22:31:31 +02001954static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1955 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001956 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001957 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958{
1959 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001960 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001961
1962 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001963 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1964 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001965 batadv_tt_global_del(bat_priv, orig_node,
1966 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001967 "tt removed by changes",
1968 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001969 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001970 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001971 (tt_change + i)->addr,
1972 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001973 /* In case of problem while storing a
1974 * global_entry, we stop the updating
1975 * procedure without committing the
1976 * ttvn change. This will avoid to send
1977 * corrupted data on tt_request
1978 */
1979 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001980 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001981 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001982 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983}
1984
Sven Eckelmann56303d32012-06-05 22:31:31 +02001985static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001986 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001987{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001988 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001989
Sven Eckelmannda641192012-05-12 13:48:56 +02001990 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991 if (!orig_node)
1992 goto out;
1993
1994 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001995 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996
Sven Eckelmanna5130882012-05-16 20:23:16 +02001997 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001998 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001999 ntohs(tt_response->tt_data),
2000 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001
2002 spin_lock_bh(&orig_node->tt_buff_lock);
2003 kfree(orig_node->tt_buff);
2004 orig_node->tt_buff_len = 0;
2005 orig_node->tt_buff = NULL;
2006 spin_unlock_bh(&orig_node->tt_buff_lock);
2007
2008 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2009
2010out:
2011 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002012 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013}
2014
Sven Eckelmann56303d32012-06-05 22:31:31 +02002015static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2016 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002017 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002018 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002020 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2021 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002022
Sven Eckelmanna5130882012-05-16 20:23:16 +02002023 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2024 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025 atomic_set(&orig_node->last_ttvn, ttvn);
2026}
2027
Sven Eckelmann56303d32012-06-05 22:31:31 +02002028bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002029{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002030 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002031 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002032
Sven Eckelmanna5130882012-05-16 20:23:16 +02002033 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002034 if (!tt_local_entry)
2035 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002036 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002037 * consistency purpose)
2038 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002039 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2040 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002041 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002042 ret = true;
2043out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002044 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002045 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002046 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002047}
2048
Sven Eckelmann56303d32012-06-05 22:31:31 +02002049void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002050 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002051{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002052 struct batadv_tt_req_node *node, *safe;
2053 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002054 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002055
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002056 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002057 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2058 tt_response->src, tt_response->ttvn,
2059 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002060 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002062 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002063 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002064 goto out;
2065
Sven Eckelmannda641192012-05-12 13:48:56 +02002066 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002067 if (!orig_node)
2068 goto out;
2069
Sven Eckelmann96412692012-06-05 22:31:30 +02002070 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002071 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002072 } else {
2073 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002074 batadv_tt_update_changes(bat_priv, orig_node,
2075 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002076 tt_response->ttvn, tt_change);
2077 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002078
2079 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002080 spin_lock_bh(&bat_priv->tt.req_list_lock);
2081 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002082 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083 continue;
2084 list_del(&node->list);
2085 kfree(node);
2086 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002087 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002088
2089 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002090 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091out:
2092 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002093 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002094}
2095
Sven Eckelmann56303d32012-06-05 22:31:31 +02002096int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002098 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002099
Sven Eckelmanna5130882012-05-16 20:23:16 +02002100 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002101 if (ret < 0)
2102 return ret;
2103
Sven Eckelmanna5130882012-05-16 20:23:16 +02002104 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002105 if (ret < 0)
2106 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002107
Antonio Quartulli72414442012-12-25 13:14:37 +01002108 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2109 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2110 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111
2112 return 1;
2113}
2114
Sven Eckelmann56303d32012-06-05 22:31:31 +02002115static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002117 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002118
Sven Eckelmann807736f2012-07-15 22:26:51 +02002119 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002120
Sven Eckelmann807736f2012-07-15 22:26:51 +02002121 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002122 list_del(&node->list);
2123 kfree(node);
2124 }
2125
Sven Eckelmann807736f2012-07-15 22:26:51 +02002126 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002127}
2128
Sven Eckelmann56303d32012-06-05 22:31:31 +02002129static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002130{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002131 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002132
Sven Eckelmann807736f2012-07-15 22:26:51 +02002133 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2134 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002135 if (!batadv_has_timed_out(node->first_time,
2136 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002137 continue;
2138
2139 list_del(&node->list);
2140 kfree(node);
2141 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002142 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002143}
2144
2145/* This function checks whether the client already reached the
2146 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2147 * will not be sent.
2148 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002149 * returns true if the ROAMING_ADV can be sent, false otherwise
2150 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002151static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002152 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002153{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002155 bool ret = false;
2156
Sven Eckelmann807736f2012-07-15 22:26:51 +02002157 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002158 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002159 * reply from the same orig_node yet
2160 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002161 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002162 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002163 continue;
2164
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002165 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002166 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002167 continue;
2168
Sven Eckelmann3e348192012-05-16 20:23:22 +02002169 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002170 /* Sorry, you roamed too many times! */
2171 goto unlock;
2172 ret = true;
2173 break;
2174 }
2175
2176 if (!ret) {
2177 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2178 if (!tt_roam_node)
2179 goto unlock;
2180
2181 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002182 atomic_set(&tt_roam_node->counter,
2183 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002184 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2185
Sven Eckelmann807736f2012-07-15 22:26:51 +02002186 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002187 ret = true;
2188 }
2189
2190unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002191 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002192 return ret;
2193}
2194
Sven Eckelmann56303d32012-06-05 22:31:31 +02002195static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2196 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002197{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002198 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002199 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002200 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002201 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002202 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002203
2204 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002205 * already roamed to us too many times
2206 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002207 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208 goto out;
2209
Sven Eckelmann5b246572012-11-04 17:11:45 +01002210 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002211 if (!skb)
2212 goto out;
2213
Sven Eckelmann5b246572012-11-04 17:11:45 +01002214 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002215
Sven Eckelmann96412692012-06-05 22:31:30 +02002216 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002217
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002218 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002219 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002220 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002221 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002222 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002223 if (!primary_if)
2224 goto out;
2225 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002226 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002227 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2228 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2229
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002230 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002231 "Sending ROAMING_ADV to %pM (client %pM)\n",
2232 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002233
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002234 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002235
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002236 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2237 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002238
2239out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002240 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002241 kfree_skb(skb);
2242 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002243}
2244
Sven Eckelmanna5130882012-05-16 20:23:16 +02002245static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002246{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002247 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002248 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002249 struct batadv_priv *bat_priv;
2250
2251 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002252 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2253 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002254
Sven Eckelmanna5130882012-05-16 20:23:16 +02002255 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002256 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002257 batadv_tt_req_purge(bat_priv);
2258 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002259
Antonio Quartulli72414442012-12-25 13:14:37 +01002260 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2261 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002262}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002263
Sven Eckelmann56303d32012-06-05 22:31:31 +02002264void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002265{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002266 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002267
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268 batadv_tt_local_table_free(bat_priv);
2269 batadv_tt_global_table_free(bat_priv);
2270 batadv_tt_req_list_free(bat_priv);
2271 batadv_tt_changes_list_free(bat_priv);
2272 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002273
Sven Eckelmann807736f2012-07-15 22:26:51 +02002274 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002275}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002276
Antonio Quartulli697f2532011-11-07 16:47:01 +01002277/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002278 * in the given hash table and returns the number of modified entries
2279 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002280static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2281 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002282{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002283 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002284 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002285 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002286 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002287
2288 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002289 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002290
2291 for (i = 0; i < hash->size; i++) {
2292 head = &hash->table[i];
2293
2294 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002295 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002296 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002297 if (enable) {
2298 if ((tt_common_entry->flags & flags) == flags)
2299 continue;
2300 tt_common_entry->flags |= flags;
2301 } else {
2302 if (!(tt_common_entry->flags & flags))
2303 continue;
2304 tt_common_entry->flags &= ~flags;
2305 }
2306 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002307 }
2308 rcu_read_unlock();
2309 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002310out:
2311 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002312}
2313
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002314/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002315static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002316{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002317 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002318 struct batadv_tt_common_entry *tt_common;
2319 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002320 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002321 struct hlist_head *head;
2322 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002323 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002324
2325 if (!hash)
2326 return;
2327
2328 for (i = 0; i < hash->size; i++) {
2329 head = &hash->table[i];
2330 list_lock = &hash->list_locks[i];
2331
2332 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002333 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002334 hash_entry) {
2335 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002336 continue;
2337
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002338 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002339 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002340 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002341
Sven Eckelmann807736f2012-07-15 22:26:51 +02002342 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002343 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002344 tt_local = container_of(tt_common,
2345 struct batadv_tt_local_entry,
2346 common);
2347 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002348 }
2349 spin_unlock_bh(list_lock);
2350 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002351}
2352
Sven Eckelmann56303d32012-06-05 22:31:31 +02002353static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002354 unsigned char **packet_buff,
2355 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002356{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002357 uint16_t changed_num = 0;
2358
Sven Eckelmann807736f2012-07-15 22:26:51 +02002359 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002360 return -ENOENT;
2361
Sven Eckelmann807736f2012-07-15 22:26:51 +02002362 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002363 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002364
2365 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002366 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002367 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002368 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002369
2370 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002371 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002372 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002373 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002374 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002375
2376 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002377 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002378
Sven Eckelmanna5130882012-05-16 20:23:16 +02002379 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2380 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002381}
2382
2383/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002384int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002385 unsigned char **packet_buff, int *packet_buff_len,
2386 int packet_min_len)
2387{
2388 int tt_num_changes;
2389
2390 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002391 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2392 packet_buff_len,
2393 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002394
2395 /* if the changes have been sent often enough */
2396 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002397 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002398 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2399 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002400 tt_num_changes = 0;
2401 }
2402
2403 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002404}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002405
Sven Eckelmann56303d32012-06-05 22:31:31 +02002406bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002407 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002408{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002409 struct batadv_tt_local_entry *tt_local_entry = NULL;
2410 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002411 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002412
2413 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002414 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002415
Sven Eckelmanna5130882012-05-16 20:23:16 +02002416 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002417 if (!tt_local_entry)
2418 goto out;
2419
Sven Eckelmanna5130882012-05-16 20:23:16 +02002420 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002421 if (!tt_global_entry)
2422 goto out;
2423
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002424 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002425 goto out;
2426
Marek Lindner5870adc2012-06-20 17:16:05 +02002427 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002428
2429out:
2430 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002431 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002432 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002433 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002434 return ret;
2435}
Marek Lindnera943cac2011-07-30 13:10:18 +02002436
Sven Eckelmann56303d32012-06-05 22:31:31 +02002437void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2438 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002439 const unsigned char *tt_buff, uint8_t tt_num_changes,
2440 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002441{
2442 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2443 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002444 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002445
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002446 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002447 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002448 return;
2449
Antonio Quartulli17071572011-11-07 16:36:40 +01002450 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002451 * increased by one -> we can apply the attached changes
2452 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002453 if ((!orig_node->tt_initialised && ttvn == 1) ||
2454 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002455 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002456 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2457 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002458 * In this case send a tt request
2459 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002460 if (!tt_num_changes) {
2461 full_table = false;
2462 goto request_table;
2463 }
2464
Sven Eckelmann96412692012-06-05 22:31:30 +02002465 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002466 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002467 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002468
2469 /* Even if we received the precomputed crc with the OGM, we
2470 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002471 * in the global table
2472 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002473 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002474
2475 /* The ttvn alone is not enough to guarantee consistency
2476 * because a single value could represent different states
2477 * (due to the wrap around). Thus a node has to check whether
2478 * the resulting table (after applying the changes) is still
2479 * consistent or not. E.g. a node could disconnect while its
2480 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2481 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002482 * inconsistency
2483 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002484 if (orig_node->tt_crc != tt_crc)
2485 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002486 } else {
2487 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002488 * in sync anymore -> request fresh tt data
2489 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002490 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2491 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002492request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002493 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002494 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002495 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2496 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002497 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2498 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002499 return;
2500 }
2501 }
2502}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002503
2504/* returns true whether we know that the client has moved from its old
2505 * originator to another one. This entry is kept is still kept for consistency
2506 * purposes
2507 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002508bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002509 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002510{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002511 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002512 bool ret = false;
2513
Sven Eckelmanna5130882012-05-16 20:23:16 +02002514 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002515 if (!tt_global_entry)
2516 goto out;
2517
Antonio Quartullic1d07432013-01-15 22:17:19 +10002518 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002519 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002520out:
2521 return ret;
2522}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002523
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002524/**
2525 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2526 * @bat_priv: the bat priv with all the soft interface information
2527 * @addr: the MAC address of the local client to query
2528 *
2529 * Returns true if the local client is known to be roaming (it is not served by
2530 * this node anymore) or not. If yes, the client is still present in the table
2531 * to keep the latter consistent with the node TTVN
2532 */
2533bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2534 uint8_t *addr)
2535{
2536 struct batadv_tt_local_entry *tt_local_entry;
2537 bool ret = false;
2538
2539 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2540 if (!tt_local_entry)
2541 goto out;
2542
2543 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2544 batadv_tt_local_entry_free_ref(tt_local_entry);
2545out:
2546 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002547}
2548
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002549bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2550 struct batadv_orig_node *orig_node,
2551 const unsigned char *addr)
2552{
2553 bool ret = false;
2554
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002555 /* if the originator is a backbone node (meaning it belongs to the same
2556 * LAN of this node) the temporary client must not be added because to
2557 * reach such destination the node must use the LAN instead of the mesh
2558 */
2559 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2560 goto out;
2561
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002562 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2563 BATADV_TT_CLIENT_TEMP,
2564 atomic_read(&orig_node->last_ttvn)))
2565 goto out;
2566
2567 batadv_dbg(BATADV_DBG_TT, bat_priv,
2568 "Added temporary global client (addr: %pM orig: %pM)\n",
2569 addr, orig_node->orig);
2570 ret = true;
2571out:
2572 return ret;
2573}