blob: f7a615261f4e9b5946abea05e86360d5925382b5 [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);
145 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200146 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200147 kfree(orig_entry);
148}
149
Sven Eckelmanna5130882012-05-16 20:23:16 +0200150static void
151batadv_tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200152{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200153 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void batadv_tt_local_event(struct bat_priv *bat_priv,
157 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200158{
159 struct tt_change_node *tt_change_node;
160
161 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
162
163 if (!tt_change_node)
164 return;
165
Antonio Quartulliff66c972011-06-30 01:14:00 +0200166 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200167 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
168
169 spin_lock_bh(&bat_priv->tt_changes_list_lock);
170 /* track the change in the OGMinterval list */
171 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
172 atomic_inc(&bat_priv->tt_local_changes);
173 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
174
175 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
176}
177
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200178int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179{
180 return changes_num * sizeof(struct tt_change);
181}
182
Sven Eckelmanna5130882012-05-16 20:23:16 +0200183static int batadv_tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200185 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200186 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200188 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200190 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200191 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Sven Eckelmann5346c352012-05-05 13:27:28 +0200193 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194}
195
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200196void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
197 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198{
199 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200200 struct tt_local_entry *tt_local_entry = NULL;
201 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200202 struct hlist_head *head;
203 struct hlist_node *node;
204 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100205 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Sven Eckelmanna5130882012-05-16 20:23:16 +0200207 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200209 if (tt_local_entry) {
210 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100211 /* possibly unset the TT_CLIENT_PENDING flag */
212 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200213 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 }
215
Sven Eckelmann704509b2011-05-14 23:14:54 +0200216 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200217 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200218 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200219
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200220 batadv_dbg(DBG_TT, bat_priv,
221 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
222 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100224 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
225 tt_local_entry->common.flags = NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200226 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100227 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
228 atomic_set(&tt_local_entry->common.refcount, 2);
229 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
231 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200232 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100233 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100235 /* The local entry has to be marked as NEW to avoid to send it in
236 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200237 * (consistency check)
238 */
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100239 tt_local_entry->common.flags |= TT_CLIENT_NEW;
240
Sven Eckelmanna5130882012-05-16 20:23:16 +0200241 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200242 batadv_choose_orig,
243 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200244 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100245
246 if (unlikely(hash_added != 0)) {
247 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200248 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100249 goto out;
250 }
251
Sven Eckelmanna5130882012-05-16 20:23:16 +0200252 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200253
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200255 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Antonio Quartullicc47f662011-04-27 14:27:57 +0200257 /* Check whether it is a roaming! */
258 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200259 /* These node are probably going to update their tt table */
260 head = &tt_global_entry->orig_list;
261 rcu_read_lock();
262 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
263 orig_entry->orig_node->tt_poss_change = true;
264
Sven Eckelmanna5130882012-05-16 20:23:16 +0200265 batadv_send_roam_adv(bat_priv,
266 tt_global_entry->common.addr,
267 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200268 }
269 rcu_read_unlock();
270 /* The global entry has to be marked as ROAMING and
271 * has to be kept for consistency purpose
272 */
David S. Miller220b07e2011-12-16 15:07:28 -0500273 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100274 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200275 }
276out:
277 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200278 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200279 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200280 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281}
282
Sven Eckelmanna5130882012-05-16 20:23:16 +0200283static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
284 int *packet_buff_len,
285 int min_packet_len,
286 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800288 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800290 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
291
292 /* keep old buffer if kmalloc should fail */
293 if (new_buff) {
294 memcpy(new_buff, *packet_buff, min_packet_len);
295 kfree(*packet_buff);
296 *packet_buff = new_buff;
297 *packet_buff_len = new_packet_len;
298 }
299}
300
Sven Eckelmanna5130882012-05-16 20:23:16 +0200301static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
302 unsigned char **packet_buff,
303 int *packet_buff_len,
304 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800305{
306 struct hard_iface *primary_if;
307 int req_len;
308
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200309 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800310
311 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200312 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800313
314 /* if we have too many changes for one packet don't send any
315 * and wait for the tt table request which will be fragmented
316 */
317 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
318 req_len = min_packet_len;
319
Sven Eckelmanna5130882012-05-16 20:23:16 +0200320 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
321 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800322
323 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200324 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800325}
326
Sven Eckelmanna5130882012-05-16 20:23:16 +0200327static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
328 unsigned char **packet_buff,
329 int *packet_buff_len,
330 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800331{
332 struct tt_change_node *entry, *safe;
333 int count = 0, tot_changes = 0, new_len;
334 unsigned char *tt_buff;
335
Sven Eckelmanna5130882012-05-16 20:23:16 +0200336 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
337 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800338
339 new_len = *packet_buff_len - min_packet_len;
340 tt_buff = *packet_buff + min_packet_len;
341
342 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200343 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344
Antonio Quartullia73105b2011-04-27 14:27:44 +0200345 spin_lock_bh(&bat_priv->tt_changes_list_lock);
346 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347
Antonio Quartullia73105b2011-04-27 14:27:44 +0200348 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100349 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200350 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200351 memcpy(tt_buff + batadv_tt_len(count),
Antonio Quartullia73105b2011-04-27 14:27:44 +0200352 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 count++;
354 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200355 list_del(&entry->list);
356 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200358 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Antonio Quartullia73105b2011-04-27 14:27:44 +0200360 /* Keep the buffer for possible tt_request */
361 spin_lock_bh(&bat_priv->tt_buff_lock);
362 kfree(bat_priv->tt_buff);
363 bat_priv->tt_buff_len = 0;
364 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800365 /* check whether this new OGM has no changes due to size problems */
366 if (new_len > 0) {
367 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200368 * instead of providing the diff
369 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800370 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200371 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800372 memcpy(bat_priv->tt_buff, tt_buff, new_len);
373 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200374 }
375 }
376 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindner08ad76e2012-04-23 16:32:55 +0800378 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379}
380
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200381int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382{
383 struct net_device *net_dev = (struct net_device *)seq->private;
384 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200385 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100386 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200387 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000388 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200390 uint32_t i;
391 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200393 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200394 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100395 ret = seq_printf(seq,
396 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200397 net_dev->name);
398 goto out;
399 }
400
401 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100402 ret = seq_printf(seq,
403 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200404 net_dev->name);
405 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 }
407
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100408 seq_printf(seq,
409 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200410 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412 for (i = 0; i < hash->size; i++) {
413 head = &hash->table[i];
414
Marek Lindner7aadf882011-02-18 12:28:09 +0000415 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100416 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000417 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200418 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100419 tt_common_entry->addr,
420 (tt_common_entry->flags &
421 TT_CLIENT_ROAM ? 'R' : '.'),
422 (tt_common_entry->flags &
423 TT_CLIENT_NOPURGE ? 'P' : '.'),
424 (tt_common_entry->flags &
425 TT_CLIENT_NEW ? 'N' : '.'),
426 (tt_common_entry->flags &
427 TT_CLIENT_PENDING ? 'X' : '.'),
428 (tt_common_entry->flags &
429 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000431 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200433out:
434 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200435 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200436 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437}
438
Sven Eckelmanna5130882012-05-16 20:23:16 +0200439static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
440 struct tt_local_entry *tt_local_entry,
441 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200443 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
444 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Antonio Quartulli015758d2011-07-09 17:52:13 +0200446 /* The local client has to be marked as "pending to be removed" but has
447 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200448 * response issued before the net ttvn increment (consistency check)
449 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100450 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100451
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200452 batadv_dbg(DBG_TT, bat_priv,
453 "Local tt entry (%pM) pending to be removed: %s\n",
454 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455}
456
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200457void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
458 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200460 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
Sven Eckelmanna5130882012-05-16 20:23:16 +0200462 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200463 if (!tt_local_entry)
464 goto out;
465
Sven Eckelmanna5130882012-05-16 20:23:16 +0200466 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
467 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
468 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200469out:
470 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200471 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472}
473
Sven Eckelmanna5130882012-05-16 20:23:16 +0200474static void batadv_tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200476 struct hashtable_t *hash = bat_priv->tt_local_hash;
477 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100478 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000479 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200481 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200482 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484 for (i = 0; i < hash->size; i++) {
485 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200486 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200488 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100489 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000490 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100491 tt_local_entry = container_of(tt_common_entry,
492 struct tt_local_entry,
493 common);
494 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000495 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200497 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100498 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200499 continue;
500
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200501 if (!batadv_has_timed_out(tt_local_entry->last_seen,
502 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000503 continue;
504
Sven Eckelmanna5130882012-05-16 20:23:16 +0200505 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
506 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200508 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509 }
510
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511}
512
Sven Eckelmanna5130882012-05-16 20:23:16 +0200513static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200516 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100517 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200518 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200519 struct hlist_node *node, *node_tmp;
520 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200521 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200522
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200523 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 return;
525
Antonio Quartullia73105b2011-04-27 14:27:44 +0200526 hash = bat_priv->tt_local_hash;
527
528 for (i = 0; i < hash->size; i++) {
529 head = &hash->table[i];
530 list_lock = &hash->list_locks[i];
531
532 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100533 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200534 head, hash_entry) {
535 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100536 tt_local_entry = container_of(tt_common_entry,
537 struct tt_local_entry,
538 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200539 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200540 }
541 spin_unlock_bh(list_lock);
542 }
543
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200544 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200545
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200546 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547}
548
Sven Eckelmanna5130882012-05-16 20:23:16 +0200549static int batadv_tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200551 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200552 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200554 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200556 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200557 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558
Sven Eckelmann5346c352012-05-05 13:27:28 +0200559 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560}
561
Sven Eckelmanna5130882012-05-16 20:23:16 +0200562static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200563{
564 struct tt_change_node *entry, *safe;
565
566 spin_lock_bh(&bat_priv->tt_changes_list_lock);
567
568 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
569 list) {
570 list_del(&entry->list);
571 kfree(entry);
572 }
573
574 atomic_set(&bat_priv->tt_local_changes, 0);
575 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
576}
577
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200578/* find out if an orig_node is already in the list of a tt_global_entry.
579 * returns 1 if found, 0 otherwise
580 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200581static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
582 const struct orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200583{
584 struct tt_orig_list_entry *tmp_orig_entry;
585 const struct hlist_head *head;
586 struct hlist_node *node;
587 bool found = false;
588
589 rcu_read_lock();
590 head = &entry->orig_list;
591 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
592 if (tmp_orig_entry->orig_node == orig_node) {
593 found = true;
594 break;
595 }
596 }
597 rcu_read_unlock();
598 return found;
599}
600
Sven Eckelmanna5130882012-05-16 20:23:16 +0200601static void
602batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
603 struct orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200604{
605 struct tt_orig_list_entry *orig_entry;
606
607 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
608 if (!orig_entry)
609 return;
610
611 INIT_HLIST_NODE(&orig_entry->list);
612 atomic_inc(&orig_node->refcount);
613 atomic_inc(&orig_node->tt_size);
614 orig_entry->orig_node = orig_node;
615 orig_entry->ttvn = ttvn;
616
617 spin_lock_bh(&tt_global_entry->list_lock);
618 hlist_add_head_rcu(&orig_entry->list,
619 &tt_global_entry->orig_list);
620 spin_unlock_bh(&tt_global_entry->list_lock);
621}
622
Antonio Quartullia73105b2011-04-27 14:27:44 +0200623/* caller must hold orig_node refcount */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200624int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
625 const unsigned char *tt_addr, uint8_t ttvn,
626 bool roaming, bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200628 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200629 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100630 int hash_added;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200631 struct tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmanna5130882012-05-16 20:23:16 +0200633 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634
Antonio Quartullia73105b2011-04-27 14:27:44 +0200635 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200636 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
637 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 goto out;
640
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200641 common = &tt_global_entry->common;
642 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200643
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200644 common->flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200645 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200646 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200647
648 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
649 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200650
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200651 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200652 batadv_compare_tt,
653 batadv_choose_orig, common,
654 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100655
656 if (unlikely(hash_added != 0)) {
657 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200658 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100659 goto out_remove;
660 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200661
Sven Eckelmanna5130882012-05-16 20:23:16 +0200662 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
663 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200664 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200665 /* there is already a global entry, use this one. */
666
667 /* If there is the TT_CLIENT_ROAM flag set, there is only one
668 * originator left in the list and we previously received a
669 * delete + roaming change for this originator.
670 *
671 * We should first delete the old originator before adding the
672 * new one.
673 */
674 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200675 batadv_tt_global_del_orig_list(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200676 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
677 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200679
Sven Eckelmanna5130882012-05-16 20:23:16 +0200680 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
681 orig_node))
682 batadv_tt_global_add_orig_entry(tt_global_entry,
683 orig_node, ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200685
Antonio Quartullibc279082011-07-07 15:35:35 +0200686 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100687 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200688
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200689 batadv_dbg(DBG_TT, bat_priv,
690 "Creating new global tt entry: %pM (via %pM)\n",
691 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200692
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100693out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200695 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
696 "global tt received", roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200697 ret = 1;
698out:
699 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200700 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200701 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702}
703
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200704/* print all orig nodes who announce the address for this global entry.
705 * it is assumed that the caller holds rcu_read_lock();
706 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200707static void
708batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
709 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200710{
711 struct hlist_head *head;
712 struct hlist_node *node;
713 struct tt_orig_list_entry *orig_entry;
714 struct tt_common_entry *tt_common_entry;
715 uint16_t flags;
716 uint8_t last_ttvn;
717
718 tt_common_entry = &tt_global_entry->common;
719
720 head = &tt_global_entry->orig_list;
721
722 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
723 flags = tt_common_entry->flags;
724 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
725 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
726 tt_global_entry->common.addr, orig_entry->ttvn,
727 orig_entry->orig_node->orig, last_ttvn,
728 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
729 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
730 }
731}
732
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200733int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734{
735 struct net_device *net_dev = (struct net_device *)seq->private;
736 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200737 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100738 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200739 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200740 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000741 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200743 uint32_t i;
744 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200746 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200747 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100748 ret = seq_printf(seq,
749 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200750 net_dev->name);
751 goto out;
752 }
753
754 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100755 ret = seq_printf(seq,
756 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200757 net_dev->name);
758 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759 }
760
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200761 seq_printf(seq,
762 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200764 seq_printf(seq, " %-13s %s %-15s %s %s\n",
765 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 for (i = 0; i < hash->size; i++) {
768 head = &hash->table[i];
769
Marek Lindner7aadf882011-02-18 12:28:09 +0000770 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100771 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000772 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100773 tt_global_entry = container_of(tt_common_entry,
774 struct tt_global_entry,
775 common);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200776 batadv_tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000778 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200780out:
781 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200782 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200783 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784}
785
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200786/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200787static void
788batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200790 struct hlist_head *head;
791 struct hlist_node *node, *safe;
792 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200793
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200794 spin_lock_bh(&tt_global_entry->list_lock);
795 head = &tt_global_entry->orig_list;
796 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
797 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200798 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200799 }
800 spin_unlock_bh(&tt_global_entry->list_lock);
801
802}
803
Sven Eckelmanna5130882012-05-16 20:23:16 +0200804static void
805batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
806 struct tt_global_entry *tt_global_entry,
807 struct orig_node *orig_node,
808 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200809{
810 struct hlist_head *head;
811 struct hlist_node *node, *safe;
812 struct tt_orig_list_entry *orig_entry;
813
814 spin_lock_bh(&tt_global_entry->list_lock);
815 head = &tt_global_entry->orig_list;
816 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
817 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200818 batadv_dbg(DBG_TT, bat_priv,
819 "Deleting %pM from global tt entry %pM: %s\n",
820 orig_node->orig,
821 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200822 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200823 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200824 }
825 }
826 spin_unlock_bh(&tt_global_entry->list_lock);
827}
828
Sven Eckelmanna5130882012-05-16 20:23:16 +0200829static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
830 struct tt_global_entry *tt_global_entry,
831 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200832{
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200833 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
834 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200835
Sven Eckelmanna5130882012-05-16 20:23:16 +0200836 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200837 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200838 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840}
841
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200842/* If the client is to be deleted, we check if it is the last origantor entry
843 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
844 * otherwise we simply remove the originator scheduled for deletion.
845 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200846static void
847batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
848 struct tt_global_entry *tt_global_entry,
849 struct orig_node *orig_node, const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200850{
851 bool last_entry = true;
852 struct hlist_head *head;
853 struct hlist_node *node;
854 struct tt_orig_list_entry *orig_entry;
855
856 /* no local entry exists, case 1:
857 * Check if this is the last one or if other entries exist.
858 */
859
860 rcu_read_lock();
861 head = &tt_global_entry->orig_list;
862 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
863 if (orig_entry->orig_node != orig_node) {
864 last_entry = false;
865 break;
866 }
867 }
868 rcu_read_unlock();
869
870 if (last_entry) {
871 /* its the last one, mark for roaming. */
872 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
873 tt_global_entry->roam_at = jiffies;
874 } else
875 /* there is another entry, we can simply delete this
876 * one and can still use the other one.
877 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200878 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
879 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200880}
881
882
883
Sven Eckelmanna5130882012-05-16 20:23:16 +0200884static void batadv_tt_global_del(struct bat_priv *bat_priv,
885 struct orig_node *orig_node,
886 const unsigned char *addr,
887 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000888{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200889 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmanna5130882012-05-16 20:23:16 +0200890 struct tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891
Sven Eckelmanna5130882012-05-16 20:23:16 +0200892 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200893 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200894 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200895
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200896 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200897 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
898 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800899
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200900 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200901 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
902 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200903
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800904 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200905 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800906
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200907 /* if we are deleting a global entry due to a roam
908 * event, there are two possibilities:
909 * 1) the client roamed from node A to node B => if there
910 * is only one originator left for this client, we mark
911 * it with TT_CLIENT_ROAM, we start a timer and we
912 * wait for node B to claim it. In case of timeout
913 * the entry is purged.
914 *
915 * If there are other originators left, we directly delete
916 * the originator.
917 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200918 * the global entry, since it is useless now.
919 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200920 local_entry = batadv_tt_local_hash_find(bat_priv,
921 tt_global_entry->common.addr);
922 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200923 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200924 batadv_tt_global_del_orig_list(tt_global_entry);
925 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200926 } else
927 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200928 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
929 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200930
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800931
Antonio Quartullicc47f662011-04-27 14:27:57 +0200932out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200933 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200934 batadv_tt_global_entry_free_ref(tt_global_entry);
935 if (local_entry)
936 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200937}
938
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200939void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
940 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200941{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200942 struct tt_global_entry *global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100943 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200944 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200945 struct hashtable_t *hash = bat_priv->tt_global_hash;
946 struct hlist_node *node, *safe;
947 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200948 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200949
Simon Wunderlich6e801492011-10-19 10:28:26 +0200950 if (!hash)
951 return;
952
Antonio Quartullia73105b2011-04-27 14:27:44 +0200953 for (i = 0; i < hash->size; i++) {
954 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200955 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000956
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200957 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100958 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100959 head, hash_entry) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200960 global_entry = container_of(tt_common_entry,
961 struct tt_global_entry,
962 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200963
Sven Eckelmanna5130882012-05-16 20:23:16 +0200964 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
965 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200966
Sven Eckelmanna5130882012-05-16 20:23:16 +0200967 if (hlist_empty(&global_entry->orig_list)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200968 batadv_dbg(DBG_TT, bat_priv,
969 "Deleting global tt entry %pM: %s\n",
Sven Eckelmanna5130882012-05-16 20:23:16 +0200970 global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200971 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200972 batadv_tt_global_entry_free_ref(global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200973 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200974 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200975 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000976 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200977 atomic_set(&orig_node->tt_size, 0);
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;
2179 bool ret = true;
2180
2181 if (!atomic_read(&bat_priv->ap_isolation))
2182 return false;
2183
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
Sven Eckelmanna5130882012-05-16 20:23:16 +02002192 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002193 goto out;
2194
2195 ret = false;
2196
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}