blob: 9e87485758455743984bf51bf783c7bf61cee348 [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);
Linus Lüssing72822222013-04-15 21:43:29 +0800147
148 /* We are in an rcu callback here, therefore we cannot use
149 * batadv_orig_node_free_ref() and its call_rcu():
150 * An rcu_barrier() wouldn't wait for that to finish
151 */
152 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200167 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182
183 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000206 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 event_removed = true;
208 goto unlock;
209 }
210
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213
214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200217 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200218 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200219 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200223int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224{
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200226}
227
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
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 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Sven Eckelmann807736f2012-07-15 22:26:51 +0200233 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann807736f2012-07-15 22:26:51 +0200235 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Antonio Quartullidec05072012-11-10 11:00:32 +0100238 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
239 &batadv_tt_local_hash_lock_class_key);
240
Sven Eckelmann5346c352012-05-05 13:27:28 +0200241 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242}
243
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200244static void batadv_tt_global_free(struct batadv_priv *bat_priv,
245 struct batadv_tt_global_entry *tt_global,
246 const char *message)
247{
248 batadv_dbg(BATADV_DBG_TT, bat_priv,
249 "Deleting global tt entry %pM: %s\n",
250 tt_global->common.addr, message);
251
252 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
253 batadv_choose_orig, tt_global->common.addr);
254 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200255}
256
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200257void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
258 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200260 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200261 struct batadv_tt_local_entry *tt_local;
262 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200263 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200264 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100265 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200266 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
Antonio Quartulli47c94652012-09-23 22:38:35 +0200268 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200269 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
Antonio Quartulli47c94652012-09-23 22:38:35 +0200271 if (tt_local) {
272 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200273 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
274 batadv_dbg(BATADV_DBG_TT, bat_priv,
275 "Re-adding pending client %pM\n", addr);
276 /* whatever the reason why the PENDING flag was set,
277 * this is a client which was enqueued to be removed in
278 * this orig_interval. Since it popped up again, the
279 * flag can be reset like it was never enqueued
280 */
281 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
282 goto add_event;
283 }
284
285 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
286 batadv_dbg(BATADV_DBG_TT, bat_priv,
287 "Roaming client %pM came back to its original location\n",
288 addr);
289 /* the ROAM flag is set because this client roamed away
290 * and the node got a roaming_advertisement message. Now
291 * that the client popped up again at its original
292 * location such flag can be unset
293 */
294 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
295 roamed_back = true;
296 }
297 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 }
299
Antonio Quartulli47c94652012-09-23 22:38:35 +0200300 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
301 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200302 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200303
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200304 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200305 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200306 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Antonio Quartulli47c94652012-09-23 22:38:35 +0200308 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100309 /* The local entry has to be marked as NEW to avoid to send it in
310 * a full table response going out before the next ttvn increment
311 * (consistency check)
312 */
313 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200314 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200315 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
316 atomic_set(&tt_local->common.refcount, 2);
317 tt_local->last_seen = jiffies;
318 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
320 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200321 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200322 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323
Sven Eckelmann807736f2012-07-15 22:26:51 +0200324 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200325 batadv_choose_orig, &tt_local->common,
326 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100327
328 if (unlikely(hash_added != 0)) {
329 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200330 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100331 goto out;
332 }
333
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200334add_event:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200335 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200336
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200337check_roaming:
338 /* Check whether it is a roaming, but don't do anything if the roaming
339 * process has already been handled
340 */
341 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200342 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200343 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200344 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800345 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200346 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200347 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200348 }
349 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200350 if (roamed_back) {
351 batadv_tt_global_free(bat_priv, tt_global,
352 "Roaming canceled");
353 tt_global = NULL;
354 } else {
355 /* The global entry has to be marked as ROAMING and
356 * has to be kept for consistency purpose
357 */
358 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
359 tt_global->roam_at = jiffies;
360 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200361 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200362
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200363out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200364 if (tt_local)
365 batadv_tt_local_entry_free_ref(tt_local);
366 if (tt_global)
367 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368}
369
Sven Eckelmanna5130882012-05-16 20:23:16 +0200370static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
371 int *packet_buff_len,
372 int min_packet_len,
373 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800375 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800377 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
378
379 /* keep old buffer if kmalloc should fail */
380 if (new_buff) {
381 memcpy(new_buff, *packet_buff, min_packet_len);
382 kfree(*packet_buff);
383 *packet_buff = new_buff;
384 *packet_buff_len = new_packet_len;
385 }
386}
387
Sven Eckelmann56303d32012-06-05 22:31:31 +0200388static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200389 unsigned char **packet_buff,
390 int *packet_buff_len,
391 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800392{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800393 int req_len;
394
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800395 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200396 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800397
398 /* if we have too many changes for one packet don't send any
399 * and wait for the tt table request which will be fragmented
400 */
Marek Lindner736292c2013-01-12 19:19:06 +0800401 if (req_len > bat_priv->soft_iface->mtu)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800402 req_len = min_packet_len;
403
Sven Eckelmanna5130882012-05-16 20:23:16 +0200404 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
405 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800406}
407
Sven Eckelmann56303d32012-06-05 22:31:31 +0200408static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200409 unsigned char **packet_buff,
410 int *packet_buff_len,
411 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800412{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200413 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800414 int count = 0, tot_changes = 0, new_len;
415 unsigned char *tt_buff;
416
Sven Eckelmanna5130882012-05-16 20:23:16 +0200417 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
418 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800419
420 new_len = *packet_buff_len - min_packet_len;
421 tt_buff = *packet_buff + min_packet_len;
422
423 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200424 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425
Sven Eckelmann807736f2012-07-15 22:26:51 +0200426 spin_lock_bh(&bat_priv->tt.changes_list_lock);
427 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100430 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200431 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200432 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200433 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 count++;
435 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436 list_del(&entry->list);
437 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200439 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200442 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
443 kfree(bat_priv->tt.last_changeset);
444 bat_priv->tt.last_changeset_len = 0;
445 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800446 /* check whether this new OGM has no changes due to size problems */
447 if (new_len > 0) {
448 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200449 * instead of providing the diff
450 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200451 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
452 if (bat_priv->tt.last_changeset) {
453 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
454 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200455 }
456 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200457 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458
Marek Lindner08ad76e2012-04-23 16:32:55 +0800459 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460}
461
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200462int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463{
464 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200465 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200466 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200467 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100468 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200471 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100472 int last_seen_secs;
473 int last_seen_msecs;
474 unsigned long last_seen_jiffies;
475 bool no_purge;
476 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477
Marek Lindner30da63a2012-08-03 17:15:46 +0200478 primary_if = batadv_seq_print_text_primary_if_get(seq);
479 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200480 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100482 seq_printf(seq,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100483 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
484 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
485 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100486 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
487 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489 for (i = 0; i < hash->size; i++) {
490 head = &hash->table[i];
491
Marek Lindner7aadf882011-02-18 12:28:09 +0000492 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800493 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000494 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100495 tt_local = container_of(tt_common_entry,
496 struct batadv_tt_local_entry,
497 common);
498 last_seen_jiffies = jiffies - tt_local->last_seen;
499 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
500 last_seen_secs = last_seen_msecs / 1000;
501 last_seen_msecs = last_seen_msecs % 1000;
502
503 no_purge = tt_common_entry->flags & np_flag;
504
505 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100506 tt_common_entry->addr,
507 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200508 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100509 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100510 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200511 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100512 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200513 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100514 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100515 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100516 no_purge ? 0 : last_seen_secs,
517 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000519 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200521out:
522 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200523 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200524 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525}
526
Sven Eckelmann56303d32012-06-05 22:31:31 +0200527static void
528batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
529 struct batadv_tt_local_entry *tt_local_entry,
530 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200532 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
533 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Antonio Quartulli015758d2011-07-09 17:52:13 +0200535 /* The local client has to be marked as "pending to be removed" but has
536 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200537 * response issued before the net ttvn increment (consistency check)
538 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200539 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100540
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200541 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200542 "Local tt entry (%pM) pending to be removed: %s\n",
543 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544}
545
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200546/**
547 * batadv_tt_local_remove - logically remove an entry from the local table
548 * @bat_priv: the bat priv with all the soft interface information
549 * @addr: the MAC address of the client to remove
550 * @message: message to append to the log on deletion
551 * @roaming: true if the deletion is due to a roaming event
552 *
553 * Returns the flags assigned to the local entry before being deleted
554 */
555uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
556 const uint8_t *addr, const char *message,
557 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200559 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200560 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmanna5130882012-05-16 20:23:16 +0200562 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200563 if (!tt_local_entry)
564 goto out;
565
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200566 curr_flags = tt_local_entry->common.flags;
567
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200568 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200569 /* if this global entry addition is due to a roaming, the node has to
570 * mark the local entry as "roamed" in order to correctly reroute
571 * packets later
572 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200573 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200574 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200575 /* mark the local client as ROAMed */
576 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
577 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200578
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200579 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
580 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
581 message);
582 goto out;
583 }
584 /* if this client has been added right now, it is possible to
585 * immediately purge it
586 */
587 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
588 curr_flags | BATADV_TT_CLIENT_DEL);
589 hlist_del_rcu(&tt_local_entry->common.hash_entry);
590 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200591
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200592out:
593 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200594 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200595
596 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597}
598
Sven Eckelmann56303d32012-06-05 22:31:31 +0200599static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200600 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200602 struct batadv_tt_local_entry *tt_local_entry;
603 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800604 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200605
Sasha Levinb67bfe02013-02-27 17:06:00 -0800606 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200607 hash_entry) {
608 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200609 struct batadv_tt_local_entry,
610 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200611 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
612 continue;
613
614 /* entry already marked for deletion */
615 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
616 continue;
617
618 if (!batadv_has_timed_out(tt_local_entry->last_seen,
619 BATADV_TT_LOCAL_TIMEOUT))
620 continue;
621
622 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
623 BATADV_TT_CLIENT_DEL, "timed out");
624 }
625}
626
Sven Eckelmann56303d32012-06-05 22:31:31 +0200627static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200628{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200629 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200631 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200632 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 for (i = 0; i < hash->size; i++) {
635 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200636 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200639 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642}
643
Sven Eckelmann56303d32012-06-05 22:31:31 +0200644static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200646 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200647 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200648 struct batadv_tt_common_entry *tt_common_entry;
649 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800650 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200651 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200652 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200653
Sven Eckelmann807736f2012-07-15 22:26:51 +0200654 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655 return;
656
Sven Eckelmann807736f2012-07-15 22:26:51 +0200657 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200658
659 for (i = 0; i < hash->size; i++) {
660 head = &hash->table[i];
661 list_lock = &hash->list_locks[i];
662
663 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800664 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200665 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800666 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200667 tt_local = container_of(tt_common_entry,
668 struct batadv_tt_local_entry,
669 common);
670 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200671 }
672 spin_unlock_bh(list_lock);
673 }
674
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200675 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200676
Sven Eckelmann807736f2012-07-15 22:26:51 +0200677 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678}
679
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200682 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200683 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684
Sven Eckelmann807736f2012-07-15 22:26:51 +0200685 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686
Sven Eckelmann807736f2012-07-15 22:26:51 +0200687 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200688 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
Antonio Quartullidec05072012-11-10 11:00:32 +0100690 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
691 &batadv_tt_global_hash_lock_class_key);
692
Sven Eckelmann5346c352012-05-05 13:27:28 +0200693 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694}
695
Sven Eckelmann56303d32012-06-05 22:31:31 +0200696static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200697{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200698 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200699
Sven Eckelmann807736f2012-07-15 22:26:51 +0200700 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701
Sven Eckelmann807736f2012-07-15 22:26:51 +0200702 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703 list) {
704 list_del(&entry->list);
705 kfree(entry);
706 }
707
Sven Eckelmann807736f2012-07-15 22:26:51 +0200708 atomic_set(&bat_priv->tt.local_changes, 0);
709 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200710}
711
Antonio Quartullid657e622012-07-01 14:09:12 +0200712/* retrieves the orig_tt_list_entry belonging to orig_node from the
713 * batadv_tt_global_entry list
714 *
715 * returns it with an increased refcounter, NULL if not found
716 */
717static struct batadv_tt_orig_list_entry *
718batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
719 const struct batadv_orig_node *orig_node)
720{
721 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
722 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200723
724 rcu_read_lock();
725 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800726 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200727 if (tmp_orig_entry->orig_node != orig_node)
728 continue;
729 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
730 continue;
731
732 orig_entry = tmp_orig_entry;
733 break;
734 }
735 rcu_read_unlock();
736
737 return orig_entry;
738}
739
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200740/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200741 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200742 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200743static bool
744batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
745 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200746{
Antonio Quartullid657e622012-07-01 14:09:12 +0200747 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200748 bool found = false;
749
Antonio Quartullid657e622012-07-01 14:09:12 +0200750 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
751 if (orig_entry) {
752 found = true;
753 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200754 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200755
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200756 return found;
757}
758
Sven Eckelmanna5130882012-05-16 20:23:16 +0200759static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200760batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200761 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200762{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200763 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200764
Antonio Quartullid657e622012-07-01 14:09:12 +0200765 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200766 if (orig_entry) {
767 /* refresh the ttvn: the current value could be a bogus one that
768 * was added during a "temporary client detection"
769 */
770 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200771 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200772 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200773
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200774 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
775 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200776 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200777
778 INIT_HLIST_NODE(&orig_entry->list);
779 atomic_inc(&orig_node->refcount);
780 atomic_inc(&orig_node->tt_size);
781 orig_entry->orig_node = orig_node;
782 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200783 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200784
Antonio Quartullid657e622012-07-01 14:09:12 +0200785 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200786 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200787 &tt_global->orig_list);
788 spin_unlock_bh(&tt_global->list_lock);
789out:
790 if (orig_entry)
791 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200792}
793
Antonio Quartullia73105b2011-04-27 14:27:44 +0200794/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200795int batadv_tt_global_add(struct batadv_priv *bat_priv,
796 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200797 const unsigned char *tt_addr, uint8_t flags,
798 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200800 struct batadv_tt_global_entry *tt_global_entry;
801 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200802 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100803 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200804 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200805 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806
Sven Eckelmanna5130882012-05-16 20:23:16 +0200807 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200808 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
809
810 /* if the node already has a local client for this entry, it has to wait
811 * for a roaming advertisement instead of manually messing up the global
812 * table
813 */
814 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
815 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
816 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817
Antonio Quartullia73105b2011-04-27 14:27:44 +0200818 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200819 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200820 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200821 goto out;
822
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200823 common = &tt_global_entry->common;
824 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825
Antonio Quartullid4f44692012-05-25 00:00:54 +0200826 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200827 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200828 /* node must store current time in case of roaming. This is
829 * needed to purge this entry out on timeout (if nobody claims
830 * it)
831 */
832 if (flags & BATADV_TT_CLIENT_ROAM)
833 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200834 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200835 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200836
837 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
838 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200839
Sven Eckelmann807736f2012-07-15 22:26:51 +0200840 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200841 batadv_compare_tt,
842 batadv_choose_orig, common,
843 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100844
845 if (unlikely(hash_added != 0)) {
846 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200847 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100848 goto out_remove;
849 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200850 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200851 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200852 /* If there is already a global entry, we can use this one for
853 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200854 * But if we are trying to add a temporary client then here are
855 * two options at this point:
856 * 1) the global client is not a temporary client: the global
857 * client has to be left as it is, temporary information
858 * should never override any already known client state
859 * 2) the global client is a temporary client: purge the
860 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200861 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200862 if (flags & BATADV_TT_CLIENT_TEMP) {
863 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
864 goto out;
865 if (batadv_tt_global_entry_has_orig(tt_global_entry,
866 orig_node))
867 goto out_remove;
868 batadv_tt_global_del_orig_list(tt_global_entry);
869 goto add_orig_entry;
870 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200871
872 /* if the client was temporary added before receiving the first
873 * OGM announcing it, we have to clear the TEMP flag
874 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200875 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200876
Antonio Quartullie9c001362012-11-07 15:05:33 +0100877 /* the change can carry possible "attribute" flags like the
878 * TT_CLIENT_WIFI, therefore they have to be copied in the
879 * client entry
880 */
881 tt_global_entry->common.flags |= flags;
882
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200883 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
884 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200885 * delete + roaming change for this originator.
886 *
887 * We should first delete the old originator before adding the
888 * new one.
889 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200890 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200891 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200892 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200893 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894 }
895 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200896add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200897 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200898 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200899
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200900 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200901 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200902 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200903 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200904
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100905out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200906
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200908 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
909 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +1000910 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200911 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
912
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200913 if (!(flags & BATADV_TT_CLIENT_ROAM))
914 /* this is a normal global add. Therefore the client is not in a
915 * roaming state anymore.
916 */
917 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
918
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200919out:
920 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200921 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200922 if (tt_local_entry)
923 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200924 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000925}
926
Sven Eckelmann981d8902012-10-07 13:34:15 +0200927/* batadv_transtable_best_orig - Get best originator list entry from tt entry
928 * @tt_global_entry: global translation table entry to be analyzed
929 *
930 * This functon assumes the caller holds rcu_read_lock().
931 * Returns best originator list entry or NULL on errors.
932 */
933static struct batadv_tt_orig_list_entry *
934batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
935{
936 struct batadv_neigh_node *router = NULL;
937 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200938 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
939 int best_tq = 0;
940
941 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800942 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200943 router = batadv_orig_node_get_router(orig_entry->orig_node);
944 if (!router)
945 continue;
946
947 if (router->tq_avg > best_tq) {
948 best_entry = orig_entry;
949 best_tq = router->tq_avg;
950 }
951
952 batadv_neigh_node_free_ref(router);
953 }
954
955 return best_entry;
956}
957
958/* batadv_tt_global_print_entry - print all orig nodes who announce the address
959 * for this global entry
960 * @tt_global_entry: global translation table entry to be printed
961 * @seq: debugfs table seq_file struct
962 *
963 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200964 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200965static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200966batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200967 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200968{
969 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200970 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200971 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200972 uint16_t flags;
973 uint8_t last_ttvn;
974
975 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200976 flags = tt_common_entry->flags;
977
978 best_entry = batadv_transtable_best_orig(tt_global_entry);
979 if (best_entry) {
980 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100981 seq_printf(seq,
982 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200983 '*', tt_global_entry->common.addr,
984 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100985 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200986 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
987 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
988 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
989 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200990
991 head = &tt_global_entry->orig_list;
992
Sasha Levinb67bfe02013-02-27 17:06:00 -0800993 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200994 if (best_entry == orig_entry)
995 continue;
996
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200997 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +0200998 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
999 '+', tt_global_entry->common.addr,
1000 orig_entry->ttvn, orig_entry->orig_node->orig,
1001 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001002 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001003 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1004 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001005 }
1006}
1007
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001008int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009{
1010 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001011 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001012 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001013 struct batadv_tt_common_entry *tt_common_entry;
1014 struct batadv_tt_global_entry *tt_global;
1015 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001017 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018
Marek Lindner30da63a2012-08-03 17:15:46 +02001019 primary_if = batadv_seq_print_text_primary_if_get(seq);
1020 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001021 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001023 seq_printf(seq,
1024 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 net_dev->name);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001026 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1027 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1028 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 for (i = 0; i < hash->size; i++) {
1031 head = &hash->table[i];
1032
Marek Lindner7aadf882011-02-18 12:28:09 +00001033 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001034 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001035 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001036 tt_global = container_of(tt_common_entry,
1037 struct batadv_tt_global_entry,
1038 common);
1039 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001041 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001042 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001043out:
1044 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001045 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001046 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047}
1048
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001049/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001050static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001051batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001053 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001055 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001056
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001057 spin_lock_bh(&tt_global_entry->list_lock);
1058 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001059 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1060 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001061 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001062 }
1063 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001064}
1065
Sven Eckelmanna5130882012-05-16 20:23:16 +02001066static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001067batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1068 struct batadv_tt_global_entry *tt_global_entry,
1069 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001070 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001071{
1072 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001074 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001075
1076 spin_lock_bh(&tt_global_entry->list_lock);
1077 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001078 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001079 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001080 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001081 "Deleting %pM from global tt entry %pM: %s\n",
1082 orig_node->orig,
1083 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001084 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001085 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086 }
1087 }
1088 spin_unlock_bh(&tt_global_entry->list_lock);
1089}
1090
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001091/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001092 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1093 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001095static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001096batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1097 struct batadv_tt_global_entry *tt_global_entry,
1098 struct batadv_orig_node *orig_node,
1099 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001100{
1101 bool last_entry = true;
1102 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001103 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001104
1105 /* no local entry exists, case 1:
1106 * Check if this is the last one or if other entries exist.
1107 */
1108
1109 rcu_read_lock();
1110 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001111 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001112 if (orig_entry->orig_node != orig_node) {
1113 last_entry = false;
1114 break;
1115 }
1116 }
1117 rcu_read_unlock();
1118
1119 if (last_entry) {
1120 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001121 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001122 tt_global_entry->roam_at = jiffies;
1123 } else
1124 /* there is another entry, we can simply delete this
1125 * one and can still use the other one.
1126 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001127 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1128 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001129}
1130
1131
1132
Sven Eckelmann56303d32012-06-05 22:31:31 +02001133static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1134 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001135 const unsigned char *addr,
1136 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001138 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001139 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001140
Sven Eckelmanna5130882012-05-16 20:23:16 +02001141 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001142 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001143 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001144
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001145 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001146 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1147 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001148
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001149 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001150 batadv_tt_global_free(bat_priv, tt_global_entry,
1151 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001152
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001153 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001154 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001155
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001156 /* if we are deleting a global entry due to a roam
1157 * event, there are two possibilities:
1158 * 1) the client roamed from node A to node B => if there
1159 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001160 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001161 * wait for node B to claim it. In case of timeout
1162 * the entry is purged.
1163 *
1164 * If there are other originators left, we directly delete
1165 * the originator.
1166 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001167 * the global entry, since it is useless now.
1168 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001169 local_entry = batadv_tt_local_hash_find(bat_priv,
1170 tt_global_entry->common.addr);
1171 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001172 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001173 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001174 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001175 } else
1176 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001177 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1178 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001179
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001180
Antonio Quartullicc47f662011-04-27 14:27:57 +02001181out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001182 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001183 batadv_tt_global_entry_free_ref(tt_global_entry);
1184 if (local_entry)
1185 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001186}
1187
Sven Eckelmann56303d32012-06-05 22:31:31 +02001188void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1189 struct batadv_orig_node *orig_node,
1190 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001191{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001192 struct batadv_tt_global_entry *tt_global;
1193 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001194 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001195 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001196 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001197 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001198 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001199
Simon Wunderlich6e801492011-10-19 10:28:26 +02001200 if (!hash)
1201 return;
1202
Antonio Quartullia73105b2011-04-27 14:27:44 +02001203 for (i = 0; i < hash->size; i++) {
1204 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001205 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001206
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001207 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001208 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001209 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001210 tt_global = container_of(tt_common_entry,
1211 struct batadv_tt_global_entry,
1212 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001213
Sven Eckelmann56303d32012-06-05 22:31:31 +02001214 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001215 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001216
Sven Eckelmann56303d32012-06-05 22:31:31 +02001217 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001218 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001219 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001220 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001221 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001222 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001223 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001224 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001225 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001226 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001227 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001228}
1229
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001230static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1231 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001232{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001233 bool purge = false;
1234 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1235 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001236
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001237 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1238 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1239 purge = true;
1240 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001241 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001242
1243 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1244 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1245 purge = true;
1246 *msg = "Temporary client timeout\n";
1247 }
1248
1249 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001250}
1251
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001252static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001253{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001254 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001255 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001256 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001257 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001258 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001259 char *msg = NULL;
1260 struct batadv_tt_common_entry *tt_common;
1261 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001262
Antonio Quartullicc47f662011-04-27 14:27:57 +02001263 for (i = 0; i < hash->size; i++) {
1264 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001265 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001266
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001267 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001268 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001269 hash_entry) {
1270 tt_global = container_of(tt_common,
1271 struct batadv_tt_global_entry,
1272 common);
1273
1274 if (!batadv_tt_global_to_purge(tt_global, &msg))
1275 continue;
1276
1277 batadv_dbg(BATADV_DBG_TT, bat_priv,
1278 "Deleting global tt entry (%pM): %s\n",
1279 tt_global->common.addr, msg);
1280
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001282
1283 batadv_tt_global_entry_free_ref(tt_global);
1284 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001285 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001286 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001287}
1288
Sven Eckelmann56303d32012-06-05 22:31:31 +02001289static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001290{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001291 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001292 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001293 struct batadv_tt_common_entry *tt_common_entry;
1294 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001295 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001296 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001297 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001298
Sven Eckelmann807736f2012-07-15 22:26:51 +02001299 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001300 return;
1301
Sven Eckelmann807736f2012-07-15 22:26:51 +02001302 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001303
1304 for (i = 0; i < hash->size; i++) {
1305 head = &hash->table[i];
1306 list_lock = &hash->list_locks[i];
1307
1308 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001309 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001310 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001311 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001312 tt_global = container_of(tt_common_entry,
1313 struct batadv_tt_global_entry,
1314 common);
1315 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001316 }
1317 spin_unlock_bh(list_lock);
1318 }
1319
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001320 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001321
Sven Eckelmann807736f2012-07-15 22:26:51 +02001322 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001323}
1324
Sven Eckelmann56303d32012-06-05 22:31:31 +02001325static bool
1326_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1327 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001328{
1329 bool ret = false;
1330
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001331 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1332 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001333 ret = true;
1334
1335 return ret;
1336}
1337
Sven Eckelmann56303d32012-06-05 22:31:31 +02001338struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1339 const uint8_t *src,
1340 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001341{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001342 struct batadv_tt_local_entry *tt_local_entry = NULL;
1343 struct batadv_tt_global_entry *tt_global_entry = NULL;
1344 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001345 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001346
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001347 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001348 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001349 if (!tt_local_entry ||
1350 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001351 goto out;
1352 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001353
Sven Eckelmanna5130882012-05-16 20:23:16 +02001354 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001355 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001356 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001357
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001358 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001359 * isolation
1360 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001361 if (tt_local_entry &&
1362 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001363 goto out;
1364
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001365 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001366 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001367 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001368 if (best_entry)
1369 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001370 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1371 orig_node = NULL;
1372 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001373
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001374out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001375 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001376 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001377 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001378 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001379
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001380 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001381}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001382
1383/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001384static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1385 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001386{
1387 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001388 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001389 struct batadv_tt_common_entry *tt_common;
1390 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001391 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001392 uint32_t i;
1393 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394
1395 for (i = 0; i < hash->size; i++) {
1396 head = &hash->table[i];
1397
1398 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001399 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001400 tt_global = container_of(tt_common,
1401 struct batadv_tt_global_entry,
1402 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001403 /* Roaming clients are in the global table for
1404 * consistency only. They don't have to be
1405 * taken into account while computing the
1406 * global crc
1407 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001408 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001409 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001410 /* Temporary clients have not been announced yet, so
1411 * they have to be skipped while computing the global
1412 * crc
1413 */
1414 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1415 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001416
1417 /* find out if this global entry is announced by this
1418 * originator
1419 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001420 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001421 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001422 continue;
1423
1424 total_one = 0;
1425 for (j = 0; j < ETH_ALEN; j++)
1426 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001427 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001428 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001429 }
1430 rcu_read_unlock();
1431 }
1432
1433 return total;
1434}
1435
1436/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001437static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001438{
1439 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001440 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001441 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001442 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001443 uint32_t i;
1444 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445
1446 for (i = 0; i < hash->size; i++) {
1447 head = &hash->table[i];
1448
1449 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001450 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001451 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001452 * account while computing the CRC
1453 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001454 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001455 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 total_one = 0;
1457 for (j = 0; j < ETH_ALEN; j++)
1458 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001459 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001460 total ^= total_one;
1461 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001462 rcu_read_unlock();
1463 }
1464
1465 return total;
1466}
1467
Sven Eckelmann56303d32012-06-05 22:31:31 +02001468static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001469{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001470 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471
Sven Eckelmann807736f2012-07-15 22:26:51 +02001472 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473
Sven Eckelmann807736f2012-07-15 22:26:51 +02001474 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475 list_del(&node->list);
1476 kfree(node);
1477 }
1478
Sven Eckelmann807736f2012-07-15 22:26:51 +02001479 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480}
1481
Sven Eckelmann56303d32012-06-05 22:31:31 +02001482static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1483 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001484 const unsigned char *tt_buff,
1485 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001487 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001488
1489 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001490 * last OGM (the OGM could carry no changes)
1491 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001492 spin_lock_bh(&orig_node->tt_buff_lock);
1493 if (tt_buff_len > 0) {
1494 kfree(orig_node->tt_buff);
1495 orig_node->tt_buff_len = 0;
1496 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1497 if (orig_node->tt_buff) {
1498 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1499 orig_node->tt_buff_len = tt_buff_len;
1500 }
1501 }
1502 spin_unlock_bh(&orig_node->tt_buff_lock);
1503}
1504
Sven Eckelmann56303d32012-06-05 22:31:31 +02001505static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001507 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001508
Sven Eckelmann807736f2012-07-15 22:26:51 +02001509 spin_lock_bh(&bat_priv->tt.req_list_lock);
1510 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001511 if (batadv_has_timed_out(node->issued_at,
1512 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001513 list_del(&node->list);
1514 kfree(node);
1515 }
1516 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001517 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518}
1519
1520/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001521 * has already been issued for this orig_node, NULL otherwise
1522 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001523static struct batadv_tt_req_node *
1524batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1525 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001526{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001527 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528
Sven Eckelmann807736f2012-07-15 22:26:51 +02001529 spin_lock_bh(&bat_priv->tt.req_list_lock);
1530 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001531 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1532 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001533 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001534 goto unlock;
1535 }
1536
1537 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1538 if (!tt_req_node)
1539 goto unlock;
1540
1541 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1542 tt_req_node->issued_at = jiffies;
1543
Sven Eckelmann807736f2012-07-15 22:26:51 +02001544 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001545unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001546 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001547 return tt_req_node;
1548}
1549
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001550/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001551static int batadv_tt_local_valid_entry(const void *entry_ptr,
1552 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001553{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001554 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001555
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001556 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001557 return 0;
1558 return 1;
1559}
1560
Sven Eckelmanna5130882012-05-16 20:23:16 +02001561static int batadv_tt_global_valid(const void *entry_ptr,
1562 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001563{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001564 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1565 const struct batadv_tt_global_entry *tt_global_entry;
1566 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001567
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001568 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1569 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001570 return 0;
1571
Sven Eckelmann56303d32012-06-05 22:31:31 +02001572 tt_global_entry = container_of(tt_common_entry,
1573 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001574 common);
1575
Sven Eckelmanna5130882012-05-16 20:23:16 +02001576 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001577}
1578
Sven Eckelmanna5130882012-05-16 20:23:16 +02001579static struct sk_buff *
1580batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001581 struct batadv_hashtable *hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001582 struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001583 int (*valid_cb)(const void *, const void *),
1584 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001585{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001586 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001587 struct batadv_tt_query_packet *tt_response;
1588 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001589 struct hlist_head *head;
1590 struct sk_buff *skb = NULL;
1591 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001592 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001593 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001594 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595
Marek Lindner736292c2013-01-12 19:19:06 +08001596 if (tt_query_size + tt_len > bat_priv->soft_iface->mtu) {
1597 tt_len = bat_priv->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001598 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001600 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001601
Sven Eckelmann96412692012-06-05 22:31:30 +02001602 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001603 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001604 if (!skb)
1605 goto out;
1606
Sven Eckelmann5b246572012-11-04 17:11:45 +01001607 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001608 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001609 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610
Sven Eckelmann96412692012-06-05 22:31:30 +02001611 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001612 tt_count = 0;
1613
1614 rcu_read_lock();
1615 for (i = 0; i < hash->size; i++) {
1616 head = &hash->table[i];
1617
Sasha Levinb67bfe02013-02-27 17:06:00 -08001618 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619 head, hash_entry) {
1620 if (tt_count == tt_tot)
1621 break;
1622
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001623 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 continue;
1625
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001626 memcpy(tt_change->addr, tt_common_entry->addr,
1627 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001628 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001629
1630 tt_count++;
1631 tt_change++;
1632 }
1633 }
1634 rcu_read_unlock();
1635
Antonio Quartulli9d852392011-10-17 14:25:13 +02001636 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001637 * copied
1638 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001639 tt_response->tt_data = htons(tt_count);
1640
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641out:
1642 return skb;
1643}
1644
Sven Eckelmann56303d32012-06-05 22:31:31 +02001645static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1646 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001647 uint8_t ttvn, uint16_t tt_crc,
1648 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649{
1650 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001651 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001652 struct batadv_hard_iface *primary_if;
1653 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001654 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001655 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001657 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001658 if (!primary_if)
1659 goto out;
1660
1661 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001662 * reply from the same orig_node yet
1663 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001664 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001665 if (!tt_req_node)
1666 goto out;
1667
Sven Eckelmann5b246572012-11-04 17:11:45 +01001668 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669 if (!skb)
1670 goto out;
1671
Sven Eckelmann5b246572012-11-04 17:11:45 +01001672 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673
Sven Eckelmann96412692012-06-05 22:31:30 +02001674 tt_req_len = sizeof(*tt_request);
1675 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001677 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001678 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1680 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001681 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001683 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001684 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685
1686 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001687 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001689 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1690 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001692 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001693
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001694 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1695 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696
1697out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001699 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700 if (ret)
1701 kfree_skb(skb);
1702 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001703 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001705 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 kfree(tt_req_node);
1707 }
1708 return ret;
1709}
1710
Sven Eckelmann96412692012-06-05 22:31:30 +02001711static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001712batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001713 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001715 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001716 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 uint8_t orig_ttvn, req_ttvn, ttvn;
1718 int ret = false;
1719 unsigned char *tt_buff;
1720 bool full_table;
1721 uint16_t tt_len, tt_tot;
1722 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001723 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001724 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001725 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001727 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001728 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1729 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001730 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001731
1732 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001733 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001734 if (!req_dst_orig_node)
1735 goto out;
1736
Sven Eckelmannda641192012-05-12 13:48:56 +02001737 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001738 if (!res_dst_orig_node)
1739 goto out;
1740
Antonio Quartullia73105b2011-04-27 14:27:44 +02001741 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1742 req_ttvn = tt_request->ttvn;
1743
Antonio Quartulli015758d2011-07-09 17:52:13 +02001744 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001745 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001746 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 goto out;
1748
Antonio Quartulli015758d2011-07-09 17:52:13 +02001749 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001750 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 !req_dst_orig_node->tt_buff)
1752 full_table = true;
1753 else
1754 full_table = false;
1755
1756 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001757 * I'll send only one packet with as much TT entries as I can
1758 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 if (!full_table) {
1760 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1761 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001762 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763
Sven Eckelmann96412692012-06-05 22:31:30 +02001764 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001765 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001766 if (!skb)
1767 goto unlock;
1768
Sven Eckelmann5b246572012-11-04 17:11:45 +01001769 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001770 packet_pos = skb_put(skb, len);
1771 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772 tt_response->ttvn = req_ttvn;
1773 tt_response->tt_data = htons(tt_tot);
1774
Sven Eckelmann96412692012-06-05 22:31:30 +02001775 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 /* Copy the last orig_node's OGM buffer */
1777 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1778 req_dst_orig_node->tt_buff_len);
1779
1780 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1781 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001782 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1783 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1785
Sven Eckelmanna5130882012-05-16 20:23:16 +02001786 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001787 bat_priv->tt.global_hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001788 bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001789 batadv_tt_global_valid,
1790 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 if (!skb)
1792 goto out;
1793
Sven Eckelmann96412692012-06-05 22:31:30 +02001794 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795 }
1796
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001797 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001798 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001799 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001800 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1801 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001802 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001803
1804 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001805 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001806
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001807 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001808 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1809 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001811 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001812
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001813 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1814 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 goto out;
1816
1817unlock:
1818 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1819
1820out:
1821 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001822 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001824 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825 if (!ret)
1826 kfree_skb(skb);
1827 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828}
Sven Eckelmann96412692012-06-05 22:31:30 +02001829
1830static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001831batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001832 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001833{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001834 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001835 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836 uint8_t my_ttvn, req_ttvn, ttvn;
1837 int ret = false;
1838 unsigned char *tt_buff;
1839 bool full_table;
1840 uint16_t tt_len, tt_tot;
1841 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001842 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001843 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001844 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001846 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001847 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1848 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001849 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001850
1851
Sven Eckelmann807736f2012-07-15 22:26:51 +02001852 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853 req_ttvn = tt_request->ttvn;
1854
Sven Eckelmannda641192012-05-12 13:48:56 +02001855 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856 if (!orig_node)
1857 goto out;
1858
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001859 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001860 if (!primary_if)
1861 goto out;
1862
1863 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001864 * is too big send the whole local translation table
1865 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001866 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001867 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868 full_table = true;
1869 else
1870 full_table = false;
1871
1872 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001873 * I'll send only one packet with as much TT entries as I can
1874 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001876 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1877 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001878 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879
Sven Eckelmann96412692012-06-05 22:31:30 +02001880 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001881 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001882 if (!skb)
1883 goto unlock;
1884
Sven Eckelmann5b246572012-11-04 17:11:45 +01001885 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001886 packet_pos = skb_put(skb, len);
1887 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001888 tt_response->ttvn = req_ttvn;
1889 tt_response->tt_data = htons(tt_tot);
1890
Sven Eckelmann96412692012-06-05 22:31:30 +02001891 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001892 memcpy(tt_buff, bat_priv->tt.last_changeset,
1893 bat_priv->tt.last_changeset_len);
1894 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001896 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001897 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001898 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899
Sven Eckelmanna5130882012-05-16 20:23:16 +02001900 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001901 bat_priv->tt.local_hash,
Marek Lindner736292c2013-01-12 19:19:06 +08001902 bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001903 batadv_tt_local_valid_entry,
1904 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905 if (!skb)
1906 goto out;
1907
Sven Eckelmann96412692012-06-05 22:31:30 +02001908 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909 }
1910
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001911 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001912 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001913 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1915 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001916 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001917
1918 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001919 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001921 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001922 "Sending TT_RESPONSE to %pM [%c]\n",
1923 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001924 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001926 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001927
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001928 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1929 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001930 goto out;
1931
1932unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001933 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934out:
1935 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001936 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001938 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939 if (!ret)
1940 kfree_skb(skb);
1941 /* This packet was for me, so it doesn't need to be re-routed */
1942 return true;
1943}
1944
Sven Eckelmann56303d32012-06-05 22:31:31 +02001945bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001946 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001947{
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001948 if (batadv_is_my_mac(bat_priv, tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001949 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001950 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001951 return true;
1952
Sven Eckelmanna5130882012-05-16 20:23:16 +02001953 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001954 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001955 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001956 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001957}
1958
Sven Eckelmann56303d32012-06-05 22:31:31 +02001959static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1960 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001961 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001962 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963{
1964 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001965 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001966
1967 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001968 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1969 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001970 batadv_tt_global_del(bat_priv, orig_node,
1971 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001972 "tt removed by changes",
1973 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001974 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001975 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001976 (tt_change + i)->addr,
1977 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001978 /* In case of problem while storing a
1979 * global_entry, we stop the updating
1980 * procedure without committing the
1981 * ttvn change. This will avoid to send
1982 * corrupted data on tt_request
1983 */
1984 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001985 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001987 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001988}
1989
Sven Eckelmann56303d32012-06-05 22:31:31 +02001990static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001991 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001992{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001993 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001994
Sven Eckelmannda641192012-05-12 13:48:56 +02001995 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996 if (!orig_node)
1997 goto out;
1998
1999 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002000 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001
Sven Eckelmanna5130882012-05-16 20:23:16 +02002002 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002003 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002004 ntohs(tt_response->tt_data),
2005 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002006
2007 spin_lock_bh(&orig_node->tt_buff_lock);
2008 kfree(orig_node->tt_buff);
2009 orig_node->tt_buff_len = 0;
2010 orig_node->tt_buff = NULL;
2011 spin_unlock_bh(&orig_node->tt_buff_lock);
2012
2013 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2014
2015out:
2016 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002017 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002018}
2019
Sven Eckelmann56303d32012-06-05 22:31:31 +02002020static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2021 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002022 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002023 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002024{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002025 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2026 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002027
Sven Eckelmanna5130882012-05-16 20:23:16 +02002028 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2029 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002030 atomic_set(&orig_node->last_ttvn, ttvn);
2031}
2032
Sven Eckelmann56303d32012-06-05 22:31:31 +02002033bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002034{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002035 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002036 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002037
Sven Eckelmanna5130882012-05-16 20:23:16 +02002038 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002039 if (!tt_local_entry)
2040 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002041 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002042 * consistency purpose)
2043 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002044 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2045 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002046 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002047 ret = true;
2048out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002049 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002050 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002051 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002052}
2053
Sven Eckelmann56303d32012-06-05 22:31:31 +02002054void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002055 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002056{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002057 struct batadv_tt_req_node *node, *safe;
2058 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002059 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002061 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002062 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2063 tt_response->src, tt_response->ttvn,
2064 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002065 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002066
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002067 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002068 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002069 goto out;
2070
Sven Eckelmannda641192012-05-12 13:48:56 +02002071 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072 if (!orig_node)
2073 goto out;
2074
Sven Eckelmann96412692012-06-05 22:31:30 +02002075 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002076 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002077 } else {
2078 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002079 batadv_tt_update_changes(bat_priv, orig_node,
2080 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002081 tt_response->ttvn, tt_change);
2082 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083
2084 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002085 spin_lock_bh(&bat_priv->tt.req_list_lock);
2086 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002087 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002088 continue;
2089 list_del(&node->list);
2090 kfree(node);
2091 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002092 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002093
2094 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002095 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096out:
2097 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002098 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002099}
2100
Sven Eckelmann56303d32012-06-05 22:31:31 +02002101int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002102{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002103 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104
Sven Eckelmanna5130882012-05-16 20:23:16 +02002105 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002106 if (ret < 0)
2107 return ret;
2108
Sven Eckelmanna5130882012-05-16 20:23:16 +02002109 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002110 if (ret < 0)
2111 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112
Antonio Quartulli72414442012-12-25 13:14:37 +01002113 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2114 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2115 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116
2117 return 1;
2118}
2119
Sven Eckelmann56303d32012-06-05 22:31:31 +02002120static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002122 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002123
Sven Eckelmann807736f2012-07-15 22:26:51 +02002124 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002125
Sven Eckelmann807736f2012-07-15 22:26:51 +02002126 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002127 list_del(&node->list);
2128 kfree(node);
2129 }
2130
Sven Eckelmann807736f2012-07-15 22:26:51 +02002131 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002132}
2133
Sven Eckelmann56303d32012-06-05 22:31:31 +02002134static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002135{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002136 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002137
Sven Eckelmann807736f2012-07-15 22:26:51 +02002138 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2139 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002140 if (!batadv_has_timed_out(node->first_time,
2141 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002142 continue;
2143
2144 list_del(&node->list);
2145 kfree(node);
2146 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002147 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002148}
2149
2150/* This function checks whether the client already reached the
2151 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2152 * will not be sent.
2153 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002154 * returns true if the ROAMING_ADV can be sent, false otherwise
2155 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002156static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002157 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002158{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002159 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002160 bool ret = false;
2161
Sven Eckelmann807736f2012-07-15 22:26:51 +02002162 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002163 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002164 * reply from the same orig_node yet
2165 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002166 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002167 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002168 continue;
2169
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002170 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002171 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002172 continue;
2173
Sven Eckelmann3e348192012-05-16 20:23:22 +02002174 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002175 /* Sorry, you roamed too many times! */
2176 goto unlock;
2177 ret = true;
2178 break;
2179 }
2180
2181 if (!ret) {
2182 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2183 if (!tt_roam_node)
2184 goto unlock;
2185
2186 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002187 atomic_set(&tt_roam_node->counter,
2188 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002189 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2190
Sven Eckelmann807736f2012-07-15 22:26:51 +02002191 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002192 ret = true;
2193 }
2194
2195unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002196 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002197 return ret;
2198}
2199
Sven Eckelmann56303d32012-06-05 22:31:31 +02002200static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2201 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002202{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002203 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002204 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002205 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002206 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002207 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208
2209 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002210 * already roamed to us too many times
2211 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002212 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002213 goto out;
2214
Sven Eckelmann5b246572012-11-04 17:11:45 +01002215 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002216 if (!skb)
2217 goto out;
2218
Sven Eckelmann5b246572012-11-04 17:11:45 +01002219 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002220
Sven Eckelmann96412692012-06-05 22:31:30 +02002221 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002222
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002223 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002224 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002225 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002226 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002227 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002228 if (!primary_if)
2229 goto out;
2230 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002231 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002232 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2233 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2234
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002235 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002236 "Sending ROAMING_ADV to %pM (client %pM)\n",
2237 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002238
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002239 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002240
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002241 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2242 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002243
2244out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002245 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002246 kfree_skb(skb);
2247 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002248}
2249
Sven Eckelmanna5130882012-05-16 20:23:16 +02002250static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002251{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002252 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002253 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002254 struct batadv_priv *bat_priv;
2255
2256 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002257 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2258 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002259
Sven Eckelmanna5130882012-05-16 20:23:16 +02002260 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002261 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002262 batadv_tt_req_purge(bat_priv);
2263 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002264
Antonio Quartulli72414442012-12-25 13:14:37 +01002265 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2266 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002268
Sven Eckelmann56303d32012-06-05 22:31:31 +02002269void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002270{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002271 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002272
Sven Eckelmanna5130882012-05-16 20:23:16 +02002273 batadv_tt_local_table_free(bat_priv);
2274 batadv_tt_global_table_free(bat_priv);
2275 batadv_tt_req_list_free(bat_priv);
2276 batadv_tt_changes_list_free(bat_priv);
2277 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002278
Sven Eckelmann807736f2012-07-15 22:26:51 +02002279 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002280}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002281
Antonio Quartulli697f2532011-11-07 16:47:01 +01002282/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002283 * in the given hash table and returns the number of modified entries
2284 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002285static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2286 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002287{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002288 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002289 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002290 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002291 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002292
2293 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002294 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002295
2296 for (i = 0; i < hash->size; i++) {
2297 head = &hash->table[i];
2298
2299 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002300 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002301 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002302 if (enable) {
2303 if ((tt_common_entry->flags & flags) == flags)
2304 continue;
2305 tt_common_entry->flags |= flags;
2306 } else {
2307 if (!(tt_common_entry->flags & flags))
2308 continue;
2309 tt_common_entry->flags &= ~flags;
2310 }
2311 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002312 }
2313 rcu_read_unlock();
2314 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002315out:
2316 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002317}
2318
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002319/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002320static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002321{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002322 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002323 struct batadv_tt_common_entry *tt_common;
2324 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002325 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002326 struct hlist_head *head;
2327 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002328 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002329
2330 if (!hash)
2331 return;
2332
2333 for (i = 0; i < hash->size; i++) {
2334 head = &hash->table[i];
2335 list_lock = &hash->list_locks[i];
2336
2337 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002338 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002339 hash_entry) {
2340 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002341 continue;
2342
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002343 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002344 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002345 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002346
Sven Eckelmann807736f2012-07-15 22:26:51 +02002347 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002348 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002349 tt_local = container_of(tt_common,
2350 struct batadv_tt_local_entry,
2351 common);
2352 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002353 }
2354 spin_unlock_bh(list_lock);
2355 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002356}
2357
Sven Eckelmann56303d32012-06-05 22:31:31 +02002358static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002359 unsigned char **packet_buff,
2360 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002361{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002362 uint16_t changed_num = 0;
2363
Sven Eckelmann807736f2012-07-15 22:26:51 +02002364 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002365 return -ENOENT;
2366
Sven Eckelmann807736f2012-07-15 22:26:51 +02002367 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002368 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002369
2370 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002371 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002372 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002373 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002374
2375 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002376 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002377 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002378 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002379 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002380
2381 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002382 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002383
Sven Eckelmanna5130882012-05-16 20:23:16 +02002384 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2385 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002386}
2387
2388/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002389int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002390 unsigned char **packet_buff, int *packet_buff_len,
2391 int packet_min_len)
2392{
2393 int tt_num_changes;
2394
2395 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002396 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2397 packet_buff_len,
2398 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002399
2400 /* if the changes have been sent often enough */
2401 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002402 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002403 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2404 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002405 tt_num_changes = 0;
2406 }
2407
2408 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002409}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002410
Sven Eckelmann56303d32012-06-05 22:31:31 +02002411bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002412 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002413{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002414 struct batadv_tt_local_entry *tt_local_entry = NULL;
2415 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002416 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002417
2418 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002419 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002420
Sven Eckelmanna5130882012-05-16 20:23:16 +02002421 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002422 if (!tt_local_entry)
2423 goto out;
2424
Sven Eckelmanna5130882012-05-16 20:23:16 +02002425 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002426 if (!tt_global_entry)
2427 goto out;
2428
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002429 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002430 goto out;
2431
Marek Lindner5870adc2012-06-20 17:16:05 +02002432 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002433
2434out:
2435 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002436 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002437 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002438 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002439 return ret;
2440}
Marek Lindnera943cac2011-07-30 13:10:18 +02002441
Sven Eckelmann56303d32012-06-05 22:31:31 +02002442void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2443 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002444 const unsigned char *tt_buff, uint8_t tt_num_changes,
2445 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002446{
2447 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2448 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002449 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002450
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002451 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002452 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002453 return;
2454
Antonio Quartulli17071572011-11-07 16:36:40 +01002455 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002456 * increased by one -> we can apply the attached changes
2457 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002458 if ((!orig_node->tt_initialised && ttvn == 1) ||
2459 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002460 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002461 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2462 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002463 * In this case send a tt request
2464 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002465 if (!tt_num_changes) {
2466 full_table = false;
2467 goto request_table;
2468 }
2469
Sven Eckelmann96412692012-06-05 22:31:30 +02002470 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002471 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002472 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002473
2474 /* Even if we received the precomputed crc with the OGM, we
2475 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002476 * in the global table
2477 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002478 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002479
2480 /* The ttvn alone is not enough to guarantee consistency
2481 * because a single value could represent different states
2482 * (due to the wrap around). Thus a node has to check whether
2483 * the resulting table (after applying the changes) is still
2484 * consistent or not. E.g. a node could disconnect while its
2485 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2486 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002487 * inconsistency
2488 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002489 if (orig_node->tt_crc != tt_crc)
2490 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002491 } else {
2492 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002493 * in sync anymore -> request fresh tt data
2494 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002495 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2496 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002497request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002498 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002499 "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 +02002500 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2501 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002502 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2503 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002504 return;
2505 }
2506 }
2507}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002508
2509/* returns true whether we know that the client has moved from its old
2510 * originator to another one. This entry is kept is still kept for consistency
2511 * purposes
2512 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002513bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002514 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002515{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002516 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002517 bool ret = false;
2518
Sven Eckelmanna5130882012-05-16 20:23:16 +02002519 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002520 if (!tt_global_entry)
2521 goto out;
2522
Antonio Quartullic1d07432013-01-15 22:17:19 +10002523 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002524 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002525out:
2526 return ret;
2527}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002528
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002529/**
2530 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2531 * @bat_priv: the bat priv with all the soft interface information
2532 * @addr: the MAC address of the local client to query
2533 *
2534 * Returns true if the local client is known to be roaming (it is not served by
2535 * this node anymore) or not. If yes, the client is still present in the table
2536 * to keep the latter consistent with the node TTVN
2537 */
2538bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2539 uint8_t *addr)
2540{
2541 struct batadv_tt_local_entry *tt_local_entry;
2542 bool ret = false;
2543
2544 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2545 if (!tt_local_entry)
2546 goto out;
2547
2548 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2549 batadv_tt_local_entry_free_ref(tt_local_entry);
2550out:
2551 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002552}
2553
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002554bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2555 struct batadv_orig_node *orig_node,
2556 const unsigned char *addr)
2557{
2558 bool ret = false;
2559
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002560 /* if the originator is a backbone node (meaning it belongs to the same
2561 * LAN of this node) the temporary client must not be added because to
2562 * reach such destination the node must use the LAN instead of the mesh
2563 */
2564 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2565 goto out;
2566
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002567 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2568 BATADV_TT_CLIENT_TEMP,
2569 atomic_read(&orig_node->last_ttvn)))
2570 goto out;
2571
2572 batadv_dbg(BATADV_DBG_TT, bat_priv,
2573 "Added temporary global client (addr: %pM orig: %pM)\n",
2574 addr, orig_node->orig);
2575 ret = true;
2576out:
2577 return ret;
2578}