blob: cf7988342f2751e77473208812ab4424c65abcac [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 Eckelmanna5130882012-05-16 20:23:16 +020032static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
33 struct orig_node *orig_node);
34static void batadv_tt_purge(struct work_struct *work);
35static void
36batadv_tt_global_del_orig_list(struct 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{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010041 const void *data1 = container_of(node, struct 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 Eckelmanna5130882012-05-16 20:23:16 +020047static void batadv_tt_start_timer(struct bat_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 Eckelmanna5130882012-05-16 20:23:16 +020054static struct tt_common_entry *batadv_tt_hash_find(struct hashtable_t *hash,
55 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;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010059 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020060 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000061
62 if (!hash)
63 return NULL;
64
Sven Eckelmannda641192012-05-12 13:48:56 +020065 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000066 head = &hash->table[index];
67
68 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010069 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020070 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000071 continue;
72
Antonio Quartulli48100ba2011-10-30 12:17:33 +010073 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020074 continue;
75
Antonio Quartulli48100ba2011-10-30 12:17:33 +010076 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000077 break;
78 }
79 rcu_read_unlock();
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 return tt_common_entry_tmp;
82}
83
Sven Eckelmanna5130882012-05-16 20:23:16 +020084static struct tt_local_entry *
85batadv_tt_local_hash_find(struct bat_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010086{
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
89
Sven Eckelmanna5130882012-05-16 20:23:16 +020090 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091 if (tt_common_entry)
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000095}
96
Sven Eckelmanna5130882012-05-16 20:23:16 +020097static struct tt_global_entry *
98batadv_tt_global_hash_find(struct bat_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000099{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102
Sven Eckelmanna5130882012-05-16 20:23:16 +0200103 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100104 if (tt_common_entry)
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Marek Lindner7aadf882011-02-18 12:28:09 +0000109}
110
Sven Eckelmanna5130882012-05-16 20:23:16 +0200111static void
112batadv_tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200113{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
115 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200116}
117
Sven Eckelmanna5130882012-05-16 20:23:16 +0200118static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200119{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100120 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200121 struct tt_global_entry *tt_global_entry;
122
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100123 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
124 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
125 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126
Simon Wunderlich531027f2011-10-19 11:02:25 +0200127 kfree(tt_global_entry);
128}
129
Sven Eckelmanna5130882012-05-16 20:23:16 +0200130static void
131batadv_tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200132{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200133 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200134 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100135 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200136 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200137 }
138}
139
Sven Eckelmanna5130882012-05-16 20:23:16 +0200140static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200141{
142 struct tt_orig_list_entry *orig_entry;
143
144 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200145 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200146 kfree(orig_entry);
147}
148
Sven Eckelmanna5130882012-05-16 20:23:16 +0200149static void
150batadv_tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200151{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200152 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200153}
154
Sven Eckelmanna5130882012-05-16 20:23:16 +0200155static void batadv_tt_local_event(struct bat_priv *bat_priv,
156 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200157{
158 struct tt_change_node *tt_change_node;
159
160 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
161
162 if (!tt_change_node)
163 return;
164
Antonio Quartulliff66c972011-06-30 01:14:00 +0200165 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200166 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
167
168 spin_lock_bh(&bat_priv->tt_changes_list_lock);
169 /* track the change in the OGMinterval list */
170 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
171 atomic_inc(&bat_priv->tt_local_changes);
172 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
173
174 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
175}
176
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200177int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200178{
179 return changes_num * sizeof(struct tt_change);
180}
181
Sven Eckelmanna5130882012-05-16 20:23:16 +0200182static int batadv_tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200184 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200185 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200187 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200189 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200190 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Sven Eckelmann5346c352012-05-05 13:27:28 +0200192 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193}
194
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200195void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
196 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197{
198 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199 struct tt_local_entry *tt_local_entry = NULL;
200 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200201 struct hlist_head *head;
202 struct hlist_node *node;
203 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100204 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Sven Eckelmanna5130882012-05-16 20:23:16 +0200206 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200208 if (tt_local_entry) {
209 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100210 /* possibly unset the TT_CLIENT_PENDING flag */
211 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200212 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 }
214
Sven Eckelmann704509b2011-05-14 23:14:54 +0200215 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200216 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200217 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200218
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200219 batadv_dbg(DBG_TT, bat_priv,
220 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
221 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100223 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
224 tt_local_entry->common.flags = NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200225 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100226 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
227 atomic_set(&tt_local_entry->common.refcount, 2);
228 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
230 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200231 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100232 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100234 /* The local entry has to be marked as NEW to avoid to send it in
235 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200236 * (consistency check)
237 */
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100238 tt_local_entry->common.flags |= TT_CLIENT_NEW;
239
Sven Eckelmanna5130882012-05-16 20:23:16 +0200240 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200241 batadv_choose_orig,
242 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200243 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100244
245 if (unlikely(hash_added != 0)) {
246 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200247 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100248 goto out;
249 }
250
Sven Eckelmanna5130882012-05-16 20:23:16 +0200251 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200252
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200254 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255
Antonio Quartullicc47f662011-04-27 14:27:57 +0200256 /* Check whether it is a roaming! */
257 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200258 /* These node are probably going to update their tt table */
259 head = &tt_global_entry->orig_list;
260 rcu_read_lock();
261 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
262 orig_entry->orig_node->tt_poss_change = true;
263
Sven Eckelmanna5130882012-05-16 20:23:16 +0200264 batadv_send_roam_adv(bat_priv,
265 tt_global_entry->common.addr,
266 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200267 }
268 rcu_read_unlock();
269 /* The global entry has to be marked as ROAMING and
270 * has to be kept for consistency purpose
271 */
David S. Miller220b07e2011-12-16 15:07:28 -0500272 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100273 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200274 }
275out:
276 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200277 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200278 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200279 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280}
281
Sven Eckelmanna5130882012-05-16 20:23:16 +0200282static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
283 int *packet_buff_len,
284 int min_packet_len,
285 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800287 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800289 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
290
291 /* keep old buffer if kmalloc should fail */
292 if (new_buff) {
293 memcpy(new_buff, *packet_buff, min_packet_len);
294 kfree(*packet_buff);
295 *packet_buff = new_buff;
296 *packet_buff_len = new_packet_len;
297 }
298}
299
Sven Eckelmanna5130882012-05-16 20:23:16 +0200300static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
301 unsigned char **packet_buff,
302 int *packet_buff_len,
303 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800304{
305 struct hard_iface *primary_if;
306 int req_len;
307
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200308 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800309
310 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200311 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800312
313 /* if we have too many changes for one packet don't send any
314 * and wait for the tt table request which will be fragmented
315 */
316 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
317 req_len = min_packet_len;
318
Sven Eckelmanna5130882012-05-16 20:23:16 +0200319 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
320 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800321
322 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200323 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800324}
325
Sven Eckelmanna5130882012-05-16 20:23:16 +0200326static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
327 unsigned char **packet_buff,
328 int *packet_buff_len,
329 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800330{
331 struct tt_change_node *entry, *safe;
332 int count = 0, tot_changes = 0, new_len;
333 unsigned char *tt_buff;
334
Sven Eckelmanna5130882012-05-16 20:23:16 +0200335 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
336 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800337
338 new_len = *packet_buff_len - min_packet_len;
339 tt_buff = *packet_buff + min_packet_len;
340
341 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200342 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
Antonio Quartullia73105b2011-04-27 14:27:44 +0200344 spin_lock_bh(&bat_priv->tt_changes_list_lock);
345 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
Antonio Quartullia73105b2011-04-27 14:27:44 +0200347 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100348 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200349 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200350 memcpy(tt_buff + batadv_tt_len(count),
Antonio Quartullia73105b2011-04-27 14:27:44 +0200351 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352 count++;
353 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200354 list_del(&entry->list);
355 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200357 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358
Antonio Quartullia73105b2011-04-27 14:27:44 +0200359 /* Keep the buffer for possible tt_request */
360 spin_lock_bh(&bat_priv->tt_buff_lock);
361 kfree(bat_priv->tt_buff);
362 bat_priv->tt_buff_len = 0;
363 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800364 /* check whether this new OGM has no changes due to size problems */
365 if (new_len > 0) {
366 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200367 * instead of providing the diff
368 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800369 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200370 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800371 memcpy(bat_priv->tt_buff, tt_buff, new_len);
372 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200373 }
374 }
375 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
Marek Lindner08ad76e2012-04-23 16:32:55 +0800377 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378}
379
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200380int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
382 struct net_device *net_dev = (struct net_device *)seq->private;
383 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200384 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100385 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200386 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000387 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200389 uint32_t i;
390 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200392 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200393 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100394 ret = seq_printf(seq,
395 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200396 net_dev->name);
397 goto out;
398 }
399
400 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100401 ret = seq_printf(seq,
402 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200403 net_dev->name);
404 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 }
406
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100407 seq_printf(seq,
408 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200409 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411 for (i = 0; i < hash->size; i++) {
412 head = &hash->table[i];
413
Marek Lindner7aadf882011-02-18 12:28:09 +0000414 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100415 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000416 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200417 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100418 tt_common_entry->addr,
419 (tt_common_entry->flags &
420 TT_CLIENT_ROAM ? 'R' : '.'),
421 (tt_common_entry->flags &
422 TT_CLIENT_NOPURGE ? 'P' : '.'),
423 (tt_common_entry->flags &
424 TT_CLIENT_NEW ? 'N' : '.'),
425 (tt_common_entry->flags &
426 TT_CLIENT_PENDING ? 'X' : '.'),
427 (tt_common_entry->flags &
428 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000430 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200432out:
433 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200434 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200435 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436}
437
Sven Eckelmanna5130882012-05-16 20:23:16 +0200438static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
439 struct tt_local_entry *tt_local_entry,
440 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200442 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
443 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Antonio Quartulli015758d2011-07-09 17:52:13 +0200445 /* The local client has to be marked as "pending to be removed" but has
446 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200447 * response issued before the net ttvn increment (consistency check)
448 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100449 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100450
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200451 batadv_dbg(DBG_TT, bat_priv,
452 "Local tt entry (%pM) pending to be removed: %s\n",
453 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454}
455
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200456void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
457 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200459 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460
Sven Eckelmanna5130882012-05-16 20:23:16 +0200461 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200462 if (!tt_local_entry)
463 goto out;
464
Sven Eckelmanna5130882012-05-16 20:23:16 +0200465 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
466 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
467 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200468out:
469 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200470 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471}
472
Sven Eckelmanna5130882012-05-16 20:23:16 +0200473static void batadv_tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200475 struct hashtable_t *hash = bat_priv->tt_local_hash;
476 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100477 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000478 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200480 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200481 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 for (i = 0; i < hash->size; i++) {
484 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200485 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200487 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100488 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000489 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100490 tt_local_entry = container_of(tt_common_entry,
491 struct tt_local_entry,
492 common);
493 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000494 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200496 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100497 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200498 continue;
499
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200500 if (!batadv_has_timed_out(tt_local_entry->last_seen,
501 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000502 continue;
503
Sven Eckelmanna5130882012-05-16 20:23:16 +0200504 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
505 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200507 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
509
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510}
511
Sven Eckelmanna5130882012-05-16 20:23:16 +0200512static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200514 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100516 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200518 struct hlist_node *node, *node_tmp;
519 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200520 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200521
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200522 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523 return;
524
Antonio Quartullia73105b2011-04-27 14:27:44 +0200525 hash = bat_priv->tt_local_hash;
526
527 for (i = 0; i < hash->size; i++) {
528 head = &hash->table[i];
529 list_lock = &hash->list_locks[i];
530
531 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100532 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200533 head, hash_entry) {
534 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100535 tt_local_entry = container_of(tt_common_entry,
536 struct tt_local_entry,
537 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200538 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539 }
540 spin_unlock_bh(list_lock);
541 }
542
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200543 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200544
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200545 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546}
547
Sven Eckelmanna5130882012-05-16 20:23:16 +0200548static int batadv_tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200550 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200551 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200553 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200555 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200556 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557
Sven Eckelmann5346c352012-05-05 13:27:28 +0200558 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559}
560
Sven Eckelmanna5130882012-05-16 20:23:16 +0200561static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200562{
563 struct tt_change_node *entry, *safe;
564
565 spin_lock_bh(&bat_priv->tt_changes_list_lock);
566
567 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
568 list) {
569 list_del(&entry->list);
570 kfree(entry);
571 }
572
573 atomic_set(&bat_priv->tt_local_changes, 0);
574 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
575}
576
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200577/* find out if an orig_node is already in the list of a tt_global_entry.
578 * returns 1 if found, 0 otherwise
579 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200580static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
581 const struct orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200582{
583 struct tt_orig_list_entry *tmp_orig_entry;
584 const struct hlist_head *head;
585 struct hlist_node *node;
586 bool found = false;
587
588 rcu_read_lock();
589 head = &entry->orig_list;
590 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
591 if (tmp_orig_entry->orig_node == orig_node) {
592 found = true;
593 break;
594 }
595 }
596 rcu_read_unlock();
597 return found;
598}
599
Sven Eckelmanna5130882012-05-16 20:23:16 +0200600static void
601batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
602 struct orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200603{
604 struct tt_orig_list_entry *orig_entry;
605
606 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
607 if (!orig_entry)
608 return;
609
610 INIT_HLIST_NODE(&orig_entry->list);
611 atomic_inc(&orig_node->refcount);
612 atomic_inc(&orig_node->tt_size);
613 orig_entry->orig_node = orig_node;
614 orig_entry->ttvn = ttvn;
615
616 spin_lock_bh(&tt_global_entry->list_lock);
617 hlist_add_head_rcu(&orig_entry->list,
618 &tt_global_entry->orig_list);
619 spin_unlock_bh(&tt_global_entry->list_lock);
620}
621
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622/* caller must hold orig_node refcount */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200623int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
624 const unsigned char *tt_addr, uint8_t ttvn,
625 bool roaming, bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200627 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200628 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100629 int hash_added;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200630 struct tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631
Sven Eckelmanna5130882012-05-16 20:23:16 +0200632 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
Antonio Quartullia73105b2011-04-27 14:27:44 +0200634 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200635 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
636 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200637 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 goto out;
639
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200640 common = &tt_global_entry->common;
641 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200642
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200643 common->flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200644 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200645 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200646
647 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
648 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200649
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200650 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200651 batadv_compare_tt,
652 batadv_choose_orig, common,
653 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100654
655 if (unlikely(hash_added != 0)) {
656 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200657 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100658 goto out_remove;
659 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200660
Sven Eckelmanna5130882012-05-16 20:23:16 +0200661 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
662 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200663 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200664 /* there is already a global entry, use this one. */
665
666 /* If there is the TT_CLIENT_ROAM flag set, there is only one
667 * originator left in the list and we previously received a
668 * delete + roaming change for this originator.
669 *
670 * We should first delete the old originator before adding the
671 * new one.
672 */
673 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200674 batadv_tt_global_del_orig_list(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200675 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
676 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200678
Sven Eckelmanna5130882012-05-16 20:23:16 +0200679 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
680 orig_node))
681 batadv_tt_global_add_orig_entry(tt_global_entry,
682 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200684
Antonio Quartullibc279082011-07-07 15:35:35 +0200685 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100686 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200687
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200688 batadv_dbg(DBG_TT, bat_priv,
689 "Creating new global tt entry: %pM (via %pM)\n",
690 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200691
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100692out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200693 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200694 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
695 "global tt received", roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200696 ret = 1;
697out:
698 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200699 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200700 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701}
702
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200703/* print all orig nodes who announce the address for this global entry.
704 * it is assumed that the caller holds rcu_read_lock();
705 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200706static void
707batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
708 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200709{
710 struct hlist_head *head;
711 struct hlist_node *node;
712 struct tt_orig_list_entry *orig_entry;
713 struct tt_common_entry *tt_common_entry;
714 uint16_t flags;
715 uint8_t last_ttvn;
716
717 tt_common_entry = &tt_global_entry->common;
718
719 head = &tt_global_entry->orig_list;
720
721 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
722 flags = tt_common_entry->flags;
723 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
724 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
725 tt_global_entry->common.addr, orig_entry->ttvn,
726 orig_entry->orig_node->orig, last_ttvn,
727 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
728 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
729 }
730}
731
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200732int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733{
734 struct net_device *net_dev = (struct net_device *)seq->private;
735 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200736 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100737 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200738 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200739 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000740 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200742 uint32_t i;
743 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200745 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200746 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100747 ret = seq_printf(seq,
748 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200749 net_dev->name);
750 goto out;
751 }
752
753 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100754 ret = seq_printf(seq,
755 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200756 net_dev->name);
757 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758 }
759
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200760 seq_printf(seq,
761 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200763 seq_printf(seq, " %-13s %s %-15s %s %s\n",
764 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 for (i = 0; i < hash->size; i++) {
767 head = &hash->table[i];
768
Marek Lindner7aadf882011-02-18 12:28:09 +0000769 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100770 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000771 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100772 tt_global_entry = container_of(tt_common_entry,
773 struct tt_global_entry,
774 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200775 batadv_tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000777 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200779out:
780 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200781 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200782 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783}
784
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200785/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200786static void
787batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200789 struct hlist_head *head;
790 struct hlist_node *node, *safe;
791 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200792
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200793 spin_lock_bh(&tt_global_entry->list_lock);
794 head = &tt_global_entry->orig_list;
795 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
796 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200797 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200798 }
799 spin_unlock_bh(&tt_global_entry->list_lock);
800
801}
802
Sven Eckelmanna5130882012-05-16 20:23:16 +0200803static void
804batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
805 struct tt_global_entry *tt_global_entry,
806 struct orig_node *orig_node,
807 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200808{
809 struct hlist_head *head;
810 struct hlist_node *node, *safe;
811 struct tt_orig_list_entry *orig_entry;
812
813 spin_lock_bh(&tt_global_entry->list_lock);
814 head = &tt_global_entry->orig_list;
815 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
816 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200817 batadv_dbg(DBG_TT, bat_priv,
818 "Deleting %pM from global tt entry %pM: %s\n",
819 orig_node->orig,
820 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200821 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200822 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200823 }
824 }
825 spin_unlock_bh(&tt_global_entry->list_lock);
826}
827
Sven Eckelmanna5130882012-05-16 20:23:16 +0200828static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
829 struct tt_global_entry *tt_global_entry,
830 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200831{
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200832 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
833 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200834
Sven Eckelmanna5130882012-05-16 20:23:16 +0200835 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200836 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200837 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200838
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839}
840
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200841/* If the client is to be deleted, we check if it is the last origantor entry
842 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
843 * otherwise we simply remove the originator scheduled for deletion.
844 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200845static void
846batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
847 struct tt_global_entry *tt_global_entry,
848 struct orig_node *orig_node, const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200849{
850 bool last_entry = true;
851 struct hlist_head *head;
852 struct hlist_node *node;
853 struct tt_orig_list_entry *orig_entry;
854
855 /* no local entry exists, case 1:
856 * Check if this is the last one or if other entries exist.
857 */
858
859 rcu_read_lock();
860 head = &tt_global_entry->orig_list;
861 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
862 if (orig_entry->orig_node != orig_node) {
863 last_entry = false;
864 break;
865 }
866 }
867 rcu_read_unlock();
868
869 if (last_entry) {
870 /* its the last one, mark for roaming. */
871 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
872 tt_global_entry->roam_at = jiffies;
873 } else
874 /* there is another entry, we can simply delete this
875 * one and can still use the other one.
876 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200877 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
878 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200879}
880
881
882
Sven Eckelmanna5130882012-05-16 20:23:16 +0200883static void batadv_tt_global_del(struct bat_priv *bat_priv,
884 struct orig_node *orig_node,
885 const unsigned char *addr,
886 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200888 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmanna5130882012-05-16 20:23:16 +0200889 struct tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890
Sven Eckelmanna5130882012-05-16 20:23:16 +0200891 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200892 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200893 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200895 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200896 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
897 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800898
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200899 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200900 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
901 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200902
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800903 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200904 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800905
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200906 /* if we are deleting a global entry due to a roam
907 * event, there are two possibilities:
908 * 1) the client roamed from node A to node B => if there
909 * is only one originator left for this client, we mark
910 * it with TT_CLIENT_ROAM, we start a timer and we
911 * wait for node B to claim it. In case of timeout
912 * the entry is purged.
913 *
914 * If there are other originators left, we directly delete
915 * the originator.
916 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200917 * the global entry, since it is useless now.
918 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200919 local_entry = batadv_tt_local_hash_find(bat_priv,
920 tt_global_entry->common.addr);
921 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200922 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200923 batadv_tt_global_del_orig_list(tt_global_entry);
924 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200925 } else
926 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200927 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
928 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200929
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800930
Antonio Quartullicc47f662011-04-27 14:27:57 +0200931out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200932 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200933 batadv_tt_global_entry_free_ref(tt_global_entry);
934 if (local_entry)
935 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200936}
937
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200938void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
939 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200940{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200941 struct tt_global_entry *global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100942 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200943 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200944 struct hashtable_t *hash = bat_priv->tt_global_hash;
945 struct hlist_node *node, *safe;
946 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200947 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948
Simon Wunderlich6e801492011-10-19 10:28:26 +0200949 if (!hash)
950 return;
951
Antonio Quartullia73105b2011-04-27 14:27:44 +0200952 for (i = 0; i < hash->size; i++) {
953 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200954 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000955
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200956 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100957 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100958 head, hash_entry) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200959 global_entry = container_of(tt_common_entry,
960 struct tt_global_entry,
961 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200962
Sven Eckelmanna5130882012-05-16 20:23:16 +0200963 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
964 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200965
Sven Eckelmanna5130882012-05-16 20:23:16 +0200966 if (hlist_empty(&global_entry->orig_list)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200967 batadv_dbg(DBG_TT, bat_priv,
968 "Deleting global tt entry %pM: %s\n",
Sven Eckelmanna5130882012-05-16 20:23:16 +0200969 global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200970 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200971 batadv_tt_global_entry_free_ref(global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200972 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200973 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200974 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975 }
Antonio Quartulli17071572011-11-07 16:36:40 +0100976 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977}
978
Sven Eckelmanna5130882012-05-16 20:23:16 +0200979static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200980{
981 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100982 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200983 struct tt_global_entry *tt_global_entry;
984 struct hlist_node *node, *node_tmp;
985 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200986 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200987 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200988
Antonio Quartullicc47f662011-04-27 14:27:57 +0200989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200991 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200992
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200993 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100994 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200995 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100996 tt_global_entry = container_of(tt_common_entry,
997 struct tt_global_entry,
998 common);
999 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001000 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001001 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1002 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001003 continue;
1004
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001005 batadv_dbg(DBG_TT, bat_priv,
1006 "Deleting global tt entry (%pM): Roaming timeout\n",
1007 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001008
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001009 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001010 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001011 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001012 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001013 }
1014
Antonio Quartullicc47f662011-04-27 14:27:57 +02001015}
1016
Sven Eckelmanna5130882012-05-16 20:23:16 +02001017static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001019 struct hashtable_t *hash;
1020 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001021 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001022 struct tt_global_entry *tt_global_entry;
1023 struct hlist_node *node, *node_tmp;
1024 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001025 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001026
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001027 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 return;
1029
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001030 hash = bat_priv->tt_global_hash;
1031
1032 for (i = 0; i < hash->size; i++) {
1033 head = &hash->table[i];
1034 list_lock = &hash->list_locks[i];
1035
1036 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001037 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001038 head, hash_entry) {
1039 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001040 tt_global_entry = container_of(tt_common_entry,
1041 struct tt_global_entry,
1042 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001043 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001044 }
1045 spin_unlock_bh(list_lock);
1046 }
1047
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001048 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001049
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001050 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051}
1052
Sven Eckelmanna5130882012-05-16 20:23:16 +02001053static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1054 struct tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001055{
1056 bool ret = false;
1057
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001058 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1059 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001060 ret = true;
1061
1062 return ret;
1063}
1064
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001065struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1066 const uint8_t *src,
1067 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001069 struct tt_local_entry *tt_local_entry = NULL;
1070 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001071 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072 struct neigh_node *router = NULL;
1073 struct hlist_head *head;
1074 struct hlist_node *node;
1075 struct tt_orig_list_entry *orig_entry;
1076 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001077
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001078 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001079 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001080 if (!tt_local_entry)
1081 goto out;
1082 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001083
Sven Eckelmanna5130882012-05-16 20:23:16 +02001084 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001085 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001086 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001088 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001089 * isolation
1090 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001091 if (tt_local_entry &&
1092 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001093 goto out;
1094
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001096
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001097 rcu_read_lock();
1098 head = &tt_global_entry->orig_list;
1099 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001100 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001101 if (!router)
1102 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001103
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001104 if (router->tq_avg > best_tq) {
1105 orig_node = orig_entry->orig_node;
1106 best_tq = router->tq_avg;
1107 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001108 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001109 }
1110 /* found anything? */
1111 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1112 orig_node = NULL;
1113 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001114out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001115 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001116 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001117 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001118 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001119
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001120 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001121}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001122
1123/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001124static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1125 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001126{
1127 uint16_t total = 0, total_one;
1128 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001129 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001130 struct tt_global_entry *tt_global_entry;
1131 struct hlist_node *node;
1132 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001133 uint32_t i;
1134 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001135
1136 for (i = 0; i < hash->size; i++) {
1137 head = &hash->table[i];
1138
1139 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001140 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001141 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001142 tt_global_entry = container_of(tt_common_entry,
1143 struct tt_global_entry,
1144 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001145 /* Roaming clients are in the global table for
1146 * consistency only. They don't have to be
1147 * taken into account while computing the
1148 * global crc
1149 */
1150 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1151 continue;
1152
1153 /* find out if this global entry is announced by this
1154 * originator
1155 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001156 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1157 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001158 continue;
1159
1160 total_one = 0;
1161 for (j = 0; j < ETH_ALEN; j++)
1162 total_one = crc16_byte(total_one,
1163 tt_global_entry->common.addr[j]);
1164 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001165 }
1166 rcu_read_unlock();
1167 }
1168
1169 return total;
1170}
1171
1172/* Calculates the checksum of the local table */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001173static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001174{
1175 uint16_t total = 0, total_one;
1176 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001177 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001178 struct hlist_node *node;
1179 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001180 uint32_t i;
1181 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001182
1183 for (i = 0; i < hash->size; i++) {
1184 head = &hash->table[i];
1185
1186 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001187 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001188 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001189 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001190 * account while computing the CRC
1191 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001192 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001193 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194 total_one = 0;
1195 for (j = 0; j < ETH_ALEN; j++)
1196 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001197 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001198 total ^= total_one;
1199 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200 rcu_read_unlock();
1201 }
1202
1203 return total;
1204}
1205
Sven Eckelmanna5130882012-05-16 20:23:16 +02001206static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001207{
1208 struct tt_req_node *node, *safe;
1209
1210 spin_lock_bh(&bat_priv->tt_req_list_lock);
1211
1212 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1213 list_del(&node->list);
1214 kfree(node);
1215 }
1216
1217 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1218}
1219
Sven Eckelmanna5130882012-05-16 20:23:16 +02001220static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1221 struct orig_node *orig_node,
1222 const unsigned char *tt_buff,
1223 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001224{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001225 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226
1227 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001228 * last OGM (the OGM could carry no changes)
1229 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230 spin_lock_bh(&orig_node->tt_buff_lock);
1231 if (tt_buff_len > 0) {
1232 kfree(orig_node->tt_buff);
1233 orig_node->tt_buff_len = 0;
1234 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1235 if (orig_node->tt_buff) {
1236 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1237 orig_node->tt_buff_len = tt_buff_len;
1238 }
1239 }
1240 spin_unlock_bh(&orig_node->tt_buff_lock);
1241}
1242
Sven Eckelmanna5130882012-05-16 20:23:16 +02001243static void batadv_tt_req_purge(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001244{
1245 struct tt_req_node *node, *safe;
1246
1247 spin_lock_bh(&bat_priv->tt_req_list_lock);
1248 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001249 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001250 list_del(&node->list);
1251 kfree(node);
1252 }
1253 }
1254 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1255}
1256
1257/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001258 * has already been issued for this orig_node, NULL otherwise
1259 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001260static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1261 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262{
1263 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1264
1265 spin_lock_bh(&bat_priv->tt_req_list_lock);
1266 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001267 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1268 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1269 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001270 goto unlock;
1271 }
1272
1273 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1274 if (!tt_req_node)
1275 goto unlock;
1276
1277 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1278 tt_req_node->issued_at = jiffies;
1279
1280 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1281unlock:
1282 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1283 return tt_req_node;
1284}
1285
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001286/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001287static int batadv_tt_local_valid_entry(const void *entry_ptr,
1288 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001289{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001290 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001291
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001292 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001293 return 0;
1294 return 1;
1295}
1296
Sven Eckelmanna5130882012-05-16 20:23:16 +02001297static int batadv_tt_global_valid(const void *entry_ptr,
1298 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001299{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001300 const struct tt_common_entry *tt_common_entry = entry_ptr;
1301 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001302 const struct orig_node *orig_node = data_ptr;
1303
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001304 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001305 return 0;
1306
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001307 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1308 common);
1309
Sven Eckelmanna5130882012-05-16 20:23:16 +02001310 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001311}
1312
Sven Eckelmanna5130882012-05-16 20:23:16 +02001313static struct sk_buff *
1314batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1315 struct hashtable_t *hash,
1316 struct hard_iface *primary_if,
1317 int (*valid_cb)(const void *, const void *),
1318 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001319{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001320 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001321 struct tt_query_packet *tt_response;
1322 struct tt_change *tt_change;
1323 struct hlist_node *node;
1324 struct hlist_head *head;
1325 struct sk_buff *skb = NULL;
1326 uint16_t tt_tot, tt_count;
1327 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001328 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001329
1330 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1331 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1332 tt_len -= tt_len % sizeof(struct tt_change);
1333 }
1334 tt_tot = tt_len / sizeof(struct tt_change);
1335
1336 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1337 if (!skb)
1338 goto out;
1339
1340 skb_reserve(skb, ETH_HLEN);
1341 tt_response = (struct tt_query_packet *)skb_put(skb,
1342 tt_query_size + tt_len);
1343 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001344
1345 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1346 tt_count = 0;
1347
1348 rcu_read_lock();
1349 for (i = 0; i < hash->size; i++) {
1350 head = &hash->table[i];
1351
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001352 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001353 head, hash_entry) {
1354 if (tt_count == tt_tot)
1355 break;
1356
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001357 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358 continue;
1359
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001360 memcpy(tt_change->addr, tt_common_entry->addr,
1361 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001362 tt_change->flags = NO_FLAGS;
1363
1364 tt_count++;
1365 tt_change++;
1366 }
1367 }
1368 rcu_read_unlock();
1369
Antonio Quartulli9d852392011-10-17 14:25:13 +02001370 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001371 * copied
1372 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001373 tt_response->tt_data = htons(tt_count);
1374
Antonio Quartullia73105b2011-04-27 14:27:44 +02001375out:
1376 return skb;
1377}
1378
Sven Eckelmanna5130882012-05-16 20:23:16 +02001379static int batadv_send_tt_request(struct bat_priv *bat_priv,
1380 struct orig_node *dst_orig_node,
1381 uint8_t ttvn, uint16_t tt_crc,
1382 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001383{
1384 struct sk_buff *skb = NULL;
1385 struct tt_query_packet *tt_request;
1386 struct neigh_node *neigh_node = NULL;
1387 struct hard_iface *primary_if;
1388 struct tt_req_node *tt_req_node = NULL;
1389 int ret = 1;
1390
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001391 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001392 if (!primary_if)
1393 goto out;
1394
1395 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001396 * reply from the same orig_node yet
1397 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001398 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399 if (!tt_req_node)
1400 goto out;
1401
1402 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1403 if (!skb)
1404 goto out;
1405
1406 skb_reserve(skb, ETH_HLEN);
1407
1408 tt_request = (struct tt_query_packet *)skb_put(skb,
1409 sizeof(struct tt_query_packet));
1410
Sven Eckelmann76543d12011-11-20 15:47:38 +01001411 tt_request->header.packet_type = BAT_TT_QUERY;
1412 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001413 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1414 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001415 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001417 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001418 tt_request->flags = TT_REQUEST;
1419
1420 if (full_table)
1421 tt_request->flags |= TT_FULL_TABLE;
1422
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001423 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001424 if (!neigh_node)
1425 goto out;
1426
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001427 batadv_dbg(DBG_TT, bat_priv,
1428 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1429 dst_orig_node->orig, neigh_node->addr,
1430 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001431
Martin Hundebøllf8214862012-04-20 17:02:45 +02001432 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1433
Sven Eckelmann9455e342012-05-12 02:09:37 +02001434 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435 ret = 0;
1436
1437out:
1438 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001439 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001440 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001441 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001442 if (ret)
1443 kfree_skb(skb);
1444 if (ret && tt_req_node) {
1445 spin_lock_bh(&bat_priv->tt_req_list_lock);
1446 list_del(&tt_req_node->list);
1447 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1448 kfree(tt_req_node);
1449 }
1450 return ret;
1451}
1452
Sven Eckelmanna5130882012-05-16 20:23:16 +02001453static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1454 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455{
1456 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1457 struct neigh_node *neigh_node = NULL;
1458 struct hard_iface *primary_if = NULL;
1459 uint8_t orig_ttvn, req_ttvn, ttvn;
1460 int ret = false;
1461 unsigned char *tt_buff;
1462 bool full_table;
1463 uint16_t tt_len, tt_tot;
1464 struct sk_buff *skb = NULL;
1465 struct tt_query_packet *tt_response;
1466
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001467 batadv_dbg(DBG_TT, bat_priv,
1468 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1469 tt_request->src, tt_request->ttvn, tt_request->dst,
1470 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471
1472 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001473 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 if (!req_dst_orig_node)
1475 goto out;
1476
Sven Eckelmannda641192012-05-12 13:48:56 +02001477 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478 if (!res_dst_orig_node)
1479 goto out;
1480
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001481 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482 if (!neigh_node)
1483 goto out;
1484
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001485 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486 if (!primary_if)
1487 goto out;
1488
1489 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1490 req_ttvn = tt_request->ttvn;
1491
Antonio Quartulli015758d2011-07-09 17:52:13 +02001492 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001493 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001494 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495 goto out;
1496
Antonio Quartulli015758d2011-07-09 17:52:13 +02001497 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001498 if (tt_request->flags & TT_FULL_TABLE ||
1499 !req_dst_orig_node->tt_buff)
1500 full_table = true;
1501 else
1502 full_table = false;
1503
1504 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001505 * I'll send only one packet with as much TT entries as I can
1506 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507 if (!full_table) {
1508 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1509 tt_len = req_dst_orig_node->tt_buff_len;
1510 tt_tot = tt_len / sizeof(struct tt_change);
1511
1512 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1513 tt_len + ETH_HLEN);
1514 if (!skb)
1515 goto unlock;
1516
1517 skb_reserve(skb, ETH_HLEN);
1518 tt_response = (struct tt_query_packet *)skb_put(skb,
1519 sizeof(struct tt_query_packet) + tt_len);
1520 tt_response->ttvn = req_ttvn;
1521 tt_response->tt_data = htons(tt_tot);
1522
1523 tt_buff = skb->data + sizeof(struct tt_query_packet);
1524 /* Copy the last orig_node's OGM buffer */
1525 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1526 req_dst_orig_node->tt_buff_len);
1527
1528 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1529 } else {
1530 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1531 sizeof(struct tt_change);
1532 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1533
Sven Eckelmanna5130882012-05-16 20:23:16 +02001534 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1535 bat_priv->tt_global_hash,
1536 primary_if,
1537 batadv_tt_global_valid,
1538 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539 if (!skb)
1540 goto out;
1541
1542 tt_response = (struct tt_query_packet *)skb->data;
1543 }
1544
Sven Eckelmann76543d12011-11-20 15:47:38 +01001545 tt_response->header.packet_type = BAT_TT_QUERY;
1546 tt_response->header.version = COMPAT_VERSION;
1547 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001548 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1549 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1550 tt_response->flags = TT_RESPONSE;
1551
1552 if (full_table)
1553 tt_response->flags |= TT_FULL_TABLE;
1554
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001555 batadv_dbg(DBG_TT, bat_priv,
1556 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1557 res_dst_orig_node->orig, neigh_node->addr,
1558 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559
Martin Hundebøllf8214862012-04-20 17:02:45 +02001560 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1561
Sven Eckelmann9455e342012-05-12 02:09:37 +02001562 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001563 ret = true;
1564 goto out;
1565
1566unlock:
1567 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1568
1569out:
1570 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001571 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001572 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001573 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001575 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001577 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578 if (!ret)
1579 kfree_skb(skb);
1580 return ret;
1581
1582}
Sven Eckelmanna5130882012-05-16 20:23:16 +02001583static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1584 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001585{
1586 struct orig_node *orig_node = NULL;
1587 struct neigh_node *neigh_node = NULL;
1588 struct hard_iface *primary_if = NULL;
1589 uint8_t my_ttvn, req_ttvn, ttvn;
1590 int ret = false;
1591 unsigned char *tt_buff;
1592 bool full_table;
1593 uint16_t tt_len, tt_tot;
1594 struct sk_buff *skb = NULL;
1595 struct tt_query_packet *tt_response;
1596
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001597 batadv_dbg(DBG_TT, bat_priv,
1598 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1599 tt_request->src, tt_request->ttvn,
1600 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001601
1602
1603 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1604 req_ttvn = tt_request->ttvn;
1605
Sven Eckelmannda641192012-05-12 13:48:56 +02001606 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607 if (!orig_node)
1608 goto out;
1609
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001610 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 if (!neigh_node)
1612 goto out;
1613
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001614 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615 if (!primary_if)
1616 goto out;
1617
1618 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001619 * is too big send the whole local translation table
1620 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1622 !bat_priv->tt_buff)
1623 full_table = true;
1624 else
1625 full_table = false;
1626
1627 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001628 * I'll send only one packet with as much TT entries as I can
1629 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630 if (!full_table) {
1631 spin_lock_bh(&bat_priv->tt_buff_lock);
1632 tt_len = bat_priv->tt_buff_len;
1633 tt_tot = tt_len / sizeof(struct tt_change);
1634
1635 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1636 tt_len + ETH_HLEN);
1637 if (!skb)
1638 goto unlock;
1639
1640 skb_reserve(skb, ETH_HLEN);
1641 tt_response = (struct tt_query_packet *)skb_put(skb,
1642 sizeof(struct tt_query_packet) + tt_len);
1643 tt_response->ttvn = req_ttvn;
1644 tt_response->tt_data = htons(tt_tot);
1645
1646 tt_buff = skb->data + sizeof(struct tt_query_packet);
1647 memcpy(tt_buff, bat_priv->tt_buff,
1648 bat_priv->tt_buff_len);
1649 spin_unlock_bh(&bat_priv->tt_buff_lock);
1650 } else {
1651 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1652 sizeof(struct tt_change);
1653 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1654
Sven Eckelmanna5130882012-05-16 20:23:16 +02001655 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1656 bat_priv->tt_local_hash,
1657 primary_if,
1658 batadv_tt_local_valid_entry,
1659 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001660 if (!skb)
1661 goto out;
1662
1663 tt_response = (struct tt_query_packet *)skb->data;
1664 }
1665
Sven Eckelmann76543d12011-11-20 15:47:38 +01001666 tt_response->header.packet_type = BAT_TT_QUERY;
1667 tt_response->header.version = COMPAT_VERSION;
1668 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1670 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1671 tt_response->flags = TT_RESPONSE;
1672
1673 if (full_table)
1674 tt_response->flags |= TT_FULL_TABLE;
1675
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001676 batadv_dbg(DBG_TT, bat_priv,
1677 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1678 orig_node->orig, neigh_node->addr,
1679 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001680
Martin Hundebøllf8214862012-04-20 17:02:45 +02001681 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1682
Sven Eckelmann9455e342012-05-12 02:09:37 +02001683 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684 ret = true;
1685 goto out;
1686
1687unlock:
1688 spin_unlock_bh(&bat_priv->tt_buff_lock);
1689out:
1690 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001691 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001693 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001695 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696 if (!ret)
1697 kfree_skb(skb);
1698 /* This packet was for me, so it doesn't need to be re-routed */
1699 return true;
1700}
1701
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001702bool batadv_send_tt_response(struct bat_priv *bat_priv,
1703 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001705 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001706 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001707 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001708 return true;
1709
Sven Eckelmanna5130882012-05-16 20:23:16 +02001710 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001711 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001712 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001713 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714}
1715
Sven Eckelmanna5130882012-05-16 20:23:16 +02001716static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1717 struct orig_node *orig_node,
1718 struct tt_change *tt_change,
1719 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001720{
1721 int i;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001722 int is_wifi;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001723 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001724
1725 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001726 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001727 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1728 batadv_tt_global_del(bat_priv, orig_node,
1729 (tt_change + i)->addr,
1730 "tt removed by changes",
1731 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001732 } else {
1733 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1734 if (!batadv_tt_global_add(bat_priv, orig_node,
1735 (tt_change + i)->addr, ttvn,
1736 false, is_wifi))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001737 /* In case of problem while storing a
1738 * global_entry, we stop the updating
1739 * procedure without committing the
1740 * ttvn change. This will avoid to send
1741 * corrupted data on tt_request
1742 */
1743 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001744 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001745 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001746 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747}
1748
Sven Eckelmanna5130882012-05-16 20:23:16 +02001749static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1750 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751{
1752 struct orig_node *orig_node = NULL;
1753
Sven Eckelmannda641192012-05-12 13:48:56 +02001754 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755 if (!orig_node)
1756 goto out;
1757
1758 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001759 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001760
Sven Eckelmanna5130882012-05-16 20:23:16 +02001761 _batadv_tt_update_changes(bat_priv, orig_node,
1762 (struct tt_change *)(tt_response + 1),
1763 ntohs(tt_response->tt_data),
1764 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765
1766 spin_lock_bh(&orig_node->tt_buff_lock);
1767 kfree(orig_node->tt_buff);
1768 orig_node->tt_buff_len = 0;
1769 orig_node->tt_buff = NULL;
1770 spin_unlock_bh(&orig_node->tt_buff_lock);
1771
1772 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1773
1774out:
1775 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001776 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001777}
1778
Sven Eckelmanna5130882012-05-16 20:23:16 +02001779static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1780 struct orig_node *orig_node,
1781 uint16_t tt_num_changes, uint8_t ttvn,
1782 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001784 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1785 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786
Sven Eckelmanna5130882012-05-16 20:23:16 +02001787 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1788 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 atomic_set(&orig_node->last_ttvn, ttvn);
1790}
1791
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001792bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001794 struct tt_local_entry *tt_local_entry = NULL;
1795 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796
Sven Eckelmanna5130882012-05-16 20:23:16 +02001797 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001798 if (!tt_local_entry)
1799 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001800 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001801 * consistency purpose)
1802 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001803 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001804 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001805 ret = true;
1806out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001807 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001808 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001809 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810}
1811
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001812void batadv_handle_tt_response(struct bat_priv *bat_priv,
1813 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814{
1815 struct tt_req_node *node, *safe;
1816 struct orig_node *orig_node = NULL;
1817
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001818 batadv_dbg(DBG_TT, bat_priv,
1819 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1820 tt_response->src, tt_response->ttvn,
1821 ntohs(tt_response->tt_data),
1822 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001824 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001825 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001826 goto out;
1827
Sven Eckelmannda641192012-05-12 13:48:56 +02001828 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829 if (!orig_node)
1830 goto out;
1831
1832 if (tt_response->flags & TT_FULL_TABLE)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001833 batadv_tt_fill_gtable(bat_priv, tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001834 else
Sven Eckelmanna5130882012-05-16 20:23:16 +02001835 batadv_tt_update_changes(bat_priv, orig_node,
1836 ntohs(tt_response->tt_data),
1837 tt_response->ttvn,
1838 (struct tt_change *)(tt_response + 1));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839
1840 /* Delete the tt_req_node from pending tt_requests list */
1841 spin_lock_bh(&bat_priv->tt_req_list_lock);
1842 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001843 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 continue;
1845 list_del(&node->list);
1846 kfree(node);
1847 }
1848 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1849
1850 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001851 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001852 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001853 * unset the flag
1854 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001855 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856out:
1857 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001858 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859}
1860
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001861int batadv_tt_init(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001862{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001863 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864
Sven Eckelmanna5130882012-05-16 20:23:16 +02001865 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001866 if (ret < 0)
1867 return ret;
1868
Sven Eckelmanna5130882012-05-16 20:23:16 +02001869 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001870 if (ret < 0)
1871 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872
Sven Eckelmanna5130882012-05-16 20:23:16 +02001873 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874
1875 return 1;
1876}
1877
Sven Eckelmanna5130882012-05-16 20:23:16 +02001878static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001880 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881
Antonio Quartullicc47f662011-04-27 14:27:57 +02001882 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883
Antonio Quartullicc47f662011-04-27 14:27:57 +02001884 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1885 list_del(&node->list);
1886 kfree(node);
1887 }
1888
1889 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1890}
1891
Sven Eckelmanna5130882012-05-16 20:23:16 +02001892static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001893{
1894 struct tt_roam_node *node, *safe;
1895
1896 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1897 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001898 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001899 continue;
1900
1901 list_del(&node->list);
1902 kfree(node);
1903 }
1904 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1905}
1906
1907/* This function checks whether the client already reached the
1908 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1909 * will not be sent.
1910 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001911 * returns true if the ROAMING_ADV can be sent, false otherwise
1912 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001913static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1914 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001915{
1916 struct tt_roam_node *tt_roam_node;
1917 bool ret = false;
1918
1919 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1920 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001921 * reply from the same orig_node yet
1922 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001923 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001924 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001925 continue;
1926
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001927 if (batadv_has_timed_out(tt_roam_node->first_time,
1928 ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001929 continue;
1930
1931 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1932 /* Sorry, you roamed too many times! */
1933 goto unlock;
1934 ret = true;
1935 break;
1936 }
1937
1938 if (!ret) {
1939 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1940 if (!tt_roam_node)
1941 goto unlock;
1942
1943 tt_roam_node->first_time = jiffies;
1944 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1945 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1946
1947 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1948 ret = true;
1949 }
1950
1951unlock:
1952 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1953 return ret;
1954}
1955
Sven Eckelmanna5130882012-05-16 20:23:16 +02001956static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1957 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001958{
1959 struct neigh_node *neigh_node = NULL;
1960 struct sk_buff *skb = NULL;
1961 struct roam_adv_packet *roam_adv_packet;
1962 int ret = 1;
1963 struct hard_iface *primary_if;
1964
1965 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001966 * already roamed to us too many times
1967 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001968 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001969 goto out;
1970
1971 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1972 if (!skb)
1973 goto out;
1974
1975 skb_reserve(skb, ETH_HLEN);
1976
1977 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1978 sizeof(struct roam_adv_packet));
1979
Sven Eckelmann76543d12011-11-20 15:47:38 +01001980 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1981 roam_adv_packet->header.version = COMPAT_VERSION;
1982 roam_adv_packet->header.ttl = TTL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001983 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001984 if (!primary_if)
1985 goto out;
1986 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001987 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001988 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1989 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1990
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001991 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001992 if (!neigh_node)
1993 goto out;
1994
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001995 batadv_dbg(DBG_TT, bat_priv,
1996 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1997 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001998
Martin Hundebøllf8214862012-04-20 17:02:45 +02001999 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2000
Sven Eckelmann9455e342012-05-12 02:09:37 +02002001 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002002 ret = 0;
2003
2004out:
2005 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002006 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002007 if (ret)
2008 kfree_skb(skb);
2009 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002010}
2011
Sven Eckelmanna5130882012-05-16 20:23:16 +02002012static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013{
2014 struct delayed_work *delayed_work =
2015 container_of(work, struct delayed_work, work);
2016 struct bat_priv *bat_priv =
2017 container_of(delayed_work, struct bat_priv, tt_work);
2018
Sven Eckelmanna5130882012-05-16 20:23:16 +02002019 batadv_tt_local_purge(bat_priv);
2020 batadv_tt_global_roam_purge(bat_priv);
2021 batadv_tt_req_purge(bat_priv);
2022 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023
Sven Eckelmanna5130882012-05-16 20:23:16 +02002024 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002026
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002027void batadv_tt_free(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028{
2029 cancel_delayed_work_sync(&bat_priv->tt_work);
2030
Sven Eckelmanna5130882012-05-16 20:23:16 +02002031 batadv_tt_local_table_free(bat_priv);
2032 batadv_tt_global_table_free(bat_priv);
2033 batadv_tt_req_list_free(bat_priv);
2034 batadv_tt_changes_list_free(bat_priv);
2035 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002036
2037 kfree(bat_priv->tt_buff);
2038}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002039
Antonio Quartulli697f2532011-11-07 16:47:01 +01002040/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002041 * in the given hash table and returns the number of modified entries
2042 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002043static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2044 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002045{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002046 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002047 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002048 struct hlist_head *head;
2049 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002050 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002051
2052 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002053 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002054
2055 for (i = 0; i < hash->size; i++) {
2056 head = &hash->table[i];
2057
2058 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002059 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002060 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002061 if (enable) {
2062 if ((tt_common_entry->flags & flags) == flags)
2063 continue;
2064 tt_common_entry->flags |= flags;
2065 } else {
2066 if (!(tt_common_entry->flags & flags))
2067 continue;
2068 tt_common_entry->flags &= ~flags;
2069 }
2070 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002071 }
2072 rcu_read_unlock();
2073 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002074out:
2075 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002076}
2077
2078/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002079static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002080{
2081 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002082 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002083 struct tt_local_entry *tt_local_entry;
2084 struct hlist_node *node, *node_tmp;
2085 struct hlist_head *head;
2086 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002087 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002088
2089 if (!hash)
2090 return;
2091
2092 for (i = 0; i < hash->size; i++) {
2093 head = &hash->table[i];
2094 list_lock = &hash->list_locks[i];
2095
2096 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002097 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002098 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002099 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002100 continue;
2101
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002102 batadv_dbg(DBG_TT, bat_priv,
2103 "Deleting local tt entry (%pM): pending\n",
2104 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002105
2106 atomic_dec(&bat_priv->num_local_tt);
2107 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002108 tt_local_entry = container_of(tt_common_entry,
2109 struct tt_local_entry,
2110 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002111 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002112 }
2113 spin_unlock_bh(list_lock);
2114 }
2115
2116}
2117
Sven Eckelmanna5130882012-05-16 20:23:16 +02002118static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2119 unsigned char **packet_buff,
2120 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002121{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002122 uint16_t changed_num = 0;
2123
2124 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2125 return -ENOENT;
2126
Sven Eckelmanna5130882012-05-16 20:23:16 +02002127 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2128 TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002129
2130 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002131 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002132 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002133 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002134
2135 /* Increment the TTVN only once per OGM interval */
2136 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002137 batadv_dbg(DBG_TT, bat_priv,
2138 "Local changes committed, updating to ttvn %u\n",
2139 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002140 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002141
2142 /* reset the sending counter */
2143 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2144
Sven Eckelmanna5130882012-05-16 20:23:16 +02002145 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2146 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002147}
2148
2149/* when calling this function (hard_iface == primary_if) has to be true */
2150int batadv_tt_append_diff(struct bat_priv *bat_priv,
2151 unsigned char **packet_buff, int *packet_buff_len,
2152 int packet_min_len)
2153{
2154 int tt_num_changes;
2155
2156 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002157 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2158 packet_buff_len,
2159 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002160
2161 /* if the changes have been sent often enough */
2162 if ((tt_num_changes < 0) &&
2163 (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002164 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2165 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002166 tt_num_changes = 0;
2167 }
2168
2169 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002170}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002171
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002172bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2173 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002174{
2175 struct tt_local_entry *tt_local_entry = NULL;
2176 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002177 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002178
2179 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002180 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002181
Sven Eckelmanna5130882012-05-16 20:23:16 +02002182 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002183 if (!tt_local_entry)
2184 goto out;
2185
Sven Eckelmanna5130882012-05-16 20:23:16 +02002186 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002187 if (!tt_global_entry)
2188 goto out;
2189
Sven Eckelmanna5130882012-05-16 20:23:16 +02002190 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002191 goto out;
2192
Marek Lindner5870adc2012-06-20 17:16:05 +02002193 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002194
2195out:
2196 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002197 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002198 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002199 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002200 return ret;
2201}
Marek Lindnera943cac2011-07-30 13:10:18 +02002202
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002203void batadv_tt_update_orig(struct bat_priv *bat_priv,
2204 struct orig_node *orig_node,
2205 const unsigned char *tt_buff, uint8_t tt_num_changes,
2206 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002207{
2208 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2209 bool full_table = true;
2210
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002211 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002212 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002213 return;
2214
Antonio Quartulli17071572011-11-07 16:36:40 +01002215 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002216 * increased by one -> we can apply the attached changes
2217 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002218 if ((!orig_node->tt_initialised && ttvn == 1) ||
2219 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002220 /* the OGM could not contain the changes due to their size or
2221 * because they have already been sent TT_OGM_APPEND_MAX times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002222 * In this case send a tt request
2223 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002224 if (!tt_num_changes) {
2225 full_table = false;
2226 goto request_table;
2227 }
2228
Sven Eckelmanna5130882012-05-16 20:23:16 +02002229 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2230 ttvn, (struct tt_change *)tt_buff);
Marek Lindnera943cac2011-07-30 13:10:18 +02002231
2232 /* Even if we received the precomputed crc with the OGM, we
2233 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002234 * in the global table
2235 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002236 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002237
2238 /* The ttvn alone is not enough to guarantee consistency
2239 * because a single value could represent different states
2240 * (due to the wrap around). Thus a node has to check whether
2241 * the resulting table (after applying the changes) is still
2242 * consistent or not. E.g. a node could disconnect while its
2243 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2244 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002245 * inconsistency
2246 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002247 if (orig_node->tt_crc != tt_crc)
2248 goto request_table;
2249
2250 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002251 * unset the flag
2252 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002253 orig_node->tt_poss_change = false;
2254 } else {
2255 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002256 * in sync anymore -> request fresh tt data
2257 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002258 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2259 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002260request_table:
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002261 batadv_dbg(DBG_TT, bat_priv,
2262 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2263 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2264 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002265 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2266 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002267 return;
2268 }
2269 }
2270}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002271
2272/* returns true whether we know that the client has moved from its old
2273 * originator to another one. This entry is kept is still kept for consistency
2274 * purposes
2275 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002276bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2277 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002278{
2279 struct tt_global_entry *tt_global_entry;
2280 bool ret = false;
2281
Sven Eckelmanna5130882012-05-16 20:23:16 +02002282 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002283 if (!tt_global_entry)
2284 goto out;
2285
2286 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002287 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002288out:
2289 return ret;
2290}