blob: 3ca2e48d669e7f852e84aa5b07c8319c9dbba10d [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,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200660 const unsigned char *tt_addr, uint8_t flags,
661 uint8_t ttvn)
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) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200671 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200672 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200673 goto out;
674
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200675 common = &tt_global_entry->common;
676 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200677
Antonio Quartullid4f44692012-05-25 00:00:54 +0200678 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200679 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200680 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200681
682 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
683 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200684
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200685 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200686 batadv_compare_tt,
687 batadv_choose_orig, common,
688 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100689
690 if (unlikely(hash_added != 0)) {
691 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200692 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100693 goto out_remove;
694 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200695
Sven Eckelmanna5130882012-05-16 20:23:16 +0200696 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
697 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200698 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200699 /* there is already a global entry, use this one. */
700
701 /* If there is the TT_CLIENT_ROAM flag set, there is only one
702 * originator left in the list and we previously received a
703 * delete + roaming change for this originator.
704 *
705 * We should first delete the old originator before adding the
706 * new one.
707 */
708 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200709 batadv_tt_global_del_orig_list(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200710 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
711 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200713
Sven Eckelmanna5130882012-05-16 20:23:16 +0200714 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
715 orig_node))
716 batadv_tt_global_add_orig_entry(tt_global_entry,
717 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200719
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200720 batadv_dbg(DBG_TT, bat_priv,
721 "Creating new global tt entry: %pM (via %pM)\n",
722 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200723
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100724out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200725 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200726 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200727 "global tt received", flags & TT_CLIENT_ROAM);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200728 ret = 1;
729out:
730 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200731 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200732 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733}
734
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200735/* print all orig nodes who announce the address for this global entry.
736 * it is assumed that the caller holds rcu_read_lock();
737 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200738static void
739batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
740 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741{
742 struct hlist_head *head;
743 struct hlist_node *node;
744 struct tt_orig_list_entry *orig_entry;
745 struct tt_common_entry *tt_common_entry;
746 uint16_t flags;
747 uint8_t last_ttvn;
748
749 tt_common_entry = &tt_global_entry->common;
750
751 head = &tt_global_entry->orig_list;
752
753 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
754 flags = tt_common_entry->flags;
755 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
756 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
757 tt_global_entry->common.addr, orig_entry->ttvn,
758 orig_entry->orig_node->orig, last_ttvn,
759 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
760 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
761 }
762}
763
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200764int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765{
766 struct net_device *net_dev = (struct net_device *)seq->private;
767 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200768 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100769 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200770 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200771 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000772 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200774 uint32_t i;
775 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200777 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200778 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100779 ret = seq_printf(seq,
780 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200781 net_dev->name);
782 goto out;
783 }
784
785 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100786 ret = seq_printf(seq,
787 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200788 net_dev->name);
789 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 }
791
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200792 seq_printf(seq,
793 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200795 seq_printf(seq, " %-13s %s %-15s %s %s\n",
796 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798 for (i = 0; i < hash->size; i++) {
799 head = &hash->table[i];
800
Marek Lindner7aadf882011-02-18 12:28:09 +0000801 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100802 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000803 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100804 tt_global_entry = container_of(tt_common_entry,
805 struct tt_global_entry,
806 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200807 batadv_tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000809 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200811out:
812 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200813 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200814 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815}
816
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200817/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200818static void
819batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200821 struct hlist_head *head;
822 struct hlist_node *node, *safe;
823 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200824
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 spin_lock_bh(&tt_global_entry->list_lock);
826 head = &tt_global_entry->orig_list;
827 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
828 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200829 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200830 }
831 spin_unlock_bh(&tt_global_entry->list_lock);
832
833}
834
Sven Eckelmanna5130882012-05-16 20:23:16 +0200835static void
836batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
837 struct tt_global_entry *tt_global_entry,
838 struct orig_node *orig_node,
839 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200840{
841 struct hlist_head *head;
842 struct hlist_node *node, *safe;
843 struct tt_orig_list_entry *orig_entry;
844
845 spin_lock_bh(&tt_global_entry->list_lock);
846 head = &tt_global_entry->orig_list;
847 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
848 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200849 batadv_dbg(DBG_TT, bat_priv,
850 "Deleting %pM from global tt entry %pM: %s\n",
851 orig_node->orig,
852 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200853 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200854 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200855 }
856 }
857 spin_unlock_bh(&tt_global_entry->list_lock);
858}
859
Sven Eckelmanna5130882012-05-16 20:23:16 +0200860static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
861 struct tt_global_entry *tt_global_entry,
862 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200863{
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200864 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
865 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200866
Sven Eckelmanna5130882012-05-16 20:23:16 +0200867 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200868 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200869 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200870
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871}
872
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200873/* If the client is to be deleted, we check if it is the last origantor entry
874 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
875 * otherwise we simply remove the originator scheduled for deletion.
876 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200877static void
878batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
879 struct tt_global_entry *tt_global_entry,
880 struct orig_node *orig_node, const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200881{
882 bool last_entry = true;
883 struct hlist_head *head;
884 struct hlist_node *node;
885 struct tt_orig_list_entry *orig_entry;
886
887 /* no local entry exists, case 1:
888 * Check if this is the last one or if other entries exist.
889 */
890
891 rcu_read_lock();
892 head = &tt_global_entry->orig_list;
893 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
894 if (orig_entry->orig_node != orig_node) {
895 last_entry = false;
896 break;
897 }
898 }
899 rcu_read_unlock();
900
901 if (last_entry) {
902 /* its the last one, mark for roaming. */
903 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
904 tt_global_entry->roam_at = jiffies;
905 } else
906 /* there is another entry, we can simply delete this
907 * one and can still use the other one.
908 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200909 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
910 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200911}
912
913
914
Sven Eckelmanna5130882012-05-16 20:23:16 +0200915static void batadv_tt_global_del(struct bat_priv *bat_priv,
916 struct orig_node *orig_node,
917 const unsigned char *addr,
918 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200920 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmanna5130882012-05-16 20:23:16 +0200921 struct tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922
Sven Eckelmanna5130882012-05-16 20:23:16 +0200923 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200924 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200925 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200926
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200927 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200928 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
929 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800930
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200931 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200932 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
933 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200934
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800935 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200936 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800937
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200938 /* if we are deleting a global entry due to a roam
939 * event, there are two possibilities:
940 * 1) the client roamed from node A to node B => if there
941 * is only one originator left for this client, we mark
942 * it with TT_CLIENT_ROAM, we start a timer and we
943 * wait for node B to claim it. In case of timeout
944 * the entry is purged.
945 *
946 * If there are other originators left, we directly delete
947 * the originator.
948 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200949 * the global entry, since it is useless now.
950 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200951 local_entry = batadv_tt_local_hash_find(bat_priv,
952 tt_global_entry->common.addr);
953 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200954 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200955 batadv_tt_global_del_orig_list(tt_global_entry);
956 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200957 } else
958 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200959 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
960 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200961
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800962
Antonio Quartullicc47f662011-04-27 14:27:57 +0200963out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200964 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200965 batadv_tt_global_entry_free_ref(tt_global_entry);
966 if (local_entry)
967 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200968}
969
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200970void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
971 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200972{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973 struct tt_global_entry *global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100974 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200975 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200976 struct hashtable_t *hash = bat_priv->tt_global_hash;
977 struct hlist_node *node, *safe;
978 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200979 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980
Simon Wunderlich6e801492011-10-19 10:28:26 +0200981 if (!hash)
982 return;
983
Antonio Quartullia73105b2011-04-27 14:27:44 +0200984 for (i = 0; i < hash->size; i++) {
985 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200986 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000987
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200988 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100989 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100990 head, hash_entry) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200991 global_entry = container_of(tt_common_entry,
992 struct tt_global_entry,
993 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200994
Sven Eckelmanna5130882012-05-16 20:23:16 +0200995 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
996 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200997
Sven Eckelmanna5130882012-05-16 20:23:16 +0200998 if (hlist_empty(&global_entry->orig_list)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200999 batadv_dbg(DBG_TT, bat_priv,
1000 "Deleting global tt entry %pM: %s\n",
Sven Eckelmanna5130882012-05-16 20:23:16 +02001001 global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001002 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001003 batadv_tt_global_entry_free_ref(global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001004 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001005 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001006 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001007 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001008 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009}
1010
Sven Eckelmanna5130882012-05-16 20:23:16 +02001011static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001012{
1013 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001014 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001015 struct tt_global_entry *tt_global_entry;
1016 struct hlist_node *node, *node_tmp;
1017 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001018 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001019 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001020
Antonio Quartullicc47f662011-04-27 14:27:57 +02001021 for (i = 0; i < hash->size; i++) {
1022 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001023 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001024
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001025 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001026 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001027 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001028 tt_global_entry = container_of(tt_common_entry,
1029 struct tt_global_entry,
1030 common);
1031 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001032 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001033 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1034 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001035 continue;
1036
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001037 batadv_dbg(DBG_TT, bat_priv,
1038 "Deleting global tt entry (%pM): Roaming timeout\n",
1039 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001040
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001041 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001042 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001043 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001044 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001045 }
1046
Antonio Quartullicc47f662011-04-27 14:27:57 +02001047}
1048
Sven Eckelmanna5130882012-05-16 20:23:16 +02001049static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001051 struct hashtable_t *hash;
1052 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001053 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001054 struct tt_global_entry *tt_global_entry;
1055 struct hlist_node *node, *node_tmp;
1056 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001057 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001058
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001059 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060 return;
1061
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001062 hash = bat_priv->tt_global_hash;
1063
1064 for (i = 0; i < hash->size; i++) {
1065 head = &hash->table[i];
1066 list_lock = &hash->list_locks[i];
1067
1068 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001069 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001070 head, hash_entry) {
1071 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001072 tt_global_entry = container_of(tt_common_entry,
1073 struct tt_global_entry,
1074 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001075 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001076 }
1077 spin_unlock_bh(list_lock);
1078 }
1079
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001080 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001081
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001082 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083}
1084
Sven Eckelmanna5130882012-05-16 20:23:16 +02001085static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1086 struct tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001087{
1088 bool ret = false;
1089
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001090 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1091 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001092 ret = true;
1093
1094 return ret;
1095}
1096
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001097struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1098 const uint8_t *src,
1099 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001100{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001101 struct tt_local_entry *tt_local_entry = NULL;
1102 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001103 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001104 struct neigh_node *router = NULL;
1105 struct hlist_head *head;
1106 struct hlist_node *node;
1107 struct tt_orig_list_entry *orig_entry;
1108 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001109
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001110 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001111 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001112 if (!tt_local_entry)
1113 goto out;
1114 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001115
Sven Eckelmanna5130882012-05-16 20:23:16 +02001116 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001117 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001118 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001119
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001120 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001121 * isolation
1122 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001123 if (tt_local_entry &&
1124 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001125 goto out;
1126
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001127 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001128
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001129 rcu_read_lock();
1130 head = &tt_global_entry->orig_list;
1131 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001132 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001133 if (!router)
1134 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001135
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001136 if (router->tq_avg > best_tq) {
1137 orig_node = orig_entry->orig_node;
1138 best_tq = router->tq_avg;
1139 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001140 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001141 }
1142 /* found anything? */
1143 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1144 orig_node = NULL;
1145 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001146out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001147 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001148 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001149 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001150 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001151
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001152 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001153}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001154
1155/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001156static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1157 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001158{
1159 uint16_t total = 0, total_one;
1160 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001161 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001162 struct tt_global_entry *tt_global_entry;
1163 struct hlist_node *node;
1164 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001165 uint32_t i;
1166 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167
1168 for (i = 0; i < hash->size; i++) {
1169 head = &hash->table[i];
1170
1171 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001172 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001174 tt_global_entry = container_of(tt_common_entry,
1175 struct tt_global_entry,
1176 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001177 /* Roaming clients are in the global table for
1178 * consistency only. They don't have to be
1179 * taken into account while computing the
1180 * global crc
1181 */
1182 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1183 continue;
1184
1185 /* find out if this global entry is announced by this
1186 * originator
1187 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001188 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1189 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001190 continue;
1191
1192 total_one = 0;
1193 for (j = 0; j < ETH_ALEN; j++)
1194 total_one = crc16_byte(total_one,
1195 tt_global_entry->common.addr[j]);
1196 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001197 }
1198 rcu_read_unlock();
1199 }
1200
1201 return total;
1202}
1203
1204/* Calculates the checksum of the local table */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001205static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001206{
1207 uint16_t total = 0, total_one;
1208 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001209 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001210 struct hlist_node *node;
1211 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001212 uint32_t i;
1213 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001214
1215 for (i = 0; i < hash->size; i++) {
1216 head = &hash->table[i];
1217
1218 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001219 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001220 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001221 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001222 * account while computing the CRC
1223 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001224 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001225 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226 total_one = 0;
1227 for (j = 0; j < ETH_ALEN; j++)
1228 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001229 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230 total ^= total_one;
1231 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 rcu_read_unlock();
1233 }
1234
1235 return total;
1236}
1237
Sven Eckelmanna5130882012-05-16 20:23:16 +02001238static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001239{
1240 struct tt_req_node *node, *safe;
1241
1242 spin_lock_bh(&bat_priv->tt_req_list_lock);
1243
1244 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1245 list_del(&node->list);
1246 kfree(node);
1247 }
1248
1249 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1250}
1251
Sven Eckelmanna5130882012-05-16 20:23:16 +02001252static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1253 struct orig_node *orig_node,
1254 const unsigned char *tt_buff,
1255 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001256{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001257 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001258
1259 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001260 * last OGM (the OGM could carry no changes)
1261 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262 spin_lock_bh(&orig_node->tt_buff_lock);
1263 if (tt_buff_len > 0) {
1264 kfree(orig_node->tt_buff);
1265 orig_node->tt_buff_len = 0;
1266 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1267 if (orig_node->tt_buff) {
1268 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1269 orig_node->tt_buff_len = tt_buff_len;
1270 }
1271 }
1272 spin_unlock_bh(&orig_node->tt_buff_lock);
1273}
1274
Sven Eckelmanna5130882012-05-16 20:23:16 +02001275static void batadv_tt_req_purge(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001276{
1277 struct tt_req_node *node, *safe;
1278
1279 spin_lock_bh(&bat_priv->tt_req_list_lock);
1280 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001281 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001282 list_del(&node->list);
1283 kfree(node);
1284 }
1285 }
1286 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1287}
1288
1289/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001290 * has already been issued for this orig_node, NULL otherwise
1291 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001292static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1293 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001294{
1295 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1296
1297 spin_lock_bh(&bat_priv->tt_req_list_lock);
1298 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001299 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1300 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1301 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001302 goto unlock;
1303 }
1304
1305 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1306 if (!tt_req_node)
1307 goto unlock;
1308
1309 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1310 tt_req_node->issued_at = jiffies;
1311
1312 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1313unlock:
1314 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1315 return tt_req_node;
1316}
1317
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001318/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001319static int batadv_tt_local_valid_entry(const void *entry_ptr,
1320 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001321{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001322 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001323
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001324 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001325 return 0;
1326 return 1;
1327}
1328
Sven Eckelmanna5130882012-05-16 20:23:16 +02001329static int batadv_tt_global_valid(const void *entry_ptr,
1330 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001332 const struct tt_common_entry *tt_common_entry = entry_ptr;
1333 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001334 const struct orig_node *orig_node = data_ptr;
1335
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001336 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001337 return 0;
1338
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001339 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1340 common);
1341
Sven Eckelmanna5130882012-05-16 20:23:16 +02001342 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001343}
1344
Sven Eckelmanna5130882012-05-16 20:23:16 +02001345static struct sk_buff *
1346batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1347 struct hashtable_t *hash,
1348 struct hard_iface *primary_if,
1349 int (*valid_cb)(const void *, const void *),
1350 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001351{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001352 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001353 struct tt_query_packet *tt_response;
1354 struct tt_change *tt_change;
1355 struct hlist_node *node;
1356 struct hlist_head *head;
1357 struct sk_buff *skb = NULL;
1358 uint16_t tt_tot, tt_count;
1359 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001360 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001361
1362 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1363 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1364 tt_len -= tt_len % sizeof(struct tt_change);
1365 }
1366 tt_tot = tt_len / sizeof(struct tt_change);
1367
1368 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1369 if (!skb)
1370 goto out;
1371
1372 skb_reserve(skb, ETH_HLEN);
1373 tt_response = (struct tt_query_packet *)skb_put(skb,
1374 tt_query_size + tt_len);
1375 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001376
1377 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1378 tt_count = 0;
1379
1380 rcu_read_lock();
1381 for (i = 0; i < hash->size; i++) {
1382 head = &hash->table[i];
1383
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001384 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385 head, hash_entry) {
1386 if (tt_count == tt_tot)
1387 break;
1388
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001389 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390 continue;
1391
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001392 memcpy(tt_change->addr, tt_common_entry->addr,
1393 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394 tt_change->flags = NO_FLAGS;
1395
1396 tt_count++;
1397 tt_change++;
1398 }
1399 }
1400 rcu_read_unlock();
1401
Antonio Quartulli9d852392011-10-17 14:25:13 +02001402 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001403 * copied
1404 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001405 tt_response->tt_data = htons(tt_count);
1406
Antonio Quartullia73105b2011-04-27 14:27:44 +02001407out:
1408 return skb;
1409}
1410
Sven Eckelmanna5130882012-05-16 20:23:16 +02001411static int batadv_send_tt_request(struct bat_priv *bat_priv,
1412 struct orig_node *dst_orig_node,
1413 uint8_t ttvn, uint16_t tt_crc,
1414 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415{
1416 struct sk_buff *skb = NULL;
1417 struct tt_query_packet *tt_request;
1418 struct neigh_node *neigh_node = NULL;
1419 struct hard_iface *primary_if;
1420 struct tt_req_node *tt_req_node = NULL;
1421 int ret = 1;
1422
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001423 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001424 if (!primary_if)
1425 goto out;
1426
1427 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001428 * reply from the same orig_node yet
1429 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001430 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001431 if (!tt_req_node)
1432 goto out;
1433
1434 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1435 if (!skb)
1436 goto out;
1437
1438 skb_reserve(skb, ETH_HLEN);
1439
1440 tt_request = (struct tt_query_packet *)skb_put(skb,
1441 sizeof(struct tt_query_packet));
1442
Sven Eckelmann76543d12011-11-20 15:47:38 +01001443 tt_request->header.packet_type = BAT_TT_QUERY;
1444 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1446 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001447 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001449 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001450 tt_request->flags = TT_REQUEST;
1451
1452 if (full_table)
1453 tt_request->flags |= TT_FULL_TABLE;
1454
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001455 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 if (!neigh_node)
1457 goto out;
1458
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001459 batadv_dbg(DBG_TT, bat_priv,
1460 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1461 dst_orig_node->orig, neigh_node->addr,
1462 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463
Martin Hundebøllf8214862012-04-20 17:02:45 +02001464 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1465
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001466 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 ret = 0;
1468
1469out:
1470 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001471 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001473 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 if (ret)
1475 kfree_skb(skb);
1476 if (ret && tt_req_node) {
1477 spin_lock_bh(&bat_priv->tt_req_list_lock);
1478 list_del(&tt_req_node->list);
1479 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1480 kfree(tt_req_node);
1481 }
1482 return ret;
1483}
1484
Sven Eckelmanna5130882012-05-16 20:23:16 +02001485static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1486 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487{
1488 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1489 struct neigh_node *neigh_node = NULL;
1490 struct hard_iface *primary_if = NULL;
1491 uint8_t orig_ttvn, req_ttvn, ttvn;
1492 int ret = false;
1493 unsigned char *tt_buff;
1494 bool full_table;
1495 uint16_t tt_len, tt_tot;
1496 struct sk_buff *skb = NULL;
1497 struct tt_query_packet *tt_response;
1498
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001499 batadv_dbg(DBG_TT, bat_priv,
1500 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1501 tt_request->src, tt_request->ttvn, tt_request->dst,
1502 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503
1504 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001505 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506 if (!req_dst_orig_node)
1507 goto out;
1508
Sven Eckelmannda641192012-05-12 13:48:56 +02001509 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510 if (!res_dst_orig_node)
1511 goto out;
1512
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001513 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001514 if (!neigh_node)
1515 goto out;
1516
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001517 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518 if (!primary_if)
1519 goto out;
1520
1521 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1522 req_ttvn = tt_request->ttvn;
1523
Antonio Quartulli015758d2011-07-09 17:52:13 +02001524 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001525 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001526 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527 goto out;
1528
Antonio Quartulli015758d2011-07-09 17:52:13 +02001529 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530 if (tt_request->flags & TT_FULL_TABLE ||
1531 !req_dst_orig_node->tt_buff)
1532 full_table = true;
1533 else
1534 full_table = false;
1535
1536 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001537 * I'll send only one packet with as much TT entries as I can
1538 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539 if (!full_table) {
1540 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1541 tt_len = req_dst_orig_node->tt_buff_len;
1542 tt_tot = tt_len / sizeof(struct tt_change);
1543
1544 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1545 tt_len + ETH_HLEN);
1546 if (!skb)
1547 goto unlock;
1548
1549 skb_reserve(skb, ETH_HLEN);
1550 tt_response = (struct tt_query_packet *)skb_put(skb,
1551 sizeof(struct tt_query_packet) + tt_len);
1552 tt_response->ttvn = req_ttvn;
1553 tt_response->tt_data = htons(tt_tot);
1554
1555 tt_buff = skb->data + sizeof(struct tt_query_packet);
1556 /* Copy the last orig_node's OGM buffer */
1557 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1558 req_dst_orig_node->tt_buff_len);
1559
1560 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1561 } else {
1562 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1563 sizeof(struct tt_change);
1564 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1565
Sven Eckelmanna5130882012-05-16 20:23:16 +02001566 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1567 bat_priv->tt_global_hash,
1568 primary_if,
1569 batadv_tt_global_valid,
1570 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001571 if (!skb)
1572 goto out;
1573
1574 tt_response = (struct tt_query_packet *)skb->data;
1575 }
1576
Sven Eckelmann76543d12011-11-20 15:47:38 +01001577 tt_response->header.packet_type = BAT_TT_QUERY;
1578 tt_response->header.version = COMPAT_VERSION;
1579 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001580 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1581 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1582 tt_response->flags = TT_RESPONSE;
1583
1584 if (full_table)
1585 tt_response->flags |= TT_FULL_TABLE;
1586
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001587 batadv_dbg(DBG_TT, bat_priv,
1588 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1589 res_dst_orig_node->orig, neigh_node->addr,
1590 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001591
Martin Hundebøllf8214862012-04-20 17:02:45 +02001592 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1593
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001594 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595 ret = true;
1596 goto out;
1597
1598unlock:
1599 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1600
1601out:
1602 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001603 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001604 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001605 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001607 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001609 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 if (!ret)
1611 kfree_skb(skb);
1612 return ret;
1613
1614}
Sven Eckelmanna5130882012-05-16 20:23:16 +02001615static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1616 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617{
1618 struct orig_node *orig_node = NULL;
1619 struct neigh_node *neigh_node = NULL;
1620 struct hard_iface *primary_if = NULL;
1621 uint8_t my_ttvn, req_ttvn, ttvn;
1622 int ret = false;
1623 unsigned char *tt_buff;
1624 bool full_table;
1625 uint16_t tt_len, tt_tot;
1626 struct sk_buff *skb = NULL;
1627 struct tt_query_packet *tt_response;
1628
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001629 batadv_dbg(DBG_TT, bat_priv,
1630 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1631 tt_request->src, tt_request->ttvn,
1632 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001633
1634
1635 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1636 req_ttvn = tt_request->ttvn;
1637
Sven Eckelmannda641192012-05-12 13:48:56 +02001638 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 if (!orig_node)
1640 goto out;
1641
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001642 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643 if (!neigh_node)
1644 goto out;
1645
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001646 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001647 if (!primary_if)
1648 goto out;
1649
1650 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001651 * is too big send the whole local translation table
1652 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1654 !bat_priv->tt_buff)
1655 full_table = true;
1656 else
1657 full_table = false;
1658
1659 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001660 * I'll send only one packet with as much TT entries as I can
1661 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001662 if (!full_table) {
1663 spin_lock_bh(&bat_priv->tt_buff_lock);
1664 tt_len = bat_priv->tt_buff_len;
1665 tt_tot = tt_len / sizeof(struct tt_change);
1666
1667 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1668 tt_len + ETH_HLEN);
1669 if (!skb)
1670 goto unlock;
1671
1672 skb_reserve(skb, ETH_HLEN);
1673 tt_response = (struct tt_query_packet *)skb_put(skb,
1674 sizeof(struct tt_query_packet) + tt_len);
1675 tt_response->ttvn = req_ttvn;
1676 tt_response->tt_data = htons(tt_tot);
1677
1678 tt_buff = skb->data + sizeof(struct tt_query_packet);
1679 memcpy(tt_buff, bat_priv->tt_buff,
1680 bat_priv->tt_buff_len);
1681 spin_unlock_bh(&bat_priv->tt_buff_lock);
1682 } else {
1683 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1684 sizeof(struct tt_change);
1685 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1686
Sven Eckelmanna5130882012-05-16 20:23:16 +02001687 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1688 bat_priv->tt_local_hash,
1689 primary_if,
1690 batadv_tt_local_valid_entry,
1691 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692 if (!skb)
1693 goto out;
1694
1695 tt_response = (struct tt_query_packet *)skb->data;
1696 }
1697
Sven Eckelmann76543d12011-11-20 15:47:38 +01001698 tt_response->header.packet_type = BAT_TT_QUERY;
1699 tt_response->header.version = COMPAT_VERSION;
1700 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1702 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1703 tt_response->flags = TT_RESPONSE;
1704
1705 if (full_table)
1706 tt_response->flags |= TT_FULL_TABLE;
1707
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001708 batadv_dbg(DBG_TT, bat_priv,
1709 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1710 orig_node->orig, neigh_node->addr,
1711 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712
Martin Hundebøllf8214862012-04-20 17:02:45 +02001713 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1714
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001715 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 ret = true;
1717 goto out;
1718
1719unlock:
1720 spin_unlock_bh(&bat_priv->tt_buff_lock);
1721out:
1722 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001723 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001724 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001725 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001727 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728 if (!ret)
1729 kfree_skb(skb);
1730 /* This packet was for me, so it doesn't need to be re-routed */
1731 return true;
1732}
1733
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001734bool batadv_send_tt_response(struct bat_priv *bat_priv,
1735 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001736{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001737 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001738 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001739 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001740 return true;
1741
Sven Eckelmanna5130882012-05-16 20:23:16 +02001742 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001743 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001744 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001745 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746}
1747
Sven Eckelmanna5130882012-05-16 20:23:16 +02001748static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1749 struct orig_node *orig_node,
1750 struct tt_change *tt_change,
1751 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001752{
1753 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001754 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755
1756 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001757 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001758 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1759 batadv_tt_global_del(bat_priv, orig_node,
1760 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001761 "tt removed by changes",
1762 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001763 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001764 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001765 (tt_change + i)->addr,
1766 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001767 /* In case of problem while storing a
1768 * global_entry, we stop the updating
1769 * procedure without committing the
1770 * ttvn change. This will avoid to send
1771 * corrupted data on tt_request
1772 */
1773 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001774 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001775 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001776 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001777}
1778
Sven Eckelmanna5130882012-05-16 20:23:16 +02001779static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1780 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001781{
1782 struct orig_node *orig_node = NULL;
1783
Sven Eckelmannda641192012-05-12 13:48:56 +02001784 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785 if (!orig_node)
1786 goto out;
1787
1788 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001789 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001790
Sven Eckelmanna5130882012-05-16 20:23:16 +02001791 _batadv_tt_update_changes(bat_priv, orig_node,
1792 (struct tt_change *)(tt_response + 1),
1793 ntohs(tt_response->tt_data),
1794 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795
1796 spin_lock_bh(&orig_node->tt_buff_lock);
1797 kfree(orig_node->tt_buff);
1798 orig_node->tt_buff_len = 0;
1799 orig_node->tt_buff = NULL;
1800 spin_unlock_bh(&orig_node->tt_buff_lock);
1801
1802 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1803
1804out:
1805 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001806 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001807}
1808
Sven Eckelmanna5130882012-05-16 20:23:16 +02001809static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1810 struct orig_node *orig_node,
1811 uint16_t tt_num_changes, uint8_t ttvn,
1812 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001814 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1815 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816
Sven Eckelmanna5130882012-05-16 20:23:16 +02001817 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1818 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001819 atomic_set(&orig_node->last_ttvn, ttvn);
1820}
1821
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001822bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001824 struct tt_local_entry *tt_local_entry = NULL;
1825 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001826
Sven Eckelmanna5130882012-05-16 20:23:16 +02001827 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001828 if (!tt_local_entry)
1829 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001830 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001831 * consistency purpose)
1832 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001833 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001834 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001835 ret = true;
1836out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001837 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001838 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001839 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840}
1841
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001842void batadv_handle_tt_response(struct bat_priv *bat_priv,
1843 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844{
1845 struct tt_req_node *node, *safe;
1846 struct orig_node *orig_node = NULL;
1847
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001848 batadv_dbg(DBG_TT, bat_priv,
1849 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1850 tt_response->src, tt_response->ttvn,
1851 ntohs(tt_response->tt_data),
1852 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001854 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001855 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001856 goto out;
1857
Sven Eckelmannda641192012-05-12 13:48:56 +02001858 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859 if (!orig_node)
1860 goto out;
1861
1862 if (tt_response->flags & TT_FULL_TABLE)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001863 batadv_tt_fill_gtable(bat_priv, tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864 else
Sven Eckelmanna5130882012-05-16 20:23:16 +02001865 batadv_tt_update_changes(bat_priv, orig_node,
1866 ntohs(tt_response->tt_data),
1867 tt_response->ttvn,
1868 (struct tt_change *)(tt_response + 1));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869
1870 /* Delete the tt_req_node from pending tt_requests list */
1871 spin_lock_bh(&bat_priv->tt_req_list_lock);
1872 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001873 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874 continue;
1875 list_del(&node->list);
1876 kfree(node);
1877 }
1878 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1879
1880 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001881 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001882 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001883 * unset the flag
1884 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001885 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001886out:
1887 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001888 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001889}
1890
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001891int batadv_tt_init(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001893 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001894
Sven Eckelmanna5130882012-05-16 20:23:16 +02001895 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001896 if (ret < 0)
1897 return ret;
1898
Sven Eckelmanna5130882012-05-16 20:23:16 +02001899 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001900 if (ret < 0)
1901 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902
Sven Eckelmanna5130882012-05-16 20:23:16 +02001903 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001904
1905 return 1;
1906}
1907
Sven Eckelmanna5130882012-05-16 20:23:16 +02001908static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001910 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001911
Antonio Quartullicc47f662011-04-27 14:27:57 +02001912 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001913
Antonio Quartullicc47f662011-04-27 14:27:57 +02001914 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1915 list_del(&node->list);
1916 kfree(node);
1917 }
1918
1919 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1920}
1921
Sven Eckelmanna5130882012-05-16 20:23:16 +02001922static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001923{
1924 struct tt_roam_node *node, *safe;
1925
1926 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1927 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001928 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001929 continue;
1930
1931 list_del(&node->list);
1932 kfree(node);
1933 }
1934 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1935}
1936
1937/* This function checks whether the client already reached the
1938 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1939 * will not be sent.
1940 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001941 * returns true if the ROAMING_ADV can be sent, false otherwise
1942 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001943static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1944 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001945{
1946 struct tt_roam_node *tt_roam_node;
1947 bool ret = false;
1948
1949 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1950 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001951 * reply from the same orig_node yet
1952 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001953 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001954 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001955 continue;
1956
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001957 if (batadv_has_timed_out(tt_roam_node->first_time,
1958 ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001959 continue;
1960
Sven Eckelmann3e348192012-05-16 20:23:22 +02001961 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001962 /* Sorry, you roamed too many times! */
1963 goto unlock;
1964 ret = true;
1965 break;
1966 }
1967
1968 if (!ret) {
1969 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1970 if (!tt_roam_node)
1971 goto unlock;
1972
1973 tt_roam_node->first_time = jiffies;
1974 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1975 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1976
1977 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1978 ret = true;
1979 }
1980
1981unlock:
1982 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1983 return ret;
1984}
1985
Sven Eckelmanna5130882012-05-16 20:23:16 +02001986static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1987 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001988{
1989 struct neigh_node *neigh_node = NULL;
1990 struct sk_buff *skb = NULL;
1991 struct roam_adv_packet *roam_adv_packet;
1992 int ret = 1;
1993 struct hard_iface *primary_if;
1994
1995 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001996 * already roamed to us too many times
1997 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001998 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001999 goto out;
2000
2001 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
2002 if (!skb)
2003 goto out;
2004
2005 skb_reserve(skb, ETH_HLEN);
2006
2007 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
2008 sizeof(struct roam_adv_packet));
2009
Sven Eckelmann76543d12011-11-20 15:47:38 +01002010 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
2011 roam_adv_packet->header.version = COMPAT_VERSION;
2012 roam_adv_packet->header.ttl = TTL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002013 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002014 if (!primary_if)
2015 goto out;
2016 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002017 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002018 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2019 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2020
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002021 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002022 if (!neigh_node)
2023 goto out;
2024
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002025 batadv_dbg(DBG_TT, bat_priv,
2026 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2027 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028
Martin Hundebøllf8214862012-04-20 17:02:45 +02002029 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2030
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02002031 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002032 ret = 0;
2033
2034out:
2035 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002036 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002037 if (ret)
2038 kfree_skb(skb);
2039 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002040}
2041
Sven Eckelmanna5130882012-05-16 20:23:16 +02002042static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043{
2044 struct delayed_work *delayed_work =
2045 container_of(work, struct delayed_work, work);
2046 struct bat_priv *bat_priv =
2047 container_of(delayed_work, struct bat_priv, tt_work);
2048
Sven Eckelmanna5130882012-05-16 20:23:16 +02002049 batadv_tt_local_purge(bat_priv);
2050 batadv_tt_global_roam_purge(bat_priv);
2051 batadv_tt_req_purge(bat_priv);
2052 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002053
Sven Eckelmanna5130882012-05-16 20:23:16 +02002054 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002055}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002056
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002057void batadv_tt_free(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002058{
2059 cancel_delayed_work_sync(&bat_priv->tt_work);
2060
Sven Eckelmanna5130882012-05-16 20:23:16 +02002061 batadv_tt_local_table_free(bat_priv);
2062 batadv_tt_global_table_free(bat_priv);
2063 batadv_tt_req_list_free(bat_priv);
2064 batadv_tt_changes_list_free(bat_priv);
2065 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002066
2067 kfree(bat_priv->tt_buff);
2068}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002069
Antonio Quartulli697f2532011-11-07 16:47:01 +01002070/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002071 * in the given hash table and returns the number of modified entries
2072 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002073static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2074 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002075{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002076 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002077 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002078 struct hlist_head *head;
2079 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002080 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002081
2082 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002083 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002084
2085 for (i = 0; i < hash->size; i++) {
2086 head = &hash->table[i];
2087
2088 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002089 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002090 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002091 if (enable) {
2092 if ((tt_common_entry->flags & flags) == flags)
2093 continue;
2094 tt_common_entry->flags |= flags;
2095 } else {
2096 if (!(tt_common_entry->flags & flags))
2097 continue;
2098 tt_common_entry->flags &= ~flags;
2099 }
2100 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002101 }
2102 rcu_read_unlock();
2103 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002104out:
2105 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002106}
2107
2108/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002109static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002110{
2111 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002112 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002113 struct tt_local_entry *tt_local_entry;
2114 struct hlist_node *node, *node_tmp;
2115 struct hlist_head *head;
2116 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002117 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002118
2119 if (!hash)
2120 return;
2121
2122 for (i = 0; i < hash->size; i++) {
2123 head = &hash->table[i];
2124 list_lock = &hash->list_locks[i];
2125
2126 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002127 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002128 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002129 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002130 continue;
2131
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002132 batadv_dbg(DBG_TT, bat_priv,
2133 "Deleting local tt entry (%pM): pending\n",
2134 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002135
2136 atomic_dec(&bat_priv->num_local_tt);
2137 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002138 tt_local_entry = container_of(tt_common_entry,
2139 struct tt_local_entry,
2140 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002141 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002142 }
2143 spin_unlock_bh(list_lock);
2144 }
2145
2146}
2147
Sven Eckelmanna5130882012-05-16 20:23:16 +02002148static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2149 unsigned char **packet_buff,
2150 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002151{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002152 uint16_t changed_num = 0;
2153
2154 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2155 return -ENOENT;
2156
Sven Eckelmanna5130882012-05-16 20:23:16 +02002157 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2158 TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002159
2160 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002161 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002162 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002163 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002164
2165 /* Increment the TTVN only once per OGM interval */
2166 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002167 batadv_dbg(DBG_TT, bat_priv,
2168 "Local changes committed, updating to ttvn %u\n",
2169 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002170 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002171
2172 /* reset the sending counter */
2173 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2174
Sven Eckelmanna5130882012-05-16 20:23:16 +02002175 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2176 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002177}
2178
2179/* when calling this function (hard_iface == primary_if) has to be true */
2180int batadv_tt_append_diff(struct bat_priv *bat_priv,
2181 unsigned char **packet_buff, int *packet_buff_len,
2182 int packet_min_len)
2183{
2184 int tt_num_changes;
2185
2186 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002187 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2188 packet_buff_len,
2189 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002190
2191 /* if the changes have been sent often enough */
2192 if ((tt_num_changes < 0) &&
Sven Eckelmann3e348192012-05-16 20:23:22 +02002193 (!batadv_atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002194 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2195 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002196 tt_num_changes = 0;
2197 }
2198
2199 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002200}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002201
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002202bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2203 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002204{
2205 struct tt_local_entry *tt_local_entry = NULL;
2206 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002207 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002208
2209 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002210 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002211
Sven Eckelmanna5130882012-05-16 20:23:16 +02002212 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002213 if (!tt_local_entry)
2214 goto out;
2215
Sven Eckelmanna5130882012-05-16 20:23:16 +02002216 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002217 if (!tt_global_entry)
2218 goto out;
2219
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002220 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002221 goto out;
2222
Marek Lindner5870adc2012-06-20 17:16:05 +02002223 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002224
2225out:
2226 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002227 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002228 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002229 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002230 return ret;
2231}
Marek Lindnera943cac2011-07-30 13:10:18 +02002232
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002233void batadv_tt_update_orig(struct bat_priv *bat_priv,
2234 struct orig_node *orig_node,
2235 const unsigned char *tt_buff, uint8_t tt_num_changes,
2236 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002237{
2238 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2239 bool full_table = true;
2240
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002241 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002242 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002243 return;
2244
Antonio Quartulli17071572011-11-07 16:36:40 +01002245 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002246 * increased by one -> we can apply the attached changes
2247 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002248 if ((!orig_node->tt_initialised && ttvn == 1) ||
2249 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002250 /* the OGM could not contain the changes due to their size or
2251 * because they have already been sent TT_OGM_APPEND_MAX times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002252 * In this case send a tt request
2253 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002254 if (!tt_num_changes) {
2255 full_table = false;
2256 goto request_table;
2257 }
2258
Sven Eckelmanna5130882012-05-16 20:23:16 +02002259 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2260 ttvn, (struct tt_change *)tt_buff);
Marek Lindnera943cac2011-07-30 13:10:18 +02002261
2262 /* Even if we received the precomputed crc with the OGM, we
2263 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002264 * in the global table
2265 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002266 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002267
2268 /* The ttvn alone is not enough to guarantee consistency
2269 * because a single value could represent different states
2270 * (due to the wrap around). Thus a node has to check whether
2271 * the resulting table (after applying the changes) is still
2272 * consistent or not. E.g. a node could disconnect while its
2273 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2274 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002275 * inconsistency
2276 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002277 if (orig_node->tt_crc != tt_crc)
2278 goto request_table;
2279
2280 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002281 * unset the flag
2282 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002283 orig_node->tt_poss_change = false;
2284 } else {
2285 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002286 * in sync anymore -> request fresh tt data
2287 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002288 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2289 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002290request_table:
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002291 batadv_dbg(DBG_TT, bat_priv,
2292 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2293 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2294 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002295 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2296 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002297 return;
2298 }
2299 }
2300}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002301
2302/* returns true whether we know that the client has moved from its old
2303 * originator to another one. This entry is kept is still kept for consistency
2304 * purposes
2305 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002306bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2307 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002308{
2309 struct tt_global_entry *tt_global_entry;
2310 bool ret = false;
2311
Sven Eckelmanna5130882012-05-16 20:23:16 +02002312 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002313 if (!tt_global_entry)
2314 goto out;
2315
2316 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002317 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002318out:
2319 return ret;
2320}