blob: a438f4b582fc8adccba3a67dfb33326b1ecf4606 [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);
200 event_removed = true;
201 goto unlock;
202 }
203
Antonio Quartullia73105b2011-04-27 14:27:44 +0200204 /* track the change in the OGMinterval list */
205 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200206
207unlock:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200208 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
209
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200210 if (event_removed)
211 atomic_dec(&bat_priv->tt_local_changes);
212 else
213 atomic_inc(&bat_priv->tt_local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200214}
215
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200216int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200217{
Sven Eckelmann96412692012-06-05 22:31:30 +0200218 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200219}
220
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200223 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200224 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200226 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200228 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200229 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232}
233
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200234void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
235 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200237 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
238 struct batadv_tt_local_entry *tt_local_entry = NULL;
239 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200240 struct hlist_head *head;
241 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200242 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100243 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Sven Eckelmanna5130882012-05-16 20:23:16 +0200245 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200247 if (tt_local_entry) {
248 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200249 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
250 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200251 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252 }
253
Sven Eckelmann704509b2011-05-14 23:14:54 +0200254 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200255 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200256 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200257
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200258 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200259 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
260 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100262 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200263 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200264 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200265 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100266 atomic_set(&tt_local_entry->common.refcount, 2);
267 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
269 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200270 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200271 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100273 /* The local entry has to be marked as NEW to avoid to send it in
274 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200275 * (consistency check)
276 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200277 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100278
Sven Eckelmanna5130882012-05-16 20:23:16 +0200279 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200280 batadv_choose_orig,
281 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200282 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100283
284 if (unlikely(hash_added != 0)) {
285 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200286 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100287 goto out;
288 }
289
Sven Eckelmanna5130882012-05-16 20:23:16 +0200290 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200291
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200293 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294
Antonio Quartullicc47f662011-04-27 14:27:57 +0200295 /* Check whether it is a roaming! */
296 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200297 /* These node are probably going to update their tt table */
298 head = &tt_global_entry->orig_list;
299 rcu_read_lock();
300 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
301 orig_entry->orig_node->tt_poss_change = true;
302
Sven Eckelmanna5130882012-05-16 20:23:16 +0200303 batadv_send_roam_adv(bat_priv,
304 tt_global_entry->common.addr,
305 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200306 }
307 rcu_read_unlock();
308 /* The global entry has to be marked as ROAMING and
309 * has to be kept for consistency purpose
310 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200311 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100312 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200313 }
314out:
315 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200316 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200317 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200318 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319}
320
Sven Eckelmanna5130882012-05-16 20:23:16 +0200321static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
322 int *packet_buff_len,
323 int min_packet_len,
324 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800326 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800328 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
329
330 /* keep old buffer if kmalloc should fail */
331 if (new_buff) {
332 memcpy(new_buff, *packet_buff, min_packet_len);
333 kfree(*packet_buff);
334 *packet_buff = new_buff;
335 *packet_buff_len = new_packet_len;
336 }
337}
338
Sven Eckelmann56303d32012-06-05 22:31:31 +0200339static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200340 unsigned char **packet_buff,
341 int *packet_buff_len,
342 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800343{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800345 int req_len;
346
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200347 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800348
349 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200350 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800351
352 /* if we have too many changes for one packet don't send any
353 * and wait for the tt table request which will be fragmented
354 */
355 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
356 req_len = min_packet_len;
357
Sven Eckelmanna5130882012-05-16 20:23:16 +0200358 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
359 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800360
361 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200362 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800363}
364
Sven Eckelmann56303d32012-06-05 22:31:31 +0200365static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200366 unsigned char **packet_buff,
367 int *packet_buff_len,
368 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800369{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200370 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800371 int count = 0, tot_changes = 0, new_len;
372 unsigned char *tt_buff;
373
Sven Eckelmanna5130882012-05-16 20:23:16 +0200374 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
375 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800376
377 new_len = *packet_buff_len - min_packet_len;
378 tt_buff = *packet_buff + min_packet_len;
379
380 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200381 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Antonio Quartullia73105b2011-04-27 14:27:44 +0200383 spin_lock_bh(&bat_priv->tt_changes_list_lock);
384 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Antonio Quartullia73105b2011-04-27 14:27:44 +0200386 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100387 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200388 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200389 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200390 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 count++;
392 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200393 list_del(&entry->list);
394 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398 /* Keep the buffer for possible tt_request */
399 spin_lock_bh(&bat_priv->tt_buff_lock);
400 kfree(bat_priv->tt_buff);
401 bat_priv->tt_buff_len = 0;
402 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800403 /* check whether this new OGM has no changes due to size problems */
404 if (new_len > 0) {
405 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200406 * instead of providing the diff
407 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800408 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200409 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800410 memcpy(bat_priv->tt_buff, tt_buff, new_len);
411 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200412 }
413 }
414 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindner08ad76e2012-04-23 16:32:55 +0800416 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417}
418
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200419int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420{
421 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200422 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200423 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200424 struct batadv_tt_common_entry *tt_common_entry;
425 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000426 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200428 uint32_t i;
429 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200431 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200432 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100433 ret = seq_printf(seq,
434 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200435 net_dev->name);
436 goto out;
437 }
438
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200439 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100440 ret = seq_printf(seq,
441 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200442 net_dev->name);
443 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 }
445
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100446 seq_printf(seq,
447 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200448 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450 for (i = 0; i < hash->size; i++) {
451 head = &hash->table[i];
452
Marek Lindner7aadf882011-02-18 12:28:09 +0000453 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100454 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000455 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200456 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100457 tt_common_entry->addr,
458 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200459 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100460 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200461 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100462 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200463 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100464 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200465 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100466 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200467 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000469 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200471out:
472 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200473 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200474 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475}
476
Sven Eckelmann56303d32012-06-05 22:31:31 +0200477static void
478batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
479 struct batadv_tt_local_entry *tt_local_entry,
480 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200482 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
483 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Antonio Quartulli015758d2011-07-09 17:52:13 +0200485 /* The local client has to be marked as "pending to be removed" but has
486 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200487 * response issued before the net ttvn increment (consistency check)
488 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200489 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100490
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200492 "Local tt entry (%pM) pending to be removed: %s\n",
493 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494}
495
Sven Eckelmann56303d32012-06-05 22:31:31 +0200496void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200497 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499 struct batadv_tt_local_entry *tt_local_entry = NULL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200500 uint16_t flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Sven Eckelmanna5130882012-05-16 20:23:16 +0200502 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200503 if (!tt_local_entry)
504 goto out;
505
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200506 flags = BATADV_TT_CLIENT_DEL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200507 if (roaming)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200508 flags |= BATADV_TT_CLIENT_ROAM;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200509
510 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200511out:
512 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200513 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514}
515
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200517 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200519 struct batadv_tt_local_entry *tt_local_entry;
520 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000521 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200522
523 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
524 hash_entry) {
525 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526 struct batadv_tt_local_entry,
527 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200528 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
529 continue;
530
531 /* entry already marked for deletion */
532 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
533 continue;
534
535 if (!batadv_has_timed_out(tt_local_entry->last_seen,
536 BATADV_TT_LOCAL_TIMEOUT))
537 continue;
538
539 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
540 BATADV_TT_CLIENT_DEL, "timed out");
541 }
542}
543
Sven Eckelmann56303d32012-06-05 22:31:31 +0200544static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200545{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200546 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200548 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200549 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551 for (i = 0; i < hash->size; i++) {
552 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200553 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200555 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200556 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200557 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 }
559
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560}
561
Sven Eckelmann56303d32012-06-05 22:31:31 +0200562static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200564 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200565 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200566 struct batadv_tt_common_entry *tt_common_entry;
567 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200568 struct hlist_node *node, *node_tmp;
569 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200570 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200571
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200572 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573 return;
574
Antonio Quartullia73105b2011-04-27 14:27:44 +0200575 hash = bat_priv->tt_local_hash;
576
577 for (i = 0; i < hash->size; i++) {
578 head = &hash->table[i];
579 list_lock = &hash->list_locks[i];
580
581 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100582 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200583 head, hash_entry) {
584 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200585 tt_local = container_of(tt_common_entry,
586 struct batadv_tt_local_entry,
587 common);
588 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200589 }
590 spin_unlock_bh(list_lock);
591 }
592
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200593 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200594
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200595 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596}
597
Sven Eckelmann56303d32012-06-05 22:31:31 +0200598static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200600 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200601 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200603 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200605 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200606 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607
Sven Eckelmann5346c352012-05-05 13:27:28 +0200608 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609}
610
Sven Eckelmann56303d32012-06-05 22:31:31 +0200611static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200612{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200613 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200614
615 spin_lock_bh(&bat_priv->tt_changes_list_lock);
616
617 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
618 list) {
619 list_del(&entry->list);
620 kfree(entry);
621 }
622
623 atomic_set(&bat_priv->tt_local_changes, 0);
624 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
625}
626
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200627/* find out if an orig_node is already in the list of a tt_global_entry.
628 * returns 1 if found, 0 otherwise
629 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200630static bool
631batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
632 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200633{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200634 struct batadv_tt_orig_list_entry *tmp_orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200635 const struct hlist_head *head;
636 struct hlist_node *node;
637 bool found = false;
638
639 rcu_read_lock();
640 head = &entry->orig_list;
641 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
642 if (tmp_orig_entry->orig_node == orig_node) {
643 found = true;
644 break;
645 }
646 }
647 rcu_read_unlock();
648 return found;
649}
650
Sven Eckelmanna5130882012-05-16 20:23:16 +0200651static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652batadv_tt_global_add_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
653 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200654{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200655 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200656
657 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
658 if (!orig_entry)
659 return;
660
661 INIT_HLIST_NODE(&orig_entry->list);
662 atomic_inc(&orig_node->refcount);
663 atomic_inc(&orig_node->tt_size);
664 orig_entry->orig_node = orig_node;
665 orig_entry->ttvn = ttvn;
666
667 spin_lock_bh(&tt_global_entry->list_lock);
668 hlist_add_head_rcu(&orig_entry->list,
669 &tt_global_entry->orig_list);
670 spin_unlock_bh(&tt_global_entry->list_lock);
671}
672
Antonio Quartullia73105b2011-04-27 14:27:44 +0200673/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200674int batadv_tt_global_add(struct batadv_priv *bat_priv,
675 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200676 const unsigned char *tt_addr, uint8_t flags,
677 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200679 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200680 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100681 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682 struct batadv_tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
Sven Eckelmanna5130882012-05-16 20:23:16 +0200684 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Antonio Quartullia73105b2011-04-27 14:27:44 +0200686 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200687 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200688 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200689 goto out;
690
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200691 common = &tt_global_entry->common;
692 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200693
Antonio Quartullid4f44692012-05-25 00:00:54 +0200694 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200695 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200696 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200697
698 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
699 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200700
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200701 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200702 batadv_compare_tt,
703 batadv_choose_orig, common,
704 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100705
706 if (unlikely(hash_added != 0)) {
707 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200708 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100709 goto out_remove;
710 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200711
Sven Eckelmanna5130882012-05-16 20:23:16 +0200712 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
713 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200714 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200715 /* there is already a global entry, use this one. */
716
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200717 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
718 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200719 * delete + roaming change for this originator.
720 *
721 * We should first delete the old originator before adding the
722 * new one.
723 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200724 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200725 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200726 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200727 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000728 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200729
Sven Eckelmanna5130882012-05-16 20:23:16 +0200730 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
731 orig_node))
732 batadv_tt_global_add_orig_entry(tt_global_entry,
733 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200735
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200736 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200737 "Creating new global tt entry: %pM (via %pM)\n",
738 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200739
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100740out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200741 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200742 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200743 "global tt received",
744 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200745 ret = 1;
746out:
747 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200748 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200749 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750}
751
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200752/* print all orig nodes who announce the address for this global entry.
753 * it is assumed that the caller holds rcu_read_lock();
754 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200755static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200756batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200757 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200758{
759 struct hlist_head *head;
760 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200761 struct batadv_tt_orig_list_entry *orig_entry;
762 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200763 uint16_t flags;
764 uint8_t last_ttvn;
765
766 tt_common_entry = &tt_global_entry->common;
767
768 head = &tt_global_entry->orig_list;
769
770 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
771 flags = tt_common_entry->flags;
772 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
773 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
774 tt_global_entry->common.addr, orig_entry->ttvn,
775 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200776 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
777 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200778 }
779}
780
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200781int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782{
783 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200784 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200785 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200786 struct batadv_tt_common_entry *tt_common_entry;
787 struct batadv_tt_global_entry *tt_global;
788 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000789 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200791 uint32_t i;
792 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200794 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200795 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100796 ret = seq_printf(seq,
797 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200798 net_dev->name);
799 goto out;
800 }
801
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200802 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100803 ret = seq_printf(seq,
804 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200805 net_dev->name);
806 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807 }
808
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200809 seq_printf(seq,
810 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200812 seq_printf(seq, " %-13s %s %-15s %s %s\n",
813 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815 for (i = 0; i < hash->size; i++) {
816 head = &hash->table[i];
817
Marek Lindner7aadf882011-02-18 12:28:09 +0000818 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100819 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000820 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200821 tt_global = container_of(tt_common_entry,
822 struct batadv_tt_global_entry,
823 common);
824 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000826 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200828out:
829 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200830 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200831 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832}
833
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200834/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200835static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200836batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200838 struct hlist_head *head;
839 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200840 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200841
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200842 spin_lock_bh(&tt_global_entry->list_lock);
843 head = &tt_global_entry->orig_list;
844 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
845 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200846 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200847 }
848 spin_unlock_bh(&tt_global_entry->list_lock);
849
850}
851
Sven Eckelmanna5130882012-05-16 20:23:16 +0200852static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200853batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
854 struct batadv_tt_global_entry *tt_global_entry,
855 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200856 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200857{
858 struct hlist_head *head;
859 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200860 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200861
862 spin_lock_bh(&tt_global_entry->list_lock);
863 head = &tt_global_entry->orig_list;
864 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
865 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200866 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200867 "Deleting %pM from global tt entry %pM: %s\n",
868 orig_node->orig,
869 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200870 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200871 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200872 }
873 }
874 spin_unlock_bh(&tt_global_entry->list_lock);
875}
876
Sven Eckelmann56303d32012-06-05 22:31:31 +0200877static void
878batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
879 struct batadv_tt_global_entry *tt_global_entry,
880 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200881{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200882 batadv_dbg(BATADV_DBG_TT, bat_priv,
883 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200884 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200885
Sven Eckelmanna5130882012-05-16 20:23:16 +0200886 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200887 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200888 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200889
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890}
891
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200892/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200893 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
894 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200895 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200896static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200897batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
898 struct batadv_tt_global_entry *tt_global_entry,
899 struct batadv_orig_node *orig_node,
900 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200901{
902 bool last_entry = true;
903 struct hlist_head *head;
904 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200905 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200906
907 /* no local entry exists, case 1:
908 * Check if this is the last one or if other entries exist.
909 */
910
911 rcu_read_lock();
912 head = &tt_global_entry->orig_list;
913 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
914 if (orig_entry->orig_node != orig_node) {
915 last_entry = false;
916 break;
917 }
918 }
919 rcu_read_unlock();
920
921 if (last_entry) {
922 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200923 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200924 tt_global_entry->roam_at = jiffies;
925 } else
926 /* there is another entry, we can simply delete this
927 * one and can still use the other one.
928 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200929 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
930 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200931}
932
933
934
Sven Eckelmann56303d32012-06-05 22:31:31 +0200935static void batadv_tt_global_del(struct batadv_priv *bat_priv,
936 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200937 const unsigned char *addr,
938 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200940 struct batadv_tt_global_entry *tt_global_entry = NULL;
941 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942
Sven Eckelmanna5130882012-05-16 20:23:16 +0200943 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200944 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200945 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200946
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200947 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200948 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
949 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800950
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200951 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200952 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
953 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200954
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800955 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200956 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800957
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200958 /* if we are deleting a global entry due to a roam
959 * event, there are two possibilities:
960 * 1) the client roamed from node A to node B => if there
961 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200962 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200963 * wait for node B to claim it. In case of timeout
964 * the entry is purged.
965 *
966 * If there are other originators left, we directly delete
967 * the originator.
968 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200969 * the global entry, since it is useless now.
970 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200971 local_entry = batadv_tt_local_hash_find(bat_priv,
972 tt_global_entry->common.addr);
973 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200974 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200975 batadv_tt_global_del_orig_list(tt_global_entry);
976 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200977 } else
978 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200979 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
980 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200981
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800982
Antonio Quartullicc47f662011-04-27 14:27:57 +0200983out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200984 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200985 batadv_tt_global_entry_free_ref(tt_global_entry);
986 if (local_entry)
987 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988}
989
Sven Eckelmann56303d32012-06-05 22:31:31 +0200990void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
991 struct batadv_orig_node *orig_node,
992 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200993{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200994 struct batadv_tt_global_entry *tt_global;
995 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200996 uint32_t i;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200997 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200998 struct hlist_node *node, *safe;
999 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001000 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001001
Simon Wunderlich6e801492011-10-19 10:28:26 +02001002 if (!hash)
1003 return;
1004
Antonio Quartullia73105b2011-04-27 14:27:44 +02001005 for (i = 0; i < hash->size; i++) {
1006 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001007 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001008
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001009 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001010 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001011 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001012 tt_global = container_of(tt_common_entry,
1013 struct batadv_tt_global_entry,
1014 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001015
Sven Eckelmann56303d32012-06-05 22:31:31 +02001016 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001017 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001018
Sven Eckelmann56303d32012-06-05 22:31:31 +02001019 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001020 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001021 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001022 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001023 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001025 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001026 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001027 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001029 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030}
1031
Sven Eckelmann56303d32012-06-05 22:31:31 +02001032static void batadv_tt_global_roam_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001033 struct hlist_head *head)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001034{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001035 struct batadv_tt_common_entry *tt_common_entry;
1036 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001037 struct hlist_node *node, *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001038
1039 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
1040 hash_entry) {
1041 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042 struct batadv_tt_global_entry,
1043 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001044 if (!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001045 continue;
1046 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1047 BATADV_TT_CLIENT_ROAM_TIMEOUT))
1048 continue;
1049
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001050 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001051 "Deleting global tt entry (%pM): Roaming timeout\n",
1052 tt_global_entry->common.addr);
1053
1054 hlist_del_rcu(node);
1055 batadv_tt_global_entry_free_ref(tt_global_entry);
1056 }
1057}
1058
Sven Eckelmann56303d32012-06-05 22:31:31 +02001059static void batadv_tt_global_roam_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001060{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001061 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001062 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001063 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001064 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001065
Antonio Quartullicc47f662011-04-27 14:27:57 +02001066 for (i = 0; i < hash->size; i++) {
1067 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001068 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001069
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001070 spin_lock_bh(list_lock);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001071 batadv_tt_global_roam_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001072 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001073 }
1074
Antonio Quartullicc47f662011-04-27 14:27:57 +02001075}
1076
Sven Eckelmann56303d32012-06-05 22:31:31 +02001077static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001078{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001079 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001080 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001081 struct batadv_tt_common_entry *tt_common_entry;
1082 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001083 struct hlist_node *node, *node_tmp;
1084 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001085 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001086
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001087 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001088 return;
1089
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001090 hash = bat_priv->tt_global_hash;
1091
1092 for (i = 0; i < hash->size; i++) {
1093 head = &hash->table[i];
1094 list_lock = &hash->list_locks[i];
1095
1096 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001097 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001098 head, hash_entry) {
1099 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001100 tt_global = container_of(tt_common_entry,
1101 struct batadv_tt_global_entry,
1102 common);
1103 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001104 }
1105 spin_unlock_bh(list_lock);
1106 }
1107
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001108 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001109
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001110 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001111}
1112
Sven Eckelmann56303d32012-06-05 22:31:31 +02001113static bool
1114_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1115 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001116{
1117 bool ret = false;
1118
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001119 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1120 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001121 ret = true;
1122
1123 return ret;
1124}
1125
Sven Eckelmann56303d32012-06-05 22:31:31 +02001126struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1127 const uint8_t *src,
1128 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001130 struct batadv_tt_local_entry *tt_local_entry = NULL;
1131 struct batadv_tt_global_entry *tt_global_entry = NULL;
1132 struct batadv_orig_node *orig_node = NULL;
1133 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001134 struct hlist_head *head;
1135 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001136 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001137 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001138
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001139 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001140 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001141 if (!tt_local_entry)
1142 goto out;
1143 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001144
Sven Eckelmanna5130882012-05-16 20:23:16 +02001145 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001146 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001147 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001149 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001150 * isolation
1151 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001152 if (tt_local_entry &&
1153 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001154 goto out;
1155
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001156 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001157
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001158 rcu_read_lock();
1159 head = &tt_global_entry->orig_list;
1160 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001161 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001162 if (!router)
1163 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001164
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001165 if (router->tq_avg > best_tq) {
1166 orig_node = orig_entry->orig_node;
1167 best_tq = router->tq_avg;
1168 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001169 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 }
1171 /* found anything? */
1172 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1173 orig_node = NULL;
1174 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001175out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001176 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001177 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001178 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001179 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001180
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001181 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001182}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001183
1184/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001185static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1186 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001187{
1188 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001189 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001190 struct batadv_tt_common_entry *tt_common;
1191 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001192 struct hlist_node *node;
1193 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001194 uint32_t i;
1195 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001196
1197 for (i = 0; i < hash->size; i++) {
1198 head = &hash->table[i];
1199
1200 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001201 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001202 tt_global = container_of(tt_common,
1203 struct batadv_tt_global_entry,
1204 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001205 /* Roaming clients are in the global table for
1206 * consistency only. They don't have to be
1207 * taken into account while computing the
1208 * global crc
1209 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001210 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001211 continue;
1212
1213 /* find out if this global entry is announced by this
1214 * originator
1215 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001216 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001217 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001218 continue;
1219
1220 total_one = 0;
1221 for (j = 0; j < ETH_ALEN; j++)
1222 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001223 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001224 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001225 }
1226 rcu_read_unlock();
1227 }
1228
1229 return total;
1230}
1231
1232/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001233static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001234{
1235 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001236 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001237 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001238 struct hlist_node *node;
1239 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001240 uint32_t i;
1241 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001242
1243 for (i = 0; i < hash->size; i++) {
1244 head = &hash->table[i];
1245
1246 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001247 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001248 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001249 * account while computing the CRC
1250 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001251 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001252 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001253 total_one = 0;
1254 for (j = 0; j < ETH_ALEN; j++)
1255 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001256 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001257 total ^= total_one;
1258 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259 rcu_read_unlock();
1260 }
1261
1262 return total;
1263}
1264
Sven Eckelmann56303d32012-06-05 22:31:31 +02001265static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001266{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001267 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001268
1269 spin_lock_bh(&bat_priv->tt_req_list_lock);
1270
1271 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1272 list_del(&node->list);
1273 kfree(node);
1274 }
1275
1276 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1277}
1278
Sven Eckelmann56303d32012-06-05 22:31:31 +02001279static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1280 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001281 const unsigned char *tt_buff,
1282 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001283{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001284 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001285
1286 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001287 * last OGM (the OGM could carry no changes)
1288 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001289 spin_lock_bh(&orig_node->tt_buff_lock);
1290 if (tt_buff_len > 0) {
1291 kfree(orig_node->tt_buff);
1292 orig_node->tt_buff_len = 0;
1293 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1294 if (orig_node->tt_buff) {
1295 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1296 orig_node->tt_buff_len = tt_buff_len;
1297 }
1298 }
1299 spin_unlock_bh(&orig_node->tt_buff_lock);
1300}
1301
Sven Eckelmann56303d32012-06-05 22:31:31 +02001302static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001303{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001304 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001305
1306 spin_lock_bh(&bat_priv->tt_req_list_lock);
1307 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001308 if (batadv_has_timed_out(node->issued_at,
1309 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001310 list_del(&node->list);
1311 kfree(node);
1312 }
1313 }
1314 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1315}
1316
1317/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001318 * has already been issued for this orig_node, NULL otherwise
1319 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001320static struct batadv_tt_req_node *
1321batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1322 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001323{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001324 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001325
1326 spin_lock_bh(&bat_priv->tt_req_list_lock);
1327 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001328 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1329 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001330 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331 goto unlock;
1332 }
1333
1334 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1335 if (!tt_req_node)
1336 goto unlock;
1337
1338 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1339 tt_req_node->issued_at = jiffies;
1340
1341 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1342unlock:
1343 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1344 return tt_req_node;
1345}
1346
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001347/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001348static int batadv_tt_local_valid_entry(const void *entry_ptr,
1349 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001350{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001351 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001352
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001353 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001354 return 0;
1355 return 1;
1356}
1357
Sven Eckelmanna5130882012-05-16 20:23:16 +02001358static int batadv_tt_global_valid(const void *entry_ptr,
1359 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001360{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001361 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1362 const struct batadv_tt_global_entry *tt_global_entry;
1363 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001364
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001365 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001366 return 0;
1367
Sven Eckelmann56303d32012-06-05 22:31:31 +02001368 tt_global_entry = container_of(tt_common_entry,
1369 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001370 common);
1371
Sven Eckelmanna5130882012-05-16 20:23:16 +02001372 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001373}
1374
Sven Eckelmanna5130882012-05-16 20:23:16 +02001375static struct sk_buff *
1376batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001377 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001378 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001379 int (*valid_cb)(const void *, const void *),
1380 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001381{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001382 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001383 struct batadv_tt_query_packet *tt_response;
1384 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385 struct hlist_node *node;
1386 struct hlist_head *head;
1387 struct sk_buff *skb = NULL;
1388 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001389 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001390 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001391 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001392
1393 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1394 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001395 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001397 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398
Sven Eckelmann96412692012-06-05 22:31:30 +02001399 len = tt_query_size + tt_len;
1400 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401 if (!skb)
1402 goto out;
1403
1404 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001405 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001406 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001407
Sven Eckelmann96412692012-06-05 22:31:30 +02001408 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001409 tt_count = 0;
1410
1411 rcu_read_lock();
1412 for (i = 0; i < hash->size; i++) {
1413 head = &hash->table[i];
1414
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001415 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416 head, hash_entry) {
1417 if (tt_count == tt_tot)
1418 break;
1419
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001420 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001421 continue;
1422
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001423 memcpy(tt_change->addr, tt_common_entry->addr,
1424 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001425 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001426
1427 tt_count++;
1428 tt_change++;
1429 }
1430 }
1431 rcu_read_unlock();
1432
Antonio Quartulli9d852392011-10-17 14:25:13 +02001433 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001434 * copied
1435 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001436 tt_response->tt_data = htons(tt_count);
1437
Antonio Quartullia73105b2011-04-27 14:27:44 +02001438out:
1439 return skb;
1440}
1441
Sven Eckelmann56303d32012-06-05 22:31:31 +02001442static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1443 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001444 uint8_t ttvn, uint16_t tt_crc,
1445 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446{
1447 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001448 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001449 struct batadv_neigh_node *neigh_node = NULL;
1450 struct batadv_hard_iface *primary_if;
1451 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001453 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001455 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 if (!primary_if)
1457 goto out;
1458
1459 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001460 * reply from the same orig_node yet
1461 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001462 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463 if (!tt_req_node)
1464 goto out;
1465
Sven Eckelmann96412692012-06-05 22:31:30 +02001466 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 if (!skb)
1468 goto out;
1469
1470 skb_reserve(skb, ETH_HLEN);
1471
Sven Eckelmann96412692012-06-05 22:31:30 +02001472 tt_req_len = sizeof(*tt_request);
1473 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001475 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001476 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1478 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001479 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001481 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001482 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483
1484 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001485 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001487 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001488 if (!neigh_node)
1489 goto out;
1490
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001492 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1493 dst_orig_node->orig, neigh_node->addr,
1494 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001496 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001497
Sven Eckelmann9455e342012-05-12 02:09:37 +02001498 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499 ret = 0;
1500
1501out:
1502 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001503 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001505 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506 if (ret)
1507 kfree_skb(skb);
1508 if (ret && tt_req_node) {
1509 spin_lock_bh(&bat_priv->tt_req_list_lock);
1510 list_del(&tt_req_node->list);
1511 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1512 kfree(tt_req_node);
1513 }
1514 return ret;
1515}
1516
Sven Eckelmann96412692012-06-05 22:31:30 +02001517static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001519 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001521 struct batadv_orig_node *req_dst_orig_node = NULL;
1522 struct batadv_orig_node *res_dst_orig_node = NULL;
1523 struct batadv_neigh_node *neigh_node = NULL;
1524 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001525 uint8_t orig_ttvn, req_ttvn, ttvn;
1526 int ret = false;
1527 unsigned char *tt_buff;
1528 bool full_table;
1529 uint16_t tt_len, tt_tot;
1530 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001531 struct batadv_tt_query_packet *tt_response;
1532 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001533
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001534 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001535 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1536 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001537 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001538
1539 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001540 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001541 if (!req_dst_orig_node)
1542 goto out;
1543
Sven Eckelmannda641192012-05-12 13:48:56 +02001544 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001545 if (!res_dst_orig_node)
1546 goto out;
1547
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001548 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001549 if (!neigh_node)
1550 goto out;
1551
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001552 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553 if (!primary_if)
1554 goto out;
1555
1556 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1557 req_ttvn = tt_request->ttvn;
1558
Antonio Quartulli015758d2011-07-09 17:52:13 +02001559 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001560 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001561 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562 goto out;
1563
Antonio Quartulli015758d2011-07-09 17:52:13 +02001564 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001565 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001566 !req_dst_orig_node->tt_buff)
1567 full_table = true;
1568 else
1569 full_table = false;
1570
1571 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001572 * I'll send only one packet with as much TT entries as I can
1573 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574 if (!full_table) {
1575 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1576 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001577 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578
Sven Eckelmann96412692012-06-05 22:31:30 +02001579 len = sizeof(*tt_response) + tt_len;
1580 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581 if (!skb)
1582 goto unlock;
1583
1584 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001585 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1586 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001587 tt_response->ttvn = req_ttvn;
1588 tt_response->tt_data = htons(tt_tot);
1589
Sven Eckelmann96412692012-06-05 22:31:30 +02001590 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001591 /* Copy the last orig_node's OGM buffer */
1592 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1593 req_dst_orig_node->tt_buff_len);
1594
1595 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1596 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001597 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1598 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1600
Sven Eckelmanna5130882012-05-16 20:23:16 +02001601 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1602 bat_priv->tt_global_hash,
1603 primary_if,
1604 batadv_tt_global_valid,
1605 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 if (!skb)
1607 goto out;
1608
Sven Eckelmann96412692012-06-05 22:31:30 +02001609 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 }
1611
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001612 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001613 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001614 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1616 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001617 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001618
1619 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001620 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001622 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001623 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1624 res_dst_orig_node->orig, neigh_node->addr,
1625 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001626
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001627 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001628
Sven Eckelmann9455e342012-05-12 02:09:37 +02001629 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630 ret = true;
1631 goto out;
1632
1633unlock:
1634 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1635
1636out:
1637 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001638 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001640 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001642 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001644 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001645 if (!ret)
1646 kfree_skb(skb);
1647 return ret;
1648
1649}
Sven Eckelmann96412692012-06-05 22:31:30 +02001650
1651static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001652batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001653 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001654{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001655 struct batadv_orig_node *orig_node = NULL;
1656 struct batadv_neigh_node *neigh_node = NULL;
1657 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001658 uint8_t my_ttvn, req_ttvn, ttvn;
1659 int ret = false;
1660 unsigned char *tt_buff;
1661 bool full_table;
1662 uint16_t tt_len, tt_tot;
1663 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001664 struct batadv_tt_query_packet *tt_response;
1665 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001667 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001668 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1669 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001670 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671
1672
1673 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1674 req_ttvn = tt_request->ttvn;
1675
Sven Eckelmannda641192012-05-12 13:48:56 +02001676 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677 if (!orig_node)
1678 goto out;
1679
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001680 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001681 if (!neigh_node)
1682 goto out;
1683
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001684 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685 if (!primary_if)
1686 goto out;
1687
1688 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001689 * is too big send the whole local translation table
1690 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001691 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692 !bat_priv->tt_buff)
1693 full_table = true;
1694 else
1695 full_table = false;
1696
1697 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001698 * I'll send only one packet with as much TT entries as I can
1699 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700 if (!full_table) {
1701 spin_lock_bh(&bat_priv->tt_buff_lock);
1702 tt_len = bat_priv->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001703 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704
Sven Eckelmann96412692012-06-05 22:31:30 +02001705 len = sizeof(*tt_response) + tt_len;
1706 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 if (!skb)
1708 goto unlock;
1709
1710 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001711 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1712 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001713 tt_response->ttvn = req_ttvn;
1714 tt_response->tt_data = htons(tt_tot);
1715
Sven Eckelmann96412692012-06-05 22:31:30 +02001716 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 memcpy(tt_buff, bat_priv->tt_buff,
1718 bat_priv->tt_buff_len);
1719 spin_unlock_bh(&bat_priv->tt_buff_lock);
1720 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001721 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt);
1722 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001723 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1724
Sven Eckelmanna5130882012-05-16 20:23:16 +02001725 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1726 bat_priv->tt_local_hash,
1727 primary_if,
1728 batadv_tt_local_valid_entry,
1729 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730 if (!skb)
1731 goto out;
1732
Sven Eckelmann96412692012-06-05 22:31:30 +02001733 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001734 }
1735
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001736 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001737 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001738 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1740 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001741 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742
1743 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001744 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001745
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001746 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001747 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1748 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001749 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001751 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001752
Sven Eckelmann9455e342012-05-12 02:09:37 +02001753 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754 ret = true;
1755 goto out;
1756
1757unlock:
1758 spin_unlock_bh(&bat_priv->tt_buff_lock);
1759out:
1760 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001761 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001763 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001764 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001765 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001766 if (!ret)
1767 kfree_skb(skb);
1768 /* This packet was for me, so it doesn't need to be re-routed */
1769 return true;
1770}
1771
Sven Eckelmann56303d32012-06-05 22:31:31 +02001772bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001773 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001775 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001776 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001777 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001778 return true;
1779
Sven Eckelmanna5130882012-05-16 20:23:16 +02001780 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001781 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001782 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001783 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784}
1785
Sven Eckelmann56303d32012-06-05 22:31:31 +02001786static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1787 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001788 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001789 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001790{
1791 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001792 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793
1794 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001795 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1796 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001797 batadv_tt_global_del(bat_priv, orig_node,
1798 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001799 "tt removed by changes",
1800 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001801 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001802 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001803 (tt_change + i)->addr,
1804 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805 /* In case of problem while storing a
1806 * global_entry, we stop the updating
1807 * procedure without committing the
1808 * ttvn change. This will avoid to send
1809 * corrupted data on tt_request
1810 */
1811 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001812 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001814 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815}
1816
Sven Eckelmann56303d32012-06-05 22:31:31 +02001817static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001818 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001819{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001820 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001821
Sven Eckelmannda641192012-05-12 13:48:56 +02001822 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823 if (!orig_node)
1824 goto out;
1825
1826 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001827 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828
Sven Eckelmanna5130882012-05-16 20:23:16 +02001829 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001830 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001831 ntohs(tt_response->tt_data),
1832 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001833
1834 spin_lock_bh(&orig_node->tt_buff_lock);
1835 kfree(orig_node->tt_buff);
1836 orig_node->tt_buff_len = 0;
1837 orig_node->tt_buff = NULL;
1838 spin_unlock_bh(&orig_node->tt_buff_lock);
1839
1840 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1841
1842out:
1843 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001844 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845}
1846
Sven Eckelmann56303d32012-06-05 22:31:31 +02001847static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1848 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001849 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001850 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001852 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1853 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001854
Sven Eckelmanna5130882012-05-16 20:23:16 +02001855 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1856 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001857 atomic_set(&orig_node->last_ttvn, ttvn);
1858}
1859
Sven Eckelmann56303d32012-06-05 22:31:31 +02001860bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001862 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001863 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864
Sven Eckelmanna5130882012-05-16 20:23:16 +02001865 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001866 if (!tt_local_entry)
1867 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001868 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001869 * consistency purpose)
1870 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001871 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001872 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001873 ret = true;
1874out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001876 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001877 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001878}
1879
Sven Eckelmann56303d32012-06-05 22:31:31 +02001880void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001881 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001882{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001883 struct batadv_tt_req_node *node, *safe;
1884 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001885 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001886
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001887 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001888 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1889 tt_response->src, tt_response->ttvn,
1890 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001891 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001893 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001894 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001895 goto out;
1896
Sven Eckelmannda641192012-05-12 13:48:56 +02001897 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898 if (!orig_node)
1899 goto out;
1900
Sven Eckelmann96412692012-06-05 22:31:30 +02001901 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001902 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001903 } else {
1904 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001905 batadv_tt_update_changes(bat_priv, orig_node,
1906 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001907 tt_response->ttvn, tt_change);
1908 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909
1910 /* Delete the tt_req_node from pending tt_requests list */
1911 spin_lock_bh(&bat_priv->tt_req_list_lock);
1912 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001913 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001914 continue;
1915 list_del(&node->list);
1916 kfree(node);
1917 }
1918 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1919
1920 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001921 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001922 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001923 * unset the flag
1924 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001925 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001926out:
1927 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001928 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001929}
1930
Sven Eckelmann56303d32012-06-05 22:31:31 +02001931int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001933 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934
Sven Eckelmanna5130882012-05-16 20:23:16 +02001935 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001936 if (ret < 0)
1937 return ret;
1938
Sven Eckelmanna5130882012-05-16 20:23:16 +02001939 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001940 if (ret < 0)
1941 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942
Sven Eckelmanna5130882012-05-16 20:23:16 +02001943 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001944
1945 return 1;
1946}
1947
Sven Eckelmann56303d32012-06-05 22:31:31 +02001948static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001949{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001950 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001951
Antonio Quartullicc47f662011-04-27 14:27:57 +02001952 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953
Antonio Quartullicc47f662011-04-27 14:27:57 +02001954 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1955 list_del(&node->list);
1956 kfree(node);
1957 }
1958
1959 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1960}
1961
Sven Eckelmann56303d32012-06-05 22:31:31 +02001962static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001963{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001964 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001965
1966 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1967 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001968 if (!batadv_has_timed_out(node->first_time,
1969 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001970 continue;
1971
1972 list_del(&node->list);
1973 kfree(node);
1974 }
1975 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1976}
1977
1978/* This function checks whether the client already reached the
1979 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1980 * will not be sent.
1981 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001982 * returns true if the ROAMING_ADV can be sent, false otherwise
1983 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001984static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001985 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001986{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001987 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001988 bool ret = false;
1989
1990 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1991 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001992 * reply from the same orig_node yet
1993 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001994 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001995 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001996 continue;
1997
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001998 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001999 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002000 continue;
2001
Sven Eckelmann3e348192012-05-16 20:23:22 +02002002 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002003 /* Sorry, you roamed too many times! */
2004 goto unlock;
2005 ret = true;
2006 break;
2007 }
2008
2009 if (!ret) {
2010 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2011 if (!tt_roam_node)
2012 goto unlock;
2013
2014 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002015 atomic_set(&tt_roam_node->counter,
2016 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002017 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2018
2019 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
2020 ret = true;
2021 }
2022
2023unlock:
2024 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
2025 return ret;
2026}
2027
Sven Eckelmann56303d32012-06-05 22:31:31 +02002028static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2029 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002030{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002031 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002032 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002033 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002034 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002035 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002036 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002037
2038 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002039 * already roamed to us too many times
2040 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002041 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002042 goto out;
2043
Sven Eckelmann96412692012-06-05 22:31:30 +02002044 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002045 if (!skb)
2046 goto out;
2047
2048 skb_reserve(skb, ETH_HLEN);
2049
Sven Eckelmann96412692012-06-05 22:31:30 +02002050 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002051
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002052 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002053 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002054 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002055 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002056 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002057 if (!primary_if)
2058 goto out;
2059 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002060 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002061 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2062 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2063
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002064 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002065 if (!neigh_node)
2066 goto out;
2067
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002068 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002069 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2070 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002071
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002072 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002073
Sven Eckelmann9455e342012-05-12 02:09:37 +02002074 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002075 ret = 0;
2076
2077out:
2078 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002079 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002080 if (ret)
2081 kfree_skb(skb);
2082 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083}
2084
Sven Eckelmanna5130882012-05-16 20:23:16 +02002085static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002086{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002087 struct delayed_work *delayed_work;
2088 struct batadv_priv *bat_priv;
2089
2090 delayed_work = container_of(work, struct delayed_work, work);
2091 bat_priv = container_of(delayed_work, struct batadv_priv, tt_work);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002092
Sven Eckelmanna5130882012-05-16 20:23:16 +02002093 batadv_tt_local_purge(bat_priv);
2094 batadv_tt_global_roam_purge(bat_priv);
2095 batadv_tt_req_purge(bat_priv);
2096 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097
Sven Eckelmanna5130882012-05-16 20:23:16 +02002098 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002099}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002100
Sven Eckelmann56303d32012-06-05 22:31:31 +02002101void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002102{
2103 cancel_delayed_work_sync(&bat_priv->tt_work);
2104
Sven Eckelmanna5130882012-05-16 20:23:16 +02002105 batadv_tt_local_table_free(bat_priv);
2106 batadv_tt_global_table_free(bat_priv);
2107 batadv_tt_req_list_free(bat_priv);
2108 batadv_tt_changes_list_free(bat_priv);
2109 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002110
2111 kfree(bat_priv->tt_buff);
2112}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002113
Antonio Quartulli697f2532011-11-07 16:47:01 +01002114/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002115 * in the given hash table and returns the number of modified entries
2116 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002117static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2118 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002119{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002120 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002121 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002122 struct hlist_head *head;
2123 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002124 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002125
2126 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002127 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002128
2129 for (i = 0; i < hash->size; i++) {
2130 head = &hash->table[i];
2131
2132 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002133 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002134 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002135 if (enable) {
2136 if ((tt_common_entry->flags & flags) == flags)
2137 continue;
2138 tt_common_entry->flags |= flags;
2139 } else {
2140 if (!(tt_common_entry->flags & flags))
2141 continue;
2142 tt_common_entry->flags &= ~flags;
2143 }
2144 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002145 }
2146 rcu_read_unlock();
2147 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002148out:
2149 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002150}
2151
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002152/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002153static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002154{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002155 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002156 struct batadv_tt_common_entry *tt_common;
2157 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002158 struct hlist_node *node, *node_tmp;
2159 struct hlist_head *head;
2160 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002161 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002162
2163 if (!hash)
2164 return;
2165
2166 for (i = 0; i < hash->size; i++) {
2167 head = &hash->table[i];
2168 list_lock = &hash->list_locks[i];
2169
2170 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002171 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2172 hash_entry) {
2173 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002174 continue;
2175
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002176 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002177 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002178 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002179
2180 atomic_dec(&bat_priv->num_local_tt);
2181 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002182 tt_local = container_of(tt_common,
2183 struct batadv_tt_local_entry,
2184 common);
2185 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002186 }
2187 spin_unlock_bh(list_lock);
2188 }
2189
2190}
2191
Sven Eckelmann56303d32012-06-05 22:31:31 +02002192static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002193 unsigned char **packet_buff,
2194 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002195{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002196 uint16_t changed_num = 0;
2197
2198 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2199 return -ENOENT;
2200
Sven Eckelmanna5130882012-05-16 20:23:16 +02002201 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002202 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002203
2204 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002205 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002206 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002207 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002208
2209 /* Increment the TTVN only once per OGM interval */
2210 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002211 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002212 "Local changes committed, updating to ttvn %u\n",
2213 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002214 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002215
2216 /* reset the sending counter */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002217 atomic_set(&bat_priv->tt_ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002218
Sven Eckelmanna5130882012-05-16 20:23:16 +02002219 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2220 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002221}
2222
2223/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002224int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002225 unsigned char **packet_buff, int *packet_buff_len,
2226 int packet_min_len)
2227{
2228 int tt_num_changes;
2229
2230 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002231 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2232 packet_buff_len,
2233 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002234
2235 /* if the changes have been sent often enough */
2236 if ((tt_num_changes < 0) &&
Sven Eckelmann3e348192012-05-16 20:23:22 +02002237 (!batadv_atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002238 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2239 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002240 tt_num_changes = 0;
2241 }
2242
2243 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002244}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002245
Sven Eckelmann56303d32012-06-05 22:31:31 +02002246bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002247 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002248{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002249 struct batadv_tt_local_entry *tt_local_entry = NULL;
2250 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002251 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002252
2253 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002254 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002255
Sven Eckelmanna5130882012-05-16 20:23:16 +02002256 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002257 if (!tt_local_entry)
2258 goto out;
2259
Sven Eckelmanna5130882012-05-16 20:23:16 +02002260 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002261 if (!tt_global_entry)
2262 goto out;
2263
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002264 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002265 goto out;
2266
Marek Lindner5870adc2012-06-20 17:16:05 +02002267 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002268
2269out:
2270 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002271 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002272 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002273 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002274 return ret;
2275}
Marek Lindnera943cac2011-07-30 13:10:18 +02002276
Sven Eckelmann56303d32012-06-05 22:31:31 +02002277void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2278 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002279 const unsigned char *tt_buff, uint8_t tt_num_changes,
2280 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002281{
2282 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2283 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002284 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002285
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002286 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002287 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002288 return;
2289
Antonio Quartulli17071572011-11-07 16:36:40 +01002290 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002291 * increased by one -> we can apply the attached changes
2292 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002293 if ((!orig_node->tt_initialised && ttvn == 1) ||
2294 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002295 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002296 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2297 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002298 * In this case send a tt request
2299 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002300 if (!tt_num_changes) {
2301 full_table = false;
2302 goto request_table;
2303 }
2304
Sven Eckelmann96412692012-06-05 22:31:30 +02002305 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002306 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002307 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002308
2309 /* Even if we received the precomputed crc with the OGM, we
2310 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002311 * in the global table
2312 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002313 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002314
2315 /* The ttvn alone is not enough to guarantee consistency
2316 * because a single value could represent different states
2317 * (due to the wrap around). Thus a node has to check whether
2318 * the resulting table (after applying the changes) is still
2319 * consistent or not. E.g. a node could disconnect while its
2320 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2321 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002322 * inconsistency
2323 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002324 if (orig_node->tt_crc != tt_crc)
2325 goto request_table;
2326
2327 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002328 * unset the flag
2329 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002330 orig_node->tt_poss_change = false;
2331 } else {
2332 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002333 * in sync anymore -> request fresh tt data
2334 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002335 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2336 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002337request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002338 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002339 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2340 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2341 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002342 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2343 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002344 return;
2345 }
2346 }
2347}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002348
2349/* returns true whether we know that the client has moved from its old
2350 * originator to another one. This entry is kept is still kept for consistency
2351 * purposes
2352 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002353bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002354 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002355{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002356 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002357 bool ret = false;
2358
Sven Eckelmanna5130882012-05-16 20:23:16 +02002359 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002360 if (!tt_global_entry)
2361 goto out;
2362
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002363 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002364 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002365out:
2366 return ret;
2367}