blob: 877acd149178688a2fc1b928835acf8d7e5f583c [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 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;
59 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020060 struct batadv_tt_common_entry *tt_common_entry;
61 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020062 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000063
64 if (!hash)
65 return NULL;
66
Sven Eckelmannda641192012-05-12 13:48:56 +020067 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000068 head = &hash->table[index];
69
70 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010071 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020072 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000073 continue;
74
Antonio Quartulli48100ba2011-10-30 12:17:33 +010075 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020076 continue;
77
Antonio Quartulli48100ba2011-10-30 12:17:33 +010078 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000079 break;
80 }
81 rcu_read_unlock();
82
Antonio Quartulli48100ba2011-10-30 12:17:33 +010083 return tt_common_entry_tmp;
84}
85
Sven Eckelmann56303d32012-06-05 22:31:31 +020086static struct batadv_tt_local_entry *
87batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010088{
Sven Eckelmann56303d32012-06-05 22:31:31 +020089 struct batadv_tt_common_entry *tt_common_entry;
90 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091
Sven Eckelmann807736f2012-07-15 22:26:51 +020092 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010093 if (tt_common_entry)
94 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020095 struct batadv_tt_local_entry,
96 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010097 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000098}
99
Sven Eckelmann56303d32012-06-05 22:31:31 +0200100static struct batadv_tt_global_entry *
101batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000102{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103 struct batadv_tt_common_entry *tt_common_entry;
104 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000105
Sven Eckelmann807736f2012-07-15 22:26:51 +0200106 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100107 if (tt_common_entry)
108 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109 struct batadv_tt_global_entry,
110 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100111 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000112}
113
Sven Eckelmanna5130882012-05-16 20:23:16 +0200114static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200116{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100117 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
118 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200119}
120
Sven Eckelmanna5130882012-05-16 20:23:16 +0200121static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200122{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200123 struct batadv_tt_common_entry *tt_common_entry;
124 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125
Sven Eckelmann56303d32012-06-05 22:31:31 +0200126 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
127 tt_global_entry = container_of(tt_common_entry,
128 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Simon Wunderlich531027f2011-10-19 11:02:25 +0200130 kfree(tt_global_entry);
131}
132
Sven Eckelmanna5130882012-05-16 20:23:16 +0200133static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200134batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200135{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200136 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100138 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200139 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 }
141}
142
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200145 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200146
Sven Eckelmann56303d32012-06-05 22:31:31 +0200147 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200148 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200149 kfree(orig_entry);
150}
151
Sven Eckelmanna5130882012-05-16 20:23:16 +0200152static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200153batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200154{
Antonio Quartullid657e622012-07-01 14:09:12 +0200155 if (!atomic_dec_and_test(&orig_entry->refcount))
156 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000157 /* to avoid race conditions, immediately decrease the tt counter */
158 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200159 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200160}
161
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200164{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200166 bool event_removed = false;
167 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168
169 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
170
171 if (!tt_change_node)
172 return;
173
Antonio Quartulliff66c972011-06-30 01:14:00 +0200174 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
176
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200177 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200178
179 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200180 spin_lock_bh(&bat_priv->tt.changes_list_lock);
181 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182 list) {
183 if (!batadv_compare_eth(entry->change.addr, addr))
184 continue;
185
186 /* DEL+ADD in the same orig interval have no effect and can be
187 * removed to avoid silly behaviour on the receiver side. The
188 * other way around (ADD+DEL) can happen in case of roaming of
189 * a client still in the NEW state. Roaming of NEW clients is
190 * now possible due to automatically recognition of "temporary"
191 * clients
192 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200193 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200194 if (!del_op_requested && del_op_entry)
195 goto del;
196 if (del_op_requested && !del_op_entry)
197 goto del;
198 continue;
199del:
200 list_del(&entry->list);
201 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000202 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200203 event_removed = true;
204 goto unlock;
205 }
206
Antonio Quartullia73105b2011-04-27 14:27:44 +0200207 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200208 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200209
210unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200211 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200212
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200214 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200215 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200216 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200217}
218
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200219int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220{
Sven Eckelmann96412692012-06-05 22:31:30 +0200221 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200222}
223
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200226 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200227 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Sven Eckelmann807736f2012-07-15 22:26:51 +0200229 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Sven Eckelmann807736f2012-07-15 22:26:51 +0200231 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200232 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
Antonio Quartullidec05072012-11-10 11:00:32 +0100234 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
235 &batadv_tt_local_hash_lock_class_key);
236
Sven Eckelmann5346c352012-05-05 13:27:28 +0200237 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238}
239
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200240static void batadv_tt_global_free(struct batadv_priv *bat_priv,
241 struct batadv_tt_global_entry *tt_global,
242 const char *message)
243{
244 batadv_dbg(BATADV_DBG_TT, bat_priv,
245 "Deleting global tt entry %pM: %s\n",
246 tt_global->common.addr, message);
247
248 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
249 batadv_choose_orig, tt_global->common.addr);
250 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200251}
252
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200253void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
254 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200256 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200257 struct batadv_tt_local_entry *tt_local;
258 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200259 struct hlist_head *head;
260 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200261 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100262 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200263 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264
Antonio Quartulli47c94652012-09-23 22:38:35 +0200265 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200266 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
Antonio Quartulli47c94652012-09-23 22:38:35 +0200268 if (tt_local) {
269 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200270 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
271 batadv_dbg(BATADV_DBG_TT, bat_priv,
272 "Re-adding pending client %pM\n", addr);
273 /* whatever the reason why the PENDING flag was set,
274 * this is a client which was enqueued to be removed in
275 * this orig_interval. Since it popped up again, the
276 * flag can be reset like it was never enqueued
277 */
278 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
279 goto add_event;
280 }
281
282 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
283 batadv_dbg(BATADV_DBG_TT, bat_priv,
284 "Roaming client %pM came back to its original location\n",
285 addr);
286 /* the ROAM flag is set because this client roamed away
287 * and the node got a roaming_advertisement message. Now
288 * that the client popped up again at its original
289 * location such flag can be unset
290 */
291 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
292 roamed_back = true;
293 }
294 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 }
296
Antonio Quartulli47c94652012-09-23 22:38:35 +0200297 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
298 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200299 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200300
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200301 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200302 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200303 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Antonio Quartulli47c94652012-09-23 22:38:35 +0200305 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100306 /* The local entry has to be marked as NEW to avoid to send it in
307 * a full table response going out before the next ttvn increment
308 * (consistency check)
309 */
310 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200311 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200312 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
313 atomic_set(&tt_local->common.refcount, 2);
314 tt_local->last_seen = jiffies;
315 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
317 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200318 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200319 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
Sven Eckelmann807736f2012-07-15 22:26:51 +0200321 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200322 batadv_choose_orig, &tt_local->common,
323 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100324
325 if (unlikely(hash_added != 0)) {
326 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200327 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100328 goto out;
329 }
330
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200331add_event:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200332 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200333
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200334check_roaming:
335 /* Check whether it is a roaming, but don't do anything if the roaming
336 * process has already been handled
337 */
338 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200339 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200340 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200341 rcu_read_lock();
342 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200343 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200344 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200345 }
346 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200347 if (roamed_back) {
348 batadv_tt_global_free(bat_priv, tt_global,
349 "Roaming canceled");
350 tt_global = NULL;
351 } else {
352 /* The global entry has to be marked as ROAMING and
353 * has to be kept for consistency purpose
354 */
355 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
356 tt_global->roam_at = jiffies;
357 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200358 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200359
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200360out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200361 if (tt_local)
362 batadv_tt_local_entry_free_ref(tt_local);
363 if (tt_global)
364 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365}
366
Sven Eckelmanna5130882012-05-16 20:23:16 +0200367static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
368 int *packet_buff_len,
369 int min_packet_len,
370 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800372 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800374 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
375
376 /* keep old buffer if kmalloc should fail */
377 if (new_buff) {
378 memcpy(new_buff, *packet_buff, min_packet_len);
379 kfree(*packet_buff);
380 *packet_buff = new_buff;
381 *packet_buff_len = new_packet_len;
382 }
383}
384
Sven Eckelmann56303d32012-06-05 22:31:31 +0200385static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200386 unsigned char **packet_buff,
387 int *packet_buff_len,
388 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800389{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200390 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800391 int req_len;
392
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200393 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800394
395 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 */
401 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
402 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 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200408 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800409}
410
Sven Eckelmann56303d32012-06-05 22:31:31 +0200411static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200412 unsigned char **packet_buff,
413 int *packet_buff_len,
414 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800415{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200416 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800417 int count = 0, tot_changes = 0, new_len;
418 unsigned char *tt_buff;
419
Sven Eckelmanna5130882012-05-16 20:23:16 +0200420 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
421 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800422
423 new_len = *packet_buff_len - min_packet_len;
424 tt_buff = *packet_buff + min_packet_len;
425
426 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200427 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 spin_lock_bh(&bat_priv->tt.changes_list_lock);
430 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431
Sven Eckelmann807736f2012-07-15 22:26:51 +0200432 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100433 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200434 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200435 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200436 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 count++;
438 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200439 list_del(&entry->list);
440 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200442 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
Antonio Quartullia73105b2011-04-27 14:27:44 +0200444 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200445 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
446 kfree(bat_priv->tt.last_changeset);
447 bat_priv->tt.last_changeset_len = 0;
448 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800449 /* check whether this new OGM has no changes due to size problems */
450 if (new_len > 0) {
451 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200452 * instead of providing the diff
453 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200454 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
455 if (bat_priv->tt.last_changeset) {
456 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
457 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200458 }
459 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200460 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
Marek Lindner08ad76e2012-04-23 16:32:55 +0800462 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463}
464
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200465int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466{
467 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200468 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200469 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100471 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200472 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000473 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200475 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100476 int last_seen_secs;
477 int last_seen_msecs;
478 unsigned long last_seen_jiffies;
479 bool no_purge;
480 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481
Marek Lindner30da63a2012-08-03 17:15:46 +0200482 primary_if = batadv_seq_print_text_primary_if_get(seq);
483 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200484 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100486 seq_printf(seq,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100487 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
488 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
489 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100490 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
491 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 for (i = 0; i < hash->size; i++) {
494 head = &hash->table[i];
495
Marek Lindner7aadf882011-02-18 12:28:09 +0000496 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100497 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000498 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100499 tt_local = container_of(tt_common_entry,
500 struct batadv_tt_local_entry,
501 common);
502 last_seen_jiffies = jiffies - tt_local->last_seen;
503 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
504 last_seen_secs = last_seen_msecs / 1000;
505 last_seen_msecs = last_seen_msecs % 1000;
506
507 no_purge = tt_common_entry->flags & np_flag;
508
509 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100510 tt_common_entry->addr,
511 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100513 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100514 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200515 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100516 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200517 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100518 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100519 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
520 no_purge ? last_seen_secs : 0,
521 no_purge ? last_seen_msecs : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000523 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200525out:
526 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200527 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200528 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529}
530
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531static void
532batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
533 struct batadv_tt_local_entry *tt_local_entry,
534 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200536 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
537 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538
Antonio Quartulli015758d2011-07-09 17:52:13 +0200539 /* The local client has to be marked as "pending to be removed" but has
540 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200541 * response issued before the net ttvn increment (consistency check)
542 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200543 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100544
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200545 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200546 "Local tt entry (%pM) pending to be removed: %s\n",
547 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548}
549
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200550/**
551 * batadv_tt_local_remove - logically remove an entry from the local table
552 * @bat_priv: the bat priv with all the soft interface information
553 * @addr: the MAC address of the client to remove
554 * @message: message to append to the log on deletion
555 * @roaming: true if the deletion is due to a roaming event
556 *
557 * Returns the flags assigned to the local entry before being deleted
558 */
559uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
560 const uint8_t *addr, const char *message,
561 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200563 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200564 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Sven Eckelmanna5130882012-05-16 20:23:16 +0200566 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200567 if (!tt_local_entry)
568 goto out;
569
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200570 curr_flags = tt_local_entry->common.flags;
571
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200572 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200573 /* if this global entry addition is due to a roaming, the node has to
574 * mark the local entry as "roamed" in order to correctly reroute
575 * packets later
576 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200577 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200578 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200579 /* mark the local client as ROAMed */
580 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
581 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200582
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200583 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
584 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
585 message);
586 goto out;
587 }
588 /* if this client has been added right now, it is possible to
589 * immediately purge it
590 */
591 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
592 curr_flags | BATADV_TT_CLIENT_DEL);
593 hlist_del_rcu(&tt_local_entry->common.hash_entry);
594 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200595
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200596out:
597 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200598 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200599
600 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601}
602
Sven Eckelmann56303d32012-06-05 22:31:31 +0200603static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200604 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200606 struct batadv_tt_local_entry *tt_local_entry;
607 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000608 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200609
610 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
611 hash_entry) {
612 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200613 struct batadv_tt_local_entry,
614 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200615 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
616 continue;
617
618 /* entry already marked for deletion */
619 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
620 continue;
621
622 if (!batadv_has_timed_out(tt_local_entry->last_seen,
623 BATADV_TT_LOCAL_TIMEOUT))
624 continue;
625
626 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
627 BATADV_TT_CLIENT_DEL, "timed out");
628 }
629}
630
Sven Eckelmann56303d32012-06-05 22:31:31 +0200631static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200632{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200633 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200635 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200636 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638 for (i = 0; i < hash->size; i++) {
639 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200642 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200643 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200644 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646}
647
Sven Eckelmann56303d32012-06-05 22:31:31 +0200648static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200650 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200651 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652 struct batadv_tt_common_entry *tt_common_entry;
653 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200654 struct hlist_node *node, *node_tmp;
655 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200656 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200657
Sven Eckelmann807736f2012-07-15 22:26:51 +0200658 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659 return;
660
Sven Eckelmann807736f2012-07-15 22:26:51 +0200661 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662
663 for (i = 0; i < hash->size; i++) {
664 head = &hash->table[i];
665 list_lock = &hash->list_locks[i];
666
667 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100668 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200669 head, hash_entry) {
670 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200671 tt_local = container_of(tt_common_entry,
672 struct batadv_tt_local_entry,
673 common);
674 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200675 }
676 spin_unlock_bh(list_lock);
677 }
678
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200679 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200680
Sven Eckelmann807736f2012-07-15 22:26:51 +0200681 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682}
683
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200686 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200687 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688
Sven Eckelmann807736f2012-07-15 22:26:51 +0200689 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690
Sven Eckelmann807736f2012-07-15 22:26:51 +0200691 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200692 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
Antonio Quartullidec05072012-11-10 11:00:32 +0100694 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
695 &batadv_tt_global_hash_lock_class_key);
696
Sven Eckelmann5346c352012-05-05 13:27:28 +0200697 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698}
699
Sven Eckelmann56303d32012-06-05 22:31:31 +0200700static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200702 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703
Sven Eckelmann807736f2012-07-15 22:26:51 +0200704 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705
Sven Eckelmann807736f2012-07-15 22:26:51 +0200706 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200707 list) {
708 list_del(&entry->list);
709 kfree(entry);
710 }
711
Sven Eckelmann807736f2012-07-15 22:26:51 +0200712 atomic_set(&bat_priv->tt.local_changes, 0);
713 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200714}
715
Antonio Quartullid657e622012-07-01 14:09:12 +0200716/* retrieves the orig_tt_list_entry belonging to orig_node from the
717 * batadv_tt_global_entry list
718 *
719 * returns it with an increased refcounter, NULL if not found
720 */
721static struct batadv_tt_orig_list_entry *
722batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
723 const struct batadv_orig_node *orig_node)
724{
725 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
726 const struct hlist_head *head;
727 struct hlist_node *node;
728
729 rcu_read_lock();
730 head = &entry->orig_list;
731 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
732 if (tmp_orig_entry->orig_node != orig_node)
733 continue;
734 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
735 continue;
736
737 orig_entry = tmp_orig_entry;
738 break;
739 }
740 rcu_read_unlock();
741
742 return orig_entry;
743}
744
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200745/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200746 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200748static bool
749batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
750 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200751{
Antonio Quartullid657e622012-07-01 14:09:12 +0200752 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753 bool found = false;
754
Antonio Quartullid657e622012-07-01 14:09:12 +0200755 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
756 if (orig_entry) {
757 found = true;
758 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200759 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200760
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200761 return found;
762}
763
Sven Eckelmanna5130882012-05-16 20:23:16 +0200764static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200765batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200766 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200767{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200768 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200769
Antonio Quartullid657e622012-07-01 14:09:12 +0200770 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200771 if (orig_entry) {
772 /* refresh the ttvn: the current value could be a bogus one that
773 * was added during a "temporary client detection"
774 */
775 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200776 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200777 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200778
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
780 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200781 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200782
783 INIT_HLIST_NODE(&orig_entry->list);
784 atomic_inc(&orig_node->refcount);
785 atomic_inc(&orig_node->tt_size);
786 orig_entry->orig_node = orig_node;
787 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200788 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200789
Antonio Quartullid657e622012-07-01 14:09:12 +0200790 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200791 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200792 &tt_global->orig_list);
793 spin_unlock_bh(&tt_global->list_lock);
794out:
795 if (orig_entry)
796 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200797}
798
Antonio Quartullia73105b2011-04-27 14:27:44 +0200799/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200800int batadv_tt_global_add(struct batadv_priv *bat_priv,
801 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200802 const unsigned char *tt_addr, uint8_t flags,
803 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200805 struct batadv_tt_global_entry *tt_global_entry;
806 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200807 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100808 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200809 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200810 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811
Sven Eckelmanna5130882012-05-16 20:23:16 +0200812 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200813 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
814
815 /* if the node already has a local client for this entry, it has to wait
816 * for a roaming advertisement instead of manually messing up the global
817 * table
818 */
819 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
820 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
821 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822
Antonio Quartullia73105b2011-04-27 14:27:44 +0200823 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200824 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200825 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200826 goto out;
827
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200828 common = &tt_global_entry->common;
829 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200830
Antonio Quartullid4f44692012-05-25 00:00:54 +0200831 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200832 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200833 /* node must store current time in case of roaming. This is
834 * needed to purge this entry out on timeout (if nobody claims
835 * it)
836 */
837 if (flags & BATADV_TT_CLIENT_ROAM)
838 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200839 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200840 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200841
842 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
843 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200844
Sven Eckelmann807736f2012-07-15 22:26:51 +0200845 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200846 batadv_compare_tt,
847 batadv_choose_orig, common,
848 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100849
850 if (unlikely(hash_added != 0)) {
851 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200852 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100853 goto out_remove;
854 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200855 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200856 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200857 /* If there is already a global entry, we can use this one for
858 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200859 * But if we are trying to add a temporary client then here are
860 * two options at this point:
861 * 1) the global client is not a temporary client: the global
862 * client has to be left as it is, temporary information
863 * should never override any already known client state
864 * 2) the global client is a temporary client: purge the
865 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200866 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200867 if (flags & BATADV_TT_CLIENT_TEMP) {
868 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
869 goto out;
870 if (batadv_tt_global_entry_has_orig(tt_global_entry,
871 orig_node))
872 goto out_remove;
873 batadv_tt_global_del_orig_list(tt_global_entry);
874 goto add_orig_entry;
875 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200876
877 /* if the client was temporary added before receiving the first
878 * OGM announcing it, we have to clear the TEMP flag
879 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200880 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200881
Antonio Quartullie9c001362012-11-07 15:05:33 +0100882 /* the change can carry possible "attribute" flags like the
883 * TT_CLIENT_WIFI, therefore they have to be copied in the
884 * client entry
885 */
886 tt_global_entry->common.flags |= flags;
887
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200888 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
889 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200890 * delete + roaming change for this originator.
891 *
892 * We should first delete the old originator before adding the
893 * new one.
894 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200895 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200896 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200897 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200898 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899 }
900 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200901add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200902 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200903 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200904
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200905 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200906 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200907 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200908 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200909
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100910out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200911
Antonio Quartullia73105b2011-04-27 14:27:44 +0200912 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200913 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
914 "global tt received",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200915 !!(flags & BATADV_TT_CLIENT_ROAM));
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200916 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
917
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200918 if (!(flags & BATADV_TT_CLIENT_ROAM))
919 /* this is a normal global add. Therefore the client is not in a
920 * roaming state anymore.
921 */
922 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
923
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200924out:
925 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200926 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200927 if (tt_local_entry)
928 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200929 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000930}
931
Sven Eckelmann981d8902012-10-07 13:34:15 +0200932/* batadv_transtable_best_orig - Get best originator list entry from tt entry
933 * @tt_global_entry: global translation table entry to be analyzed
934 *
935 * This functon assumes the caller holds rcu_read_lock().
936 * Returns best originator list entry or NULL on errors.
937 */
938static struct batadv_tt_orig_list_entry *
939batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
940{
941 struct batadv_neigh_node *router = NULL;
942 struct hlist_head *head;
943 struct hlist_node *node;
944 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
945 int best_tq = 0;
946
947 head = &tt_global_entry->orig_list;
948 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
949 router = batadv_orig_node_get_router(orig_entry->orig_node);
950 if (!router)
951 continue;
952
953 if (router->tq_avg > best_tq) {
954 best_entry = orig_entry;
955 best_tq = router->tq_avg;
956 }
957
958 batadv_neigh_node_free_ref(router);
959 }
960
961 return best_entry;
962}
963
964/* batadv_tt_global_print_entry - print all orig nodes who announce the address
965 * for this global entry
966 * @tt_global_entry: global translation table entry to be printed
967 * @seq: debugfs table seq_file struct
968 *
969 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200970 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200971static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200972batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200974{
975 struct hlist_head *head;
976 struct hlist_node *node;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200977 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200978 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200979 uint16_t flags;
980 uint8_t last_ttvn;
981
982 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200983 flags = tt_common_entry->flags;
984
985 best_entry = batadv_transtable_best_orig(tt_global_entry);
986 if (best_entry) {
987 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100988 seq_printf(seq,
989 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200990 '*', tt_global_entry->common.addr,
991 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100992 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200993 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
994 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
995 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
996 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200997
998 head = &tt_global_entry->orig_list;
999
1000 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001001 if (best_entry == orig_entry)
1002 continue;
1003
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001004 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001005 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1006 '+', tt_global_entry->common.addr,
1007 orig_entry->ttvn, orig_entry->orig_node->orig,
1008 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001009 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001010 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1011 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001012 }
1013}
1014
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001015int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016{
1017 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001018 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001019 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 struct batadv_tt_common_entry *tt_common_entry;
1021 struct batadv_tt_global_entry *tt_global;
1022 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +00001023 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001025 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026
Marek Lindner30da63a2012-08-03 17:15:46 +02001027 primary_if = batadv_seq_print_text_primary_if_get(seq);
1028 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001029 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001031 seq_printf(seq,
1032 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 net_dev->name);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001034 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1035 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1036 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001038 for (i = 0; i < hash->size; i++) {
1039 head = &hash->table[i];
1040
Marek Lindner7aadf882011-02-18 12:28:09 +00001041 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001042 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +00001043 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001044 tt_global = container_of(tt_common_entry,
1045 struct batadv_tt_global_entry,
1046 common);
1047 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001049 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001051out:
1052 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001053 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001054 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001055}
1056
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001057/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001058static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001059batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001061 struct hlist_head *head;
1062 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001063 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001064
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001065 spin_lock_bh(&tt_global_entry->list_lock);
1066 head = &tt_global_entry->orig_list;
1067 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1068 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001069 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070 }
1071 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072}
1073
Sven Eckelmanna5130882012-05-16 20:23:16 +02001074static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1076 struct batadv_tt_global_entry *tt_global_entry,
1077 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001078 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001079{
1080 struct hlist_head *head;
1081 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001082 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001083
1084 spin_lock_bh(&tt_global_entry->list_lock);
1085 head = &tt_global_entry->orig_list;
1086 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1087 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001088 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001089 "Deleting %pM from global tt entry %pM: %s\n",
1090 orig_node->orig,
1091 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001092 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001093 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094 }
1095 }
1096 spin_unlock_bh(&tt_global_entry->list_lock);
1097}
1098
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001099/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001100 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1101 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001102 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001103static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001104batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1105 struct batadv_tt_global_entry *tt_global_entry,
1106 struct batadv_orig_node *orig_node,
1107 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001108{
1109 bool last_entry = true;
1110 struct hlist_head *head;
1111 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001112 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001113
1114 /* no local entry exists, case 1:
1115 * Check if this is the last one or if other entries exist.
1116 */
1117
1118 rcu_read_lock();
1119 head = &tt_global_entry->orig_list;
1120 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1121 if (orig_entry->orig_node != orig_node) {
1122 last_entry = false;
1123 break;
1124 }
1125 }
1126 rcu_read_unlock();
1127
1128 if (last_entry) {
1129 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001130 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001131 tt_global_entry->roam_at = jiffies;
1132 } else
1133 /* there is another entry, we can simply delete this
1134 * one and can still use the other one.
1135 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001136 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1137 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001138}
1139
1140
1141
Sven Eckelmann56303d32012-06-05 22:31:31 +02001142static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1143 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001144 const unsigned char *addr,
1145 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001147 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001148 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149
Sven Eckelmanna5130882012-05-16 20:23:16 +02001150 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001152 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001153
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001154 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001155 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1156 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001157
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001158 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001159 batadv_tt_global_free(bat_priv, tt_global_entry,
1160 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001161
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001162 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001164
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001165 /* if we are deleting a global entry due to a roam
1166 * event, there are two possibilities:
1167 * 1) the client roamed from node A to node B => if there
1168 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001169 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 * wait for node B to claim it. In case of timeout
1171 * the entry is purged.
1172 *
1173 * If there are other originators left, we directly delete
1174 * the originator.
1175 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001176 * the global entry, since it is useless now.
1177 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001178 local_entry = batadv_tt_local_hash_find(bat_priv,
1179 tt_global_entry->common.addr);
1180 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001182 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001183 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001184 } else
1185 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001186 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1187 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001188
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001189
Antonio Quartullicc47f662011-04-27 14:27:57 +02001190out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001191 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001192 batadv_tt_global_entry_free_ref(tt_global_entry);
1193 if (local_entry)
1194 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001195}
1196
Sven Eckelmann56303d32012-06-05 22:31:31 +02001197void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1198 struct batadv_orig_node *orig_node,
1199 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001201 struct batadv_tt_global_entry *tt_global;
1202 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001203 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001204 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205 struct hlist_node *node, *safe;
1206 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001207 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001208
Simon Wunderlich6e801492011-10-19 10:28:26 +02001209 if (!hash)
1210 return;
1211
Antonio Quartullia73105b2011-04-27 14:27:44 +02001212 for (i = 0; i < hash->size; i++) {
1213 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001214 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001216 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001217 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001218 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001219 tt_global = container_of(tt_common_entry,
1220 struct batadv_tt_global_entry,
1221 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001222
Sven Eckelmann56303d32012-06-05 22:31:31 +02001223 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001224 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225
Sven Eckelmann56303d32012-06-05 22:31:31 +02001226 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001227 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001228 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001229 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001230 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001231 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001232 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001233 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001234 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001235 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001236 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237}
1238
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001239static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1240 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001241{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001242 bool purge = false;
1243 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1244 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001245
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001246 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1247 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1248 purge = true;
1249 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001250 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001251
1252 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1253 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1254 purge = true;
1255 *msg = "Temporary client timeout\n";
1256 }
1257
1258 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001259}
1260
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001261static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001262{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001263 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001264 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001265 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001266 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001267 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001268 char *msg = NULL;
1269 struct batadv_tt_common_entry *tt_common;
1270 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001271
Antonio Quartullicc47f662011-04-27 14:27:57 +02001272 for (i = 0; i < hash->size; i++) {
1273 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001274 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001275
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001276 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001277 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1278 hash_entry) {
1279 tt_global = container_of(tt_common,
1280 struct batadv_tt_global_entry,
1281 common);
1282
1283 if (!batadv_tt_global_to_purge(tt_global, &msg))
1284 continue;
1285
1286 batadv_dbg(BATADV_DBG_TT, bat_priv,
1287 "Deleting global tt entry (%pM): %s\n",
1288 tt_global->common.addr, msg);
1289
1290 hlist_del_rcu(node);
1291
1292 batadv_tt_global_entry_free_ref(tt_global);
1293 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001294 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001295 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001296}
1297
Sven Eckelmann56303d32012-06-05 22:31:31 +02001298static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001299{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001300 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001301 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001302 struct batadv_tt_common_entry *tt_common_entry;
1303 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304 struct hlist_node *node, *node_tmp;
1305 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001306 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001307
Sven Eckelmann807736f2012-07-15 22:26:51 +02001308 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001309 return;
1310
Sven Eckelmann807736f2012-07-15 22:26:51 +02001311 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001312
1313 for (i = 0; i < hash->size; i++) {
1314 head = &hash->table[i];
1315 list_lock = &hash->list_locks[i];
1316
1317 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001318 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001319 head, hash_entry) {
1320 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 tt_global = container_of(tt_common_entry,
1322 struct batadv_tt_global_entry,
1323 common);
1324 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001325 }
1326 spin_unlock_bh(list_lock);
1327 }
1328
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001329 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001330
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001332}
1333
Sven Eckelmann56303d32012-06-05 22:31:31 +02001334static bool
1335_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1336 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001337{
1338 bool ret = false;
1339
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001340 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1341 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001342 ret = true;
1343
1344 return ret;
1345}
1346
Sven Eckelmann56303d32012-06-05 22:31:31 +02001347struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1348 const uint8_t *src,
1349 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001351 struct batadv_tt_local_entry *tt_local_entry = NULL;
1352 struct batadv_tt_global_entry *tt_global_entry = NULL;
1353 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001354 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001356 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001357 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001358 if (!tt_local_entry ||
1359 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001360 goto out;
1361 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001362
Sven Eckelmanna5130882012-05-16 20:23:16 +02001363 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001364 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001365 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001366
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001367 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001368 * isolation
1369 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001370 if (tt_local_entry &&
1371 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001372 goto out;
1373
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001374 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001375 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001376 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001377 if (best_entry)
1378 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001379 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1380 orig_node = NULL;
1381 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001382
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001383out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001384 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001385 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001386 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001387 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001388
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001389 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001390}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001391
1392/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001393static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1394 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395{
1396 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001397 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398 struct batadv_tt_common_entry *tt_common;
1399 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 struct hlist_node *node;
1401 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001402 uint32_t i;
1403 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404
1405 for (i = 0; i < hash->size; i++) {
1406 head = &hash->table[i];
1407
1408 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001409 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001410 tt_global = container_of(tt_common,
1411 struct batadv_tt_global_entry,
1412 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001413 /* Roaming clients are in the global table for
1414 * consistency only. They don't have to be
1415 * taken into account while computing the
1416 * global crc
1417 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001418 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001420 /* Temporary clients have not been announced yet, so
1421 * they have to be skipped while computing the global
1422 * crc
1423 */
1424 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1425 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001426
1427 /* find out if this global entry is announced by this
1428 * originator
1429 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001430 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001431 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001432 continue;
1433
1434 total_one = 0;
1435 for (j = 0; j < ETH_ALEN; j++)
1436 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001437 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001438 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439 }
1440 rcu_read_unlock();
1441 }
1442
1443 return total;
1444}
1445
1446/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001447static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448{
1449 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001450 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001451 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 struct hlist_node *node;
1453 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001454 uint32_t i;
1455 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456
1457 for (i = 0; i < hash->size; i++) {
1458 head = &hash->table[i];
1459
1460 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001461 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001462 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001463 * account while computing the CRC
1464 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001465 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001466 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 total_one = 0;
1468 for (j = 0; j < ETH_ALEN; j++)
1469 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001470 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 total ^= total_one;
1472 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473 rcu_read_unlock();
1474 }
1475
1476 return total;
1477}
1478
Sven Eckelmann56303d32012-06-05 22:31:31 +02001479static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001481 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484
Sven Eckelmann807736f2012-07-15 22:26:51 +02001485 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486 list_del(&node->list);
1487 kfree(node);
1488 }
1489
Sven Eckelmann807736f2012-07-15 22:26:51 +02001490 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491}
1492
Sven Eckelmann56303d32012-06-05 22:31:31 +02001493static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1494 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001495 const unsigned char *tt_buff,
1496 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001498 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499
1500 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001501 * last OGM (the OGM could carry no changes)
1502 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503 spin_lock_bh(&orig_node->tt_buff_lock);
1504 if (tt_buff_len > 0) {
1505 kfree(orig_node->tt_buff);
1506 orig_node->tt_buff_len = 0;
1507 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1508 if (orig_node->tt_buff) {
1509 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1510 orig_node->tt_buff_len = tt_buff_len;
1511 }
1512 }
1513 spin_unlock_bh(&orig_node->tt_buff_lock);
1514}
1515
Sven Eckelmann56303d32012-06-05 22:31:31 +02001516static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001517{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519
Sven Eckelmann807736f2012-07-15 22:26:51 +02001520 spin_lock_bh(&bat_priv->tt.req_list_lock);
1521 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001522 if (batadv_has_timed_out(node->issued_at,
1523 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001524 list_del(&node->list);
1525 kfree(node);
1526 }
1527 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001528 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529}
1530
1531/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001532 * has already been issued for this orig_node, NULL otherwise
1533 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001534static struct batadv_tt_req_node *
1535batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1536 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001537{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001538 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539
Sven Eckelmann807736f2012-07-15 22:26:51 +02001540 spin_lock_bh(&bat_priv->tt.req_list_lock);
1541 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001542 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1543 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001544 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001545 goto unlock;
1546 }
1547
1548 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1549 if (!tt_req_node)
1550 goto unlock;
1551
1552 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1553 tt_req_node->issued_at = jiffies;
1554
Sven Eckelmann807736f2012-07-15 22:26:51 +02001555 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001557 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558 return tt_req_node;
1559}
1560
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001561/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001562static int batadv_tt_local_valid_entry(const void *entry_ptr,
1563 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001564{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001565 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001566
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001567 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001568 return 0;
1569 return 1;
1570}
1571
Sven Eckelmanna5130882012-05-16 20:23:16 +02001572static int batadv_tt_global_valid(const void *entry_ptr,
1573 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001575 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1576 const struct batadv_tt_global_entry *tt_global_entry;
1577 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001579 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1580 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001581 return 0;
1582
Sven Eckelmann56303d32012-06-05 22:31:31 +02001583 tt_global_entry = container_of(tt_common_entry,
1584 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001585 common);
1586
Sven Eckelmanna5130882012-05-16 20:23:16 +02001587 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588}
1589
Sven Eckelmanna5130882012-05-16 20:23:16 +02001590static struct sk_buff *
1591batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001592 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001593 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001594 int (*valid_cb)(const void *, const void *),
1595 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001597 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001598 struct batadv_tt_query_packet *tt_response;
1599 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001600 struct hlist_node *node;
1601 struct hlist_head *head;
1602 struct sk_buff *skb = NULL;
1603 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001604 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001605 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001606 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607
1608 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1609 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001610 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001612 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613
Sven Eckelmann96412692012-06-05 22:31:30 +02001614 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001615 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616 if (!skb)
1617 goto out;
1618
Sven Eckelmann5b246572012-11-04 17:11:45 +01001619 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001620 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001622
Sven Eckelmann96412692012-06-05 22:31:30 +02001623 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 tt_count = 0;
1625
1626 rcu_read_lock();
1627 for (i = 0; i < hash->size; i++) {
1628 head = &hash->table[i];
1629
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001630 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631 head, hash_entry) {
1632 if (tt_count == tt_tot)
1633 break;
1634
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001635 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636 continue;
1637
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001638 memcpy(tt_change->addr, tt_common_entry->addr,
1639 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001640 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641
1642 tt_count++;
1643 tt_change++;
1644 }
1645 }
1646 rcu_read_unlock();
1647
Antonio Quartulli9d852392011-10-17 14:25:13 +02001648 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001649 * copied
1650 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001651 tt_response->tt_data = htons(tt_count);
1652
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653out:
1654 return skb;
1655}
1656
Sven Eckelmann56303d32012-06-05 22:31:31 +02001657static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1658 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001659 uint8_t ttvn, uint16_t tt_crc,
1660 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001661{
1662 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001663 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001664 struct batadv_hard_iface *primary_if;
1665 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001667 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001669 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001670 if (!primary_if)
1671 goto out;
1672
1673 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001674 * reply from the same orig_node yet
1675 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001676 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677 if (!tt_req_node)
1678 goto out;
1679
Sven Eckelmann5b246572012-11-04 17:11:45 +01001680 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001681 if (!skb)
1682 goto out;
1683
Sven Eckelmann5b246572012-11-04 17:11:45 +01001684 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685
Sven Eckelmann96412692012-06-05 22:31:30 +02001686 tt_req_len = sizeof(*tt_request);
1687 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001689 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001690 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1692 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001693 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001695 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001696 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697
1698 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001699 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001701 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1702 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001704 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001705
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001706 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1707 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708
1709out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001710 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001711 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712 if (ret)
1713 kfree_skb(skb);
1714 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001715 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 kfree(tt_req_node);
1719 }
1720 return ret;
1721}
1722
Sven Eckelmann96412692012-06-05 22:31:30 +02001723static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001724batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001725 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001727 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001728 struct batadv_orig_node *res_dst_orig_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730 uint8_t orig_ttvn, req_ttvn, ttvn;
1731 int ret = false;
1732 unsigned char *tt_buff;
1733 bool full_table;
1734 uint16_t tt_len, tt_tot;
1735 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001736 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001737 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001738 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001740 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001741 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1742 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001743 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744
1745 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001746 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 if (!req_dst_orig_node)
1748 goto out;
1749
Sven Eckelmannda641192012-05-12 13:48:56 +02001750 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 if (!res_dst_orig_node)
1752 goto out;
1753
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001754 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755 if (!primary_if)
1756 goto out;
1757
1758 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1759 req_ttvn = tt_request->ttvn;
1760
Antonio Quartulli015758d2011-07-09 17:52:13 +02001761 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001763 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001764 goto out;
1765
Antonio Quartulli015758d2011-07-09 17:52:13 +02001766 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001767 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001768 !req_dst_orig_node->tt_buff)
1769 full_table = true;
1770 else
1771 full_table = false;
1772
1773 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001774 * I'll send only one packet with as much TT entries as I can
1775 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 if (!full_table) {
1777 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1778 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001779 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Sven Eckelmann96412692012-06-05 22:31:30 +02001781 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001782 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783 if (!skb)
1784 goto unlock;
1785
Sven Eckelmann5b246572012-11-04 17:11:45 +01001786 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001787 packet_pos = skb_put(skb, len);
1788 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 tt_response->ttvn = req_ttvn;
1790 tt_response->tt_data = htons(tt_tot);
1791
Sven Eckelmann96412692012-06-05 22:31:30 +02001792 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793 /* Copy the last orig_node's OGM buffer */
1794 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1795 req_dst_orig_node->tt_buff_len);
1796
1797 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1798 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001799 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1800 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1802
Sven Eckelmanna5130882012-05-16 20:23:16 +02001803 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001804 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001805 primary_if,
1806 batadv_tt_global_valid,
1807 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808 if (!skb)
1809 goto out;
1810
Sven Eckelmann96412692012-06-05 22:31:30 +02001811 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812 }
1813
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001814 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001815 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001816 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001817 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1818 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001819 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820
1821 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001822 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001824 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001825 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1826 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001828 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001829
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001830 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1831 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832 goto out;
1833
1834unlock:
1835 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1836
1837out:
1838 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001839 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001841 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001843 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 if (!ret)
1845 kfree_skb(skb);
1846 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001847}
Sven Eckelmann96412692012-06-05 22:31:30 +02001848
1849static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001850batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001851 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001853 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001854 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 uint8_t my_ttvn, req_ttvn, ttvn;
1856 int ret = false;
1857 unsigned char *tt_buff;
1858 bool full_table;
1859 uint16_t tt_len, tt_tot;
1860 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001861 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001862 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001863 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001865 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001866 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1867 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001868 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869
1870
Sven Eckelmann807736f2012-07-15 22:26:51 +02001871 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872 req_ttvn = tt_request->ttvn;
1873
Sven Eckelmannda641192012-05-12 13:48:56 +02001874 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875 if (!orig_node)
1876 goto out;
1877
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001878 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879 if (!primary_if)
1880 goto out;
1881
1882 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001883 * is too big send the whole local translation table
1884 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001885 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001886 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001887 full_table = true;
1888 else
1889 full_table = false;
1890
1891 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001892 * I'll send only one packet with as much TT entries as I can
1893 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001894 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001895 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1896 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001897 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898
Sven Eckelmann96412692012-06-05 22:31:30 +02001899 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001900 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001901 if (!skb)
1902 goto unlock;
1903
Sven Eckelmann5b246572012-11-04 17:11:45 +01001904 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001905 packet_pos = skb_put(skb, len);
1906 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001907 tt_response->ttvn = req_ttvn;
1908 tt_response->tt_data = htons(tt_tot);
1909
Sven Eckelmann96412692012-06-05 22:31:30 +02001910 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001911 memcpy(tt_buff, bat_priv->tt.last_changeset,
1912 bat_priv->tt.last_changeset_len);
1913 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001915 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001916 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001917 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918
Sven Eckelmanna5130882012-05-16 20:23:16 +02001919 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001920 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001921 primary_if,
1922 batadv_tt_local_valid_entry,
1923 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001924 if (!skb)
1925 goto out;
1926
Sven Eckelmann96412692012-06-05 22:31:30 +02001927 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928 }
1929
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001930 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001931 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001932 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1934 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001935 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001936
1937 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001938 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001940 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001941 "Sending TT_RESPONSE to %pM [%c]\n",
1942 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001943 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001944
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001945 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001946
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001947 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1948 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001949 goto out;
1950
1951unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001952 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953out:
1954 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001955 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001957 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958 if (!ret)
1959 kfree_skb(skb);
1960 /* This packet was for me, so it doesn't need to be re-routed */
1961 return true;
1962}
1963
Sven Eckelmann56303d32012-06-05 22:31:31 +02001964bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001965 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001966{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001967 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001968 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001969 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001970 return true;
1971
Sven Eckelmanna5130882012-05-16 20:23:16 +02001972 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001973 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001974 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001975 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001976}
1977
Sven Eckelmann56303d32012-06-05 22:31:31 +02001978static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1979 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001980 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001981 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001982{
1983 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001984 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001985
1986 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001987 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1988 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001989 batadv_tt_global_del(bat_priv, orig_node,
1990 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001991 "tt removed by changes",
1992 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001993 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001994 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001995 (tt_change + i)->addr,
1996 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001997 /* In case of problem while storing a
1998 * global_entry, we stop the updating
1999 * procedure without committing the
2000 * ttvn change. This will avoid to send
2001 * corrupted data on tt_request
2002 */
2003 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002004 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002005 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002006 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007}
2008
Sven Eckelmann56303d32012-06-05 22:31:31 +02002009static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002010 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002012 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013
Sven Eckelmannda641192012-05-12 13:48:56 +02002014 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002015 if (!orig_node)
2016 goto out;
2017
2018 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002019 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002020
Sven Eckelmanna5130882012-05-16 20:23:16 +02002021 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002022 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002023 ntohs(tt_response->tt_data),
2024 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025
2026 spin_lock_bh(&orig_node->tt_buff_lock);
2027 kfree(orig_node->tt_buff);
2028 orig_node->tt_buff_len = 0;
2029 orig_node->tt_buff = NULL;
2030 spin_unlock_bh(&orig_node->tt_buff_lock);
2031
2032 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2033
2034out:
2035 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002036 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002037}
2038
Sven Eckelmann56303d32012-06-05 22:31:31 +02002039static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2040 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002041 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002042 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002044 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2045 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002046
Sven Eckelmanna5130882012-05-16 20:23:16 +02002047 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2048 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002049 atomic_set(&orig_node->last_ttvn, ttvn);
2050}
2051
Sven Eckelmann56303d32012-06-05 22:31:31 +02002052bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002053{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002054 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002055 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002056
Sven Eckelmanna5130882012-05-16 20:23:16 +02002057 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002058 if (!tt_local_entry)
2059 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002060 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002061 * consistency purpose)
2062 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002063 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2064 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002065 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002066 ret = true;
2067out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002068 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002069 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002070 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071}
2072
Sven Eckelmann56303d32012-06-05 22:31:31 +02002073void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002074 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002075{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002076 struct batadv_tt_req_node *node, *safe;
2077 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002078 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002079
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002080 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002081 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2082 tt_response->src, tt_response->ttvn,
2083 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002084 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002085
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002086 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002087 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002088 goto out;
2089
Sven Eckelmannda641192012-05-12 13:48:56 +02002090 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091 if (!orig_node)
2092 goto out;
2093
Sven Eckelmann96412692012-06-05 22:31:30 +02002094 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002095 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002096 } else {
2097 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002098 batadv_tt_update_changes(bat_priv, orig_node,
2099 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002100 tt_response->ttvn, tt_change);
2101 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002102
2103 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002104 spin_lock_bh(&bat_priv->tt.req_list_lock);
2105 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002106 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002107 continue;
2108 list_del(&node->list);
2109 kfree(node);
2110 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002111 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112
2113 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002114 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002115out:
2116 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002117 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002118}
2119
Sven Eckelmann56303d32012-06-05 22:31:31 +02002120int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002122 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002123
Sven Eckelmanna5130882012-05-16 20:23:16 +02002124 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002125 if (ret < 0)
2126 return ret;
2127
Sven Eckelmanna5130882012-05-16 20:23:16 +02002128 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002129 if (ret < 0)
2130 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002131
Antonio Quartulli72414442012-12-25 13:14:37 +01002132 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2133 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2134 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002135
2136 return 1;
2137}
2138
Sven Eckelmann56303d32012-06-05 22:31:31 +02002139static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002140{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002141 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002142
Sven Eckelmann807736f2012-07-15 22:26:51 +02002143 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144
Sven Eckelmann807736f2012-07-15 22:26:51 +02002145 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002146 list_del(&node->list);
2147 kfree(node);
2148 }
2149
Sven Eckelmann807736f2012-07-15 22:26:51 +02002150 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002151}
2152
Sven Eckelmann56303d32012-06-05 22:31:31 +02002153static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002154{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002155 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002156
Sven Eckelmann807736f2012-07-15 22:26:51 +02002157 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2158 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002159 if (!batadv_has_timed_out(node->first_time,
2160 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002161 continue;
2162
2163 list_del(&node->list);
2164 kfree(node);
2165 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002166 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002167}
2168
2169/* This function checks whether the client already reached the
2170 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2171 * will not be sent.
2172 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002173 * returns true if the ROAMING_ADV can be sent, false otherwise
2174 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002175static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002176 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002177{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002178 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002179 bool ret = false;
2180
Sven Eckelmann807736f2012-07-15 22:26:51 +02002181 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002182 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002183 * reply from the same orig_node yet
2184 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002185 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002186 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002187 continue;
2188
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002189 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002190 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002191 continue;
2192
Sven Eckelmann3e348192012-05-16 20:23:22 +02002193 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002194 /* Sorry, you roamed too many times! */
2195 goto unlock;
2196 ret = true;
2197 break;
2198 }
2199
2200 if (!ret) {
2201 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2202 if (!tt_roam_node)
2203 goto unlock;
2204
2205 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002206 atomic_set(&tt_roam_node->counter,
2207 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2209
Sven Eckelmann807736f2012-07-15 22:26:51 +02002210 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002211 ret = true;
2212 }
2213
2214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002215 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002216 return ret;
2217}
2218
Sven Eckelmann56303d32012-06-05 22:31:31 +02002219static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2220 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002221{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002222 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002223 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002224 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002225 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002226 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002227
2228 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002229 * already roamed to us too many times
2230 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002231 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002232 goto out;
2233
Sven Eckelmann5b246572012-11-04 17:11:45 +01002234 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002235 if (!skb)
2236 goto out;
2237
Sven Eckelmann5b246572012-11-04 17:11:45 +01002238 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002239
Sven Eckelmann96412692012-06-05 22:31:30 +02002240 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002241
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002242 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002243 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002244 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002245 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002246 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002247 if (!primary_if)
2248 goto out;
2249 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002250 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002251 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2252 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2253
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002254 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002255 "Sending ROAMING_ADV to %pM (client %pM)\n",
2256 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002257
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002258 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002259
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002260 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2261 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002262
2263out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002264 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002265 kfree_skb(skb);
2266 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267}
2268
Sven Eckelmanna5130882012-05-16 20:23:16 +02002269static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002270{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002271 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002272 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002273 struct batadv_priv *bat_priv;
2274
2275 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002276 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2277 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002278
Sven Eckelmanna5130882012-05-16 20:23:16 +02002279 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002280 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002281 batadv_tt_req_purge(bat_priv);
2282 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002283
Antonio Quartulli72414442012-12-25 13:14:37 +01002284 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2285 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002286}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002287
Sven Eckelmann56303d32012-06-05 22:31:31 +02002288void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002289{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002290 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002291
Sven Eckelmanna5130882012-05-16 20:23:16 +02002292 batadv_tt_local_table_free(bat_priv);
2293 batadv_tt_global_table_free(bat_priv);
2294 batadv_tt_req_list_free(bat_priv);
2295 batadv_tt_changes_list_free(bat_priv);
2296 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002297
Sven Eckelmann807736f2012-07-15 22:26:51 +02002298 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002299}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002300
Antonio Quartulli697f2532011-11-07 16:47:01 +01002301/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002302 * in the given hash table and returns the number of modified entries
2303 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002304static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2305 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002306{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002307 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002308 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002309 struct hlist_head *head;
2310 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002311 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002312
2313 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002314 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002315
2316 for (i = 0; i < hash->size; i++) {
2317 head = &hash->table[i];
2318
2319 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002320 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002321 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002322 if (enable) {
2323 if ((tt_common_entry->flags & flags) == flags)
2324 continue;
2325 tt_common_entry->flags |= flags;
2326 } else {
2327 if (!(tt_common_entry->flags & flags))
2328 continue;
2329 tt_common_entry->flags &= ~flags;
2330 }
2331 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002332 }
2333 rcu_read_unlock();
2334 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002335out:
2336 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002337}
2338
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002339/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002340static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002341{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002342 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002343 struct batadv_tt_common_entry *tt_common;
2344 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002345 struct hlist_node *node, *node_tmp;
2346 struct hlist_head *head;
2347 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002348 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002349
2350 if (!hash)
2351 return;
2352
2353 for (i = 0; i < hash->size; i++) {
2354 head = &hash->table[i];
2355 list_lock = &hash->list_locks[i];
2356
2357 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002358 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2359 hash_entry) {
2360 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002361 continue;
2362
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002363 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002364 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002365 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002366
Sven Eckelmann807736f2012-07-15 22:26:51 +02002367 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002368 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002369 tt_local = container_of(tt_common,
2370 struct batadv_tt_local_entry,
2371 common);
2372 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002373 }
2374 spin_unlock_bh(list_lock);
2375 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002376}
2377
Sven Eckelmann56303d32012-06-05 22:31:31 +02002378static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002379 unsigned char **packet_buff,
2380 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002381{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002382 uint16_t changed_num = 0;
2383
Sven Eckelmann807736f2012-07-15 22:26:51 +02002384 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002385 return -ENOENT;
2386
Sven Eckelmann807736f2012-07-15 22:26:51 +02002387 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002388 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002389
2390 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002391 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002392 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002393 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002394
2395 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002396 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002397 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002398 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002399 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002400
2401 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002402 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002403
Sven Eckelmanna5130882012-05-16 20:23:16 +02002404 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2405 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002406}
2407
2408/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002409int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002410 unsigned char **packet_buff, int *packet_buff_len,
2411 int packet_min_len)
2412{
2413 int tt_num_changes;
2414
2415 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002416 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2417 packet_buff_len,
2418 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002419
2420 /* if the changes have been sent often enough */
2421 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002422 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002423 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2424 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002425 tt_num_changes = 0;
2426 }
2427
2428 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002429}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002430
Sven Eckelmann56303d32012-06-05 22:31:31 +02002431bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002432 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002433{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002434 struct batadv_tt_local_entry *tt_local_entry = NULL;
2435 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002436 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002437
2438 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002439 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002440
Sven Eckelmanna5130882012-05-16 20:23:16 +02002441 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002442 if (!tt_local_entry)
2443 goto out;
2444
Sven Eckelmanna5130882012-05-16 20:23:16 +02002445 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002446 if (!tt_global_entry)
2447 goto out;
2448
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002449 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002450 goto out;
2451
Marek Lindner5870adc2012-06-20 17:16:05 +02002452 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002453
2454out:
2455 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002456 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002457 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002458 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002459 return ret;
2460}
Marek Lindnera943cac2011-07-30 13:10:18 +02002461
Sven Eckelmann56303d32012-06-05 22:31:31 +02002462void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2463 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002464 const unsigned char *tt_buff, uint8_t tt_num_changes,
2465 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002466{
2467 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2468 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002469 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002470
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002471 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002472 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002473 return;
2474
Antonio Quartulli17071572011-11-07 16:36:40 +01002475 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002476 * increased by one -> we can apply the attached changes
2477 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002478 if ((!orig_node->tt_initialised && ttvn == 1) ||
2479 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002480 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002481 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2482 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002483 * In this case send a tt request
2484 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002485 if (!tt_num_changes) {
2486 full_table = false;
2487 goto request_table;
2488 }
2489
Sven Eckelmann96412692012-06-05 22:31:30 +02002490 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002491 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002492 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002493
2494 /* Even if we received the precomputed crc with the OGM, we
2495 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002496 * in the global table
2497 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002498 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002499
2500 /* The ttvn alone is not enough to guarantee consistency
2501 * because a single value could represent different states
2502 * (due to the wrap around). Thus a node has to check whether
2503 * the resulting table (after applying the changes) is still
2504 * consistent or not. E.g. a node could disconnect while its
2505 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2506 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002507 * inconsistency
2508 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002509 if (orig_node->tt_crc != tt_crc)
2510 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002511 } else {
2512 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002513 * in sync anymore -> request fresh tt data
2514 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002515 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2516 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002517request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002518 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002519 "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 +02002520 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2521 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002522 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2523 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002524 return;
2525 }
2526 }
2527}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002528
2529/* returns true whether we know that the client has moved from its old
2530 * originator to another one. This entry is kept is still kept for consistency
2531 * purposes
2532 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002533bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002534 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002535{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002536 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002537 bool ret = false;
2538
Sven Eckelmanna5130882012-05-16 20:23:16 +02002539 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002540 if (!tt_global_entry)
2541 goto out;
2542
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002543 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002544 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002545out:
2546 return ret;
2547}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002548
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002549/**
2550 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2551 * @bat_priv: the bat priv with all the soft interface information
2552 * @addr: the MAC address of the local client to query
2553 *
2554 * Returns true if the local client is known to be roaming (it is not served by
2555 * this node anymore) or not. If yes, the client is still present in the table
2556 * to keep the latter consistent with the node TTVN
2557 */
2558bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2559 uint8_t *addr)
2560{
2561 struct batadv_tt_local_entry *tt_local_entry;
2562 bool ret = false;
2563
2564 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2565 if (!tt_local_entry)
2566 goto out;
2567
2568 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2569 batadv_tt_local_entry_free_ref(tt_local_entry);
2570out:
2571 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002572}
2573
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002574bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2575 struct batadv_orig_node *orig_node,
2576 const unsigned char *addr)
2577{
2578 bool ret = false;
2579
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002580 /* if the originator is a backbone node (meaning it belongs to the same
2581 * LAN of this node) the temporary client must not be added because to
2582 * reach such destination the node must use the LAN instead of the mesh
2583 */
2584 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2585 goto out;
2586
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002587 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2588 BATADV_TT_CLIENT_TEMP,
2589 atomic_read(&orig_node->last_ttvn)))
2590 goto out;
2591
2592 batadv_dbg(BATADV_DBG_TT, bat_priv,
2593 "Added temporary global client (addr: %pM orig: %pM)\n",
2594 addr, orig_node->orig);
2595 ret = true;
2596out:
2597 return ret;
2598}