blob: 98a66a021a602678d515df58199895bceef31a92 [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{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200388 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800389 int req_len;
390
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200391 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800392
393 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200394 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800395
396 /* if we have too many changes for one packet don't send any
397 * and wait for the tt table request which will be fragmented
398 */
399 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
400 req_len = min_packet_len;
401
Sven Eckelmanna5130882012-05-16 20:23:16 +0200402 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
403 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800404
405 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200406 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800407}
408
Sven Eckelmann56303d32012-06-05 22:31:31 +0200409static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200410 unsigned char **packet_buff,
411 int *packet_buff_len,
412 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800413{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200414 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800415 int count = 0, tot_changes = 0, new_len;
416 unsigned char *tt_buff;
417
Sven Eckelmanna5130882012-05-16 20:23:16 +0200418 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
419 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800420
421 new_len = *packet_buff_len - min_packet_len;
422 tt_buff = *packet_buff + min_packet_len;
423
424 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200425 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Sven Eckelmann807736f2012-07-15 22:26:51 +0200427 spin_lock_bh(&bat_priv->tt.changes_list_lock);
428 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429
Sven Eckelmann807736f2012-07-15 22:26:51 +0200430 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100431 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200433 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200434 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 count++;
436 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 list_del(&entry->list);
438 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200440 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Antonio Quartullia73105b2011-04-27 14:27:44 +0200442 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200443 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
444 kfree(bat_priv->tt.last_changeset);
445 bat_priv->tt.last_changeset_len = 0;
446 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800447 /* check whether this new OGM has no changes due to size problems */
448 if (new_len > 0) {
449 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200450 * instead of providing the diff
451 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200452 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
453 if (bat_priv->tt.last_changeset) {
454 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
455 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 }
457 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200458 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
Marek Lindner08ad76e2012-04-23 16:32:55 +0800460 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461}
462
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200463int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464{
465 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200466 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200467 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200468 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100469 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200472 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100473 int last_seen_secs;
474 int last_seen_msecs;
475 unsigned long last_seen_jiffies;
476 bool no_purge;
477 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Marek Lindner30da63a2012-08-03 17:15:46 +0200479 primary_if = batadv_seq_print_text_primary_if_get(seq);
480 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200481 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100483 seq_printf(seq,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100484 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
485 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
486 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100487 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
488 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 for (i = 0; i < hash->size; i++) {
491 head = &hash->table[i];
492
Marek Lindner7aadf882011-02-18 12:28:09 +0000493 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800494 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000495 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100496 tt_local = container_of(tt_common_entry,
497 struct batadv_tt_local_entry,
498 common);
499 last_seen_jiffies = jiffies - tt_local->last_seen;
500 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
501 last_seen_secs = last_seen_msecs / 1000;
502 last_seen_msecs = last_seen_msecs % 1000;
503
504 no_purge = tt_common_entry->flags & np_flag;
505
506 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100507 tt_common_entry->addr,
508 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200509 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100510 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100511 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100513 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200514 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100515 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100516 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100517 no_purge ? 0 : last_seen_secs,
518 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000520 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200522out:
523 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200524 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200525 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526}
527
Sven Eckelmann56303d32012-06-05 22:31:31 +0200528static void
529batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
530 struct batadv_tt_local_entry *tt_local_entry,
531 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200533 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
534 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535
Antonio Quartulli015758d2011-07-09 17:52:13 +0200536 /* The local client has to be marked as "pending to be removed" but has
537 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200538 * response issued before the net ttvn increment (consistency check)
539 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200540 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100541
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200542 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200543 "Local tt entry (%pM) pending to be removed: %s\n",
544 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545}
546
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200547/**
548 * batadv_tt_local_remove - logically remove an entry from the local table
549 * @bat_priv: the bat priv with all the soft interface information
550 * @addr: the MAC address of the client to remove
551 * @message: message to append to the log on deletion
552 * @roaming: true if the deletion is due to a roaming event
553 *
554 * Returns the flags assigned to the local entry before being deleted
555 */
556uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
557 const uint8_t *addr, const char *message,
558 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200560 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200561 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
Sven Eckelmanna5130882012-05-16 20:23:16 +0200563 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200564 if (!tt_local_entry)
565 goto out;
566
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200567 curr_flags = tt_local_entry->common.flags;
568
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200569 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200570 /* if this global entry addition is due to a roaming, the node has to
571 * mark the local entry as "roamed" in order to correctly reroute
572 * packets later
573 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200574 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200575 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200576 /* mark the local client as ROAMed */
577 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
578 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200579
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200580 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
581 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
582 message);
583 goto out;
584 }
585 /* if this client has been added right now, it is possible to
586 * immediately purge it
587 */
588 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
589 curr_flags | BATADV_TT_CLIENT_DEL);
590 hlist_del_rcu(&tt_local_entry->common.hash_entry);
591 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200592
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200593out:
594 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200595 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200596
597 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598}
599
Sven Eckelmann56303d32012-06-05 22:31:31 +0200600static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200601 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200603 struct batadv_tt_local_entry *tt_local_entry;
604 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800605 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200606
Sasha Levinb67bfe02013-02-27 17:06:00 -0800607 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200608 hash_entry) {
609 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610 struct batadv_tt_local_entry,
611 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200612 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
613 continue;
614
615 /* entry already marked for deletion */
616 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
617 continue;
618
619 if (!batadv_has_timed_out(tt_local_entry->last_seen,
620 BATADV_TT_LOCAL_TIMEOUT))
621 continue;
622
623 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
624 BATADV_TT_CLIENT_DEL, "timed out");
625 }
626}
627
Sven Eckelmann56303d32012-06-05 22:31:31 +0200628static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200629{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200630 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200632 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200633 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635 for (i = 0; i < hash->size; i++) {
636 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200637 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200640 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200641 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643}
644
Sven Eckelmann56303d32012-06-05 22:31:31 +0200645static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200647 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200648 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200649 struct batadv_tt_common_entry *tt_common_entry;
650 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800651 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200652 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200653 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200654
Sven Eckelmann807736f2012-07-15 22:26:51 +0200655 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656 return;
657
Sven Eckelmann807736f2012-07-15 22:26:51 +0200658 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200659
660 for (i = 0; i < hash->size; i++) {
661 head = &hash->table[i];
662 list_lock = &hash->list_locks[i];
663
664 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800665 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200666 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800667 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200668 tt_local = container_of(tt_common_entry,
669 struct batadv_tt_local_entry,
670 common);
671 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200672 }
673 spin_unlock_bh(list_lock);
674 }
675
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200676 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677
Sven Eckelmann807736f2012-07-15 22:26:51 +0200678 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679}
680
Sven Eckelmann56303d32012-06-05 22:31:31 +0200681static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200683 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200684 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Sven Eckelmann807736f2012-07-15 22:26:51 +0200686 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687
Sven Eckelmann807736f2012-07-15 22:26:51 +0200688 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200689 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690
Antonio Quartullidec05072012-11-10 11:00:32 +0100691 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
692 &batadv_tt_global_hash_lock_class_key);
693
Sven Eckelmann5346c352012-05-05 13:27:28 +0200694 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695}
696
Sven Eckelmann56303d32012-06-05 22:31:31 +0200697static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200698{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200699 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200700
Sven Eckelmann807736f2012-07-15 22:26:51 +0200701 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200702
Sven Eckelmann807736f2012-07-15 22:26:51 +0200703 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200704 list) {
705 list_del(&entry->list);
706 kfree(entry);
707 }
708
Sven Eckelmann807736f2012-07-15 22:26:51 +0200709 atomic_set(&bat_priv->tt.local_changes, 0);
710 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200711}
712
Antonio Quartullid657e622012-07-01 14:09:12 +0200713/* retrieves the orig_tt_list_entry belonging to orig_node from the
714 * batadv_tt_global_entry list
715 *
716 * returns it with an increased refcounter, NULL if not found
717 */
718static struct batadv_tt_orig_list_entry *
719batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
720 const struct batadv_orig_node *orig_node)
721{
722 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
723 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200724
725 rcu_read_lock();
726 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800727 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200728 if (tmp_orig_entry->orig_node != orig_node)
729 continue;
730 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
731 continue;
732
733 orig_entry = tmp_orig_entry;
734 break;
735 }
736 rcu_read_unlock();
737
738 return orig_entry;
739}
740
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200742 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200743 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744static bool
745batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
746 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747{
Antonio Quartullid657e622012-07-01 14:09:12 +0200748 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200749 bool found = false;
750
Antonio Quartullid657e622012-07-01 14:09:12 +0200751 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
752 if (orig_entry) {
753 found = true;
754 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200755 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200756
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200757 return found;
758}
759
Sven Eckelmanna5130882012-05-16 20:23:16 +0200760static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200761batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200762 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200763{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200764 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200765
Antonio Quartullid657e622012-07-01 14:09:12 +0200766 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200767 if (orig_entry) {
768 /* refresh the ttvn: the current value could be a bogus one that
769 * was added during a "temporary client detection"
770 */
771 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200772 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200773 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200774
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200775 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
776 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200777 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200778
779 INIT_HLIST_NODE(&orig_entry->list);
780 atomic_inc(&orig_node->refcount);
781 atomic_inc(&orig_node->tt_size);
782 orig_entry->orig_node = orig_node;
783 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200784 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200785
Antonio Quartullid657e622012-07-01 14:09:12 +0200786 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200787 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200788 &tt_global->orig_list);
789 spin_unlock_bh(&tt_global->list_lock);
790out:
791 if (orig_entry)
792 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200793}
794
Antonio Quartullia73105b2011-04-27 14:27:44 +0200795/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200796int batadv_tt_global_add(struct batadv_priv *bat_priv,
797 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200798 const unsigned char *tt_addr, uint8_t flags,
799 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200801 struct batadv_tt_global_entry *tt_global_entry;
802 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200803 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100804 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200805 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200806 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807
Sven Eckelmanna5130882012-05-16 20:23:16 +0200808 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200809 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
810
811 /* if the node already has a local client for this entry, it has to wait
812 * for a roaming advertisement instead of manually messing up the global
813 * table
814 */
815 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
816 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
817 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000818
Antonio Quartullia73105b2011-04-27 14:27:44 +0200819 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200820 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200821 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200822 goto out;
823
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200824 common = &tt_global_entry->common;
825 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200826
Antonio Quartullid4f44692012-05-25 00:00:54 +0200827 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200828 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200829 /* node must store current time in case of roaming. This is
830 * needed to purge this entry out on timeout (if nobody claims
831 * it)
832 */
833 if (flags & BATADV_TT_CLIENT_ROAM)
834 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200835 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200836 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200837
838 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
839 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200840
Sven Eckelmann807736f2012-07-15 22:26:51 +0200841 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200842 batadv_compare_tt,
843 batadv_choose_orig, common,
844 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100845
846 if (unlikely(hash_added != 0)) {
847 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200848 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100849 goto out_remove;
850 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200851 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200852 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200853 /* If there is already a global entry, we can use this one for
854 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200855 * But if we are trying to add a temporary client then here are
856 * two options at this point:
857 * 1) the global client is not a temporary client: the global
858 * client has to be left as it is, temporary information
859 * should never override any already known client state
860 * 2) the global client is a temporary client: purge the
861 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200862 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200863 if (flags & BATADV_TT_CLIENT_TEMP) {
864 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
865 goto out;
866 if (batadv_tt_global_entry_has_orig(tt_global_entry,
867 orig_node))
868 goto out_remove;
869 batadv_tt_global_del_orig_list(tt_global_entry);
870 goto add_orig_entry;
871 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200872
873 /* if the client was temporary added before receiving the first
874 * OGM announcing it, we have to clear the TEMP flag
875 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200876 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200877
Antonio Quartullie9c001362012-11-07 15:05:33 +0100878 /* the change can carry possible "attribute" flags like the
879 * TT_CLIENT_WIFI, therefore they have to be copied in the
880 * client entry
881 */
882 tt_global_entry->common.flags |= flags;
883
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200884 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
885 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200886 * delete + roaming change for this originator.
887 *
888 * We should first delete the old originator before adding the
889 * new one.
890 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200891 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200892 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200893 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200894 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895 }
896 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200897add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200898 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200899 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200900
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200901 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200902 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200903 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200904 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200905
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100906out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200907
Antonio Quartullia73105b2011-04-27 14:27:44 +0200908 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200909 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
910 "global tt received",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200911 !!(flags & BATADV_TT_CLIENT_ROAM));
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200912 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
913
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200914 if (!(flags & BATADV_TT_CLIENT_ROAM))
915 /* this is a normal global add. Therefore the client is not in a
916 * roaming state anymore.
917 */
918 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
919
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200920out:
921 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200922 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200923 if (tt_local_entry)
924 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200925 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926}
927
Sven Eckelmann981d8902012-10-07 13:34:15 +0200928/* batadv_transtable_best_orig - Get best originator list entry from tt entry
929 * @tt_global_entry: global translation table entry to be analyzed
930 *
931 * This functon assumes the caller holds rcu_read_lock().
932 * Returns best originator list entry or NULL on errors.
933 */
934static struct batadv_tt_orig_list_entry *
935batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
936{
937 struct batadv_neigh_node *router = NULL;
938 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200939 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
940 int best_tq = 0;
941
942 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800943 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200944 router = batadv_orig_node_get_router(orig_entry->orig_node);
945 if (!router)
946 continue;
947
948 if (router->tq_avg > best_tq) {
949 best_entry = orig_entry;
950 best_tq = router->tq_avg;
951 }
952
953 batadv_neigh_node_free_ref(router);
954 }
955
956 return best_entry;
957}
958
959/* batadv_tt_global_print_entry - print all orig nodes who announce the address
960 * for this global entry
961 * @tt_global_entry: global translation table entry to be printed
962 * @seq: debugfs table seq_file struct
963 *
964 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200965 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200966static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200967batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200968 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200969{
970 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200971 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200972 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200973 uint16_t flags;
974 uint8_t last_ttvn;
975
976 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200977 flags = tt_common_entry->flags;
978
979 best_entry = batadv_transtable_best_orig(tt_global_entry);
980 if (best_entry) {
981 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100982 seq_printf(seq,
983 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200984 '*', tt_global_entry->common.addr,
985 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100986 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200987 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
988 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
989 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
990 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200991
992 head = &tt_global_entry->orig_list;
993
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200995 if (best_entry == orig_entry)
996 continue;
997
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200998 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +0200999 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1000 '+', tt_global_entry->common.addr,
1001 orig_entry->ttvn, orig_entry->orig_node->orig,
1002 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001003 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001004 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1005 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001006 }
1007}
1008
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001009int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001010{
1011 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001012 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001013 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001014 struct batadv_tt_common_entry *tt_common_entry;
1015 struct batadv_tt_global_entry *tt_global;
1016 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001018 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019
Marek Lindner30da63a2012-08-03 17:15:46 +02001020 primary_if = batadv_seq_print_text_primary_if_get(seq);
1021 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001022 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001024 seq_printf(seq,
1025 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026 net_dev->name);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001027 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1028 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1029 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031 for (i = 0; i < hash->size; i++) {
1032 head = &hash->table[i];
1033
Marek Lindner7aadf882011-02-18 12:28:09 +00001034 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001035 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001036 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001037 tt_global = container_of(tt_common_entry,
1038 struct batadv_tt_global_entry,
1039 common);
1040 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001042 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001044out:
1045 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001046 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001047 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048}
1049
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001050/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001051static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001052batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001054 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001055 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001056 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001057
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001058 spin_lock_bh(&tt_global_entry->list_lock);
1059 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001060 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1061 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001062 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001063 }
1064 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001065}
1066
Sven Eckelmanna5130882012-05-16 20:23:16 +02001067static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001068batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1069 struct batadv_tt_global_entry *tt_global_entry,
1070 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001071 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072{
1073 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001074 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076
1077 spin_lock_bh(&tt_global_entry->list_lock);
1078 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001079 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001080 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001081 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001082 "Deleting %pM from global tt entry %pM: %s\n",
1083 orig_node->orig,
1084 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001085 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001086 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001087 }
1088 }
1089 spin_unlock_bh(&tt_global_entry->list_lock);
1090}
1091
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001092/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001093 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1094 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001096static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001097batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1098 struct batadv_tt_global_entry *tt_global_entry,
1099 struct batadv_orig_node *orig_node,
1100 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001101{
1102 bool last_entry = true;
1103 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001104 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001105
1106 /* no local entry exists, case 1:
1107 * Check if this is the last one or if other entries exist.
1108 */
1109
1110 rcu_read_lock();
1111 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001112 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001113 if (orig_entry->orig_node != orig_node) {
1114 last_entry = false;
1115 break;
1116 }
1117 }
1118 rcu_read_unlock();
1119
1120 if (last_entry) {
1121 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001122 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001123 tt_global_entry->roam_at = jiffies;
1124 } else
1125 /* there is another entry, we can simply delete this
1126 * one and can still use the other one.
1127 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001128 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1129 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001130}
1131
1132
1133
Sven Eckelmann56303d32012-06-05 22:31:31 +02001134static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1135 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001136 const unsigned char *addr,
1137 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001138{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001139 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001140 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141
Sven Eckelmanna5130882012-05-16 20:23:16 +02001142 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001143 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001144 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001145
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001146 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001147 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1148 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001149
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001150 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001151 batadv_tt_global_free(bat_priv, tt_global_entry,
1152 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001153
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001154 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001155 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001156
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001157 /* if we are deleting a global entry due to a roam
1158 * event, there are two possibilities:
1159 * 1) the client roamed from node A to node B => if there
1160 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001161 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001162 * wait for node B to claim it. In case of timeout
1163 * the entry is purged.
1164 *
1165 * If there are other originators left, we directly delete
1166 * the originator.
1167 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001168 * the global entry, since it is useless now.
1169 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001170 local_entry = batadv_tt_local_hash_find(bat_priv,
1171 tt_global_entry->common.addr);
1172 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001173 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001174 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001175 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001176 } else
1177 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001178 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1179 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001180
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001181
Antonio Quartullicc47f662011-04-27 14:27:57 +02001182out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001183 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001184 batadv_tt_global_entry_free_ref(tt_global_entry);
1185 if (local_entry)
1186 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001187}
1188
Sven Eckelmann56303d32012-06-05 22:31:31 +02001189void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1190 struct batadv_orig_node *orig_node,
1191 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001192{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001193 struct batadv_tt_global_entry *tt_global;
1194 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001195 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001196 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001198 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001199 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200
Simon Wunderlich6e801492011-10-19 10:28:26 +02001201 if (!hash)
1202 return;
1203
Antonio Quartullia73105b2011-04-27 14:27:44 +02001204 for (i = 0; i < hash->size; i++) {
1205 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001206 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001208 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001210 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001211 tt_global = container_of(tt_common_entry,
1212 struct batadv_tt_global_entry,
1213 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001214
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001216 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001217
Sven Eckelmann56303d32012-06-05 22:31:31 +02001218 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001219 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001220 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001221 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001222 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001223 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001224 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001225 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001226 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001227 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001228 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229}
1230
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001231static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1232 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001233{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001234 bool purge = false;
1235 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1236 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001237
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001238 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1239 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1240 purge = true;
1241 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001242 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001243
1244 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1245 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1246 purge = true;
1247 *msg = "Temporary client timeout\n";
1248 }
1249
1250 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001251}
1252
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001253static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001254{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001255 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001256 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001257 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001258 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001259 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001260 char *msg = NULL;
1261 struct batadv_tt_common_entry *tt_common;
1262 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001263
Antonio Quartullicc47f662011-04-27 14:27:57 +02001264 for (i = 0; i < hash->size; i++) {
1265 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001266 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001267
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001268 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001269 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001270 hash_entry) {
1271 tt_global = container_of(tt_common,
1272 struct batadv_tt_global_entry,
1273 common);
1274
1275 if (!batadv_tt_global_to_purge(tt_global, &msg))
1276 continue;
1277
1278 batadv_dbg(BATADV_DBG_TT, bat_priv,
1279 "Deleting global tt entry (%pM): %s\n",
1280 tt_global->common.addr, msg);
1281
Sasha Levinb67bfe02013-02-27 17:06:00 -08001282 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001283
1284 batadv_tt_global_entry_free_ref(tt_global);
1285 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001286 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001287 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001288}
1289
Sven Eckelmann56303d32012-06-05 22:31:31 +02001290static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001291{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001292 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001293 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001294 struct batadv_tt_common_entry *tt_common_entry;
1295 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001296 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001297 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001298 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001299
Sven Eckelmann807736f2012-07-15 22:26:51 +02001300 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001301 return;
1302
Sven Eckelmann807736f2012-07-15 22:26:51 +02001303 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304
1305 for (i = 0; i < hash->size; i++) {
1306 head = &hash->table[i];
1307 list_lock = &hash->list_locks[i];
1308
1309 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001310 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001311 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001312 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001313 tt_global = container_of(tt_common_entry,
1314 struct batadv_tt_global_entry,
1315 common);
1316 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001317 }
1318 spin_unlock_bh(list_lock);
1319 }
1320
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001321 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001322
Sven Eckelmann807736f2012-07-15 22:26:51 +02001323 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001324}
1325
Sven Eckelmann56303d32012-06-05 22:31:31 +02001326static bool
1327_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1328 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001329{
1330 bool ret = false;
1331
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001332 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1333 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001334 ret = true;
1335
1336 return ret;
1337}
1338
Sven Eckelmann56303d32012-06-05 22:31:31 +02001339struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1340 const uint8_t *src,
1341 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001342{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001343 struct batadv_tt_local_entry *tt_local_entry = NULL;
1344 struct batadv_tt_global_entry *tt_global_entry = NULL;
1345 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001346 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001348 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001349 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001350 if (!tt_local_entry ||
1351 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001352 goto out;
1353 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001354
Sven Eckelmanna5130882012-05-16 20:23:16 +02001355 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001356 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001357 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001358
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001359 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001360 * isolation
1361 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001362 if (tt_local_entry &&
1363 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001364 goto out;
1365
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001366 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001367 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001368 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001369 if (best_entry)
1370 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001371 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1372 orig_node = NULL;
1373 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001374
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001375out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001376 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001377 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001378 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001379 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001380
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001381 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001382}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001383
1384/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001385static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1386 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001387{
1388 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001389 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001390 struct batadv_tt_common_entry *tt_common;
1391 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001392 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001393 uint32_t i;
1394 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395
1396 for (i = 0; i < hash->size; i++) {
1397 head = &hash->table[i];
1398
1399 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001400 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001401 tt_global = container_of(tt_common,
1402 struct batadv_tt_global_entry,
1403 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001404 /* Roaming clients are in the global table for
1405 * consistency only. They don't have to be
1406 * taken into account while computing the
1407 * global crc
1408 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001409 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001410 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001411 /* Temporary clients have not been announced yet, so
1412 * they have to be skipped while computing the global
1413 * crc
1414 */
1415 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1416 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001417
1418 /* find out if this global entry is announced by this
1419 * originator
1420 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001421 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001422 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001423 continue;
1424
1425 total_one = 0;
1426 for (j = 0; j < ETH_ALEN; j++)
1427 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001428 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001429 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001430 }
1431 rcu_read_unlock();
1432 }
1433
1434 return total;
1435}
1436
1437/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001438static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439{
1440 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001441 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001442 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001443 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001444 uint32_t i;
1445 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446
1447 for (i = 0; i < hash->size; i++) {
1448 head = &hash->table[i];
1449
1450 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001451 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001452 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001453 * account while computing the CRC
1454 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001455 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001456 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457 total_one = 0;
1458 for (j = 0; j < ETH_ALEN; j++)
1459 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001460 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001461 total ^= total_one;
1462 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463 rcu_read_unlock();
1464 }
1465
1466 return total;
1467}
1468
Sven Eckelmann56303d32012-06-05 22:31:31 +02001469static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001471 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472
Sven Eckelmann807736f2012-07-15 22:26:51 +02001473 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474
Sven Eckelmann807736f2012-07-15 22:26:51 +02001475 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 list_del(&node->list);
1477 kfree(node);
1478 }
1479
Sven Eckelmann807736f2012-07-15 22:26:51 +02001480 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481}
1482
Sven Eckelmann56303d32012-06-05 22:31:31 +02001483static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1484 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001485 const unsigned char *tt_buff,
1486 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001488 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489
1490 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001491 * last OGM (the OGM could carry no changes)
1492 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001493 spin_lock_bh(&orig_node->tt_buff_lock);
1494 if (tt_buff_len > 0) {
1495 kfree(orig_node->tt_buff);
1496 orig_node->tt_buff_len = 0;
1497 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1498 if (orig_node->tt_buff) {
1499 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1500 orig_node->tt_buff_len = tt_buff_len;
1501 }
1502 }
1503 spin_unlock_bh(&orig_node->tt_buff_lock);
1504}
1505
Sven Eckelmann56303d32012-06-05 22:31:31 +02001506static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001508 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001509
Sven Eckelmann807736f2012-07-15 22:26:51 +02001510 spin_lock_bh(&bat_priv->tt.req_list_lock);
1511 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001512 if (batadv_has_timed_out(node->issued_at,
1513 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001514 list_del(&node->list);
1515 kfree(node);
1516 }
1517 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001518 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519}
1520
1521/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001522 * has already been issued for this orig_node, NULL otherwise
1523 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001524static struct batadv_tt_req_node *
1525batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1526 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001528 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529
Sven Eckelmann807736f2012-07-15 22:26:51 +02001530 spin_lock_bh(&bat_priv->tt.req_list_lock);
1531 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001532 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1533 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001534 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001535 goto unlock;
1536 }
1537
1538 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1539 if (!tt_req_node)
1540 goto unlock;
1541
1542 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1543 tt_req_node->issued_at = jiffies;
1544
Sven Eckelmann807736f2012-07-15 22:26:51 +02001545 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001546unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001547 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001548 return tt_req_node;
1549}
1550
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001551/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001552static int batadv_tt_local_valid_entry(const void *entry_ptr,
1553 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001554{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001555 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001556
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001557 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001558 return 0;
1559 return 1;
1560}
1561
Sven Eckelmanna5130882012-05-16 20:23:16 +02001562static int batadv_tt_global_valid(const void *entry_ptr,
1563 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001564{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001565 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1566 const struct batadv_tt_global_entry *tt_global_entry;
1567 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001568
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001569 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1570 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001571 return 0;
1572
Sven Eckelmann56303d32012-06-05 22:31:31 +02001573 tt_global_entry = container_of(tt_common_entry,
1574 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001575 common);
1576
Sven Eckelmanna5130882012-05-16 20:23:16 +02001577 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578}
1579
Sven Eckelmanna5130882012-05-16 20:23:16 +02001580static struct sk_buff *
1581batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001582 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001583 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001584 int (*valid_cb)(const void *, const void *),
1585 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001587 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001588 struct batadv_tt_query_packet *tt_response;
1589 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001590 struct hlist_head *head;
1591 struct sk_buff *skb = NULL;
1592 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001593 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001594 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001595 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596
1597 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1598 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001599 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001600 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001601 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001602
Sven Eckelmann96412692012-06-05 22:31:30 +02001603 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001604 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001605 if (!skb)
1606 goto out;
1607
Sven Eckelmann5b246572012-11-04 17:11:45 +01001608 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001609 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611
Sven Eckelmann96412692012-06-05 22:31:30 +02001612 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613 tt_count = 0;
1614
1615 rcu_read_lock();
1616 for (i = 0; i < hash->size; i++) {
1617 head = &hash->table[i];
1618
Sasha Levinb67bfe02013-02-27 17:06:00 -08001619 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620 head, hash_entry) {
1621 if (tt_count == tt_tot)
1622 break;
1623
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001624 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001625 continue;
1626
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001627 memcpy(tt_change->addr, tt_common_entry->addr,
1628 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001629 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630
1631 tt_count++;
1632 tt_change++;
1633 }
1634 }
1635 rcu_read_unlock();
1636
Antonio Quartulli9d852392011-10-17 14:25:13 +02001637 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001638 * copied
1639 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001640 tt_response->tt_data = htons(tt_count);
1641
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642out:
1643 return skb;
1644}
1645
Sven Eckelmann56303d32012-06-05 22:31:31 +02001646static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1647 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001648 uint8_t ttvn, uint16_t tt_crc,
1649 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001650{
1651 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001652 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001653 struct batadv_hard_iface *primary_if;
1654 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001655 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001656 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001657
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001658 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659 if (!primary_if)
1660 goto out;
1661
1662 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001663 * reply from the same orig_node yet
1664 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001665 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666 if (!tt_req_node)
1667 goto out;
1668
Sven Eckelmann5b246572012-11-04 17:11:45 +01001669 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001670 if (!skb)
1671 goto out;
1672
Sven Eckelmann5b246572012-11-04 17:11:45 +01001673 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001674
Sven Eckelmann96412692012-06-05 22:31:30 +02001675 tt_req_len = sizeof(*tt_request);
1676 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001678 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001679 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001680 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1681 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001682 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001683 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001684 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001685 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686
1687 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001688 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001689
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001690 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1691 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001693 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001694
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001695 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1696 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697
1698out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001699 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001700 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701 if (ret)
1702 kfree_skb(skb);
1703 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001704 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001706 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 kfree(tt_req_node);
1708 }
1709 return ret;
1710}
1711
Sven Eckelmann96412692012-06-05 22:31:30 +02001712static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001713batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001714 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001716 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001717 struct batadv_orig_node *res_dst_orig_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001718 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719 uint8_t orig_ttvn, req_ttvn, ttvn;
1720 int ret = false;
1721 unsigned char *tt_buff;
1722 bool full_table;
1723 uint16_t tt_len, tt_tot;
1724 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001725 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001726 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001727 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001729 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001730 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1731 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001732 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001733
1734 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001735 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001736 if (!req_dst_orig_node)
1737 goto out;
1738
Sven Eckelmannda641192012-05-12 13:48:56 +02001739 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001740 if (!res_dst_orig_node)
1741 goto out;
1742
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001743 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744 if (!primary_if)
1745 goto out;
1746
1747 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1748 req_ttvn = tt_request->ttvn;
1749
Antonio Quartulli015758d2011-07-09 17:52:13 +02001750 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001752 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753 goto out;
1754
Antonio Quartulli015758d2011-07-09 17:52:13 +02001755 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001756 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 !req_dst_orig_node->tt_buff)
1758 full_table = true;
1759 else
1760 full_table = false;
1761
1762 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001763 * I'll send only one packet with as much TT entries as I can
1764 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 if (!full_table) {
1766 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1767 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001768 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001769
Sven Eckelmann96412692012-06-05 22:31:30 +02001770 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001771 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772 if (!skb)
1773 goto unlock;
1774
Sven Eckelmann5b246572012-11-04 17:11:45 +01001775 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001776 packet_pos = skb_put(skb, len);
1777 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001778 tt_response->ttvn = req_ttvn;
1779 tt_response->tt_data = htons(tt_tot);
1780
Sven Eckelmann96412692012-06-05 22:31:30 +02001781 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782 /* Copy the last orig_node's OGM buffer */
1783 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1784 req_dst_orig_node->tt_buff_len);
1785
1786 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1787 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001788 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1789 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001790 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1791
Sven Eckelmanna5130882012-05-16 20:23:16 +02001792 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001793 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001794 primary_if,
1795 batadv_tt_global_valid,
1796 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001797 if (!skb)
1798 goto out;
1799
Sven Eckelmann96412692012-06-05 22:31:30 +02001800 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801 }
1802
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001803 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001804 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001805 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001806 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1807 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001808 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001809
1810 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001811 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001813 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001814 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1815 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001817 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001818
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001819 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1820 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001821 goto out;
1822
1823unlock:
1824 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1825
1826out:
1827 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001828 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001830 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001831 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001832 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001833 if (!ret)
1834 kfree_skb(skb);
1835 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836}
Sven Eckelmann96412692012-06-05 22:31:30 +02001837
1838static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001839batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001840 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001842 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001843 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 uint8_t my_ttvn, req_ttvn, ttvn;
1845 int ret = false;
1846 unsigned char *tt_buff;
1847 bool full_table;
1848 uint16_t tt_len, tt_tot;
1849 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001850 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001851 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001852 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001854 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001855 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1856 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001857 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001858
1859
Sven Eckelmann807736f2012-07-15 22:26:51 +02001860 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861 req_ttvn = tt_request->ttvn;
1862
Sven Eckelmannda641192012-05-12 13:48:56 +02001863 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864 if (!orig_node)
1865 goto out;
1866
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001867 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868 if (!primary_if)
1869 goto out;
1870
1871 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001872 * is too big send the whole local translation table
1873 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001874 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001875 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876 full_table = true;
1877 else
1878 full_table = false;
1879
1880 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001881 * I'll send only one packet with as much TT entries as I can
1882 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001884 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1885 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001886 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001887
Sven Eckelmann96412692012-06-05 22:31:30 +02001888 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001889 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001890 if (!skb)
1891 goto unlock;
1892
Sven Eckelmann5b246572012-11-04 17:11:45 +01001893 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001894 packet_pos = skb_put(skb, len);
1895 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001896 tt_response->ttvn = req_ttvn;
1897 tt_response->tt_data = htons(tt_tot);
1898
Sven Eckelmann96412692012-06-05 22:31:30 +02001899 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001900 memcpy(tt_buff, bat_priv->tt.last_changeset,
1901 bat_priv->tt.last_changeset_len);
1902 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001904 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001905 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001906 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001907
Sven Eckelmanna5130882012-05-16 20:23:16 +02001908 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001909 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001910 primary_if,
1911 batadv_tt_local_valid_entry,
1912 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001913 if (!skb)
1914 goto out;
1915
Sven Eckelmann96412692012-06-05 22:31:30 +02001916 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001917 }
1918
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001919 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001920 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001921 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1923 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001924 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925
1926 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001927 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001929 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001930 "Sending TT_RESPONSE to %pM [%c]\n",
1931 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001932 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001934 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001935
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001936 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1937 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001938 goto out;
1939
1940unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001941 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942out:
1943 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001944 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001945 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001946 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001947 if (!ret)
1948 kfree_skb(skb);
1949 /* This packet was for me, so it doesn't need to be re-routed */
1950 return true;
1951}
1952
Sven Eckelmann56303d32012-06-05 22:31:31 +02001953bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001954 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001956 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001957 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001958 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001959 return true;
1960
Sven Eckelmanna5130882012-05-16 20:23:16 +02001961 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001962 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001963 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001964 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001965}
1966
Sven Eckelmann56303d32012-06-05 22:31:31 +02001967static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1968 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001969 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001970 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001971{
1972 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001973 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001974
1975 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001976 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1977 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001978 batadv_tt_global_del(bat_priv, orig_node,
1979 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001980 "tt removed by changes",
1981 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001982 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001983 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001984 (tt_change + i)->addr,
1985 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986 /* In case of problem while storing a
1987 * global_entry, we stop the updating
1988 * procedure without committing the
1989 * ttvn change. This will avoid to send
1990 * corrupted data on tt_request
1991 */
1992 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001993 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001994 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001995 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996}
1997
Sven Eckelmann56303d32012-06-05 22:31:31 +02001998static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001999 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002000{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002001 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002002
Sven Eckelmannda641192012-05-12 13:48:56 +02002003 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002004 if (!orig_node)
2005 goto out;
2006
2007 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002008 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009
Sven Eckelmanna5130882012-05-16 20:23:16 +02002010 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002011 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002012 ntohs(tt_response->tt_data),
2013 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014
2015 spin_lock_bh(&orig_node->tt_buff_lock);
2016 kfree(orig_node->tt_buff);
2017 orig_node->tt_buff_len = 0;
2018 orig_node->tt_buff = NULL;
2019 spin_unlock_bh(&orig_node->tt_buff_lock);
2020
2021 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2022
2023out:
2024 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002025 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026}
2027
Sven Eckelmann56303d32012-06-05 22:31:31 +02002028static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2029 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002030 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002031 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002032{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002033 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2034 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002035
Sven Eckelmanna5130882012-05-16 20:23:16 +02002036 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2037 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002038 atomic_set(&orig_node->last_ttvn, ttvn);
2039}
2040
Sven Eckelmann56303d32012-06-05 22:31:31 +02002041bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002042{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002043 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002044 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002045
Sven Eckelmanna5130882012-05-16 20:23:16 +02002046 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002047 if (!tt_local_entry)
2048 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002049 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002050 * consistency purpose)
2051 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002052 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2053 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002054 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002055 ret = true;
2056out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002057 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002058 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002059 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060}
2061
Sven Eckelmann56303d32012-06-05 22:31:31 +02002062void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002063 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002064{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002065 struct batadv_tt_req_node *node, *safe;
2066 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002067 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002068
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002069 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002070 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2071 tt_response->src, tt_response->ttvn,
2072 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002073 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002074
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002075 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002076 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002077 goto out;
2078
Sven Eckelmannda641192012-05-12 13:48:56 +02002079 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080 if (!orig_node)
2081 goto out;
2082
Sven Eckelmann96412692012-06-05 22:31:30 +02002083 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002084 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002085 } else {
2086 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002087 batadv_tt_update_changes(bat_priv, orig_node,
2088 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002089 tt_response->ttvn, tt_change);
2090 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091
2092 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002093 spin_lock_bh(&bat_priv->tt.req_list_lock);
2094 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002095 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096 continue;
2097 list_del(&node->list);
2098 kfree(node);
2099 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002100 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101
2102 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002103 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104out:
2105 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002106 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002107}
2108
Sven Eckelmann56303d32012-06-05 22:31:31 +02002109int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002110{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002111 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112
Sven Eckelmanna5130882012-05-16 20:23:16 +02002113 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002114 if (ret < 0)
2115 return ret;
2116
Sven Eckelmanna5130882012-05-16 20:23:16 +02002117 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002118 if (ret < 0)
2119 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002120
Antonio Quartulli72414442012-12-25 13:14:37 +01002121 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2122 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2123 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124
2125 return 1;
2126}
2127
Sven Eckelmann56303d32012-06-05 22:31:31 +02002128static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002129{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002130 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002131
Sven Eckelmann807736f2012-07-15 22:26:51 +02002132 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002133
Sven Eckelmann807736f2012-07-15 22:26:51 +02002134 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002135 list_del(&node->list);
2136 kfree(node);
2137 }
2138
Sven Eckelmann807736f2012-07-15 22:26:51 +02002139 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002140}
2141
Sven Eckelmann56303d32012-06-05 22:31:31 +02002142static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002143{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002144 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002145
Sven Eckelmann807736f2012-07-15 22:26:51 +02002146 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2147 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002148 if (!batadv_has_timed_out(node->first_time,
2149 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002150 continue;
2151
2152 list_del(&node->list);
2153 kfree(node);
2154 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002155 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002156}
2157
2158/* This function checks whether the client already reached the
2159 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2160 * will not be sent.
2161 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002162 * returns true if the ROAMING_ADV can be sent, false otherwise
2163 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002164static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002165 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002166{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002167 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002168 bool ret = false;
2169
Sven Eckelmann807736f2012-07-15 22:26:51 +02002170 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002171 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002172 * reply from the same orig_node yet
2173 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002174 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002175 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002176 continue;
2177
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002178 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002179 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002180 continue;
2181
Sven Eckelmann3e348192012-05-16 20:23:22 +02002182 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002183 /* Sorry, you roamed too many times! */
2184 goto unlock;
2185 ret = true;
2186 break;
2187 }
2188
2189 if (!ret) {
2190 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2191 if (!tt_roam_node)
2192 goto unlock;
2193
2194 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002195 atomic_set(&tt_roam_node->counter,
2196 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002197 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2198
Sven Eckelmann807736f2012-07-15 22:26:51 +02002199 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002200 ret = true;
2201 }
2202
2203unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002204 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002205 return ret;
2206}
2207
Sven Eckelmann56303d32012-06-05 22:31:31 +02002208static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2209 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002210{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002211 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002212 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002213 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002214 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002215 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002216
2217 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002218 * already roamed to us too many times
2219 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002220 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002221 goto out;
2222
Sven Eckelmann5b246572012-11-04 17:11:45 +01002223 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002224 if (!skb)
2225 goto out;
2226
Sven Eckelmann5b246572012-11-04 17:11:45 +01002227 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002228
Sven Eckelmann96412692012-06-05 22:31:30 +02002229 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002230
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002231 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002232 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002233 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002234 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002235 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002236 if (!primary_if)
2237 goto out;
2238 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002239 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002240 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2241 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2242
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002243 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002244 "Sending ROAMING_ADV to %pM (client %pM)\n",
2245 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002246
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002247 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002248
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002249 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2250 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002251
2252out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002253 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002254 kfree_skb(skb);
2255 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002256}
2257
Sven Eckelmanna5130882012-05-16 20:23:16 +02002258static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002259{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002260 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002261 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002262 struct batadv_priv *bat_priv;
2263
2264 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002265 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2266 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002269 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002270 batadv_tt_req_purge(bat_priv);
2271 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002272
Antonio Quartulli72414442012-12-25 13:14:37 +01002273 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2274 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002275}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002276
Sven Eckelmann56303d32012-06-05 22:31:31 +02002277void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002278{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002279 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002280
Sven Eckelmanna5130882012-05-16 20:23:16 +02002281 batadv_tt_local_table_free(bat_priv);
2282 batadv_tt_global_table_free(bat_priv);
2283 batadv_tt_req_list_free(bat_priv);
2284 batadv_tt_changes_list_free(bat_priv);
2285 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002286
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002288}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002289
Antonio Quartulli697f2532011-11-07 16:47:01 +01002290/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002291 * in the given hash table and returns the number of modified entries
2292 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002293static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2294 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002295{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002296 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002297 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002298 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002299 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002300
2301 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002302 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002303
2304 for (i = 0; i < hash->size; i++) {
2305 head = &hash->table[i];
2306
2307 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002308 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002309 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002310 if (enable) {
2311 if ((tt_common_entry->flags & flags) == flags)
2312 continue;
2313 tt_common_entry->flags |= flags;
2314 } else {
2315 if (!(tt_common_entry->flags & flags))
2316 continue;
2317 tt_common_entry->flags &= ~flags;
2318 }
2319 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002320 }
2321 rcu_read_unlock();
2322 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002323out:
2324 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002325}
2326
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002327/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002328static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002329{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002330 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002331 struct batadv_tt_common_entry *tt_common;
2332 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002333 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002334 struct hlist_head *head;
2335 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002336 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002337
2338 if (!hash)
2339 return;
2340
2341 for (i = 0; i < hash->size; i++) {
2342 head = &hash->table[i];
2343 list_lock = &hash->list_locks[i];
2344
2345 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002346 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002347 hash_entry) {
2348 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002349 continue;
2350
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002351 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002352 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002353 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002354
Sven Eckelmann807736f2012-07-15 22:26:51 +02002355 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002356 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002357 tt_local = container_of(tt_common,
2358 struct batadv_tt_local_entry,
2359 common);
2360 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002361 }
2362 spin_unlock_bh(list_lock);
2363 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002364}
2365
Sven Eckelmann56303d32012-06-05 22:31:31 +02002366static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002367 unsigned char **packet_buff,
2368 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002369{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002370 uint16_t changed_num = 0;
2371
Sven Eckelmann807736f2012-07-15 22:26:51 +02002372 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002373 return -ENOENT;
2374
Sven Eckelmann807736f2012-07-15 22:26:51 +02002375 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002376 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002377
2378 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002379 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002380 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002381 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002382
2383 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002384 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002385 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002386 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002387 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002388
2389 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002390 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002391
Sven Eckelmanna5130882012-05-16 20:23:16 +02002392 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2393 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002394}
2395
2396/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002397int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002398 unsigned char **packet_buff, int *packet_buff_len,
2399 int packet_min_len)
2400{
2401 int tt_num_changes;
2402
2403 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002404 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2405 packet_buff_len,
2406 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002407
2408 /* if the changes have been sent often enough */
2409 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002410 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002411 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2412 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002413 tt_num_changes = 0;
2414 }
2415
2416 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002417}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002418
Sven Eckelmann56303d32012-06-05 22:31:31 +02002419bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002420 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002421{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002422 struct batadv_tt_local_entry *tt_local_entry = NULL;
2423 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002424 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002425
2426 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002427 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002428
Sven Eckelmanna5130882012-05-16 20:23:16 +02002429 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002430 if (!tt_local_entry)
2431 goto out;
2432
Sven Eckelmanna5130882012-05-16 20:23:16 +02002433 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002434 if (!tt_global_entry)
2435 goto out;
2436
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002437 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002438 goto out;
2439
Marek Lindner5870adc2012-06-20 17:16:05 +02002440 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002441
2442out:
2443 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002444 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002445 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002446 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002447 return ret;
2448}
Marek Lindnera943cac2011-07-30 13:10:18 +02002449
Sven Eckelmann56303d32012-06-05 22:31:31 +02002450void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2451 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002452 const unsigned char *tt_buff, uint8_t tt_num_changes,
2453 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002454{
2455 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2456 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002457 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002458
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002459 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002460 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002461 return;
2462
Antonio Quartulli17071572011-11-07 16:36:40 +01002463 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002464 * increased by one -> we can apply the attached changes
2465 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002466 if ((!orig_node->tt_initialised && ttvn == 1) ||
2467 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002468 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002469 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2470 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002471 * In this case send a tt request
2472 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002473 if (!tt_num_changes) {
2474 full_table = false;
2475 goto request_table;
2476 }
2477
Sven Eckelmann96412692012-06-05 22:31:30 +02002478 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002479 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002480 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002481
2482 /* Even if we received the precomputed crc with the OGM, we
2483 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002484 * in the global table
2485 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002486 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002487
2488 /* The ttvn alone is not enough to guarantee consistency
2489 * because a single value could represent different states
2490 * (due to the wrap around). Thus a node has to check whether
2491 * the resulting table (after applying the changes) is still
2492 * consistent or not. E.g. a node could disconnect while its
2493 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2494 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002495 * inconsistency
2496 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002497 if (orig_node->tt_crc != tt_crc)
2498 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002499 } else {
2500 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002501 * in sync anymore -> request fresh tt data
2502 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002503 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2504 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002505request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002506 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002507 "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 +02002508 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2509 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002510 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2511 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002512 return;
2513 }
2514 }
2515}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002516
2517/* returns true whether we know that the client has moved from its old
2518 * originator to another one. This entry is kept is still kept for consistency
2519 * purposes
2520 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002521bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002522 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002523{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002524 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002525 bool ret = false;
2526
Sven Eckelmanna5130882012-05-16 20:23:16 +02002527 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002528 if (!tt_global_entry)
2529 goto out;
2530
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002531 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002532 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002533out:
2534 return ret;
2535}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002536
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002537/**
2538 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2539 * @bat_priv: the bat priv with all the soft interface information
2540 * @addr: the MAC address of the local client to query
2541 *
2542 * Returns true if the local client is known to be roaming (it is not served by
2543 * this node anymore) or not. If yes, the client is still present in the table
2544 * to keep the latter consistent with the node TTVN
2545 */
2546bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2547 uint8_t *addr)
2548{
2549 struct batadv_tt_local_entry *tt_local_entry;
2550 bool ret = false;
2551
2552 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2553 if (!tt_local_entry)
2554 goto out;
2555
2556 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2557 batadv_tt_local_entry_free_ref(tt_local_entry);
2558out:
2559 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002560}
2561
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002562bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2563 struct batadv_orig_node *orig_node,
2564 const unsigned char *addr)
2565{
2566 bool ret = false;
2567
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002568 /* if the originator is a backbone node (meaning it belongs to the same
2569 * LAN of this node) the temporary client must not be added because to
2570 * reach such destination the node must use the LAN instead of the mesh
2571 */
2572 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2573 goto out;
2574
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002575 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2576 BATADV_TT_CLIENT_TEMP,
2577 atomic_read(&orig_node->last_ttvn)))
2578 goto out;
2579
2580 batadv_dbg(BATADV_DBG_TT, bat_priv,
2581 "Added temporary global client (addr: %pM orig: %pM)\n",
2582 addr, orig_node->orig);
2583 ret = true;
2584out:
2585 return ret;
2586}