blob: c673b58f3ee14134dbcdc2450566d04f4f911f0f [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{
160 struct tt_change_node *tt_change_node;
161
162 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
163
164 if (!tt_change_node)
165 return;
166
Antonio Quartulliff66c972011-06-30 01:14:00 +0200167 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
169
170 spin_lock_bh(&bat_priv->tt_changes_list_lock);
171 /* track the change in the OGMinterval list */
172 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
173 atomic_inc(&bat_priv->tt_local_changes);
174 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
175
176 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
177}
178
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200179int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200180{
181 return changes_num * sizeof(struct tt_change);
182}
183
Sven Eckelmanna5130882012-05-16 20:23:16 +0200184static int batadv_tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200186 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200187 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200189 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200191 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200192 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193
Sven Eckelmann5346c352012-05-05 13:27:28 +0200194 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195}
196
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200197void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
198 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199{
200 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200201 struct tt_local_entry *tt_local_entry = NULL;
202 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200203 struct hlist_head *head;
204 struct hlist_node *node;
205 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100206 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207
Sven Eckelmanna5130882012-05-16 20:23:16 +0200208 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200210 if (tt_local_entry) {
211 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100212 /* possibly unset the TT_CLIENT_PENDING flag */
213 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200214 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215 }
216
Sven Eckelmann704509b2011-05-14 23:14:54 +0200217 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200218 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200219 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200221 batadv_dbg(DBG_TT, bat_priv,
222 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
223 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100225 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
226 tt_local_entry->common.flags = NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200227 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100228 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
229 atomic_set(&tt_local_entry->common.refcount, 2);
230 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231
232 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200233 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100234 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100236 /* The local entry has to be marked as NEW to avoid to send it in
237 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200238 * (consistency check)
239 */
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100240 tt_local_entry->common.flags |= TT_CLIENT_NEW;
241
Sven Eckelmanna5130882012-05-16 20:23:16 +0200242 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200243 batadv_choose_orig,
244 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200245 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100246
247 if (unlikely(hash_added != 0)) {
248 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200249 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100250 goto out;
251 }
252
Sven Eckelmanna5130882012-05-16 20:23:16 +0200253 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200254
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200256 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
Antonio Quartullicc47f662011-04-27 14:27:57 +0200258 /* Check whether it is a roaming! */
259 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200260 /* These node are probably going to update their tt table */
261 head = &tt_global_entry->orig_list;
262 rcu_read_lock();
263 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
264 orig_entry->orig_node->tt_poss_change = true;
265
Sven Eckelmanna5130882012-05-16 20:23:16 +0200266 batadv_send_roam_adv(bat_priv,
267 tt_global_entry->common.addr,
268 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200269 }
270 rcu_read_unlock();
271 /* The global entry has to be marked as ROAMING and
272 * has to be kept for consistency purpose
273 */
David S. Miller220b07e2011-12-16 15:07:28 -0500274 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100275 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200276 }
277out:
278 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200279 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200280 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200281 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282}
283
Sven Eckelmanna5130882012-05-16 20:23:16 +0200284static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
285 int *packet_buff_len,
286 int min_packet_len,
287 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800289 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800291 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
292
293 /* keep old buffer if kmalloc should fail */
294 if (new_buff) {
295 memcpy(new_buff, *packet_buff, min_packet_len);
296 kfree(*packet_buff);
297 *packet_buff = new_buff;
298 *packet_buff_len = new_packet_len;
299 }
300}
301
Sven Eckelmanna5130882012-05-16 20:23:16 +0200302static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
303 unsigned char **packet_buff,
304 int *packet_buff_len,
305 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800306{
307 struct hard_iface *primary_if;
308 int req_len;
309
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200310 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800311
312 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200313 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800314
315 /* if we have too many changes for one packet don't send any
316 * and wait for the tt table request which will be fragmented
317 */
318 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
319 req_len = min_packet_len;
320
Sven Eckelmanna5130882012-05-16 20:23:16 +0200321 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
322 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800323
324 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200325 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800326}
327
Sven Eckelmanna5130882012-05-16 20:23:16 +0200328static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
329 unsigned char **packet_buff,
330 int *packet_buff_len,
331 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800332{
333 struct tt_change_node *entry, *safe;
334 int count = 0, tot_changes = 0, new_len;
335 unsigned char *tt_buff;
336
Sven Eckelmanna5130882012-05-16 20:23:16 +0200337 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
338 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800339
340 new_len = *packet_buff_len - min_packet_len;
341 tt_buff = *packet_buff + min_packet_len;
342
343 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200344 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345
Antonio Quartullia73105b2011-04-27 14:27:44 +0200346 spin_lock_bh(&bat_priv->tt_changes_list_lock);
347 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
Antonio Quartullia73105b2011-04-27 14:27:44 +0200349 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100350 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200351 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200352 memcpy(tt_buff + batadv_tt_len(count),
Antonio Quartullia73105b2011-04-27 14:27:44 +0200353 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 count++;
355 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200356 list_del(&entry->list);
357 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200359 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
Antonio Quartullia73105b2011-04-27 14:27:44 +0200361 /* Keep the buffer for possible tt_request */
362 spin_lock_bh(&bat_priv->tt_buff_lock);
363 kfree(bat_priv->tt_buff);
364 bat_priv->tt_buff_len = 0;
365 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800366 /* check whether this new OGM has no changes due to size problems */
367 if (new_len > 0) {
368 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200369 * instead of providing the diff
370 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800371 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200372 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800373 memcpy(bat_priv->tt_buff, tt_buff, new_len);
374 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200375 }
376 }
377 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
Marek Lindner08ad76e2012-04-23 16:32:55 +0800379 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380}
381
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200382int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
384 struct net_device *net_dev = (struct net_device *)seq->private;
385 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200386 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100387 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200388 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000389 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200391 uint32_t i;
392 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200394 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200395 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100396 ret = seq_printf(seq,
397 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200398 net_dev->name);
399 goto out;
400 }
401
402 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100403 ret = seq_printf(seq,
404 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200405 net_dev->name);
406 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407 }
408
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100409 seq_printf(seq,
410 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200411 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 for (i = 0; i < hash->size; i++) {
414 head = &hash->table[i];
415
Marek Lindner7aadf882011-02-18 12:28:09 +0000416 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100417 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000418 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200419 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100420 tt_common_entry->addr,
421 (tt_common_entry->flags &
422 TT_CLIENT_ROAM ? 'R' : '.'),
423 (tt_common_entry->flags &
424 TT_CLIENT_NOPURGE ? 'P' : '.'),
425 (tt_common_entry->flags &
426 TT_CLIENT_NEW ? 'N' : '.'),
427 (tt_common_entry->flags &
428 TT_CLIENT_PENDING ? 'X' : '.'),
429 (tt_common_entry->flags &
430 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000432 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200434out:
435 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200436 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200437 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438}
439
Sven Eckelmanna5130882012-05-16 20:23:16 +0200440static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
441 struct tt_local_entry *tt_local_entry,
442 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200444 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
445 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Antonio Quartulli015758d2011-07-09 17:52:13 +0200447 /* The local client has to be marked as "pending to be removed" but has
448 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200449 * response issued before the net ttvn increment (consistency check)
450 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100451 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100452
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200453 batadv_dbg(DBG_TT, bat_priv,
454 "Local tt entry (%pM) pending to be removed: %s\n",
455 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456}
457
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200458void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
459 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200461 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462
Sven Eckelmanna5130882012-05-16 20:23:16 +0200463 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200464 if (!tt_local_entry)
465 goto out;
466
Sven Eckelmanna5130882012-05-16 20:23:16 +0200467 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
468 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
469 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200470out:
471 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200472 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473}
474
Sven Eckelmanna5130882012-05-16 20:23:16 +0200475static void batadv_tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200477 struct hashtable_t *hash = bat_priv->tt_local_hash;
478 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100479 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000480 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200482 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200483 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 for (i = 0; i < hash->size; i++) {
486 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200487 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200489 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100490 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000491 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100492 tt_local_entry = container_of(tt_common_entry,
493 struct tt_local_entry,
494 common);
495 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000496 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200498 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100499 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200500 continue;
501
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200502 if (!batadv_has_timed_out(tt_local_entry->last_seen,
503 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000504 continue;
505
Sven Eckelmanna5130882012-05-16 20:23:16 +0200506 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
507 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200509 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510 }
511
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512}
513
Sven Eckelmanna5130882012-05-16 20:23:16 +0200514static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200516 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100518 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200519 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200520 struct hlist_node *node, *node_tmp;
521 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200522 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200523
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200524 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 return;
526
Antonio Quartullia73105b2011-04-27 14:27:44 +0200527 hash = bat_priv->tt_local_hash;
528
529 for (i = 0; i < hash->size; i++) {
530 head = &hash->table[i];
531 list_lock = &hash->list_locks[i];
532
533 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100534 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200535 head, hash_entry) {
536 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100537 tt_local_entry = container_of(tt_common_entry,
538 struct tt_local_entry,
539 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200540 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200541 }
542 spin_unlock_bh(list_lock);
543 }
544
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200545 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200546
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200547 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548}
549
Sven Eckelmanna5130882012-05-16 20:23:16 +0200550static int batadv_tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200552 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200553 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200555 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200557 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200558 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
Sven Eckelmann5346c352012-05-05 13:27:28 +0200560 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561}
562
Sven Eckelmanna5130882012-05-16 20:23:16 +0200563static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200564{
565 struct tt_change_node *entry, *safe;
566
567 spin_lock_bh(&bat_priv->tt_changes_list_lock);
568
569 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
570 list) {
571 list_del(&entry->list);
572 kfree(entry);
573 }
574
575 atomic_set(&bat_priv->tt_local_changes, 0);
576 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
577}
578
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200579/* find out if an orig_node is already in the list of a tt_global_entry.
580 * returns 1 if found, 0 otherwise
581 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200582static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
583 const struct orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200584{
585 struct tt_orig_list_entry *tmp_orig_entry;
586 const struct hlist_head *head;
587 struct hlist_node *node;
588 bool found = false;
589
590 rcu_read_lock();
591 head = &entry->orig_list;
592 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
593 if (tmp_orig_entry->orig_node == orig_node) {
594 found = true;
595 break;
596 }
597 }
598 rcu_read_unlock();
599 return found;
600}
601
Sven Eckelmanna5130882012-05-16 20:23:16 +0200602static void
603batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
604 struct orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200605{
606 struct tt_orig_list_entry *orig_entry;
607
608 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
609 if (!orig_entry)
610 return;
611
612 INIT_HLIST_NODE(&orig_entry->list);
613 atomic_inc(&orig_node->refcount);
614 atomic_inc(&orig_node->tt_size);
615 orig_entry->orig_node = orig_node;
616 orig_entry->ttvn = ttvn;
617
618 spin_lock_bh(&tt_global_entry->list_lock);
619 hlist_add_head_rcu(&orig_entry->list,
620 &tt_global_entry->orig_list);
621 spin_unlock_bh(&tt_global_entry->list_lock);
622}
623
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624/* caller must hold orig_node refcount */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200625int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
626 const unsigned char *tt_addr, uint8_t ttvn,
627 bool roaming, bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200629 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200630 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100631 int hash_added;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200632 struct tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
Sven Eckelmanna5130882012-05-16 20:23:16 +0200634 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635
Antonio Quartullia73105b2011-04-27 14:27:44 +0200636 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200637 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
638 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200639 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 goto out;
641
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200642 common = &tt_global_entry->common;
643 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200644
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200645 common->flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200646 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200647 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200648
649 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
650 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200651
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200652 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200653 batadv_compare_tt,
654 batadv_choose_orig, common,
655 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100656
657 if (unlikely(hash_added != 0)) {
658 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200659 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100660 goto out_remove;
661 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200662
Sven Eckelmanna5130882012-05-16 20:23:16 +0200663 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
664 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200665 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200666 /* there is already a global entry, use this one. */
667
668 /* If there is the TT_CLIENT_ROAM flag set, there is only one
669 * originator left in the list and we previously received a
670 * delete + roaming change for this originator.
671 *
672 * We should first delete the old originator before adding the
673 * new one.
674 */
675 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200676 batadv_tt_global_del_orig_list(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200677 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
678 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200680
Sven Eckelmanna5130882012-05-16 20:23:16 +0200681 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
682 orig_node))
683 batadv_tt_global_add_orig_entry(tt_global_entry,
684 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200686
Antonio Quartullibc279082011-07-07 15:35:35 +0200687 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100688 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200689
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200690 batadv_dbg(DBG_TT, bat_priv,
691 "Creating new global tt entry: %pM (via %pM)\n",
692 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200693
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100694out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200695 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200696 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
697 "global tt received", roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200698 ret = 1;
699out:
700 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200701 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200702 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703}
704
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200705/* print all orig nodes who announce the address for this global entry.
706 * it is assumed that the caller holds rcu_read_lock();
707 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200708static void
709batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
710 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200711{
712 struct hlist_head *head;
713 struct hlist_node *node;
714 struct tt_orig_list_entry *orig_entry;
715 struct tt_common_entry *tt_common_entry;
716 uint16_t flags;
717 uint8_t last_ttvn;
718
719 tt_common_entry = &tt_global_entry->common;
720
721 head = &tt_global_entry->orig_list;
722
723 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
724 flags = tt_common_entry->flags;
725 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
726 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
727 tt_global_entry->common.addr, orig_entry->ttvn,
728 orig_entry->orig_node->orig, last_ttvn,
729 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
730 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
731 }
732}
733
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200734int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735{
736 struct net_device *net_dev = (struct net_device *)seq->private;
737 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200738 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100739 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200740 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200741 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000742 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200744 uint32_t i;
745 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200747 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200748 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100749 ret = seq_printf(seq,
750 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200751 net_dev->name);
752 goto out;
753 }
754
755 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100756 ret = seq_printf(seq,
757 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200758 net_dev->name);
759 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760 }
761
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200762 seq_printf(seq,
763 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200765 seq_printf(seq, " %-13s %s %-15s %s %s\n",
766 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768 for (i = 0; i < hash->size; i++) {
769 head = &hash->table[i];
770
Marek Lindner7aadf882011-02-18 12:28:09 +0000771 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100772 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000773 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100774 tt_global_entry = container_of(tt_common_entry,
775 struct tt_global_entry,
776 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200777 batadv_tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000779 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200781out:
782 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200783 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200784 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785}
786
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200787/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200788static void
789batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200791 struct hlist_head *head;
792 struct hlist_node *node, *safe;
793 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200794
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200795 spin_lock_bh(&tt_global_entry->list_lock);
796 head = &tt_global_entry->orig_list;
797 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
798 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200799 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200800 }
801 spin_unlock_bh(&tt_global_entry->list_lock);
802
803}
804
Sven Eckelmanna5130882012-05-16 20:23:16 +0200805static void
806batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
807 struct tt_global_entry *tt_global_entry,
808 struct orig_node *orig_node,
809 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200810{
811 struct hlist_head *head;
812 struct hlist_node *node, *safe;
813 struct tt_orig_list_entry *orig_entry;
814
815 spin_lock_bh(&tt_global_entry->list_lock);
816 head = &tt_global_entry->orig_list;
817 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
818 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200819 batadv_dbg(DBG_TT, bat_priv,
820 "Deleting %pM from global tt entry %pM: %s\n",
821 orig_node->orig,
822 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200823 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200824 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 }
826 }
827 spin_unlock_bh(&tt_global_entry->list_lock);
828}
829
Sven Eckelmanna5130882012-05-16 20:23:16 +0200830static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
831 struct tt_global_entry *tt_global_entry,
832 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200833{
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200834 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
835 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200836
Sven Eckelmanna5130882012-05-16 20:23:16 +0200837 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200838 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200839 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200840
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000841}
842
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200843/* If the client is to be deleted, we check if it is the last origantor entry
844 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
845 * otherwise we simply remove the originator scheduled for deletion.
846 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200847static void
848batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
849 struct tt_global_entry *tt_global_entry,
850 struct orig_node *orig_node, const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200851{
852 bool last_entry = true;
853 struct hlist_head *head;
854 struct hlist_node *node;
855 struct tt_orig_list_entry *orig_entry;
856
857 /* no local entry exists, case 1:
858 * Check if this is the last one or if other entries exist.
859 */
860
861 rcu_read_lock();
862 head = &tt_global_entry->orig_list;
863 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
864 if (orig_entry->orig_node != orig_node) {
865 last_entry = false;
866 break;
867 }
868 }
869 rcu_read_unlock();
870
871 if (last_entry) {
872 /* its the last one, mark for roaming. */
873 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
874 tt_global_entry->roam_at = jiffies;
875 } else
876 /* there is another entry, we can simply delete this
877 * one and can still use the other one.
878 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200879 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
880 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200881}
882
883
884
Sven Eckelmanna5130882012-05-16 20:23:16 +0200885static void batadv_tt_global_del(struct bat_priv *bat_priv,
886 struct orig_node *orig_node,
887 const unsigned char *addr,
888 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200890 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmanna5130882012-05-16 20:23:16 +0200891 struct tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
Sven Eckelmanna5130882012-05-16 20:23:16 +0200893 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200894 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200895 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200896
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200897 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200898 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
899 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800900
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200901 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200902 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
903 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200904
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800905 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200906 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800907
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200908 /* if we are deleting a global entry due to a roam
909 * event, there are two possibilities:
910 * 1) the client roamed from node A to node B => if there
911 * is only one originator left for this client, we mark
912 * it with TT_CLIENT_ROAM, we start a timer and we
913 * wait for node B to claim it. In case of timeout
914 * the entry is purged.
915 *
916 * If there are other originators left, we directly delete
917 * the originator.
918 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200919 * the global entry, since it is useless now.
920 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200921 local_entry = batadv_tt_local_hash_find(bat_priv,
922 tt_global_entry->common.addr);
923 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200924 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200925 batadv_tt_global_del_orig_list(tt_global_entry);
926 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200927 } else
928 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200929 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
930 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200931
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800932
Antonio Quartullicc47f662011-04-27 14:27:57 +0200933out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200934 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200935 batadv_tt_global_entry_free_ref(tt_global_entry);
936 if (local_entry)
937 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200938}
939
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200940void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
941 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200942{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200943 struct tt_global_entry *global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100944 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200945 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200946 struct hashtable_t *hash = bat_priv->tt_global_hash;
947 struct hlist_node *node, *safe;
948 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200949 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200950
Simon Wunderlich6e801492011-10-19 10:28:26 +0200951 if (!hash)
952 return;
953
Antonio Quartullia73105b2011-04-27 14:27:44 +0200954 for (i = 0; i < hash->size; i++) {
955 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200956 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000957
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200958 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100959 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100960 head, hash_entry) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200961 global_entry = container_of(tt_common_entry,
962 struct tt_global_entry,
963 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200964
Sven Eckelmanna5130882012-05-16 20:23:16 +0200965 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
966 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200967
Sven Eckelmanna5130882012-05-16 20:23:16 +0200968 if (hlist_empty(&global_entry->orig_list)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200969 batadv_dbg(DBG_TT, bat_priv,
970 "Deleting global tt entry %pM: %s\n",
Sven Eckelmanna5130882012-05-16 20:23:16 +0200971 global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200972 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973 batadv_tt_global_entry_free_ref(global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200974 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200975 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200976 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977 }
Antonio Quartulli17071572011-11-07 16:36:40 +0100978 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000979}
980
Sven Eckelmanna5130882012-05-16 20:23:16 +0200981static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200982{
983 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100984 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200985 struct tt_global_entry *tt_global_entry;
986 struct hlist_node *node, *node_tmp;
987 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200988 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200989 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200990
Antonio Quartullicc47f662011-04-27 14:27:57 +0200991 for (i = 0; i < hash->size; i++) {
992 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200993 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200994
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200995 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100996 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200997 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100998 tt_global_entry = container_of(tt_common_entry,
999 struct tt_global_entry,
1000 common);
1001 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001002 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001003 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1004 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001005 continue;
1006
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001007 batadv_dbg(DBG_TT, bat_priv,
1008 "Deleting global tt entry (%pM): Roaming timeout\n",
1009 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001010
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001011 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001012 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001013 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001014 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001015 }
1016
Antonio Quartullicc47f662011-04-27 14:27:57 +02001017}
1018
Sven Eckelmanna5130882012-05-16 20:23:16 +02001019static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001021 struct hashtable_t *hash;
1022 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001023 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001024 struct tt_global_entry *tt_global_entry;
1025 struct hlist_node *node, *node_tmp;
1026 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001027 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001028
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001029 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 return;
1031
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001032 hash = bat_priv->tt_global_hash;
1033
1034 for (i = 0; i < hash->size; i++) {
1035 head = &hash->table[i];
1036 list_lock = &hash->list_locks[i];
1037
1038 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001039 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001040 head, hash_entry) {
1041 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001042 tt_global_entry = container_of(tt_common_entry,
1043 struct tt_global_entry,
1044 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001045 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001046 }
1047 spin_unlock_bh(list_lock);
1048 }
1049
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001050 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001051
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001052 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053}
1054
Sven Eckelmanna5130882012-05-16 20:23:16 +02001055static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1056 struct tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001057{
1058 bool ret = false;
1059
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001060 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1061 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001062 ret = true;
1063
1064 return ret;
1065}
1066
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001067struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1068 const uint8_t *src,
1069 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001070{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001071 struct tt_local_entry *tt_local_entry = NULL;
1072 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001073 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001074 struct neigh_node *router = NULL;
1075 struct hlist_head *head;
1076 struct hlist_node *node;
1077 struct tt_orig_list_entry *orig_entry;
1078 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001080 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001081 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001082 if (!tt_local_entry)
1083 goto out;
1084 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001085
Sven Eckelmanna5130882012-05-16 20:23:16 +02001086 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001087 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001088 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001090 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001091 * isolation
1092 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001093 if (tt_local_entry &&
1094 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001095 goto out;
1096
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001097 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001098
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001099 rcu_read_lock();
1100 head = &tt_global_entry->orig_list;
1101 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001102 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001103 if (!router)
1104 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001105
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001106 if (router->tq_avg > best_tq) {
1107 orig_node = orig_entry->orig_node;
1108 best_tq = router->tq_avg;
1109 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001110 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001111 }
1112 /* found anything? */
1113 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1114 orig_node = NULL;
1115 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001116out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001117 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001118 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001119 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001120 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001121
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001122 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001124
1125/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001126static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1127 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001128{
1129 uint16_t total = 0, total_one;
1130 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001131 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001132 struct tt_global_entry *tt_global_entry;
1133 struct hlist_node *node;
1134 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001135 uint32_t i;
1136 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001137
1138 for (i = 0; i < hash->size; i++) {
1139 head = &hash->table[i];
1140
1141 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001142 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001143 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001144 tt_global_entry = container_of(tt_common_entry,
1145 struct tt_global_entry,
1146 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001147 /* Roaming clients are in the global table for
1148 * consistency only. They don't have to be
1149 * taken into account while computing the
1150 * global crc
1151 */
1152 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1153 continue;
1154
1155 /* find out if this global entry is announced by this
1156 * originator
1157 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001158 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1159 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001160 continue;
1161
1162 total_one = 0;
1163 for (j = 0; j < ETH_ALEN; j++)
1164 total_one = crc16_byte(total_one,
1165 tt_global_entry->common.addr[j]);
1166 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167 }
1168 rcu_read_unlock();
1169 }
1170
1171 return total;
1172}
1173
1174/* Calculates the checksum of the local table */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001175static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001176{
1177 uint16_t total = 0, total_one;
1178 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001179 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001180 struct hlist_node *node;
1181 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001182 uint32_t i;
1183 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001184
1185 for (i = 0; i < hash->size; i++) {
1186 head = &hash->table[i];
1187
1188 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001189 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001190 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001191 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001192 * account while computing the CRC
1193 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001194 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001195 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001196 total_one = 0;
1197 for (j = 0; j < ETH_ALEN; j++)
1198 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001199 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200 total ^= total_one;
1201 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001202 rcu_read_unlock();
1203 }
1204
1205 return total;
1206}
1207
Sven Eckelmanna5130882012-05-16 20:23:16 +02001208static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001209{
1210 struct tt_req_node *node, *safe;
1211
1212 spin_lock_bh(&bat_priv->tt_req_list_lock);
1213
1214 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1215 list_del(&node->list);
1216 kfree(node);
1217 }
1218
1219 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1220}
1221
Sven Eckelmanna5130882012-05-16 20:23:16 +02001222static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1223 struct orig_node *orig_node,
1224 const unsigned char *tt_buff,
1225 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001227 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001228
1229 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001230 * last OGM (the OGM could carry no changes)
1231 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 spin_lock_bh(&orig_node->tt_buff_lock);
1233 if (tt_buff_len > 0) {
1234 kfree(orig_node->tt_buff);
1235 orig_node->tt_buff_len = 0;
1236 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1237 if (orig_node->tt_buff) {
1238 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1239 orig_node->tt_buff_len = tt_buff_len;
1240 }
1241 }
1242 spin_unlock_bh(&orig_node->tt_buff_lock);
1243}
1244
Sven Eckelmanna5130882012-05-16 20:23:16 +02001245static void batadv_tt_req_purge(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001246{
1247 struct tt_req_node *node, *safe;
1248
1249 spin_lock_bh(&bat_priv->tt_req_list_lock);
1250 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001251 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001252 list_del(&node->list);
1253 kfree(node);
1254 }
1255 }
1256 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1257}
1258
1259/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001260 * has already been issued for this orig_node, NULL otherwise
1261 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001262static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1263 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001264{
1265 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1266
1267 spin_lock_bh(&bat_priv->tt_req_list_lock);
1268 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001269 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1270 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1271 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001272 goto unlock;
1273 }
1274
1275 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1276 if (!tt_req_node)
1277 goto unlock;
1278
1279 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1280 tt_req_node->issued_at = jiffies;
1281
1282 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1283unlock:
1284 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1285 return tt_req_node;
1286}
1287
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001288/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001289static int batadv_tt_local_valid_entry(const void *entry_ptr,
1290 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001291{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001292 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001293
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001294 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001295 return 0;
1296 return 1;
1297}
1298
Sven Eckelmanna5130882012-05-16 20:23:16 +02001299static int batadv_tt_global_valid(const void *entry_ptr,
1300 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001301{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001302 const struct tt_common_entry *tt_common_entry = entry_ptr;
1303 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001304 const struct orig_node *orig_node = data_ptr;
1305
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001306 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001307 return 0;
1308
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001309 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1310 common);
1311
Sven Eckelmanna5130882012-05-16 20:23:16 +02001312 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001313}
1314
Sven Eckelmanna5130882012-05-16 20:23:16 +02001315static struct sk_buff *
1316batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1317 struct hashtable_t *hash,
1318 struct hard_iface *primary_if,
1319 int (*valid_cb)(const void *, const void *),
1320 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001321{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001322 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001323 struct tt_query_packet *tt_response;
1324 struct tt_change *tt_change;
1325 struct hlist_node *node;
1326 struct hlist_head *head;
1327 struct sk_buff *skb = NULL;
1328 uint16_t tt_tot, tt_count;
1329 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001330 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331
1332 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1333 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1334 tt_len -= tt_len % sizeof(struct tt_change);
1335 }
1336 tt_tot = tt_len / sizeof(struct tt_change);
1337
1338 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1339 if (!skb)
1340 goto out;
1341
1342 skb_reserve(skb, ETH_HLEN);
1343 tt_response = (struct tt_query_packet *)skb_put(skb,
1344 tt_query_size + tt_len);
1345 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001346
1347 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1348 tt_count = 0;
1349
1350 rcu_read_lock();
1351 for (i = 0; i < hash->size; i++) {
1352 head = &hash->table[i];
1353
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001354 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001355 head, hash_entry) {
1356 if (tt_count == tt_tot)
1357 break;
1358
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001359 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001360 continue;
1361
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001362 memcpy(tt_change->addr, tt_common_entry->addr,
1363 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001364 tt_change->flags = NO_FLAGS;
1365
1366 tt_count++;
1367 tt_change++;
1368 }
1369 }
1370 rcu_read_unlock();
1371
Antonio Quartulli9d852392011-10-17 14:25:13 +02001372 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001373 * copied
1374 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001375 tt_response->tt_data = htons(tt_count);
1376
Antonio Quartullia73105b2011-04-27 14:27:44 +02001377out:
1378 return skb;
1379}
1380
Sven Eckelmanna5130882012-05-16 20:23:16 +02001381static int batadv_send_tt_request(struct bat_priv *bat_priv,
1382 struct orig_node *dst_orig_node,
1383 uint8_t ttvn, uint16_t tt_crc,
1384 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385{
1386 struct sk_buff *skb = NULL;
1387 struct tt_query_packet *tt_request;
1388 struct neigh_node *neigh_node = NULL;
1389 struct hard_iface *primary_if;
1390 struct tt_req_node *tt_req_node = NULL;
1391 int ret = 1;
1392
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001393 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394 if (!primary_if)
1395 goto out;
1396
1397 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001398 * reply from the same orig_node yet
1399 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001400 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401 if (!tt_req_node)
1402 goto out;
1403
1404 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1405 if (!skb)
1406 goto out;
1407
1408 skb_reserve(skb, ETH_HLEN);
1409
1410 tt_request = (struct tt_query_packet *)skb_put(skb,
1411 sizeof(struct tt_query_packet));
1412
Sven Eckelmann76543d12011-11-20 15:47:38 +01001413 tt_request->header.packet_type = BAT_TT_QUERY;
1414 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1416 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001417 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001418 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001419 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001420 tt_request->flags = TT_REQUEST;
1421
1422 if (full_table)
1423 tt_request->flags |= TT_FULL_TABLE;
1424
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001425 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001426 if (!neigh_node)
1427 goto out;
1428
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001429 batadv_dbg(DBG_TT, bat_priv,
1430 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1431 dst_orig_node->orig, neigh_node->addr,
1432 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433
Martin Hundebøllf8214862012-04-20 17:02:45 +02001434 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1435
Sven Eckelmann9455e342012-05-12 02:09:37 +02001436 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437 ret = 0;
1438
1439out:
1440 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001441 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001442 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001443 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001444 if (ret)
1445 kfree_skb(skb);
1446 if (ret && tt_req_node) {
1447 spin_lock_bh(&bat_priv->tt_req_list_lock);
1448 list_del(&tt_req_node->list);
1449 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1450 kfree(tt_req_node);
1451 }
1452 return ret;
1453}
1454
Sven Eckelmanna5130882012-05-16 20:23:16 +02001455static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1456 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457{
1458 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1459 struct neigh_node *neigh_node = NULL;
1460 struct hard_iface *primary_if = NULL;
1461 uint8_t orig_ttvn, req_ttvn, ttvn;
1462 int ret = false;
1463 unsigned char *tt_buff;
1464 bool full_table;
1465 uint16_t tt_len, tt_tot;
1466 struct sk_buff *skb = NULL;
1467 struct tt_query_packet *tt_response;
1468
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001469 batadv_dbg(DBG_TT, bat_priv,
1470 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1471 tt_request->src, tt_request->ttvn, tt_request->dst,
1472 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473
1474 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001475 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 if (!req_dst_orig_node)
1477 goto out;
1478
Sven Eckelmannda641192012-05-12 13:48:56 +02001479 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480 if (!res_dst_orig_node)
1481 goto out;
1482
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001483 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484 if (!neigh_node)
1485 goto out;
1486
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001487 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001488 if (!primary_if)
1489 goto out;
1490
1491 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1492 req_ttvn = tt_request->ttvn;
1493
Antonio Quartulli015758d2011-07-09 17:52:13 +02001494 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001496 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497 goto out;
1498
Antonio Quartulli015758d2011-07-09 17:52:13 +02001499 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500 if (tt_request->flags & TT_FULL_TABLE ||
1501 !req_dst_orig_node->tt_buff)
1502 full_table = true;
1503 else
1504 full_table = false;
1505
1506 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001507 * I'll send only one packet with as much TT entries as I can
1508 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001509 if (!full_table) {
1510 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1511 tt_len = req_dst_orig_node->tt_buff_len;
1512 tt_tot = tt_len / sizeof(struct tt_change);
1513
1514 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1515 tt_len + ETH_HLEN);
1516 if (!skb)
1517 goto unlock;
1518
1519 skb_reserve(skb, ETH_HLEN);
1520 tt_response = (struct tt_query_packet *)skb_put(skb,
1521 sizeof(struct tt_query_packet) + tt_len);
1522 tt_response->ttvn = req_ttvn;
1523 tt_response->tt_data = htons(tt_tot);
1524
1525 tt_buff = skb->data + sizeof(struct tt_query_packet);
1526 /* Copy the last orig_node's OGM buffer */
1527 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1528 req_dst_orig_node->tt_buff_len);
1529
1530 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1531 } else {
1532 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1533 sizeof(struct tt_change);
1534 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1535
Sven Eckelmanna5130882012-05-16 20:23:16 +02001536 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1537 bat_priv->tt_global_hash,
1538 primary_if,
1539 batadv_tt_global_valid,
1540 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001541 if (!skb)
1542 goto out;
1543
1544 tt_response = (struct tt_query_packet *)skb->data;
1545 }
1546
Sven Eckelmann76543d12011-11-20 15:47:38 +01001547 tt_response->header.packet_type = BAT_TT_QUERY;
1548 tt_response->header.version = COMPAT_VERSION;
1549 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001550 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1551 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1552 tt_response->flags = TT_RESPONSE;
1553
1554 if (full_table)
1555 tt_response->flags |= TT_FULL_TABLE;
1556
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001557 batadv_dbg(DBG_TT, bat_priv,
1558 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1559 res_dst_orig_node->orig, neigh_node->addr,
1560 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561
Martin Hundebøllf8214862012-04-20 17:02:45 +02001562 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1563
Sven Eckelmann9455e342012-05-12 02:09:37 +02001564 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001565 ret = true;
1566 goto out;
1567
1568unlock:
1569 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1570
1571out:
1572 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001573 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001575 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001577 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001579 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001580 if (!ret)
1581 kfree_skb(skb);
1582 return ret;
1583
1584}
Sven Eckelmanna5130882012-05-16 20:23:16 +02001585static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1586 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001587{
1588 struct orig_node *orig_node = NULL;
1589 struct neigh_node *neigh_node = NULL;
1590 struct hard_iface *primary_if = NULL;
1591 uint8_t my_ttvn, req_ttvn, ttvn;
1592 int ret = false;
1593 unsigned char *tt_buff;
1594 bool full_table;
1595 uint16_t tt_len, tt_tot;
1596 struct sk_buff *skb = NULL;
1597 struct tt_query_packet *tt_response;
1598
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001599 batadv_dbg(DBG_TT, bat_priv,
1600 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1601 tt_request->src, tt_request->ttvn,
1602 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001603
1604
1605 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1606 req_ttvn = tt_request->ttvn;
1607
Sven Eckelmannda641192012-05-12 13:48:56 +02001608 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001609 if (!orig_node)
1610 goto out;
1611
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001612 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613 if (!neigh_node)
1614 goto out;
1615
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001616 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 if (!primary_if)
1618 goto out;
1619
1620 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001621 * is too big send the whole local translation table
1622 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1624 !bat_priv->tt_buff)
1625 full_table = true;
1626 else
1627 full_table = false;
1628
1629 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001630 * I'll send only one packet with as much TT entries as I can
1631 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001632 if (!full_table) {
1633 spin_lock_bh(&bat_priv->tt_buff_lock);
1634 tt_len = bat_priv->tt_buff_len;
1635 tt_tot = tt_len / sizeof(struct tt_change);
1636
1637 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1638 tt_len + ETH_HLEN);
1639 if (!skb)
1640 goto unlock;
1641
1642 skb_reserve(skb, ETH_HLEN);
1643 tt_response = (struct tt_query_packet *)skb_put(skb,
1644 sizeof(struct tt_query_packet) + tt_len);
1645 tt_response->ttvn = req_ttvn;
1646 tt_response->tt_data = htons(tt_tot);
1647
1648 tt_buff = skb->data + sizeof(struct tt_query_packet);
1649 memcpy(tt_buff, bat_priv->tt_buff,
1650 bat_priv->tt_buff_len);
1651 spin_unlock_bh(&bat_priv->tt_buff_lock);
1652 } else {
1653 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1654 sizeof(struct tt_change);
1655 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1656
Sven Eckelmanna5130882012-05-16 20:23:16 +02001657 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1658 bat_priv->tt_local_hash,
1659 primary_if,
1660 batadv_tt_local_valid_entry,
1661 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001662 if (!skb)
1663 goto out;
1664
1665 tt_response = (struct tt_query_packet *)skb->data;
1666 }
1667
Sven Eckelmann76543d12011-11-20 15:47:38 +01001668 tt_response->header.packet_type = BAT_TT_QUERY;
1669 tt_response->header.version = COMPAT_VERSION;
1670 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1672 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1673 tt_response->flags = TT_RESPONSE;
1674
1675 if (full_table)
1676 tt_response->flags |= TT_FULL_TABLE;
1677
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001678 batadv_dbg(DBG_TT, bat_priv,
1679 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1680 orig_node->orig, neigh_node->addr,
1681 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682
Martin Hundebøllf8214862012-04-20 17:02:45 +02001683 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1684
Sven Eckelmann9455e342012-05-12 02:09:37 +02001685 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 ret = true;
1687 goto out;
1688
1689unlock:
1690 spin_unlock_bh(&bat_priv->tt_buff_lock);
1691out:
1692 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001693 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001695 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001697 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698 if (!ret)
1699 kfree_skb(skb);
1700 /* This packet was for me, so it doesn't need to be re-routed */
1701 return true;
1702}
1703
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001704bool batadv_send_tt_response(struct bat_priv *bat_priv,
1705 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001707 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001708 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001709 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001710 return true;
1711
Sven Eckelmanna5130882012-05-16 20:23:16 +02001712 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001713 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001714 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001715 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716}
1717
Sven Eckelmanna5130882012-05-16 20:23:16 +02001718static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1719 struct orig_node *orig_node,
1720 struct tt_change *tt_change,
1721 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001722{
1723 int i;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001724 int is_wifi;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001725 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726
1727 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001728 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001729 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1730 batadv_tt_global_del(bat_priv, orig_node,
1731 (tt_change + i)->addr,
1732 "tt removed by changes",
1733 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001734 } else {
1735 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1736 if (!batadv_tt_global_add(bat_priv, orig_node,
1737 (tt_change + i)->addr, ttvn,
1738 false, is_wifi))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739 /* In case of problem while storing a
1740 * global_entry, we stop the updating
1741 * procedure without committing the
1742 * ttvn change. This will avoid to send
1743 * corrupted data on tt_request
1744 */
1745 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001746 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001748 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001749}
1750
Sven Eckelmanna5130882012-05-16 20:23:16 +02001751static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1752 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753{
1754 struct orig_node *orig_node = NULL;
1755
Sven Eckelmannda641192012-05-12 13:48:56 +02001756 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 if (!orig_node)
1758 goto out;
1759
1760 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001761 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762
Sven Eckelmanna5130882012-05-16 20:23:16 +02001763 _batadv_tt_update_changes(bat_priv, orig_node,
1764 (struct tt_change *)(tt_response + 1),
1765 ntohs(tt_response->tt_data),
1766 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001767
1768 spin_lock_bh(&orig_node->tt_buff_lock);
1769 kfree(orig_node->tt_buff);
1770 orig_node->tt_buff_len = 0;
1771 orig_node->tt_buff = NULL;
1772 spin_unlock_bh(&orig_node->tt_buff_lock);
1773
1774 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1775
1776out:
1777 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001778 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001779}
1780
Sven Eckelmanna5130882012-05-16 20:23:16 +02001781static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1782 struct orig_node *orig_node,
1783 uint16_t tt_num_changes, uint8_t ttvn,
1784 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001786 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1787 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788
Sven Eckelmanna5130882012-05-16 20:23:16 +02001789 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1790 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 atomic_set(&orig_node->last_ttvn, ttvn);
1792}
1793
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001794bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001796 struct tt_local_entry *tt_local_entry = NULL;
1797 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001798
Sven Eckelmanna5130882012-05-16 20:23:16 +02001799 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001800 if (!tt_local_entry)
1801 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001802 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001803 * consistency purpose)
1804 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001805 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001806 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001807 ret = true;
1808out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001809 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001810 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001811 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812}
1813
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001814void batadv_handle_tt_response(struct bat_priv *bat_priv,
1815 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816{
1817 struct tt_req_node *node, *safe;
1818 struct orig_node *orig_node = NULL;
1819
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001820 batadv_dbg(DBG_TT, bat_priv,
1821 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1822 tt_response->src, tt_response->ttvn,
1823 ntohs(tt_response->tt_data),
1824 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001826 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001827 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001828 goto out;
1829
Sven Eckelmannda641192012-05-12 13:48:56 +02001830 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001831 if (!orig_node)
1832 goto out;
1833
1834 if (tt_response->flags & TT_FULL_TABLE)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001835 batadv_tt_fill_gtable(bat_priv, tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836 else
Sven Eckelmanna5130882012-05-16 20:23:16 +02001837 batadv_tt_update_changes(bat_priv, orig_node,
1838 ntohs(tt_response->tt_data),
1839 tt_response->ttvn,
1840 (struct tt_change *)(tt_response + 1));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841
1842 /* Delete the tt_req_node from pending tt_requests list */
1843 spin_lock_bh(&bat_priv->tt_req_list_lock);
1844 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001845 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001846 continue;
1847 list_del(&node->list);
1848 kfree(node);
1849 }
1850 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1851
1852 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001853 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001854 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001855 * unset the flag
1856 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001857 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001858out:
1859 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001860 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861}
1862
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001863int batadv_tt_init(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001865 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001866
Sven Eckelmanna5130882012-05-16 20:23:16 +02001867 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001868 if (ret < 0)
1869 return ret;
1870
Sven Eckelmanna5130882012-05-16 20:23:16 +02001871 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001872 if (ret < 0)
1873 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001874
Sven Eckelmanna5130882012-05-16 20:23:16 +02001875 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876
1877 return 1;
1878}
1879
Sven Eckelmanna5130882012-05-16 20:23:16 +02001880static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001882 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883
Antonio Quartullicc47f662011-04-27 14:27:57 +02001884 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001885
Antonio Quartullicc47f662011-04-27 14:27:57 +02001886 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1887 list_del(&node->list);
1888 kfree(node);
1889 }
1890
1891 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1892}
1893
Sven Eckelmanna5130882012-05-16 20:23:16 +02001894static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001895{
1896 struct tt_roam_node *node, *safe;
1897
1898 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1899 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001900 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001901 continue;
1902
1903 list_del(&node->list);
1904 kfree(node);
1905 }
1906 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1907}
1908
1909/* This function checks whether the client already reached the
1910 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1911 * will not be sent.
1912 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001913 * returns true if the ROAMING_ADV can be sent, false otherwise
1914 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001915static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1916 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001917{
1918 struct tt_roam_node *tt_roam_node;
1919 bool ret = false;
1920
1921 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1922 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001923 * reply from the same orig_node yet
1924 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001925 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001926 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001927 continue;
1928
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001929 if (batadv_has_timed_out(tt_roam_node->first_time,
1930 ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001931 continue;
1932
1933 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1934 /* Sorry, you roamed too many times! */
1935 goto unlock;
1936 ret = true;
1937 break;
1938 }
1939
1940 if (!ret) {
1941 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1942 if (!tt_roam_node)
1943 goto unlock;
1944
1945 tt_roam_node->first_time = jiffies;
1946 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1947 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1948
1949 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1950 ret = true;
1951 }
1952
1953unlock:
1954 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1955 return ret;
1956}
1957
Sven Eckelmanna5130882012-05-16 20:23:16 +02001958static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1959 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001960{
1961 struct neigh_node *neigh_node = NULL;
1962 struct sk_buff *skb = NULL;
1963 struct roam_adv_packet *roam_adv_packet;
1964 int ret = 1;
1965 struct hard_iface *primary_if;
1966
1967 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001968 * already roamed to us too many times
1969 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001970 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001971 goto out;
1972
1973 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1974 if (!skb)
1975 goto out;
1976
1977 skb_reserve(skb, ETH_HLEN);
1978
1979 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1980 sizeof(struct roam_adv_packet));
1981
Sven Eckelmann76543d12011-11-20 15:47:38 +01001982 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1983 roam_adv_packet->header.version = COMPAT_VERSION;
1984 roam_adv_packet->header.ttl = TTL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001985 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001986 if (!primary_if)
1987 goto out;
1988 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001989 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001990 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1991 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1992
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001993 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001994 if (!neigh_node)
1995 goto out;
1996
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001997 batadv_dbg(DBG_TT, bat_priv,
1998 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1999 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002000
Martin Hundebøllf8214862012-04-20 17:02:45 +02002001 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2002
Sven Eckelmann9455e342012-05-12 02:09:37 +02002003 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002004 ret = 0;
2005
2006out:
2007 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002008 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002009 if (ret)
2010 kfree_skb(skb);
2011 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002012}
2013
Sven Eckelmanna5130882012-05-16 20:23:16 +02002014static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002015{
2016 struct delayed_work *delayed_work =
2017 container_of(work, struct delayed_work, work);
2018 struct bat_priv *bat_priv =
2019 container_of(delayed_work, struct bat_priv, tt_work);
2020
Sven Eckelmanna5130882012-05-16 20:23:16 +02002021 batadv_tt_local_purge(bat_priv);
2022 batadv_tt_global_roam_purge(bat_priv);
2023 batadv_tt_req_purge(bat_priv);
2024 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025
Sven Eckelmanna5130882012-05-16 20:23:16 +02002026 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002027}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002029void batadv_tt_free(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002030{
2031 cancel_delayed_work_sync(&bat_priv->tt_work);
2032
Sven Eckelmanna5130882012-05-16 20:23:16 +02002033 batadv_tt_local_table_free(bat_priv);
2034 batadv_tt_global_table_free(bat_priv);
2035 batadv_tt_req_list_free(bat_priv);
2036 batadv_tt_changes_list_free(bat_priv);
2037 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038
2039 kfree(bat_priv->tt_buff);
2040}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002041
Antonio Quartulli697f2532011-11-07 16:47:01 +01002042/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002043 * in the given hash table and returns the number of modified entries
2044 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002045static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2046 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002047{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002048 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002049 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002050 struct hlist_head *head;
2051 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002052 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002053
2054 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002055 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002056
2057 for (i = 0; i < hash->size; i++) {
2058 head = &hash->table[i];
2059
2060 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002061 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002062 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002063 if (enable) {
2064 if ((tt_common_entry->flags & flags) == flags)
2065 continue;
2066 tt_common_entry->flags |= flags;
2067 } else {
2068 if (!(tt_common_entry->flags & flags))
2069 continue;
2070 tt_common_entry->flags &= ~flags;
2071 }
2072 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002073 }
2074 rcu_read_unlock();
2075 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002076out:
2077 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002078}
2079
2080/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002081static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002082{
2083 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002084 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002085 struct tt_local_entry *tt_local_entry;
2086 struct hlist_node *node, *node_tmp;
2087 struct hlist_head *head;
2088 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002089 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002090
2091 if (!hash)
2092 return;
2093
2094 for (i = 0; i < hash->size; i++) {
2095 head = &hash->table[i];
2096 list_lock = &hash->list_locks[i];
2097
2098 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002099 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002100 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002101 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002102 continue;
2103
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002104 batadv_dbg(DBG_TT, bat_priv,
2105 "Deleting local tt entry (%pM): pending\n",
2106 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002107
2108 atomic_dec(&bat_priv->num_local_tt);
2109 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002110 tt_local_entry = container_of(tt_common_entry,
2111 struct tt_local_entry,
2112 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002113 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002114 }
2115 spin_unlock_bh(list_lock);
2116 }
2117
2118}
2119
Sven Eckelmanna5130882012-05-16 20:23:16 +02002120static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2121 unsigned char **packet_buff,
2122 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002123{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002124 uint16_t changed_num = 0;
2125
2126 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2127 return -ENOENT;
2128
Sven Eckelmanna5130882012-05-16 20:23:16 +02002129 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2130 TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002131
2132 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002133 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002134 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002135 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002136
2137 /* Increment the TTVN only once per OGM interval */
2138 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002139 batadv_dbg(DBG_TT, bat_priv,
2140 "Local changes committed, updating to ttvn %u\n",
2141 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002142 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002143
2144 /* reset the sending counter */
2145 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2146
Sven Eckelmanna5130882012-05-16 20:23:16 +02002147 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2148 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002149}
2150
2151/* when calling this function (hard_iface == primary_if) has to be true */
2152int batadv_tt_append_diff(struct bat_priv *bat_priv,
2153 unsigned char **packet_buff, int *packet_buff_len,
2154 int packet_min_len)
2155{
2156 int tt_num_changes;
2157
2158 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002159 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2160 packet_buff_len,
2161 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002162
2163 /* if the changes have been sent often enough */
2164 if ((tt_num_changes < 0) &&
2165 (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002166 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2167 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002168 tt_num_changes = 0;
2169 }
2170
2171 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002172}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002173
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002174bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2175 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002176{
2177 struct tt_local_entry *tt_local_entry = NULL;
2178 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002179 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002180
2181 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002182 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002183
Sven Eckelmanna5130882012-05-16 20:23:16 +02002184 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002185 if (!tt_local_entry)
2186 goto out;
2187
Sven Eckelmanna5130882012-05-16 20:23:16 +02002188 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002189 if (!tt_global_entry)
2190 goto out;
2191
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002192 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002193 goto out;
2194
Marek Lindner5870adc2012-06-20 17:16:05 +02002195 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002196
2197out:
2198 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002199 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002200 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002201 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002202 return ret;
2203}
Marek Lindnera943cac2011-07-30 13:10:18 +02002204
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002205void batadv_tt_update_orig(struct bat_priv *bat_priv,
2206 struct orig_node *orig_node,
2207 const unsigned char *tt_buff, uint8_t tt_num_changes,
2208 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002209{
2210 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2211 bool full_table = true;
2212
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002213 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002214 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002215 return;
2216
Antonio Quartulli17071572011-11-07 16:36:40 +01002217 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002218 * increased by one -> we can apply the attached changes
2219 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002220 if ((!orig_node->tt_initialised && ttvn == 1) ||
2221 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002222 /* the OGM could not contain the changes due to their size or
2223 * because they have already been sent TT_OGM_APPEND_MAX times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002224 * In this case send a tt request
2225 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002226 if (!tt_num_changes) {
2227 full_table = false;
2228 goto request_table;
2229 }
2230
Sven Eckelmanna5130882012-05-16 20:23:16 +02002231 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2232 ttvn, (struct tt_change *)tt_buff);
Marek Lindnera943cac2011-07-30 13:10:18 +02002233
2234 /* Even if we received the precomputed crc with the OGM, we
2235 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002236 * in the global table
2237 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002238 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002239
2240 /* The ttvn alone is not enough to guarantee consistency
2241 * because a single value could represent different states
2242 * (due to the wrap around). Thus a node has to check whether
2243 * the resulting table (after applying the changes) is still
2244 * consistent or not. E.g. a node could disconnect while its
2245 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2246 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002247 * inconsistency
2248 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002249 if (orig_node->tt_crc != tt_crc)
2250 goto request_table;
2251
2252 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002253 * unset the flag
2254 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002255 orig_node->tt_poss_change = false;
2256 } else {
2257 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002258 * in sync anymore -> request fresh tt data
2259 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002260 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2261 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002262request_table:
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002263 batadv_dbg(DBG_TT, bat_priv,
2264 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2265 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2266 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002267 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2268 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002269 return;
2270 }
2271 }
2272}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002273
2274/* returns true whether we know that the client has moved from its old
2275 * originator to another one. This entry is kept is still kept for consistency
2276 * purposes
2277 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002278bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2279 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002280{
2281 struct tt_global_entry *tt_global_entry;
2282 bool ret = false;
2283
Sven Eckelmanna5130882012-05-16 20:23:16 +02002284 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002285 if (!tt_global_entry)
2286 goto out;
2287
2288 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002289 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002290out:
2291 return ret;
2292}