blob: 99dd8f75b3ff20f0d2a277e1f65ffc284b002f2e [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
Sven Eckelmann56303d32012-06-05 22:31:31 +020032static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020034static void batadv_tt_purge(struct work_struct *work);
35static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020036batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Marek Lindner7aadf882011-02-18 12:28:09 +000038/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020039static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Sven Eckelmann56303d32012-06-05 22:31:31 +020041 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020042 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
Sven Eckelmann56303d32012-06-05 22:31:31 +020047static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048{
Sven Eckelmanna5130882012-05-16 20:23:16 +020049 INIT_DELAYED_WORK(&bat_priv->tt_work, batadv_tt_purge);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020050 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt_work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020051 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
Sven Eckelmann56303d32012-06-05 22:31:31 +020054static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020055batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000056{
Marek Lindner7aadf882011-02-18 12:28:09 +000057 struct hlist_head *head;
58 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020059 struct batadv_tt_common_entry *tt_common_entry;
60 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020061 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000062
63 if (!hash)
64 return NULL;
65
Sven Eckelmannda641192012-05-12 13:48:56 +020066 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000067 head = &hash->table[index];
68
69 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010070 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020071 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000072 continue;
73
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020075 continue;
76
Antonio Quartulli48100ba2011-10-30 12:17:33 +010077 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000078 break;
79 }
80 rcu_read_unlock();
81
Antonio Quartulli48100ba2011-10-30 12:17:33 +010082 return tt_common_entry_tmp;
83}
84
Sven Eckelmann56303d32012-06-05 22:31:31 +020085static struct batadv_tt_local_entry *
86batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010087{
Sven Eckelmann56303d32012-06-05 22:31:31 +020088 struct batadv_tt_common_entry *tt_common_entry;
89 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010090
Sven Eckelmanna5130882012-05-16 20:23:16 +020091 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010092 if (tt_common_entry)
93 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020094 struct batadv_tt_local_entry,
95 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000097}
98
Sven Eckelmann56303d32012-06-05 22:31:31 +020099static struct batadv_tt_global_entry *
100batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000101{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200102 struct batadv_tt_common_entry *tt_common_entry;
103 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000104
Sven Eckelmanna5130882012-05-16 20:23:16 +0200105 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100106 if (tt_common_entry)
107 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200108 struct batadv_tt_global_entry,
109 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000111
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 Quartulli29cb99d2012-06-25 20:49:51 +0000155 /* to avoid race conditions, immediately decrease the tt counter */
156 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200157 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200158}
159
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200161 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200162{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200163 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200164 bool event_removed = false;
165 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200166
167 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
168
169 if (!tt_change_node)
170 return;
171
Antonio Quartulliff66c972011-06-30 01:14:00 +0200172 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200173 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
174
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200175 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200176
177 /* check for ADD+DEL or DEL+ADD events */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200178 spin_lock_bh(&bat_priv->tt_changes_list_lock);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200179 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
180 list) {
181 if (!batadv_compare_eth(entry->change.addr, addr))
182 continue;
183
184 /* DEL+ADD in the same orig interval have no effect and can be
185 * removed to avoid silly behaviour on the receiver side. The
186 * other way around (ADD+DEL) can happen in case of roaming of
187 * a client still in the NEW state. Roaming of NEW clients is
188 * now possible due to automatically recognition of "temporary"
189 * clients
190 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200191 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200192 if (!del_op_requested && del_op_entry)
193 goto del;
194 if (del_op_requested && !del_op_entry)
195 goto del;
196 continue;
197del:
198 list_del(&entry->list);
199 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000200 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200201 event_removed = true;
202 goto unlock;
203 }
204
Antonio Quartullia73105b2011-04-27 14:27:44 +0200205 /* track the change in the OGMinterval list */
206 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207
208unlock:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200209 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
210
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200211 if (event_removed)
212 atomic_dec(&bat_priv->tt_local_changes);
213 else
214 atomic_inc(&bat_priv->tt_local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200215}
216
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200217int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200218{
Sven Eckelmann96412692012-06-05 22:31:30 +0200219 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220}
221
Sven Eckelmann56303d32012-06-05 22:31:31 +0200222static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200224 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200225 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200227 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200229 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200230 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231
Sven Eckelmann5346c352012-05-05 13:27:28 +0200232 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233}
234
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200235void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
236 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200238 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
239 struct batadv_tt_local_entry *tt_local_entry = NULL;
240 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200241 struct hlist_head *head;
242 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200243 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100244 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
Sven Eckelmanna5130882012-05-16 20:23:16 +0200246 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200248 if (tt_local_entry) {
249 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200250 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
251 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200252 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 }
254
Sven Eckelmann704509b2011-05-14 23:14:54 +0200255 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200256 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200257 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200258
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200259 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200260 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
261 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100263 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200264 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200265 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200266 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100267 atomic_set(&tt_local_entry->common.refcount, 2);
268 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200271 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100274 /* The local entry has to be marked as NEW to avoid to send it in
275 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200276 * (consistency check)
277 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200278 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100279
Sven Eckelmanna5130882012-05-16 20:23:16 +0200280 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200281 batadv_choose_orig,
282 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200283 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100284
285 if (unlikely(hash_added != 0)) {
286 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200287 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100288 goto out;
289 }
290
Sven Eckelmanna5130882012-05-16 20:23:16 +0200291 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200292
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200294 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295
Antonio Quartullicc47f662011-04-27 14:27:57 +0200296 /* Check whether it is a roaming! */
297 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200298 /* These node are probably going to update their tt table */
299 head = &tt_global_entry->orig_list;
300 rcu_read_lock();
301 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
302 orig_entry->orig_node->tt_poss_change = true;
303
Sven Eckelmanna5130882012-05-16 20:23:16 +0200304 batadv_send_roam_adv(bat_priv,
305 tt_global_entry->common.addr,
306 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200307 }
308 rcu_read_unlock();
309 /* The global entry has to be marked as ROAMING and
310 * has to be kept for consistency purpose
311 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200312 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100313 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200314 }
315out:
316 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200317 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200318 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200319 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320}
321
Sven Eckelmanna5130882012-05-16 20:23:16 +0200322static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
323 int *packet_buff_len,
324 int min_packet_len,
325 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800327 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800329 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
330
331 /* keep old buffer if kmalloc should fail */
332 if (new_buff) {
333 memcpy(new_buff, *packet_buff, min_packet_len);
334 kfree(*packet_buff);
335 *packet_buff = new_buff;
336 *packet_buff_len = new_packet_len;
337 }
338}
339
Sven Eckelmann56303d32012-06-05 22:31:31 +0200340static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200341 unsigned char **packet_buff,
342 int *packet_buff_len,
343 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800344{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200345 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800346 int req_len;
347
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200348 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800349
350 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200351 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800352
353 /* if we have too many changes for one packet don't send any
354 * and wait for the tt table request which will be fragmented
355 */
356 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
357 req_len = min_packet_len;
358
Sven Eckelmanna5130882012-05-16 20:23:16 +0200359 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
360 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800361
362 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200363 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800364}
365
Sven Eckelmann56303d32012-06-05 22:31:31 +0200366static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200367 unsigned char **packet_buff,
368 int *packet_buff_len,
369 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800370{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200371 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800372 int count = 0, tot_changes = 0, new_len;
373 unsigned char *tt_buff;
374
Sven Eckelmanna5130882012-05-16 20:23:16 +0200375 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
376 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800377
378 new_len = *packet_buff_len - min_packet_len;
379 tt_buff = *packet_buff + min_packet_len;
380
381 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200382 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383
Antonio Quartullia73105b2011-04-27 14:27:44 +0200384 spin_lock_bh(&bat_priv->tt_changes_list_lock);
385 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Antonio Quartullia73105b2011-04-27 14:27:44 +0200387 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100388 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200390 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200391 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 count++;
393 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200394 list_del(&entry->list);
395 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Antonio Quartullia73105b2011-04-27 14:27:44 +0200399 /* Keep the buffer for possible tt_request */
400 spin_lock_bh(&bat_priv->tt_buff_lock);
401 kfree(bat_priv->tt_buff);
402 bat_priv->tt_buff_len = 0;
403 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800404 /* check whether this new OGM has no changes due to size problems */
405 if (new_len > 0) {
406 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200407 * instead of providing the diff
408 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800409 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200410 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800411 memcpy(bat_priv->tt_buff, tt_buff, new_len);
412 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200413 }
414 }
415 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416
Marek Lindner08ad76e2012-04-23 16:32:55 +0800417 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200420int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421{
422 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200423 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200424 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425 struct batadv_tt_common_entry *tt_common_entry;
426 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000427 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200429 uint32_t i;
430 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200432 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200433 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100434 ret = seq_printf(seq,
435 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200436 net_dev->name);
437 goto out;
438 }
439
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200440 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100441 ret = seq_printf(seq,
442 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200443 net_dev->name);
444 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 }
446
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100447 seq_printf(seq,
448 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200449 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451 for (i = 0; i < hash->size; i++) {
452 head = &hash->table[i];
453
Marek Lindner7aadf882011-02-18 12:28:09 +0000454 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100455 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000456 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200457 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100458 tt_common_entry->addr,
459 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200460 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100461 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200462 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100463 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200464 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100465 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200466 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100467 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200468 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000470 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200472out:
473 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200474 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200475 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476}
477
Sven Eckelmann56303d32012-06-05 22:31:31 +0200478static void
479batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
480 struct batadv_tt_local_entry *tt_local_entry,
481 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200483 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
484 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Antonio Quartulli015758d2011-07-09 17:52:13 +0200486 /* The local client has to be marked as "pending to be removed" but has
487 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200488 * response issued before the net ttvn increment (consistency check)
489 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200490 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100491
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200492 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200493 "Local tt entry (%pM) pending to be removed: %s\n",
494 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495}
496
Sven Eckelmann56303d32012-06-05 22:31:31 +0200497void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200498 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200500 struct batadv_tt_local_entry *tt_local_entry = NULL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200501 uint16_t flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Sven Eckelmanna5130882012-05-16 20:23:16 +0200503 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200504 if (!tt_local_entry)
505 goto out;
506
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200507 flags = BATADV_TT_CLIENT_DEL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200508 if (roaming)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200509 flags |= BATADV_TT_CLIENT_ROAM;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200510
511 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200512out:
513 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200514 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515}
516
Sven Eckelmann56303d32012-06-05 22:31:31 +0200517static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200518 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200520 struct batadv_tt_local_entry *tt_local_entry;
521 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000522 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200523
524 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
525 hash_entry) {
526 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200527 struct batadv_tt_local_entry,
528 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200529 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
530 continue;
531
532 /* entry already marked for deletion */
533 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
534 continue;
535
536 if (!batadv_has_timed_out(tt_local_entry->last_seen,
537 BATADV_TT_LOCAL_TIMEOUT))
538 continue;
539
540 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
541 BATADV_TT_CLIENT_DEL, "timed out");
542 }
543}
544
Sven Eckelmann56303d32012-06-05 22:31:31 +0200545static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200546{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200547 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200549 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200550 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552 for (i = 0; i < hash->size; i++) {
553 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200554 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200556 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200557 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200558 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 }
560
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561}
562
Sven Eckelmann56303d32012-06-05 22:31:31 +0200563static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200565 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200566 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200567 struct batadv_tt_common_entry *tt_common_entry;
568 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200569 struct hlist_node *node, *node_tmp;
570 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200571 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200572
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200573 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574 return;
575
Antonio Quartullia73105b2011-04-27 14:27:44 +0200576 hash = bat_priv->tt_local_hash;
577
578 for (i = 0; i < hash->size; i++) {
579 head = &hash->table[i];
580 list_lock = &hash->list_locks[i];
581
582 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100583 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200584 head, hash_entry) {
585 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200586 tt_local = container_of(tt_common_entry,
587 struct batadv_tt_local_entry,
588 common);
589 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200590 }
591 spin_unlock_bh(list_lock);
592 }
593
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200594 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200595
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200596 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597}
598
Sven Eckelmann56303d32012-06-05 22:31:31 +0200599static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200601 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200602 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200604 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200606 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200607 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Sven Eckelmann5346c352012-05-05 13:27:28 +0200609 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610}
611
Sven Eckelmann56303d32012-06-05 22:31:31 +0200612static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200613{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200614 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200615
616 spin_lock_bh(&bat_priv->tt_changes_list_lock);
617
618 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
619 list) {
620 list_del(&entry->list);
621 kfree(entry);
622 }
623
624 atomic_set(&bat_priv->tt_local_changes, 0);
625 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
626}
627
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200628/* find out if an orig_node is already in the list of a tt_global_entry.
629 * returns 1 if found, 0 otherwise
630 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200631static bool
632batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
633 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200634{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200635 struct batadv_tt_orig_list_entry *tmp_orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200636 const struct hlist_head *head;
637 struct hlist_node *node;
638 bool found = false;
639
640 rcu_read_lock();
641 head = &entry->orig_list;
642 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
643 if (tmp_orig_entry->orig_node == orig_node) {
644 found = true;
645 break;
646 }
647 }
648 rcu_read_unlock();
649 return found;
650}
651
Sven Eckelmanna5130882012-05-16 20:23:16 +0200652static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200653batadv_tt_global_add_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
654 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200655{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200656 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200657
658 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
659 if (!orig_entry)
660 return;
661
662 INIT_HLIST_NODE(&orig_entry->list);
663 atomic_inc(&orig_node->refcount);
664 atomic_inc(&orig_node->tt_size);
665 orig_entry->orig_node = orig_node;
666 orig_entry->ttvn = ttvn;
667
668 spin_lock_bh(&tt_global_entry->list_lock);
669 hlist_add_head_rcu(&orig_entry->list,
670 &tt_global_entry->orig_list);
671 spin_unlock_bh(&tt_global_entry->list_lock);
672}
673
Antonio Quartullia73105b2011-04-27 14:27:44 +0200674/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675int batadv_tt_global_add(struct batadv_priv *bat_priv,
676 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200677 const unsigned char *tt_addr, uint8_t flags,
678 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200681 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100682 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200683 struct batadv_tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684
Sven Eckelmanna5130882012-05-16 20:23:16 +0200685 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686
Antonio Quartullia73105b2011-04-27 14:27:44 +0200687 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200688 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200689 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200690 goto out;
691
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200692 common = &tt_global_entry->common;
693 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200694
Antonio Quartullid4f44692012-05-25 00:00:54 +0200695 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200696 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200697 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200698
699 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
700 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200701
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200702 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200703 batadv_compare_tt,
704 batadv_choose_orig, common,
705 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100706
707 if (unlikely(hash_added != 0)) {
708 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200709 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100710 goto out_remove;
711 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200712
Sven Eckelmanna5130882012-05-16 20:23:16 +0200713 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
714 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200715 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200716 /* there is already a global entry, use this one. */
717
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200718 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
719 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200720 * delete + roaming change for this originator.
721 *
722 * We should first delete the old originator before adding the
723 * new one.
724 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200725 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200726 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200727 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200728 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200730
Sven Eckelmanna5130882012-05-16 20:23:16 +0200731 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
732 orig_node))
733 batadv_tt_global_add_orig_entry(tt_global_entry,
734 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200737 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200738 "Creating new global tt entry: %pM (via %pM)\n",
739 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200740
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100741out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200742 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200743 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200744 "global tt received",
745 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200746 ret = 1;
747out:
748 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200749 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200750 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751}
752
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753/* print all orig nodes who announce the address for this global entry.
754 * it is assumed that the caller holds rcu_read_lock();
755 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200756static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200757batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200758 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200759{
760 struct hlist_head *head;
761 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200762 struct batadv_tt_orig_list_entry *orig_entry;
763 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200764 uint16_t flags;
765 uint8_t last_ttvn;
766
767 tt_common_entry = &tt_global_entry->common;
768
769 head = &tt_global_entry->orig_list;
770
771 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
772 flags = tt_common_entry->flags;
773 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
774 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
775 tt_global_entry->common.addr, orig_entry->ttvn,
776 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200777 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
778 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779 }
780}
781
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200782int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783{
784 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200785 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200786 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200787 struct batadv_tt_common_entry *tt_common_entry;
788 struct batadv_tt_global_entry *tt_global;
789 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000790 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200792 uint32_t i;
793 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200795 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200796 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100797 ret = seq_printf(seq,
798 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200799 net_dev->name);
800 goto out;
801 }
802
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200803 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100804 ret = seq_printf(seq,
805 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200806 net_dev->name);
807 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 }
809
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200810 seq_printf(seq,
811 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200813 seq_printf(seq, " %-13s %s %-15s %s %s\n",
814 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816 for (i = 0; i < hash->size; i++) {
817 head = &hash->table[i];
818
Marek Lindner7aadf882011-02-18 12:28:09 +0000819 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100820 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000821 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200822 tt_global = container_of(tt_common_entry,
823 struct batadv_tt_global_entry,
824 common);
825 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000827 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200829out:
830 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200831 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200832 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833}
834
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200835/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200836static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200837batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000838{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839 struct hlist_head *head;
840 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200841 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200842
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200843 spin_lock_bh(&tt_global_entry->list_lock);
844 head = &tt_global_entry->orig_list;
845 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
846 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200847 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200848 }
849 spin_unlock_bh(&tt_global_entry->list_lock);
850
851}
852
Sven Eckelmanna5130882012-05-16 20:23:16 +0200853static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200854batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
855 struct batadv_tt_global_entry *tt_global_entry,
856 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200857 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200858{
859 struct hlist_head *head;
860 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200861 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200862
863 spin_lock_bh(&tt_global_entry->list_lock);
864 head = &tt_global_entry->orig_list;
865 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
866 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200867 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200868 "Deleting %pM from global tt entry %pM: %s\n",
869 orig_node->orig,
870 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200871 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200872 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200873 }
874 }
875 spin_unlock_bh(&tt_global_entry->list_lock);
876}
877
Sven Eckelmann56303d32012-06-05 22:31:31 +0200878static void
879batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
880 struct batadv_tt_global_entry *tt_global_entry,
881 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200882{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200883 batadv_dbg(BATADV_DBG_TT, bat_priv,
884 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200885 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200886
Sven Eckelmanna5130882012-05-16 20:23:16 +0200887 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200888 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200889 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200890
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891}
892
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200893/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200894 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
895 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200896 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200897static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200898batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
899 struct batadv_tt_global_entry *tt_global_entry,
900 struct batadv_orig_node *orig_node,
901 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200902{
903 bool last_entry = true;
904 struct hlist_head *head;
905 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200906 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200907
908 /* no local entry exists, case 1:
909 * Check if this is the last one or if other entries exist.
910 */
911
912 rcu_read_lock();
913 head = &tt_global_entry->orig_list;
914 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
915 if (orig_entry->orig_node != orig_node) {
916 last_entry = false;
917 break;
918 }
919 }
920 rcu_read_unlock();
921
922 if (last_entry) {
923 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200924 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200925 tt_global_entry->roam_at = jiffies;
926 } else
927 /* there is another entry, we can simply delete this
928 * one and can still use the other one.
929 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200930 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
931 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200932}
933
934
935
Sven Eckelmann56303d32012-06-05 22:31:31 +0200936static void batadv_tt_global_del(struct batadv_priv *bat_priv,
937 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200938 const unsigned char *addr,
939 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000940{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200941 struct batadv_tt_global_entry *tt_global_entry = NULL;
942 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943
Sven Eckelmanna5130882012-05-16 20:23:16 +0200944 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200945 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200946 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200947
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200948 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200949 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
950 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800951
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200952 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200953 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
954 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200955
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800956 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200957 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800958
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200959 /* if we are deleting a global entry due to a roam
960 * event, there are two possibilities:
961 * 1) the client roamed from node A to node B => if there
962 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200963 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200964 * wait for node B to claim it. In case of timeout
965 * the entry is purged.
966 *
967 * If there are other originators left, we directly delete
968 * the originator.
969 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200970 * the global entry, since it is useless now.
971 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200972 local_entry = batadv_tt_local_hash_find(bat_priv,
973 tt_global_entry->common.addr);
974 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200975 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200976 batadv_tt_global_del_orig_list(tt_global_entry);
977 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200978 } else
979 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200980 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
981 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200982
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800983
Antonio Quartullicc47f662011-04-27 14:27:57 +0200984out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200985 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200986 batadv_tt_global_entry_free_ref(tt_global_entry);
987 if (local_entry)
988 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200989}
990
Sven Eckelmann56303d32012-06-05 22:31:31 +0200991void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
992 struct batadv_orig_node *orig_node,
993 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200994{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200995 struct batadv_tt_global_entry *tt_global;
996 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200997 uint32_t i;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200998 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200999 struct hlist_node *node, *safe;
1000 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001001 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001002
Simon Wunderlich6e801492011-10-19 10:28:26 +02001003 if (!hash)
1004 return;
1005
Antonio Quartullia73105b2011-04-27 14:27:44 +02001006 for (i = 0; i < hash->size; i++) {
1007 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001008 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001010 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001011 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001012 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001013 tt_global = container_of(tt_common_entry,
1014 struct batadv_tt_global_entry,
1015 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001016
Sven Eckelmann56303d32012-06-05 22:31:31 +02001017 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001018 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001019
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001021 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001022 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001023 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001024 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001025 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001026 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001027 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001028 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001030 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031}
1032
Sven Eckelmann56303d32012-06-05 22:31:31 +02001033static void batadv_tt_global_roam_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001034 struct hlist_head *head)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001035{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001036 struct batadv_tt_common_entry *tt_common_entry;
1037 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001038 struct hlist_node *node, *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001039
1040 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
1041 hash_entry) {
1042 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043 struct batadv_tt_global_entry,
1044 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001045 if (!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001046 continue;
1047 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1048 BATADV_TT_CLIENT_ROAM_TIMEOUT))
1049 continue;
1050
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001051 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001052 "Deleting global tt entry (%pM): Roaming timeout\n",
1053 tt_global_entry->common.addr);
1054
1055 hlist_del_rcu(node);
1056 batadv_tt_global_entry_free_ref(tt_global_entry);
1057 }
1058}
1059
Sven Eckelmann56303d32012-06-05 22:31:31 +02001060static void batadv_tt_global_roam_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001061{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001062 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001063 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001064 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001065 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001066
Antonio Quartullicc47f662011-04-27 14:27:57 +02001067 for (i = 0; i < hash->size; i++) {
1068 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001069 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001070
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001071 spin_lock_bh(list_lock);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001072 batadv_tt_global_roam_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001073 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001074 }
1075
Antonio Quartullicc47f662011-04-27 14:27:57 +02001076}
1077
Sven Eckelmann56303d32012-06-05 22:31:31 +02001078static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001080 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001081 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001082 struct batadv_tt_common_entry *tt_common_entry;
1083 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001084 struct hlist_node *node, *node_tmp;
1085 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001086 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001087
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001088 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089 return;
1090
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001091 hash = bat_priv->tt_global_hash;
1092
1093 for (i = 0; i < hash->size; i++) {
1094 head = &hash->table[i];
1095 list_lock = &hash->list_locks[i];
1096
1097 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001098 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001099 head, hash_entry) {
1100 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001101 tt_global = container_of(tt_common_entry,
1102 struct batadv_tt_global_entry,
1103 common);
1104 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001105 }
1106 spin_unlock_bh(list_lock);
1107 }
1108
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001109 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001110
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001111 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001112}
1113
Sven Eckelmann56303d32012-06-05 22:31:31 +02001114static bool
1115_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1116 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001117{
1118 bool ret = false;
1119
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001120 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1121 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001122 ret = true;
1123
1124 return ret;
1125}
1126
Sven Eckelmann56303d32012-06-05 22:31:31 +02001127struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1128 const uint8_t *src,
1129 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001131 struct batadv_tt_local_entry *tt_local_entry = NULL;
1132 struct batadv_tt_global_entry *tt_global_entry = NULL;
1133 struct batadv_orig_node *orig_node = NULL;
1134 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001135 struct hlist_head *head;
1136 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001137 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001138 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001139
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001140 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001141 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001142 if (!tt_local_entry)
1143 goto out;
1144 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001145
Sven Eckelmanna5130882012-05-16 20:23:16 +02001146 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001147 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001148 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001150 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001151 * isolation
1152 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001153 if (tt_local_entry &&
1154 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001155 goto out;
1156
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001157 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001158
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001159 rcu_read_lock();
1160 head = &tt_global_entry->orig_list;
1161 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001162 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001163 if (!router)
1164 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001165
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001166 if (router->tq_avg > best_tq) {
1167 orig_node = orig_entry->orig_node;
1168 best_tq = router->tq_avg;
1169 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001170 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001171 }
1172 /* found anything? */
1173 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1174 orig_node = NULL;
1175 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001176out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001177 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001178 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001179 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001180 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001181
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001182 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001184
1185/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001186static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1187 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001188{
1189 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001190 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001191 struct batadv_tt_common_entry *tt_common;
1192 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001193 struct hlist_node *node;
1194 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001195 uint32_t i;
1196 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001197
1198 for (i = 0; i < hash->size; i++) {
1199 head = &hash->table[i];
1200
1201 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001202 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001203 tt_global = container_of(tt_common,
1204 struct batadv_tt_global_entry,
1205 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001206 /* Roaming clients are in the global table for
1207 * consistency only. They don't have to be
1208 * taken into account while computing the
1209 * global crc
1210 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001211 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001212 continue;
1213
1214 /* find out if this global entry is announced by this
1215 * originator
1216 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001217 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001218 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001219 continue;
1220
1221 total_one = 0;
1222 for (j = 0; j < ETH_ALEN; j++)
1223 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001224 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226 }
1227 rcu_read_unlock();
1228 }
1229
1230 return total;
1231}
1232
1233/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001234static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001235{
1236 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001237 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001238 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001239 struct hlist_node *node;
1240 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001241 uint32_t i;
1242 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001243
1244 for (i = 0; i < hash->size; i++) {
1245 head = &hash->table[i];
1246
1247 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001248 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001249 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001250 * account while computing the CRC
1251 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001252 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001253 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001254 total_one = 0;
1255 for (j = 0; j < ETH_ALEN; j++)
1256 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001257 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001258 total ^= total_one;
1259 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260 rcu_read_unlock();
1261 }
1262
1263 return total;
1264}
1265
Sven Eckelmann56303d32012-06-05 22:31:31 +02001266static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001267{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001268 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001269
1270 spin_lock_bh(&bat_priv->tt_req_list_lock);
1271
1272 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1273 list_del(&node->list);
1274 kfree(node);
1275 }
1276
1277 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1278}
1279
Sven Eckelmann56303d32012-06-05 22:31:31 +02001280static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1281 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001282 const unsigned char *tt_buff,
1283 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001284{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001285 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001286
1287 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001288 * last OGM (the OGM could carry no changes)
1289 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001290 spin_lock_bh(&orig_node->tt_buff_lock);
1291 if (tt_buff_len > 0) {
1292 kfree(orig_node->tt_buff);
1293 orig_node->tt_buff_len = 0;
1294 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1295 if (orig_node->tt_buff) {
1296 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1297 orig_node->tt_buff_len = tt_buff_len;
1298 }
1299 }
1300 spin_unlock_bh(&orig_node->tt_buff_lock);
1301}
1302
Sven Eckelmann56303d32012-06-05 22:31:31 +02001303static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001304{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001305 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001306
1307 spin_lock_bh(&bat_priv->tt_req_list_lock);
1308 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001309 if (batadv_has_timed_out(node->issued_at,
1310 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001311 list_del(&node->list);
1312 kfree(node);
1313 }
1314 }
1315 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1316}
1317
1318/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001319 * has already been issued for this orig_node, NULL otherwise
1320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321static struct batadv_tt_req_node *
1322batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1323 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001324{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001325 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001326
1327 spin_lock_bh(&bat_priv->tt_req_list_lock);
1328 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001329 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1330 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001331 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001332 goto unlock;
1333 }
1334
1335 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1336 if (!tt_req_node)
1337 goto unlock;
1338
1339 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1340 tt_req_node->issued_at = jiffies;
1341
1342 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1343unlock:
1344 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1345 return tt_req_node;
1346}
1347
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001348/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001349static int batadv_tt_local_valid_entry(const void *entry_ptr,
1350 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001351{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001352 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001353
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001354 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001355 return 0;
1356 return 1;
1357}
1358
Sven Eckelmanna5130882012-05-16 20:23:16 +02001359static int batadv_tt_global_valid(const void *entry_ptr,
1360 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001361{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001362 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1363 const struct batadv_tt_global_entry *tt_global_entry;
1364 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001365
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001366 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001367 return 0;
1368
Sven Eckelmann56303d32012-06-05 22:31:31 +02001369 tt_global_entry = container_of(tt_common_entry,
1370 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001371 common);
1372
Sven Eckelmanna5130882012-05-16 20:23:16 +02001373 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001374}
1375
Sven Eckelmanna5130882012-05-16 20:23:16 +02001376static struct sk_buff *
1377batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001378 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001380 int (*valid_cb)(const void *, const void *),
1381 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001382{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001383 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001384 struct batadv_tt_query_packet *tt_response;
1385 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001386 struct hlist_node *node;
1387 struct hlist_head *head;
1388 struct sk_buff *skb = NULL;
1389 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001390 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001391 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001392 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001393
1394 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1395 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001396 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001398 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399
Sven Eckelmann96412692012-06-05 22:31:30 +02001400 len = tt_query_size + tt_len;
1401 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001402 if (!skb)
1403 goto out;
1404
1405 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001406 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001407 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001408
Sven Eckelmann96412692012-06-05 22:31:30 +02001409 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001410 tt_count = 0;
1411
1412 rcu_read_lock();
1413 for (i = 0; i < hash->size; i++) {
1414 head = &hash->table[i];
1415
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001416 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001417 head, hash_entry) {
1418 if (tt_count == tt_tot)
1419 break;
1420
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001421 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001422 continue;
1423
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001424 memcpy(tt_change->addr, tt_common_entry->addr,
1425 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001426 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001427
1428 tt_count++;
1429 tt_change++;
1430 }
1431 }
1432 rcu_read_unlock();
1433
Antonio Quartulli9d852392011-10-17 14:25:13 +02001434 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001435 * copied
1436 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001437 tt_response->tt_data = htons(tt_count);
1438
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439out:
1440 return skb;
1441}
1442
Sven Eckelmann56303d32012-06-05 22:31:31 +02001443static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1444 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001445 uint8_t ttvn, uint16_t tt_crc,
1446 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001447{
1448 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001449 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001450 struct batadv_neigh_node *neigh_node = NULL;
1451 struct batadv_hard_iface *primary_if;
1452 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001453 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001454 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001456 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457 if (!primary_if)
1458 goto out;
1459
1460 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001461 * reply from the same orig_node yet
1462 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001463 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001464 if (!tt_req_node)
1465 goto out;
1466
Sven Eckelmann96412692012-06-05 22:31:30 +02001467 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468 if (!skb)
1469 goto out;
1470
1471 skb_reserve(skb, ETH_HLEN);
1472
Sven Eckelmann96412692012-06-05 22:31:30 +02001473 tt_req_len = sizeof(*tt_request);
1474 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001476 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001477 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1479 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001480 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001482 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001483 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484
1485 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001486 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001488 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489 if (!neigh_node)
1490 goto out;
1491
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001492 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001493 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1494 dst_orig_node->orig, neigh_node->addr,
1495 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001497 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001498
Sven Eckelmann9455e342012-05-12 02:09:37 +02001499 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500 ret = 0;
1501
1502out:
1503 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001504 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001505 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001506 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507 if (ret)
1508 kfree_skb(skb);
1509 if (ret && tt_req_node) {
1510 spin_lock_bh(&bat_priv->tt_req_list_lock);
1511 list_del(&tt_req_node->list);
1512 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1513 kfree(tt_req_node);
1514 }
1515 return ret;
1516}
1517
Sven Eckelmann96412692012-06-05 22:31:30 +02001518static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001519batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001520 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001522 struct batadv_orig_node *req_dst_orig_node = NULL;
1523 struct batadv_orig_node *res_dst_orig_node = NULL;
1524 struct batadv_neigh_node *neigh_node = NULL;
1525 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001526 uint8_t orig_ttvn, req_ttvn, ttvn;
1527 int ret = false;
1528 unsigned char *tt_buff;
1529 bool full_table;
1530 uint16_t tt_len, tt_tot;
1531 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001532 struct batadv_tt_query_packet *tt_response;
1533 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001534
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001535 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001536 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1537 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001538 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539
1540 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001541 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542 if (!req_dst_orig_node)
1543 goto out;
1544
Sven Eckelmannda641192012-05-12 13:48:56 +02001545 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001546 if (!res_dst_orig_node)
1547 goto out;
1548
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001549 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001550 if (!neigh_node)
1551 goto out;
1552
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001553 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001554 if (!primary_if)
1555 goto out;
1556
1557 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1558 req_ttvn = tt_request->ttvn;
1559
Antonio Quartulli015758d2011-07-09 17:52:13 +02001560 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001562 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001563 goto out;
1564
Antonio Quartulli015758d2011-07-09 17:52:13 +02001565 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001566 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001567 !req_dst_orig_node->tt_buff)
1568 full_table = true;
1569 else
1570 full_table = false;
1571
1572 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001573 * I'll send only one packet with as much TT entries as I can
1574 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575 if (!full_table) {
1576 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1577 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001578 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001579
Sven Eckelmann96412692012-06-05 22:31:30 +02001580 len = sizeof(*tt_response) + tt_len;
1581 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001582 if (!skb)
1583 goto unlock;
1584
1585 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001586 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1587 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588 tt_response->ttvn = req_ttvn;
1589 tt_response->tt_data = htons(tt_tot);
1590
Sven Eckelmann96412692012-06-05 22:31:30 +02001591 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001592 /* Copy the last orig_node's OGM buffer */
1593 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1594 req_dst_orig_node->tt_buff_len);
1595
1596 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1597 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001598 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1599 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001600 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1601
Sven Eckelmanna5130882012-05-16 20:23:16 +02001602 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1603 bat_priv->tt_global_hash,
1604 primary_if,
1605 batadv_tt_global_valid,
1606 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607 if (!skb)
1608 goto out;
1609
Sven Eckelmann96412692012-06-05 22:31:30 +02001610 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 }
1612
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001613 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001614 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001615 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1617 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001618 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619
1620 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001621 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001622
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001623 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001624 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1625 res_dst_orig_node->orig, neigh_node->addr,
1626 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001627
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001629
Sven Eckelmann9455e342012-05-12 02:09:37 +02001630 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631 ret = true;
1632 goto out;
1633
1634unlock:
1635 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1636
1637out:
1638 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001639 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001640 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001641 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001643 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001645 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001646 if (!ret)
1647 kfree_skb(skb);
1648 return ret;
1649
1650}
Sven Eckelmann96412692012-06-05 22:31:30 +02001651
1652static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001653batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001654 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001655{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001656 struct batadv_orig_node *orig_node = NULL;
1657 struct batadv_neigh_node *neigh_node = NULL;
1658 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659 uint8_t my_ttvn, req_ttvn, ttvn;
1660 int ret = false;
1661 unsigned char *tt_buff;
1662 bool full_table;
1663 uint16_t tt_len, tt_tot;
1664 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001665 struct batadv_tt_query_packet *tt_response;
1666 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001667
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001668 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001669 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1670 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001671 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001672
1673
1674 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1675 req_ttvn = tt_request->ttvn;
1676
Sven Eckelmannda641192012-05-12 13:48:56 +02001677 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001678 if (!orig_node)
1679 goto out;
1680
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001681 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682 if (!neigh_node)
1683 goto out;
1684
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001685 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 if (!primary_if)
1687 goto out;
1688
1689 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001690 * is too big send the whole local translation table
1691 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001692 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001693 !bat_priv->tt_buff)
1694 full_table = true;
1695 else
1696 full_table = false;
1697
1698 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001699 * I'll send only one packet with as much TT entries as I can
1700 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701 if (!full_table) {
1702 spin_lock_bh(&bat_priv->tt_buff_lock);
1703 tt_len = bat_priv->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001704 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705
Sven Eckelmann96412692012-06-05 22:31:30 +02001706 len = sizeof(*tt_response) + tt_len;
1707 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708 if (!skb)
1709 goto unlock;
1710
1711 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001712 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1713 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714 tt_response->ttvn = req_ttvn;
1715 tt_response->tt_data = htons(tt_tot);
1716
Sven Eckelmann96412692012-06-05 22:31:30 +02001717 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 memcpy(tt_buff, bat_priv->tt_buff,
1719 bat_priv->tt_buff_len);
1720 spin_unlock_bh(&bat_priv->tt_buff_lock);
1721 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001722 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt);
1723 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001724 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1725
Sven Eckelmanna5130882012-05-16 20:23:16 +02001726 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1727 bat_priv->tt_local_hash,
1728 primary_if,
1729 batadv_tt_local_valid_entry,
1730 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001731 if (!skb)
1732 goto out;
1733
Sven Eckelmann96412692012-06-05 22:31:30 +02001734 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001735 }
1736
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001737 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001738 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001739 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001740 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1741 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001742 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743
1744 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001745 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001747 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001748 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1749 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001750 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001752 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001753
Sven Eckelmann9455e342012-05-12 02:09:37 +02001754 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755 ret = true;
1756 goto out;
1757
1758unlock:
1759 spin_unlock_bh(&bat_priv->tt_buff_lock);
1760out:
1761 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001762 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001764 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001766 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001767 if (!ret)
1768 kfree_skb(skb);
1769 /* This packet was for me, so it doesn't need to be re-routed */
1770 return true;
1771}
1772
Sven Eckelmann56303d32012-06-05 22:31:31 +02001773bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001774 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001775{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001776 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001777 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001778 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001779 return true;
1780
Sven Eckelmanna5130882012-05-16 20:23:16 +02001781 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001782 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001783 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001784 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785}
1786
Sven Eckelmann56303d32012-06-05 22:31:31 +02001787static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1788 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001789 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001790 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791{
1792 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001793 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794
1795 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001796 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1797 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001798 batadv_tt_global_del(bat_priv, orig_node,
1799 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001800 "tt removed by changes",
1801 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001802 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001803 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001804 (tt_change + i)->addr,
1805 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001806 /* In case of problem while storing a
1807 * global_entry, we stop the updating
1808 * procedure without committing the
1809 * ttvn change. This will avoid to send
1810 * corrupted data on tt_request
1811 */
1812 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001813 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001815 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816}
1817
Sven Eckelmann56303d32012-06-05 22:31:31 +02001818static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001819 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001821 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001822
Sven Eckelmannda641192012-05-12 13:48:56 +02001823 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001824 if (!orig_node)
1825 goto out;
1826
1827 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001828 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829
Sven Eckelmanna5130882012-05-16 20:23:16 +02001830 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001831 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001832 ntohs(tt_response->tt_data),
1833 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001834
1835 spin_lock_bh(&orig_node->tt_buff_lock);
1836 kfree(orig_node->tt_buff);
1837 orig_node->tt_buff_len = 0;
1838 orig_node->tt_buff = NULL;
1839 spin_unlock_bh(&orig_node->tt_buff_lock);
1840
1841 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1842
1843out:
1844 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001845 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001846}
1847
Sven Eckelmann56303d32012-06-05 22:31:31 +02001848static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1849 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001850 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001851 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001853 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1854 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855
Sven Eckelmanna5130882012-05-16 20:23:16 +02001856 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1857 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001858 atomic_set(&orig_node->last_ttvn, ttvn);
1859}
1860
Sven Eckelmann56303d32012-06-05 22:31:31 +02001861bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001862{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001863 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001864 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865
Sven Eckelmanna5130882012-05-16 20:23:16 +02001866 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001867 if (!tt_local_entry)
1868 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001869 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001870 * consistency purpose)
1871 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001872 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001873 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001874 ret = true;
1875out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001877 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001878 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879}
1880
Sven Eckelmann56303d32012-06-05 22:31:31 +02001881void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001882 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001884 struct batadv_tt_req_node *node, *safe;
1885 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001886 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001887
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001888 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001889 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1890 tt_response->src, tt_response->ttvn,
1891 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001892 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001893
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001894 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001895 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001896 goto out;
1897
Sven Eckelmannda641192012-05-12 13:48:56 +02001898 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899 if (!orig_node)
1900 goto out;
1901
Sven Eckelmann96412692012-06-05 22:31:30 +02001902 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001903 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001904 } else {
1905 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001906 batadv_tt_update_changes(bat_priv, orig_node,
1907 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001908 tt_response->ttvn, tt_change);
1909 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001910
1911 /* Delete the tt_req_node from pending tt_requests list */
1912 spin_lock_bh(&bat_priv->tt_req_list_lock);
1913 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001914 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915 continue;
1916 list_del(&node->list);
1917 kfree(node);
1918 }
1919 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1920
1921 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001922 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001923 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001924 * unset the flag
1925 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001926 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927out:
1928 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001929 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001930}
1931
Sven Eckelmann56303d32012-06-05 22:31:31 +02001932int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001934 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001935
Sven Eckelmanna5130882012-05-16 20:23:16 +02001936 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001937 if (ret < 0)
1938 return ret;
1939
Sven Eckelmanna5130882012-05-16 20:23:16 +02001940 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001941 if (ret < 0)
1942 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001943
Sven Eckelmanna5130882012-05-16 20:23:16 +02001944 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001945
1946 return 1;
1947}
1948
Sven Eckelmann56303d32012-06-05 22:31:31 +02001949static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001950{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001951 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952
Antonio Quartullicc47f662011-04-27 14:27:57 +02001953 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001954
Antonio Quartullicc47f662011-04-27 14:27:57 +02001955 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1956 list_del(&node->list);
1957 kfree(node);
1958 }
1959
1960 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1961}
1962
Sven Eckelmann56303d32012-06-05 22:31:31 +02001963static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001964{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001965 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001966
1967 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1968 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001969 if (!batadv_has_timed_out(node->first_time,
1970 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001971 continue;
1972
1973 list_del(&node->list);
1974 kfree(node);
1975 }
1976 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1977}
1978
1979/* This function checks whether the client already reached the
1980 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1981 * will not be sent.
1982 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001983 * returns true if the ROAMING_ADV can be sent, false otherwise
1984 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001985static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001986 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001987{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001988 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001989 bool ret = false;
1990
1991 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1992 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001993 * reply from the same orig_node yet
1994 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001995 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001996 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001997 continue;
1998
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001999 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002000 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002001 continue;
2002
Sven Eckelmann3e348192012-05-16 20:23:22 +02002003 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002004 /* Sorry, you roamed too many times! */
2005 goto unlock;
2006 ret = true;
2007 break;
2008 }
2009
2010 if (!ret) {
2011 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2012 if (!tt_roam_node)
2013 goto unlock;
2014
2015 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002016 atomic_set(&tt_roam_node->counter,
2017 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002018 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2019
2020 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
2021 ret = true;
2022 }
2023
2024unlock:
2025 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
2026 return ret;
2027}
2028
Sven Eckelmann56303d32012-06-05 22:31:31 +02002029static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2030 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002031{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002032 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002033 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002034 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002035 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002036 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002037 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038
2039 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002040 * already roamed to us too many times
2041 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002042 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002043 goto out;
2044
Sven Eckelmann96412692012-06-05 22:31:30 +02002045 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002046 if (!skb)
2047 goto out;
2048
2049 skb_reserve(skb, ETH_HLEN);
2050
Sven Eckelmann96412692012-06-05 22:31:30 +02002051 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002052
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002053 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002054 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002055 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002056 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002057 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002058 if (!primary_if)
2059 goto out;
2060 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002061 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002062 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2063 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2064
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002065 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002066 if (!neigh_node)
2067 goto out;
2068
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002069 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002070 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2071 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002072
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002073 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002074
Sven Eckelmann9455e342012-05-12 02:09:37 +02002075 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002076 ret = 0;
2077
2078out:
2079 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002080 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002081 if (ret)
2082 kfree_skb(skb);
2083 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002084}
2085
Sven Eckelmanna5130882012-05-16 20:23:16 +02002086static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002088 struct delayed_work *delayed_work;
2089 struct batadv_priv *bat_priv;
2090
2091 delayed_work = container_of(work, struct delayed_work, work);
2092 bat_priv = container_of(delayed_work, struct batadv_priv, tt_work);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002093
Sven Eckelmanna5130882012-05-16 20:23:16 +02002094 batadv_tt_local_purge(bat_priv);
2095 batadv_tt_global_roam_purge(bat_priv);
2096 batadv_tt_req_purge(bat_priv);
2097 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002098
Sven Eckelmanna5130882012-05-16 20:23:16 +02002099 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002100}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002101
Sven Eckelmann56303d32012-06-05 22:31:31 +02002102void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002103{
2104 cancel_delayed_work_sync(&bat_priv->tt_work);
2105
Sven Eckelmanna5130882012-05-16 20:23:16 +02002106 batadv_tt_local_table_free(bat_priv);
2107 batadv_tt_global_table_free(bat_priv);
2108 batadv_tt_req_list_free(bat_priv);
2109 batadv_tt_changes_list_free(bat_priv);
2110 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002111
2112 kfree(bat_priv->tt_buff);
2113}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002114
Antonio Quartulli697f2532011-11-07 16:47:01 +01002115/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002116 * in the given hash table and returns the number of modified entries
2117 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002118static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2119 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002120{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002121 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002122 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002123 struct hlist_head *head;
2124 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002125 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002126
2127 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002128 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002129
2130 for (i = 0; i < hash->size; i++) {
2131 head = &hash->table[i];
2132
2133 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002134 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002135 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002136 if (enable) {
2137 if ((tt_common_entry->flags & flags) == flags)
2138 continue;
2139 tt_common_entry->flags |= flags;
2140 } else {
2141 if (!(tt_common_entry->flags & flags))
2142 continue;
2143 tt_common_entry->flags &= ~flags;
2144 }
2145 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002146 }
2147 rcu_read_unlock();
2148 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002149out:
2150 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002151}
2152
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002153/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002155{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002156 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002157 struct batadv_tt_common_entry *tt_common;
2158 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002159 struct hlist_node *node, *node_tmp;
2160 struct hlist_head *head;
2161 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002162 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002163
2164 if (!hash)
2165 return;
2166
2167 for (i = 0; i < hash->size; i++) {
2168 head = &hash->table[i];
2169 list_lock = &hash->list_locks[i];
2170
2171 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002172 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2173 hash_entry) {
2174 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002175 continue;
2176
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002177 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002178 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002179 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002180
2181 atomic_dec(&bat_priv->num_local_tt);
2182 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002183 tt_local = container_of(tt_common,
2184 struct batadv_tt_local_entry,
2185 common);
2186 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002187 }
2188 spin_unlock_bh(list_lock);
2189 }
2190
2191}
2192
Sven Eckelmann56303d32012-06-05 22:31:31 +02002193static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002194 unsigned char **packet_buff,
2195 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002196{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002197 uint16_t changed_num = 0;
2198
2199 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2200 return -ENOENT;
2201
Sven Eckelmanna5130882012-05-16 20:23:16 +02002202 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002203 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002204
2205 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002206 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002207 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002208 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002209
2210 /* Increment the TTVN only once per OGM interval */
2211 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002212 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002213 "Local changes committed, updating to ttvn %u\n",
2214 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002215 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002216
2217 /* reset the sending counter */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002218 atomic_set(&bat_priv->tt_ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002219
Sven Eckelmanna5130882012-05-16 20:23:16 +02002220 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2221 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002222}
2223
2224/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002225int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002226 unsigned char **packet_buff, int *packet_buff_len,
2227 int packet_min_len)
2228{
2229 int tt_num_changes;
2230
2231 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002232 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2233 packet_buff_len,
2234 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002235
2236 /* if the changes have been sent often enough */
2237 if ((tt_num_changes < 0) &&
Sven Eckelmann3e348192012-05-16 20:23:22 +02002238 (!batadv_atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002239 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2240 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002241 tt_num_changes = 0;
2242 }
2243
2244 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002245}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002246
Sven Eckelmann56303d32012-06-05 22:31:31 +02002247bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002248 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002249{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002250 struct batadv_tt_local_entry *tt_local_entry = NULL;
2251 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002252 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002253
2254 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002255 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002256
Sven Eckelmanna5130882012-05-16 20:23:16 +02002257 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002258 if (!tt_local_entry)
2259 goto out;
2260
Sven Eckelmanna5130882012-05-16 20:23:16 +02002261 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002262 if (!tt_global_entry)
2263 goto out;
2264
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002265 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002266 goto out;
2267
Marek Lindner5870adc2012-06-20 17:16:05 +02002268 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002269
2270out:
2271 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002272 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002273 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002274 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002275 return ret;
2276}
Marek Lindnera943cac2011-07-30 13:10:18 +02002277
Sven Eckelmann56303d32012-06-05 22:31:31 +02002278void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2279 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002280 const unsigned char *tt_buff, uint8_t tt_num_changes,
2281 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002282{
2283 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2284 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002285 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002286
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002287 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002288 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002289 return;
2290
Antonio Quartulli17071572011-11-07 16:36:40 +01002291 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002292 * increased by one -> we can apply the attached changes
2293 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002294 if ((!orig_node->tt_initialised && ttvn == 1) ||
2295 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002296 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002297 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2298 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002299 * In this case send a tt request
2300 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002301 if (!tt_num_changes) {
2302 full_table = false;
2303 goto request_table;
2304 }
2305
Sven Eckelmann96412692012-06-05 22:31:30 +02002306 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002307 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002308 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002309
2310 /* Even if we received the precomputed crc with the OGM, we
2311 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002312 * in the global table
2313 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002314 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002315
2316 /* The ttvn alone is not enough to guarantee consistency
2317 * because a single value could represent different states
2318 * (due to the wrap around). Thus a node has to check whether
2319 * the resulting table (after applying the changes) is still
2320 * consistent or not. E.g. a node could disconnect while its
2321 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2322 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002323 * inconsistency
2324 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002325 if (orig_node->tt_crc != tt_crc)
2326 goto request_table;
2327
2328 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002329 * unset the flag
2330 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002331 orig_node->tt_poss_change = false;
2332 } else {
2333 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002334 * in sync anymore -> request fresh tt data
2335 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002336 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2337 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002338request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002339 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002340 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2341 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2342 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002343 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2344 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002345 return;
2346 }
2347 }
2348}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002349
2350/* returns true whether we know that the client has moved from its old
2351 * originator to another one. This entry is kept is still kept for consistency
2352 * purposes
2353 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002354bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002355 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002356{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002357 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002358 bool ret = false;
2359
Sven Eckelmanna5130882012-05-16 20:23:16 +02002360 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002361 if (!tt_global_entry)
2362 goto out;
2363
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002364 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002365 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002366out:
2367 return ret;
2368}