blob: d82766bfd2649acc39334c88c222d90367af23b0 [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{
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000152 /* to avoid race conditions, immediately decrease the tt counter */
153 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200154 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200155}
156
Sven Eckelmanna5130882012-05-16 20:23:16 +0200157static void batadv_tt_local_event(struct bat_priv *bat_priv,
158 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200159{
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200160 struct tt_change_node *tt_change_node, *entry, *safe;
161 bool event_removed = false;
162 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200163
164 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
165
166 if (!tt_change_node)
167 return;
168
Antonio Quartulliff66c972011-06-30 01:14:00 +0200169 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200170 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
171
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200172 del_op_requested = flags & TT_CLIENT_DEL;
173
174 /* check for ADD+DEL or DEL+ADD events */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175 spin_lock_bh(&bat_priv->tt_changes_list_lock);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200176 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
177 list) {
178 if (!batadv_compare_eth(entry->change.addr, addr))
179 continue;
180
181 /* DEL+ADD in the same orig interval have no effect and can be
182 * removed to avoid silly behaviour on the receiver side. The
183 * other way around (ADD+DEL) can happen in case of roaming of
184 * a client still in the NEW state. Roaming of NEW clients is
185 * now possible due to automatically recognition of "temporary"
186 * clients
187 */
188 del_op_entry = entry->change.flags & TT_CLIENT_DEL;
189 if (!del_op_requested && del_op_entry)
190 goto del;
191 if (del_op_requested && !del_op_entry)
192 goto del;
193 continue;
194del:
195 list_del(&entry->list);
196 kfree(entry);
197 event_removed = true;
198 goto unlock;
199 }
200
Antonio Quartullia73105b2011-04-27 14:27:44 +0200201 /* track the change in the OGMinterval list */
202 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200203
204unlock:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200205 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
206
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 if (event_removed)
208 atomic_dec(&bat_priv->tt_local_changes);
209 else
210 atomic_inc(&bat_priv->tt_local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211}
212
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200213int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200214{
215 return changes_num * sizeof(struct tt_change);
216}
217
Sven Eckelmanna5130882012-05-16 20:23:16 +0200218static int batadv_tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200220 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200221 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200223 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200225 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200226 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Sven Eckelmann5346c352012-05-05 13:27:28 +0200228 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229}
230
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200231void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
232 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233{
234 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200235 struct tt_local_entry *tt_local_entry = NULL;
236 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200237 struct hlist_head *head;
238 struct hlist_node *node;
239 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100240 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Sven Eckelmanna5130882012-05-16 20:23:16 +0200242 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200244 if (tt_local_entry) {
245 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100246 /* possibly unset the TT_CLIENT_PENDING flag */
247 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200248 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 }
250
Sven Eckelmann704509b2011-05-14 23:14:54 +0200251 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200252 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200253 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200254
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200255 batadv_dbg(DBG_TT, bat_priv,
256 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
257 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100259 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
260 tt_local_entry->common.flags = NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200261 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100262 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
263 atomic_set(&tt_local_entry->common.refcount, 2);
264 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
266 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200267 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100268 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100270 /* The local entry has to be marked as NEW to avoid to send it in
271 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200272 * (consistency check)
273 */
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100274 tt_local_entry->common.flags |= TT_CLIENT_NEW;
275
Sven Eckelmanna5130882012-05-16 20:23:16 +0200276 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200277 batadv_choose_orig,
278 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200279 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100280
281 if (unlikely(hash_added != 0)) {
282 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200283 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100284 goto out;
285 }
286
Sven Eckelmanna5130882012-05-16 20:23:16 +0200287 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200288
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200290 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291
Antonio Quartullicc47f662011-04-27 14:27:57 +0200292 /* Check whether it is a roaming! */
293 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200294 /* These node are probably going to update their tt table */
295 head = &tt_global_entry->orig_list;
296 rcu_read_lock();
297 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
298 orig_entry->orig_node->tt_poss_change = true;
299
Sven Eckelmanna5130882012-05-16 20:23:16 +0200300 batadv_send_roam_adv(bat_priv,
301 tt_global_entry->common.addr,
302 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200303 }
304 rcu_read_unlock();
305 /* The global entry has to be marked as ROAMING and
306 * has to be kept for consistency purpose
307 */
David S. Miller220b07e2011-12-16 15:07:28 -0500308 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100309 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200310 }
311out:
312 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200313 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200314 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200315 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316}
317
Sven Eckelmanna5130882012-05-16 20:23:16 +0200318static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
319 int *packet_buff_len,
320 int min_packet_len,
321 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800323 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800325 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
326
327 /* keep old buffer if kmalloc should fail */
328 if (new_buff) {
329 memcpy(new_buff, *packet_buff, min_packet_len);
330 kfree(*packet_buff);
331 *packet_buff = new_buff;
332 *packet_buff_len = new_packet_len;
333 }
334}
335
Sven Eckelmanna5130882012-05-16 20:23:16 +0200336static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
337 unsigned char **packet_buff,
338 int *packet_buff_len,
339 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800340{
341 struct hard_iface *primary_if;
342 int req_len;
343
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200344 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800345
346 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200347 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800348
349 /* if we have too many changes for one packet don't send any
350 * and wait for the tt table request which will be fragmented
351 */
352 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
353 req_len = min_packet_len;
354
Sven Eckelmanna5130882012-05-16 20:23:16 +0200355 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
356 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800357
358 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200359 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800360}
361
Sven Eckelmanna5130882012-05-16 20:23:16 +0200362static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
363 unsigned char **packet_buff,
364 int *packet_buff_len,
365 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800366{
367 struct tt_change_node *entry, *safe;
368 int count = 0, tot_changes = 0, new_len;
369 unsigned char *tt_buff;
370
Sven Eckelmanna5130882012-05-16 20:23:16 +0200371 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
372 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800373
374 new_len = *packet_buff_len - min_packet_len;
375 tt_buff = *packet_buff + min_packet_len;
376
377 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200378 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
Antonio Quartullia73105b2011-04-27 14:27:44 +0200380 spin_lock_bh(&bat_priv->tt_changes_list_lock);
381 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Antonio Quartullia73105b2011-04-27 14:27:44 +0200383 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100384 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200385 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200386 memcpy(tt_buff + batadv_tt_len(count),
Antonio Quartullia73105b2011-04-27 14:27:44 +0200387 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388 count++;
389 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200390 list_del(&entry->list);
391 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200393 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394
Antonio Quartullia73105b2011-04-27 14:27:44 +0200395 /* Keep the buffer for possible tt_request */
396 spin_lock_bh(&bat_priv->tt_buff_lock);
397 kfree(bat_priv->tt_buff);
398 bat_priv->tt_buff_len = 0;
399 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800400 /* check whether this new OGM has no changes due to size problems */
401 if (new_len > 0) {
402 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200403 * instead of providing the diff
404 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800405 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200406 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800407 memcpy(bat_priv->tt_buff, tt_buff, new_len);
408 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200409 }
410 }
411 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
Marek Lindner08ad76e2012-04-23 16:32:55 +0800413 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414}
415
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200416int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417{
418 struct net_device *net_dev = (struct net_device *)seq->private;
419 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200420 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100421 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200422 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000423 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200425 uint32_t i;
426 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200428 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200429 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100430 ret = seq_printf(seq,
431 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200432 net_dev->name);
433 goto out;
434 }
435
436 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100437 ret = seq_printf(seq,
438 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200439 net_dev->name);
440 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 }
442
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100443 seq_printf(seq,
444 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200445 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 for (i = 0; i < hash->size; i++) {
448 head = &hash->table[i];
449
Marek Lindner7aadf882011-02-18 12:28:09 +0000450 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100451 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000452 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200453 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100454 tt_common_entry->addr,
455 (tt_common_entry->flags &
456 TT_CLIENT_ROAM ? 'R' : '.'),
457 (tt_common_entry->flags &
458 TT_CLIENT_NOPURGE ? 'P' : '.'),
459 (tt_common_entry->flags &
460 TT_CLIENT_NEW ? 'N' : '.'),
461 (tt_common_entry->flags &
462 TT_CLIENT_PENDING ? 'X' : '.'),
463 (tt_common_entry->flags &
464 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000466 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200468out:
469 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200470 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200471 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472}
473
Sven Eckelmanna5130882012-05-16 20:23:16 +0200474static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
475 struct tt_local_entry *tt_local_entry,
476 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200478 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
479 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Antonio Quartulli015758d2011-07-09 17:52:13 +0200481 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200483 * response issued before the net ttvn increment (consistency check)
484 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100485 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100486
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200487 batadv_dbg(DBG_TT, bat_priv,
488 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200492void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
493 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200495 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmanna5130882012-05-16 20:23:16 +0200497 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200498 if (!tt_local_entry)
499 goto out;
500
Sven Eckelmanna5130882012-05-16 20:23:16 +0200501 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
502 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
503 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200504out:
505 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200506 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507}
508
Sven Eckelmanna5130882012-05-16 20:23:16 +0200509static void batadv_tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200511 struct hashtable_t *hash = bat_priv->tt_local_hash;
512 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100513 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000514 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200516 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200517 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519 for (i = 0; i < hash->size; i++) {
520 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200521 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200523 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100524 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000525 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100526 tt_local_entry = container_of(tt_common_entry,
527 struct tt_local_entry,
528 common);
529 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000530 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200532 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100533 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200534 continue;
535
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200536 if (!batadv_has_timed_out(tt_local_entry->last_seen,
537 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000538 continue;
539
Sven Eckelmanna5130882012-05-16 20:23:16 +0200540 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
541 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200543 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544 }
545
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546}
547
Sven Eckelmanna5130882012-05-16 20:23:16 +0200548static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200550 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200551 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100552 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200553 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200554 struct hlist_node *node, *node_tmp;
555 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200556 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200557
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200558 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 return;
560
Antonio Quartullia73105b2011-04-27 14:27:44 +0200561 hash = bat_priv->tt_local_hash;
562
563 for (i = 0; i < hash->size; i++) {
564 head = &hash->table[i];
565 list_lock = &hash->list_locks[i];
566
567 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100568 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200569 head, hash_entry) {
570 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100571 tt_local_entry = container_of(tt_common_entry,
572 struct tt_local_entry,
573 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200574 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200575 }
576 spin_unlock_bh(list_lock);
577 }
578
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200579 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200580
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200581 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582}
583
Sven Eckelmanna5130882012-05-16 20:23:16 +0200584static int batadv_tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200586 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200587 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200589 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200591 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200592 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
Sven Eckelmann5346c352012-05-05 13:27:28 +0200594 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595}
596
Sven Eckelmanna5130882012-05-16 20:23:16 +0200597static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200598{
599 struct tt_change_node *entry, *safe;
600
601 spin_lock_bh(&bat_priv->tt_changes_list_lock);
602
603 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
604 list) {
605 list_del(&entry->list);
606 kfree(entry);
607 }
608
609 atomic_set(&bat_priv->tt_local_changes, 0);
610 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
611}
612
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200613/* find out if an orig_node is already in the list of a tt_global_entry.
614 * returns 1 if found, 0 otherwise
615 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200616static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
617 const struct orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200618{
619 struct tt_orig_list_entry *tmp_orig_entry;
620 const struct hlist_head *head;
621 struct hlist_node *node;
622 bool found = false;
623
624 rcu_read_lock();
625 head = &entry->orig_list;
626 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
627 if (tmp_orig_entry->orig_node == orig_node) {
628 found = true;
629 break;
630 }
631 }
632 rcu_read_unlock();
633 return found;
634}
635
Sven Eckelmanna5130882012-05-16 20:23:16 +0200636static void
637batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
638 struct orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200639{
640 struct tt_orig_list_entry *orig_entry;
641
642 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
643 if (!orig_entry)
644 return;
645
646 INIT_HLIST_NODE(&orig_entry->list);
647 atomic_inc(&orig_node->refcount);
648 atomic_inc(&orig_node->tt_size);
649 orig_entry->orig_node = orig_node;
650 orig_entry->ttvn = ttvn;
651
652 spin_lock_bh(&tt_global_entry->list_lock);
653 hlist_add_head_rcu(&orig_entry->list,
654 &tt_global_entry->orig_list);
655 spin_unlock_bh(&tt_global_entry->list_lock);
656}
657
Antonio Quartullia73105b2011-04-27 14:27:44 +0200658/* caller must hold orig_node refcount */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200659int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
660 const unsigned char *tt_addr, uint8_t ttvn,
661 bool roaming, bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200663 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200664 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100665 int hash_added;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200666 struct tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667
Sven Eckelmanna5130882012-05-16 20:23:16 +0200668 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669
Antonio Quartullia73105b2011-04-27 14:27:44 +0200670 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200671 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
672 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200673 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200674 goto out;
675
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200676 common = &tt_global_entry->common;
677 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200678
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200679 common->flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200680 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200681 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200682
683 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
684 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200685
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200686 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200687 batadv_compare_tt,
688 batadv_choose_orig, common,
689 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100690
691 if (unlikely(hash_added != 0)) {
692 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200693 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100694 goto out_remove;
695 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200696
Sven Eckelmanna5130882012-05-16 20:23:16 +0200697 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
698 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200699 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200700 /* there is already a global entry, use this one. */
701
702 /* If there is the TT_CLIENT_ROAM flag set, there is only one
703 * originator left in the list and we previously received a
704 * delete + roaming change for this originator.
705 *
706 * We should first delete the old originator before adding the
707 * new one.
708 */
709 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200710 batadv_tt_global_del_orig_list(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200711 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
712 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200714
Sven Eckelmanna5130882012-05-16 20:23:16 +0200715 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
716 orig_node))
717 batadv_tt_global_add_orig_entry(tt_global_entry,
718 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200720
Antonio Quartullibc279082011-07-07 15:35:35 +0200721 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100722 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200723
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200724 batadv_dbg(DBG_TT, bat_priv,
725 "Creating new global tt entry: %pM (via %pM)\n",
726 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200727
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100728out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200729 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200730 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
731 "global tt received", roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200732 ret = 1;
733out:
734 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200735 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200736 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737}
738
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200739/* print all orig nodes who announce the address for this global entry.
740 * it is assumed that the caller holds rcu_read_lock();
741 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200742static void
743batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
744 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200745{
746 struct hlist_head *head;
747 struct hlist_node *node;
748 struct tt_orig_list_entry *orig_entry;
749 struct tt_common_entry *tt_common_entry;
750 uint16_t flags;
751 uint8_t last_ttvn;
752
753 tt_common_entry = &tt_global_entry->common;
754
755 head = &tt_global_entry->orig_list;
756
757 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
758 flags = tt_common_entry->flags;
759 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
760 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
761 tt_global_entry->common.addr, orig_entry->ttvn,
762 orig_entry->orig_node->orig, last_ttvn,
763 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
764 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
765 }
766}
767
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200768int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769{
770 struct net_device *net_dev = (struct net_device *)seq->private;
771 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200772 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100773 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200774 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200775 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000776 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200778 uint32_t i;
779 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200781 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200782 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100783 ret = seq_printf(seq,
784 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200785 net_dev->name);
786 goto out;
787 }
788
789 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100790 ret = seq_printf(seq,
791 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200792 net_dev->name);
793 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 }
795
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200796 seq_printf(seq,
797 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200799 seq_printf(seq, " %-13s %s %-15s %s %s\n",
800 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802 for (i = 0; i < hash->size; i++) {
803 head = &hash->table[i];
804
Marek Lindner7aadf882011-02-18 12:28:09 +0000805 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100806 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000807 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100808 tt_global_entry = container_of(tt_common_entry,
809 struct tt_global_entry,
810 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200811 batadv_tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000813 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200815out:
816 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200817 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200818 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819}
820
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200821/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200822static void
823batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 struct hlist_head *head;
826 struct hlist_node *node, *safe;
827 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200828
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200829 spin_lock_bh(&tt_global_entry->list_lock);
830 head = &tt_global_entry->orig_list;
831 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
832 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200833 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200834 }
835 spin_unlock_bh(&tt_global_entry->list_lock);
836
837}
838
Sven Eckelmanna5130882012-05-16 20:23:16 +0200839static void
840batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
841 struct tt_global_entry *tt_global_entry,
842 struct orig_node *orig_node,
843 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200844{
845 struct hlist_head *head;
846 struct hlist_node *node, *safe;
847 struct tt_orig_list_entry *orig_entry;
848
849 spin_lock_bh(&tt_global_entry->list_lock);
850 head = &tt_global_entry->orig_list;
851 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
852 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200853 batadv_dbg(DBG_TT, bat_priv,
854 "Deleting %pM from global tt entry %pM: %s\n",
855 orig_node->orig,
856 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200857 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200858 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200859 }
860 }
861 spin_unlock_bh(&tt_global_entry->list_lock);
862}
863
Sven Eckelmanna5130882012-05-16 20:23:16 +0200864static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
865 struct tt_global_entry *tt_global_entry,
866 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200867{
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200868 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
869 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200870
Sven Eckelmanna5130882012-05-16 20:23:16 +0200871 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200872 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200873 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200874
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875}
876
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200877/* If the client is to be deleted, we check if it is the last origantor entry
878 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
879 * otherwise we simply remove the originator scheduled for deletion.
880 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200881static void
882batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
883 struct tt_global_entry *tt_global_entry,
884 struct orig_node *orig_node, const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200885{
886 bool last_entry = true;
887 struct hlist_head *head;
888 struct hlist_node *node;
889 struct tt_orig_list_entry *orig_entry;
890
891 /* no local entry exists, case 1:
892 * Check if this is the last one or if other entries exist.
893 */
894
895 rcu_read_lock();
896 head = &tt_global_entry->orig_list;
897 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
898 if (orig_entry->orig_node != orig_node) {
899 last_entry = false;
900 break;
901 }
902 }
903 rcu_read_unlock();
904
905 if (last_entry) {
906 /* its the last one, mark for roaming. */
907 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
908 tt_global_entry->roam_at = jiffies;
909 } else
910 /* there is another entry, we can simply delete this
911 * one and can still use the other one.
912 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200913 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
914 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200915}
916
917
918
Sven Eckelmanna5130882012-05-16 20:23:16 +0200919static void batadv_tt_global_del(struct bat_priv *bat_priv,
920 struct orig_node *orig_node,
921 const unsigned char *addr,
922 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000923{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200924 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmanna5130882012-05-16 20:23:16 +0200925 struct tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926
Sven Eckelmanna5130882012-05-16 20:23:16 +0200927 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200928 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200929 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200930
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200931 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200932 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
933 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800934
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200935 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200936 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
937 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200938
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800939 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200940 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800941
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200942 /* if we are deleting a global entry due to a roam
943 * event, there are two possibilities:
944 * 1) the client roamed from node A to node B => if there
945 * is only one originator left for this client, we mark
946 * it with TT_CLIENT_ROAM, we start a timer and we
947 * wait for node B to claim it. In case of timeout
948 * the entry is purged.
949 *
950 * If there are other originators left, we directly delete
951 * the originator.
952 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200953 * the global entry, since it is useless now.
954 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200955 local_entry = batadv_tt_local_hash_find(bat_priv,
956 tt_global_entry->common.addr);
957 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200958 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200959 batadv_tt_global_del_orig_list(tt_global_entry);
960 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200961 } else
962 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200963 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
964 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200965
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800966
Antonio Quartullicc47f662011-04-27 14:27:57 +0200967out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200968 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200969 batadv_tt_global_entry_free_ref(tt_global_entry);
970 if (local_entry)
971 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200972}
973
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200974void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
975 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200976{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200977 struct tt_global_entry *global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100978 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200979 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980 struct hashtable_t *hash = bat_priv->tt_global_hash;
981 struct hlist_node *node, *safe;
982 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200983 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200984
Simon Wunderlich6e801492011-10-19 10:28:26 +0200985 if (!hash)
986 return;
987
Antonio Quartullia73105b2011-04-27 14:27:44 +0200988 for (i = 0; i < hash->size; i++) {
989 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200990 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000991
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200992 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100993 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100994 head, hash_entry) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200995 global_entry = container_of(tt_common_entry,
996 struct tt_global_entry,
997 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200998
Sven Eckelmanna5130882012-05-16 20:23:16 +0200999 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
1000 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001001
Sven Eckelmanna5130882012-05-16 20:23:16 +02001002 if (hlist_empty(&global_entry->orig_list)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001003 batadv_dbg(DBG_TT, bat_priv,
1004 "Deleting global tt entry %pM: %s\n",
Sven Eckelmanna5130882012-05-16 20:23:16 +02001005 global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001006 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001007 batadv_tt_global_entry_free_ref(global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001008 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001009 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001010 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001012 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013}
1014
Sven Eckelmanna5130882012-05-16 20:23:16 +02001015static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001016{
1017 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001018 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001019 struct tt_global_entry *tt_global_entry;
1020 struct hlist_node *node, *node_tmp;
1021 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001022 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001023 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001024
Antonio Quartullicc47f662011-04-27 14:27:57 +02001025 for (i = 0; i < hash->size; i++) {
1026 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001027 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001028
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001029 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001030 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001031 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001032 tt_global_entry = container_of(tt_common_entry,
1033 struct tt_global_entry,
1034 common);
1035 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001036 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001037 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1038 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001039 continue;
1040
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001041 batadv_dbg(DBG_TT, bat_priv,
1042 "Deleting global tt entry (%pM): Roaming timeout\n",
1043 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001044
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001045 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001046 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001047 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001048 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001049 }
1050
Antonio Quartullicc47f662011-04-27 14:27:57 +02001051}
1052
Sven Eckelmanna5130882012-05-16 20:23:16 +02001053static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001055 struct hashtable_t *hash;
1056 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001057 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001058 struct tt_global_entry *tt_global_entry;
1059 struct hlist_node *node, *node_tmp;
1060 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001061 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001062
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001063 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 return;
1065
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001066 hash = bat_priv->tt_global_hash;
1067
1068 for (i = 0; i < hash->size; i++) {
1069 head = &hash->table[i];
1070 list_lock = &hash->list_locks[i];
1071
1072 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001073 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001074 head, hash_entry) {
1075 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001076 tt_global_entry = container_of(tt_common_entry,
1077 struct tt_global_entry,
1078 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001079 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001080 }
1081 spin_unlock_bh(list_lock);
1082 }
1083
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001084 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001085
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001086 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087}
1088
Sven Eckelmanna5130882012-05-16 20:23:16 +02001089static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1090 struct tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001091{
1092 bool ret = false;
1093
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001094 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1095 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001096 ret = true;
1097
1098 return ret;
1099}
1100
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001101struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1102 const uint8_t *src,
1103 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001105 struct tt_local_entry *tt_local_entry = NULL;
1106 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001107 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001108 struct neigh_node *router = NULL;
1109 struct hlist_head *head;
1110 struct hlist_node *node;
1111 struct tt_orig_list_entry *orig_entry;
1112 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001113
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001114 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001115 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001116 if (!tt_local_entry)
1117 goto out;
1118 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001119
Sven Eckelmanna5130882012-05-16 20:23:16 +02001120 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001121 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001122 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001124 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001125 * isolation
1126 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001127 if (tt_local_entry &&
1128 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001129 goto out;
1130
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001131 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001132
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001133 rcu_read_lock();
1134 head = &tt_global_entry->orig_list;
1135 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001136 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001137 if (!router)
1138 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001139
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001140 if (router->tq_avg > best_tq) {
1141 orig_node = orig_entry->orig_node;
1142 best_tq = router->tq_avg;
1143 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001144 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001145 }
1146 /* found anything? */
1147 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1148 orig_node = NULL;
1149 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001150out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001151 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001152 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001153 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001154 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001155
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001156 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001157}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001158
1159/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001160static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1161 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001162{
1163 uint16_t total = 0, total_one;
1164 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001165 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001166 struct tt_global_entry *tt_global_entry;
1167 struct hlist_node *node;
1168 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001169 uint32_t i;
1170 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001171
1172 for (i = 0; i < hash->size; i++) {
1173 head = &hash->table[i];
1174
1175 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001176 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001177 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001178 tt_global_entry = container_of(tt_common_entry,
1179 struct tt_global_entry,
1180 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181 /* Roaming clients are in the global table for
1182 * consistency only. They don't have to be
1183 * taken into account while computing the
1184 * global crc
1185 */
1186 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1187 continue;
1188
1189 /* find out if this global entry is announced by this
1190 * originator
1191 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001192 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1193 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001194 continue;
1195
1196 total_one = 0;
1197 for (j = 0; j < ETH_ALEN; j++)
1198 total_one = crc16_byte(total_one,
1199 tt_global_entry->common.addr[j]);
1200 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001201 }
1202 rcu_read_unlock();
1203 }
1204
1205 return total;
1206}
1207
1208/* Calculates the checksum of the local table */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001209static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001210{
1211 uint16_t total = 0, total_one;
1212 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001213 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001214 struct hlist_node *node;
1215 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001216 uint32_t i;
1217 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001218
1219 for (i = 0; i < hash->size; i++) {
1220 head = &hash->table[i];
1221
1222 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001223 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001224 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001225 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001226 * account while computing the CRC
1227 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001228 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001229 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230 total_one = 0;
1231 for (j = 0; j < ETH_ALEN; j++)
1232 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001233 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001234 total ^= total_one;
1235 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001236 rcu_read_unlock();
1237 }
1238
1239 return total;
1240}
1241
Sven Eckelmanna5130882012-05-16 20:23:16 +02001242static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001243{
1244 struct tt_req_node *node, *safe;
1245
1246 spin_lock_bh(&bat_priv->tt_req_list_lock);
1247
1248 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1249 list_del(&node->list);
1250 kfree(node);
1251 }
1252
1253 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1254}
1255
Sven Eckelmanna5130882012-05-16 20:23:16 +02001256static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1257 struct orig_node *orig_node,
1258 const unsigned char *tt_buff,
1259 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001261 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262
1263 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001264 * last OGM (the OGM could carry no changes)
1265 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001266 spin_lock_bh(&orig_node->tt_buff_lock);
1267 if (tt_buff_len > 0) {
1268 kfree(orig_node->tt_buff);
1269 orig_node->tt_buff_len = 0;
1270 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1271 if (orig_node->tt_buff) {
1272 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1273 orig_node->tt_buff_len = tt_buff_len;
1274 }
1275 }
1276 spin_unlock_bh(&orig_node->tt_buff_lock);
1277}
1278
Sven Eckelmanna5130882012-05-16 20:23:16 +02001279static void batadv_tt_req_purge(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001280{
1281 struct tt_req_node *node, *safe;
1282
1283 spin_lock_bh(&bat_priv->tt_req_list_lock);
1284 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001285 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001286 list_del(&node->list);
1287 kfree(node);
1288 }
1289 }
1290 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1291}
1292
1293/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001294 * has already been issued for this orig_node, NULL otherwise
1295 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001296static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1297 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001298{
1299 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1300
1301 spin_lock_bh(&bat_priv->tt_req_list_lock);
1302 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001303 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1304 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1305 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001306 goto unlock;
1307 }
1308
1309 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1310 if (!tt_req_node)
1311 goto unlock;
1312
1313 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1314 tt_req_node->issued_at = jiffies;
1315
1316 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1317unlock:
1318 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1319 return tt_req_node;
1320}
1321
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001322/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001323static int batadv_tt_local_valid_entry(const void *entry_ptr,
1324 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001325{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001326 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001327
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001328 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001329 return 0;
1330 return 1;
1331}
1332
Sven Eckelmanna5130882012-05-16 20:23:16 +02001333static int batadv_tt_global_valid(const void *entry_ptr,
1334 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001335{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001336 const struct tt_common_entry *tt_common_entry = entry_ptr;
1337 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001338 const struct orig_node *orig_node = data_ptr;
1339
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001340 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001341 return 0;
1342
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001343 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1344 common);
1345
Sven Eckelmanna5130882012-05-16 20:23:16 +02001346 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001347}
1348
Sven Eckelmanna5130882012-05-16 20:23:16 +02001349static struct sk_buff *
1350batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1351 struct hashtable_t *hash,
1352 struct hard_iface *primary_if,
1353 int (*valid_cb)(const void *, const void *),
1354 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001355{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001356 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001357 struct tt_query_packet *tt_response;
1358 struct tt_change *tt_change;
1359 struct hlist_node *node;
1360 struct hlist_head *head;
1361 struct sk_buff *skb = NULL;
1362 uint16_t tt_tot, tt_count;
1363 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001364 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001365
1366 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1367 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1368 tt_len -= tt_len % sizeof(struct tt_change);
1369 }
1370 tt_tot = tt_len / sizeof(struct tt_change);
1371
1372 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1373 if (!skb)
1374 goto out;
1375
1376 skb_reserve(skb, ETH_HLEN);
1377 tt_response = (struct tt_query_packet *)skb_put(skb,
1378 tt_query_size + tt_len);
1379 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001380
1381 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1382 tt_count = 0;
1383
1384 rcu_read_lock();
1385 for (i = 0; i < hash->size; i++) {
1386 head = &hash->table[i];
1387
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001388 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001389 head, hash_entry) {
1390 if (tt_count == tt_tot)
1391 break;
1392
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001393 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394 continue;
1395
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001396 memcpy(tt_change->addr, tt_common_entry->addr,
1397 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398 tt_change->flags = NO_FLAGS;
1399
1400 tt_count++;
1401 tt_change++;
1402 }
1403 }
1404 rcu_read_unlock();
1405
Antonio Quartulli9d852392011-10-17 14:25:13 +02001406 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001407 * copied
1408 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001409 tt_response->tt_data = htons(tt_count);
1410
Antonio Quartullia73105b2011-04-27 14:27:44 +02001411out:
1412 return skb;
1413}
1414
Sven Eckelmanna5130882012-05-16 20:23:16 +02001415static int batadv_send_tt_request(struct bat_priv *bat_priv,
1416 struct orig_node *dst_orig_node,
1417 uint8_t ttvn, uint16_t tt_crc,
1418 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001419{
1420 struct sk_buff *skb = NULL;
1421 struct tt_query_packet *tt_request;
1422 struct neigh_node *neigh_node = NULL;
1423 struct hard_iface *primary_if;
1424 struct tt_req_node *tt_req_node = NULL;
1425 int ret = 1;
1426
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001427 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001428 if (!primary_if)
1429 goto out;
1430
1431 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001432 * reply from the same orig_node yet
1433 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001434 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435 if (!tt_req_node)
1436 goto out;
1437
1438 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1439 if (!skb)
1440 goto out;
1441
1442 skb_reserve(skb, ETH_HLEN);
1443
1444 tt_request = (struct tt_query_packet *)skb_put(skb,
1445 sizeof(struct tt_query_packet));
1446
Sven Eckelmann76543d12011-11-20 15:47:38 +01001447 tt_request->header.packet_type = BAT_TT_QUERY;
1448 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001449 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1450 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001451 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001453 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454 tt_request->flags = TT_REQUEST;
1455
1456 if (full_table)
1457 tt_request->flags |= TT_FULL_TABLE;
1458
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001459 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001460 if (!neigh_node)
1461 goto out;
1462
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001463 batadv_dbg(DBG_TT, bat_priv,
1464 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1465 dst_orig_node->orig, neigh_node->addr,
1466 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467
Martin Hundebøllf8214862012-04-20 17:02:45 +02001468 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1469
Sven Eckelmann9455e342012-05-12 02:09:37 +02001470 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 ret = 0;
1472
1473out:
1474 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001475 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001477 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478 if (ret)
1479 kfree_skb(skb);
1480 if (ret && tt_req_node) {
1481 spin_lock_bh(&bat_priv->tt_req_list_lock);
1482 list_del(&tt_req_node->list);
1483 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1484 kfree(tt_req_node);
1485 }
1486 return ret;
1487}
1488
Sven Eckelmanna5130882012-05-16 20:23:16 +02001489static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1490 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491{
1492 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1493 struct neigh_node *neigh_node = NULL;
1494 struct hard_iface *primary_if = NULL;
1495 uint8_t orig_ttvn, req_ttvn, ttvn;
1496 int ret = false;
1497 unsigned char *tt_buff;
1498 bool full_table;
1499 uint16_t tt_len, tt_tot;
1500 struct sk_buff *skb = NULL;
1501 struct tt_query_packet *tt_response;
1502
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001503 batadv_dbg(DBG_TT, bat_priv,
1504 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1505 tt_request->src, tt_request->ttvn, tt_request->dst,
1506 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507
1508 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001509 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510 if (!req_dst_orig_node)
1511 goto out;
1512
Sven Eckelmannda641192012-05-12 13:48:56 +02001513 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001514 if (!res_dst_orig_node)
1515 goto out;
1516
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001517 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518 if (!neigh_node)
1519 goto out;
1520
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001521 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001522 if (!primary_if)
1523 goto out;
1524
1525 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1526 req_ttvn = tt_request->ttvn;
1527
Antonio Quartulli015758d2011-07-09 17:52:13 +02001528 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001530 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001531 goto out;
1532
Antonio Quartulli015758d2011-07-09 17:52:13 +02001533 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001534 if (tt_request->flags & TT_FULL_TABLE ||
1535 !req_dst_orig_node->tt_buff)
1536 full_table = true;
1537 else
1538 full_table = false;
1539
1540 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001541 * I'll send only one packet with as much TT entries as I can
1542 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001543 if (!full_table) {
1544 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1545 tt_len = req_dst_orig_node->tt_buff_len;
1546 tt_tot = tt_len / sizeof(struct tt_change);
1547
1548 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1549 tt_len + ETH_HLEN);
1550 if (!skb)
1551 goto unlock;
1552
1553 skb_reserve(skb, ETH_HLEN);
1554 tt_response = (struct tt_query_packet *)skb_put(skb,
1555 sizeof(struct tt_query_packet) + tt_len);
1556 tt_response->ttvn = req_ttvn;
1557 tt_response->tt_data = htons(tt_tot);
1558
1559 tt_buff = skb->data + sizeof(struct tt_query_packet);
1560 /* Copy the last orig_node's OGM buffer */
1561 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1562 req_dst_orig_node->tt_buff_len);
1563
1564 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1565 } else {
1566 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1567 sizeof(struct tt_change);
1568 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1569
Sven Eckelmanna5130882012-05-16 20:23:16 +02001570 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1571 bat_priv->tt_global_hash,
1572 primary_if,
1573 batadv_tt_global_valid,
1574 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575 if (!skb)
1576 goto out;
1577
1578 tt_response = (struct tt_query_packet *)skb->data;
1579 }
1580
Sven Eckelmann76543d12011-11-20 15:47:38 +01001581 tt_response->header.packet_type = BAT_TT_QUERY;
1582 tt_response->header.version = COMPAT_VERSION;
1583 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001584 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1585 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1586 tt_response->flags = TT_RESPONSE;
1587
1588 if (full_table)
1589 tt_response->flags |= TT_FULL_TABLE;
1590
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001591 batadv_dbg(DBG_TT, bat_priv,
1592 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1593 res_dst_orig_node->orig, neigh_node->addr,
1594 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595
Martin Hundebøllf8214862012-04-20 17:02:45 +02001596 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1597
Sven Eckelmann9455e342012-05-12 02:09:37 +02001598 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599 ret = true;
1600 goto out;
1601
1602unlock:
1603 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1604
1605out:
1606 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001607 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001609 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001611 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001612 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001613 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614 if (!ret)
1615 kfree_skb(skb);
1616 return ret;
1617
1618}
Sven Eckelmanna5130882012-05-16 20:23:16 +02001619static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1620 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621{
1622 struct orig_node *orig_node = NULL;
1623 struct neigh_node *neigh_node = NULL;
1624 struct hard_iface *primary_if = NULL;
1625 uint8_t my_ttvn, req_ttvn, ttvn;
1626 int ret = false;
1627 unsigned char *tt_buff;
1628 bool full_table;
1629 uint16_t tt_len, tt_tot;
1630 struct sk_buff *skb = NULL;
1631 struct tt_query_packet *tt_response;
1632
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001633 batadv_dbg(DBG_TT, bat_priv,
1634 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1635 tt_request->src, tt_request->ttvn,
1636 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637
1638
1639 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1640 req_ttvn = tt_request->ttvn;
1641
Sven Eckelmannda641192012-05-12 13:48:56 +02001642 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643 if (!orig_node)
1644 goto out;
1645
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001646 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001647 if (!neigh_node)
1648 goto out;
1649
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001650 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651 if (!primary_if)
1652 goto out;
1653
1654 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001655 * is too big send the whole local translation table
1656 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001657 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1658 !bat_priv->tt_buff)
1659 full_table = true;
1660 else
1661 full_table = false;
1662
1663 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001664 * I'll send only one packet with as much TT entries as I can
1665 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666 if (!full_table) {
1667 spin_lock_bh(&bat_priv->tt_buff_lock);
1668 tt_len = bat_priv->tt_buff_len;
1669 tt_tot = tt_len / sizeof(struct tt_change);
1670
1671 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1672 tt_len + ETH_HLEN);
1673 if (!skb)
1674 goto unlock;
1675
1676 skb_reserve(skb, ETH_HLEN);
1677 tt_response = (struct tt_query_packet *)skb_put(skb,
1678 sizeof(struct tt_query_packet) + tt_len);
1679 tt_response->ttvn = req_ttvn;
1680 tt_response->tt_data = htons(tt_tot);
1681
1682 tt_buff = skb->data + sizeof(struct tt_query_packet);
1683 memcpy(tt_buff, bat_priv->tt_buff,
1684 bat_priv->tt_buff_len);
1685 spin_unlock_bh(&bat_priv->tt_buff_lock);
1686 } else {
1687 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1688 sizeof(struct tt_change);
1689 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1690
Sven Eckelmanna5130882012-05-16 20:23:16 +02001691 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1692 bat_priv->tt_local_hash,
1693 primary_if,
1694 batadv_tt_local_valid_entry,
1695 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696 if (!skb)
1697 goto out;
1698
1699 tt_response = (struct tt_query_packet *)skb->data;
1700 }
1701
Sven Eckelmann76543d12011-11-20 15:47:38 +01001702 tt_response->header.packet_type = BAT_TT_QUERY;
1703 tt_response->header.version = COMPAT_VERSION;
1704 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1706 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1707 tt_response->flags = TT_RESPONSE;
1708
1709 if (full_table)
1710 tt_response->flags |= TT_FULL_TABLE;
1711
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001712 batadv_dbg(DBG_TT, bat_priv,
1713 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1714 orig_node->orig, neigh_node->addr,
1715 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716
Martin Hundebøllf8214862012-04-20 17:02:45 +02001717 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1718
Sven Eckelmann9455e342012-05-12 02:09:37 +02001719 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001720 ret = true;
1721 goto out;
1722
1723unlock:
1724 spin_unlock_bh(&bat_priv->tt_buff_lock);
1725out:
1726 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001727 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001729 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001731 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001732 if (!ret)
1733 kfree_skb(skb);
1734 /* This packet was for me, so it doesn't need to be re-routed */
1735 return true;
1736}
1737
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001738bool batadv_send_tt_response(struct bat_priv *bat_priv,
1739 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001740{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001741 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001742 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001743 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001744 return true;
1745
Sven Eckelmanna5130882012-05-16 20:23:16 +02001746 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001747 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001748 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001749 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750}
1751
Sven Eckelmanna5130882012-05-16 20:23:16 +02001752static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1753 struct orig_node *orig_node,
1754 struct tt_change *tt_change,
1755 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001756{
1757 int i;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001758 int is_wifi;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001759 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001760
1761 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001762 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001763 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1764 batadv_tt_global_del(bat_priv, orig_node,
1765 (tt_change + i)->addr,
1766 "tt removed by changes",
1767 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001768 } else {
1769 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1770 if (!batadv_tt_global_add(bat_priv, orig_node,
1771 (tt_change + i)->addr, ttvn,
1772 false, is_wifi))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001773 /* In case of problem while storing a
1774 * global_entry, we stop the updating
1775 * procedure without committing the
1776 * ttvn change. This will avoid to send
1777 * corrupted data on tt_request
1778 */
1779 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001780 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001781 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001782 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783}
1784
Sven Eckelmanna5130882012-05-16 20:23:16 +02001785static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1786 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001787{
1788 struct orig_node *orig_node = NULL;
1789
Sven Eckelmannda641192012-05-12 13:48:56 +02001790 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 if (!orig_node)
1792 goto out;
1793
1794 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001795 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796
Sven Eckelmanna5130882012-05-16 20:23:16 +02001797 _batadv_tt_update_changes(bat_priv, orig_node,
1798 (struct tt_change *)(tt_response + 1),
1799 ntohs(tt_response->tt_data),
1800 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801
1802 spin_lock_bh(&orig_node->tt_buff_lock);
1803 kfree(orig_node->tt_buff);
1804 orig_node->tt_buff_len = 0;
1805 orig_node->tt_buff = NULL;
1806 spin_unlock_bh(&orig_node->tt_buff_lock);
1807
1808 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1809
1810out:
1811 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001812 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813}
1814
Sven Eckelmanna5130882012-05-16 20:23:16 +02001815static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1816 struct orig_node *orig_node,
1817 uint16_t tt_num_changes, uint8_t ttvn,
1818 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001819{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001820 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1821 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001822
Sven Eckelmanna5130882012-05-16 20:23:16 +02001823 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1824 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825 atomic_set(&orig_node->last_ttvn, ttvn);
1826}
1827
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001828bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001830 struct tt_local_entry *tt_local_entry = NULL;
1831 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832
Sven Eckelmanna5130882012-05-16 20:23:16 +02001833 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001834 if (!tt_local_entry)
1835 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001836 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001837 * consistency purpose)
1838 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001839 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001840 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001841 ret = true;
1842out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001844 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001845 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001846}
1847
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001848void batadv_handle_tt_response(struct bat_priv *bat_priv,
1849 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001850{
1851 struct tt_req_node *node, *safe;
1852 struct orig_node *orig_node = NULL;
1853
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001854 batadv_dbg(DBG_TT, bat_priv,
1855 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1856 tt_response->src, tt_response->ttvn,
1857 ntohs(tt_response->tt_data),
1858 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001860 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001861 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001862 goto out;
1863
Sven Eckelmannda641192012-05-12 13:48:56 +02001864 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865 if (!orig_node)
1866 goto out;
1867
1868 if (tt_response->flags & TT_FULL_TABLE)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001869 batadv_tt_fill_gtable(bat_priv, tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001870 else
Sven Eckelmanna5130882012-05-16 20:23:16 +02001871 batadv_tt_update_changes(bat_priv, orig_node,
1872 ntohs(tt_response->tt_data),
1873 tt_response->ttvn,
1874 (struct tt_change *)(tt_response + 1));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875
1876 /* Delete the tt_req_node from pending tt_requests list */
1877 spin_lock_bh(&bat_priv->tt_req_list_lock);
1878 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001879 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001880 continue;
1881 list_del(&node->list);
1882 kfree(node);
1883 }
1884 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1885
1886 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001887 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001888 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001889 * unset the flag
1890 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001891 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892out:
1893 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001894 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895}
1896
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001897int batadv_tt_init(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001899 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001900
Sven Eckelmanna5130882012-05-16 20:23:16 +02001901 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001902 if (ret < 0)
1903 return ret;
1904
Sven Eckelmanna5130882012-05-16 20:23:16 +02001905 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001906 if (ret < 0)
1907 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908
Sven Eckelmanna5130882012-05-16 20:23:16 +02001909 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001910
1911 return 1;
1912}
1913
Sven Eckelmanna5130882012-05-16 20:23:16 +02001914static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001916 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001917
Antonio Quartullicc47f662011-04-27 14:27:57 +02001918 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001919
Antonio Quartullicc47f662011-04-27 14:27:57 +02001920 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1921 list_del(&node->list);
1922 kfree(node);
1923 }
1924
1925 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1926}
1927
Sven Eckelmanna5130882012-05-16 20:23:16 +02001928static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001929{
1930 struct tt_roam_node *node, *safe;
1931
1932 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1933 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001934 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001935 continue;
1936
1937 list_del(&node->list);
1938 kfree(node);
1939 }
1940 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1941}
1942
1943/* This function checks whether the client already reached the
1944 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1945 * will not be sent.
1946 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001947 * returns true if the ROAMING_ADV can be sent, false otherwise
1948 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001949static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1950 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001951{
1952 struct tt_roam_node *tt_roam_node;
1953 bool ret = false;
1954
1955 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1956 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001957 * reply from the same orig_node yet
1958 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001959 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001960 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001961 continue;
1962
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001963 if (batadv_has_timed_out(tt_roam_node->first_time,
1964 ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001965 continue;
1966
Sven Eckelmann3e348192012-05-16 20:23:22 +02001967 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001968 /* Sorry, you roamed too many times! */
1969 goto unlock;
1970 ret = true;
1971 break;
1972 }
1973
1974 if (!ret) {
1975 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1976 if (!tt_roam_node)
1977 goto unlock;
1978
1979 tt_roam_node->first_time = jiffies;
1980 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1981 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1982
1983 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1984 ret = true;
1985 }
1986
1987unlock:
1988 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1989 return ret;
1990}
1991
Sven Eckelmanna5130882012-05-16 20:23:16 +02001992static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1993 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001994{
1995 struct neigh_node *neigh_node = NULL;
1996 struct sk_buff *skb = NULL;
1997 struct roam_adv_packet *roam_adv_packet;
1998 int ret = 1;
1999 struct hard_iface *primary_if;
2000
2001 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002002 * already roamed to us too many times
2003 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002004 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002005 goto out;
2006
2007 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
2008 if (!skb)
2009 goto out;
2010
2011 skb_reserve(skb, ETH_HLEN);
2012
2013 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
2014 sizeof(struct roam_adv_packet));
2015
Sven Eckelmann76543d12011-11-20 15:47:38 +01002016 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
2017 roam_adv_packet->header.version = COMPAT_VERSION;
2018 roam_adv_packet->header.ttl = TTL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002019 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002020 if (!primary_if)
2021 goto out;
2022 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002023 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002024 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2025 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2026
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002027 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028 if (!neigh_node)
2029 goto out;
2030
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002031 batadv_dbg(DBG_TT, bat_priv,
2032 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2033 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002034
Martin Hundebøllf8214862012-04-20 17:02:45 +02002035 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2036
Sven Eckelmann9455e342012-05-12 02:09:37 +02002037 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038 ret = 0;
2039
2040out:
2041 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002042 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002043 if (ret)
2044 kfree_skb(skb);
2045 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002046}
2047
Sven Eckelmanna5130882012-05-16 20:23:16 +02002048static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002049{
2050 struct delayed_work *delayed_work =
2051 container_of(work, struct delayed_work, work);
2052 struct bat_priv *bat_priv =
2053 container_of(delayed_work, struct bat_priv, tt_work);
2054
Sven Eckelmanna5130882012-05-16 20:23:16 +02002055 batadv_tt_local_purge(bat_priv);
2056 batadv_tt_global_roam_purge(bat_priv);
2057 batadv_tt_req_purge(bat_priv);
2058 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059
Sven Eckelmanna5130882012-05-16 20:23:16 +02002060 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002062
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002063void batadv_tt_free(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002064{
2065 cancel_delayed_work_sync(&bat_priv->tt_work);
2066
Sven Eckelmanna5130882012-05-16 20:23:16 +02002067 batadv_tt_local_table_free(bat_priv);
2068 batadv_tt_global_table_free(bat_priv);
2069 batadv_tt_req_list_free(bat_priv);
2070 batadv_tt_changes_list_free(bat_priv);
2071 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002072
2073 kfree(bat_priv->tt_buff);
2074}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002075
Antonio Quartulli697f2532011-11-07 16:47:01 +01002076/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002077 * in the given hash table and returns the number of modified entries
2078 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002079static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2080 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002081{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002082 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002083 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002084 struct hlist_head *head;
2085 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002086 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002087
2088 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002089 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002090
2091 for (i = 0; i < hash->size; i++) {
2092 head = &hash->table[i];
2093
2094 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002095 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002096 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002097 if (enable) {
2098 if ((tt_common_entry->flags & flags) == flags)
2099 continue;
2100 tt_common_entry->flags |= flags;
2101 } else {
2102 if (!(tt_common_entry->flags & flags))
2103 continue;
2104 tt_common_entry->flags &= ~flags;
2105 }
2106 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002107 }
2108 rcu_read_unlock();
2109 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002110out:
2111 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002112}
2113
2114/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002115static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002116{
2117 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002118 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002119 struct tt_local_entry *tt_local_entry;
2120 struct hlist_node *node, *node_tmp;
2121 struct hlist_head *head;
2122 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002123 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002124
2125 if (!hash)
2126 return;
2127
2128 for (i = 0; i < hash->size; i++) {
2129 head = &hash->table[i];
2130 list_lock = &hash->list_locks[i];
2131
2132 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002133 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002134 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002135 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002136 continue;
2137
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002138 batadv_dbg(DBG_TT, bat_priv,
2139 "Deleting local tt entry (%pM): pending\n",
2140 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002141
2142 atomic_dec(&bat_priv->num_local_tt);
2143 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002144 tt_local_entry = container_of(tt_common_entry,
2145 struct tt_local_entry,
2146 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002147 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002148 }
2149 spin_unlock_bh(list_lock);
2150 }
2151
2152}
2153
Sven Eckelmanna5130882012-05-16 20:23:16 +02002154static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2155 unsigned char **packet_buff,
2156 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002157{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002158 uint16_t changed_num = 0;
2159
2160 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2161 return -ENOENT;
2162
Sven Eckelmanna5130882012-05-16 20:23:16 +02002163 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2164 TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002165
2166 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002167 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002168 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002169 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002170
2171 /* Increment the TTVN only once per OGM interval */
2172 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002173 batadv_dbg(DBG_TT, bat_priv,
2174 "Local changes committed, updating to ttvn %u\n",
2175 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002176 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002177
2178 /* reset the sending counter */
2179 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2180
Sven Eckelmanna5130882012-05-16 20:23:16 +02002181 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2182 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002183}
2184
2185/* when calling this function (hard_iface == primary_if) has to be true */
2186int batadv_tt_append_diff(struct bat_priv *bat_priv,
2187 unsigned char **packet_buff, int *packet_buff_len,
2188 int packet_min_len)
2189{
2190 int tt_num_changes;
2191
2192 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002193 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2194 packet_buff_len,
2195 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002196
2197 /* if the changes have been sent often enough */
2198 if ((tt_num_changes < 0) &&
Sven Eckelmann3e348192012-05-16 20:23:22 +02002199 (!batadv_atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002200 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2201 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002202 tt_num_changes = 0;
2203 }
2204
2205 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002206}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002207
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002208bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2209 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002210{
2211 struct tt_local_entry *tt_local_entry = NULL;
2212 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002213 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002214
2215 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002216 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002217
Sven Eckelmanna5130882012-05-16 20:23:16 +02002218 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002219 if (!tt_local_entry)
2220 goto out;
2221
Sven Eckelmanna5130882012-05-16 20:23:16 +02002222 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002223 if (!tt_global_entry)
2224 goto out;
2225
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002226 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002227 goto out;
2228
Marek Lindner5870adc2012-06-20 17:16:05 +02002229 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002230
2231out:
2232 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002233 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002234 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002235 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002236 return ret;
2237}
Marek Lindnera943cac2011-07-30 13:10:18 +02002238
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002239void batadv_tt_update_orig(struct bat_priv *bat_priv,
2240 struct orig_node *orig_node,
2241 const unsigned char *tt_buff, uint8_t tt_num_changes,
2242 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002243{
2244 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2245 bool full_table = true;
2246
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002247 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002248 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002249 return;
2250
Antonio Quartulli17071572011-11-07 16:36:40 +01002251 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002252 * increased by one -> we can apply the attached changes
2253 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002254 if ((!orig_node->tt_initialised && ttvn == 1) ||
2255 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002256 /* the OGM could not contain the changes due to their size or
2257 * because they have already been sent TT_OGM_APPEND_MAX times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002258 * In this case send a tt request
2259 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002260 if (!tt_num_changes) {
2261 full_table = false;
2262 goto request_table;
2263 }
2264
Sven Eckelmanna5130882012-05-16 20:23:16 +02002265 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2266 ttvn, (struct tt_change *)tt_buff);
Marek Lindnera943cac2011-07-30 13:10:18 +02002267
2268 /* Even if we received the precomputed crc with the OGM, we
2269 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002270 * in the global table
2271 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002272 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002273
2274 /* The ttvn alone is not enough to guarantee consistency
2275 * because a single value could represent different states
2276 * (due to the wrap around). Thus a node has to check whether
2277 * the resulting table (after applying the changes) is still
2278 * consistent or not. E.g. a node could disconnect while its
2279 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2280 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002281 * inconsistency
2282 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002283 if (orig_node->tt_crc != tt_crc)
2284 goto request_table;
2285
2286 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002287 * unset the flag
2288 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002289 orig_node->tt_poss_change = false;
2290 } else {
2291 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002292 * in sync anymore -> request fresh tt data
2293 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002294 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2295 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002296request_table:
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002297 batadv_dbg(DBG_TT, bat_priv,
2298 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2299 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2300 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002301 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2302 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002303 return;
2304 }
2305 }
2306}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002307
2308/* returns true whether we know that the client has moved from its old
2309 * originator to another one. This entry is kept is still kept for consistency
2310 * purposes
2311 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002312bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2313 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002314{
2315 struct tt_global_entry *tt_global_entry;
2316 bool ret = false;
2317
Sven Eckelmanna5130882012-05-16 20:23:16 +02002318 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002319 if (!tt_global_entry)
2320 goto out;
2321
2322 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002323 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002324out:
2325 return ret;
2326}