blob: 1ff7fbebf92141b27a39c69723df8cd0838d8e53 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020025#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020026#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020029#include "routing.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullia73105b2011-04-27 14:27:44 +020031#include <linux/crc16.h>
32
33static void _tt_global_del(struct bat_priv *bat_priv,
34 struct tt_global_entry *tt_global_entry,
35 const char *message);
36static void tt_purge(struct work_struct *work);
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 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +010039static int 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
Antonio Quartullia73105b2011-04-27 14:27:44 +020047static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048{
Antonio Quartullia73105b2011-04-27 14:27:44 +020049 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
50 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
51 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
Antonio Quartulli48100ba2011-10-30 12:17:33 +010054static struct tt_common_entry *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
65 index = choose_orig(data, hash->size);
66 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) {
70 if (!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
84static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
85 const void *data)
86{
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
89
90 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
91 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
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020098 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
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100103 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
104 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
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200111static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
112{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100113 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
114 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115}
116
Simon Wunderlich531027f2011-10-19 11:02:25 +0200117static void tt_global_entry_free_rcu(struct rcu_head *rcu)
118{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100119 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200120 struct tt_global_entry *tt_global_entry;
121
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100122 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
123 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
124 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125
126 if (tt_global_entry->orig_node)
127 orig_node_free_ref(tt_global_entry->orig_node);
128
129 kfree(tt_global_entry);
130}
131
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200132static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
133{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100134 if (atomic_dec_and_test(&tt_global_entry->common.refcount))
135 call_rcu(&tt_global_entry->common.rcu,
136 tt_global_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200137}
138
Antonio Quartulliff66c972011-06-30 01:14:00 +0200139static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
140 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200141{
142 struct tt_change_node *tt_change_node;
143
144 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
145
146 if (!tt_change_node)
147 return;
148
Antonio Quartulliff66c972011-06-30 01:14:00 +0200149 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200150 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
151
152 spin_lock_bh(&bat_priv->tt_changes_list_lock);
153 /* track the change in the OGMinterval list */
154 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
155 atomic_inc(&bat_priv->tt_local_changes);
156 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
157
158 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
159}
160
161int tt_len(int changes_num)
162{
163 return changes_num * sizeof(struct tt_change);
164}
165
166static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200168 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169 return 1;
170
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200171 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200173 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174 return 0;
175
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 return 1;
177}
178
Antonio Quartullibc279082011-07-07 15:35:35 +0200179void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
180 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181{
182 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200183 struct tt_local_entry *tt_local_entry = NULL;
184 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100185 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200189 if (tt_local_entry) {
190 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200191 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192 }
193
Sven Eckelmann704509b2011-05-14 23:14:54 +0200194 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200195 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200196 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200197
Antonio Quartullia73105b2011-04-27 14:27:44 +0200198 bat_dbg(DBG_TT, bat_priv,
199 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
200 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100202 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
203 tt_local_entry->common.flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200204 if (is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100205 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
206 atomic_set(&tt_local_entry->common.refcount, 2);
207 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
209 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000210 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100211 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100213 /* The local entry has to be marked as NEW to avoid to send it in
214 * a full table response going out before the next ttvn increment
215 * (consistency check) */
216 tt_local_entry->common.flags |= TT_CLIENT_NEW;
217
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100218 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
219 &tt_local_entry->common,
220 &tt_local_entry->common.hash_entry);
221
222 if (unlikely(hash_added != 0)) {
223 /* remove the reference for the hash */
224 tt_local_entry_free_ref(tt_local_entry);
225 goto out;
226 }
227
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100228 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200229
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200231 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Antonio Quartullicc47f662011-04-27 14:27:57 +0200233 /* Check whether it is a roaming! */
234 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200235 /* This node is probably going to update its tt table */
236 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100237 /* The global entry has to be marked as ROAMING and has to be
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200238 * kept for consistency purpose */
David S. Miller220b07e2011-12-16 15:07:28 -0500239 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100240 tt_global_entry->roam_at = jiffies;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100241 send_roam_adv(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200242 tt_global_entry->orig_node);
243 }
244out:
245 if (tt_local_entry)
246 tt_local_entry_free_ref(tt_local_entry);
247 if (tt_global_entry)
248 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249}
250
Antonio Quartullia73105b2011-04-27 14:27:44 +0200251int tt_changes_fill_buffer(struct bat_priv *bat_priv,
252 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200254 int count = 0, tot_changes = 0;
255 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Antonio Quartullia73105b2011-04-27 14:27:44 +0200257 if (buff_len > 0)
258 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Antonio Quartullia73105b2011-04-27 14:27:44 +0200260 spin_lock_bh(&bat_priv->tt_changes_list_lock);
261 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Antonio Quartullia73105b2011-04-27 14:27:44 +0200263 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
264 list) {
265 if (count < tot_changes) {
266 memcpy(buff + tt_len(count),
267 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 count++;
269 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200270 list_del(&entry->list);
271 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200273 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Antonio Quartullia73105b2011-04-27 14:27:44 +0200275 /* Keep the buffer for possible tt_request */
276 spin_lock_bh(&bat_priv->tt_buff_lock);
277 kfree(bat_priv->tt_buff);
278 bat_priv->tt_buff_len = 0;
279 bat_priv->tt_buff = NULL;
280 /* We check whether this new OGM has no changes due to size
281 * problems */
282 if (buff_len > 0) {
283 /**
284 * if kmalloc() fails we will reply with the full table
285 * instead of providing the diff
286 */
287 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
288 if (bat_priv->tt_buff) {
289 memcpy(bat_priv->tt_buff, buff, buff_len);
290 bat_priv->tt_buff_len = buff_len;
291 }
292 }
293 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294
Antonio Quartullia73105b2011-04-27 14:27:44 +0200295 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296}
297
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200298int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299{
300 struct net_device *net_dev = (struct net_device *)seq->private;
301 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200302 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100303 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200304 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000305 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200307 uint32_t i;
308 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309
Marek Lindner32ae9b22011-04-20 15:40:58 +0200310 primary_if = primary_if_get_selected(bat_priv);
311 if (!primary_if) {
312 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
313 "please specify interfaces to enable it\n",
314 net_dev->name);
315 goto out;
316 }
317
318 if (primary_if->if_status != IF_ACTIVE) {
319 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
320 "primary interface not active\n",
321 net_dev->name);
322 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 }
324
325 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200326 "announced via TT (TTVN: %u):\n",
327 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 for (i = 0; i < hash->size; i++) {
330 head = &hash->table[i];
331
Marek Lindner7aadf882011-02-18 12:28:09 +0000332 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100333 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000334 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200335 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100336 tt_common_entry->addr,
337 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200338 TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100339 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200340 TT_CLIENT_NOPURGE ? 'P' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100341 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200342 TT_CLIENT_NEW ? 'N' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100343 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200344 TT_CLIENT_PENDING ? 'X' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100345 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200346 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000348 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200350out:
351 if (primary_if)
352 hardif_free_ref(primary_if);
353 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354}
355
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200356static void tt_local_set_pending(struct bat_priv *bat_priv,
357 struct tt_local_entry *tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100358 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100360 tt_local_event(bat_priv, tt_local_entry->common.addr,
361 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362
Antonio Quartulli015758d2011-07-09 17:52:13 +0200363 /* The local client has to be marked as "pending to be removed" but has
364 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200365 * response issued before the net ttvn increment (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100366 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100367
368 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
369 "%s\n", tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370}
371
Antonio Quartullia73105b2011-04-27 14:27:44 +0200372void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200373 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200375 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200377 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200378 if (!tt_local_entry)
379 goto out;
380
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200381 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100382 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200383out:
384 if (tt_local_entry)
385 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386}
387
Antonio Quartullia73105b2011-04-27 14:27:44 +0200388static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200390 struct hashtable_t *hash = bat_priv->tt_local_hash;
391 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100392 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000393 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200395 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200396 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 for (i = 0; i < hash->size; i++) {
399 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200400 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200402 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100403 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000404 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100405 tt_local_entry = container_of(tt_common_entry,
406 struct tt_local_entry,
407 common);
408 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000409 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200411 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100412 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200413 continue;
414
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100415 if (!has_timed_out(tt_local_entry->last_seen,
Marek Lindner032b7962011-12-20 19:30:40 +0800416 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000417 continue;
418
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200419 tt_local_set_pending(bat_priv, tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100420 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200422 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 }
424
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425}
426
Antonio Quartullia73105b2011-04-27 14:27:44 +0200427static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200429 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200430 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100431 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200433 struct hlist_node *node, *node_tmp;
434 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200435 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200437 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 return;
439
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440 hash = bat_priv->tt_local_hash;
441
442 for (i = 0; i < hash->size; i++) {
443 head = &hash->table[i];
444 list_lock = &hash->list_locks[i];
445
446 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100447 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200448 head, hash_entry) {
449 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100450 tt_local_entry = container_of(tt_common_entry,
451 struct tt_local_entry,
452 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200453 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200454 }
455 spin_unlock_bh(list_lock);
456 }
457
458 hash_destroy(hash);
459
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200460 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461}
462
Antonio Quartullia73105b2011-04-27 14:27:44 +0200463static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200465 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466 return 1;
467
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200468 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200470 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 return 0;
472
473 return 1;
474}
475
Antonio Quartullia73105b2011-04-27 14:27:44 +0200476static void tt_changes_list_free(struct bat_priv *bat_priv)
477{
478 struct tt_change_node *entry, *safe;
479
480 spin_lock_bh(&bat_priv->tt_changes_list_lock);
481
482 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
483 list) {
484 list_del(&entry->list);
485 kfree(entry);
486 }
487
488 atomic_set(&bat_priv->tt_local_changes, 0);
489 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
490}
491
492/* caller must hold orig_node refcount */
493int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200494 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
495 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200497 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200498 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200499 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100500 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Antonio Quartullia73105b2011-04-27 14:27:44 +0200502 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Antonio Quartullia73105b2011-04-27 14:27:44 +0200504 if (!tt_global_entry) {
505 tt_global_entry =
506 kmalloc(sizeof(*tt_global_entry),
507 GFP_ATOMIC);
508 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200509 goto out;
510
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100511 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
512 tt_global_entry->common.flags = NO_FLAGS;
513 atomic_set(&tt_global_entry->common.refcount, 2);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200514 /* Assign the new orig_node */
515 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200516 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200518 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200519
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100520 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
521 choose_orig, &tt_global_entry->common,
522 &tt_global_entry->common.hash_entry);
523
524 if (unlikely(hash_added != 0)) {
525 /* remove the reference for the hash */
526 tt_global_entry_free_ref(tt_global_entry);
527 goto out_remove;
528 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200529 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200530 } else {
531 if (tt_global_entry->orig_node != orig_node) {
532 atomic_dec(&tt_global_entry->orig_node->tt_size);
533 orig_node_tmp = tt_global_entry->orig_node;
534 atomic_inc(&orig_node->refcount);
535 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200536 orig_node_free_ref(orig_node_tmp);
537 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 }
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100539 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200540 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200541 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200543
Antonio Quartullibc279082011-07-07 15:35:35 +0200544 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100545 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200546
Antonio Quartullia73105b2011-04-27 14:27:44 +0200547 bat_dbg(DBG_TT, bat_priv,
548 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100549 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200550
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100551out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200552 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100553 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200554 "global tt received", roaming);
555 ret = 1;
556out:
557 if (tt_global_entry)
558 tt_global_entry_free_ref(tt_global_entry);
559 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560}
561
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200562int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563{
564 struct net_device *net_dev = (struct net_device *)seq->private;
565 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200566 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100567 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200568 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200569 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000570 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200572 uint32_t i;
573 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
Marek Lindner32ae9b22011-04-20 15:40:58 +0200575 primary_if = primary_if_get_selected(bat_priv);
576 if (!primary_if) {
577 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
578 "specify interfaces to enable it\n",
579 net_dev->name);
580 goto out;
581 }
582
583 if (primary_if->if_status != IF_ACTIVE) {
584 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
585 "primary interface not active\n",
586 net_dev->name);
587 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 }
589
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200590 seq_printf(seq,
591 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200593 seq_printf(seq, " %-13s %s %-15s %s %s\n",
594 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 for (i = 0; i < hash->size; i++) {
597 head = &hash->table[i];
598
Marek Lindner7aadf882011-02-18 12:28:09 +0000599 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100600 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000601 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100602 tt_global_entry = container_of(tt_common_entry,
603 struct tt_global_entry,
604 common);
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200605 seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100606 "[%c%c%c]\n",
607 tt_global_entry->common.addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200608 tt_global_entry->ttvn,
609 tt_global_entry->orig_node->orig,
610 (uint8_t) atomic_read(
611 &tt_global_entry->orig_node->
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200612 last_ttvn),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100613 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200614 TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100615 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200616 TT_CLIENT_PENDING ? 'X' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100617 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200618 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000620 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200622out:
623 if (primary_if)
624 hardif_free_ref(primary_if);
625 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626}
627
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628static void _tt_global_del(struct bat_priv *bat_priv,
629 struct tt_global_entry *tt_global_entry,
630 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200632 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200633 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200634
635 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200636 "Deleting global tt entry %pM (via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100637 tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638 message);
639
Antonio Quartullia73105b2011-04-27 14:27:44 +0200640 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200641
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100642 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
643 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200644out:
645 if (tt_global_entry)
646 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647}
648
Antonio Quartullia73105b2011-04-27 14:27:44 +0200649void tt_global_del(struct bat_priv *bat_priv,
650 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200651 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200653 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100654 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800657 if (!tt_global_entry || tt_global_entry->orig_node != orig_node)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200658 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200659
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800660 if (!roaming)
661 goto out_del;
662
663 /* if we are deleting a global entry due to a roam
664 * event, there are two possibilities:
665 * 1) the client roamed from node A to node B => we mark
666 * it with TT_CLIENT_ROAM, we start a timer and we
667 * wait for node B to claim it. In case of timeout
668 * the entry is purged.
669 * 2) the client roamed to us => we can directly delete
670 * the global entry, since it is useless now. */
671 tt_local_entry = tt_local_hash_find(bat_priv,
672 tt_global_entry->common.addr);
673 if (!tt_local_entry) {
674 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
675 tt_global_entry->roam_at = jiffies;
676 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800678
679out_del:
680 _tt_global_del(bat_priv, tt_global_entry, message);
681
Antonio Quartullicc47f662011-04-27 14:27:57 +0200682out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200683 if (tt_global_entry)
684 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100685 if (tt_local_entry)
686 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200687}
688
689void tt_global_del_orig(struct bat_priv *bat_priv,
690 struct orig_node *orig_node, const char *message)
691{
692 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100693 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200694 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200695 struct hashtable_t *hash = bat_priv->tt_global_hash;
696 struct hlist_node *node, *safe;
697 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200698 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200699
Simon Wunderlich6e801492011-10-19 10:28:26 +0200700 if (!hash)
701 return;
702
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703 for (i = 0; i < hash->size; i++) {
704 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200705 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200707 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100708 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200709 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100710 tt_global_entry = container_of(tt_common_entry,
711 struct tt_global_entry,
712 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200713 if (tt_global_entry->orig_node == orig_node) {
714 bat_dbg(DBG_TT, bat_priv,
715 "Deleting global tt entry %pM "
Antonio Quartulli87944972011-09-19 12:29:19 +0200716 "(via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100717 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200718 tt_global_entry->orig_node->orig,
719 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200720 hlist_del_rcu(node);
721 tt_global_entry_free_ref(tt_global_entry);
722 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200723 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200724 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200726 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100727 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000728}
729
Antonio Quartullicc47f662011-04-27 14:27:57 +0200730static void tt_global_roam_purge(struct bat_priv *bat_priv)
731{
732 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100733 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200734 struct tt_global_entry *tt_global_entry;
735 struct hlist_node *node, *node_tmp;
736 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200737 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200738 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200739
Antonio Quartullicc47f662011-04-27 14:27:57 +0200740 for (i = 0; i < hash->size; i++) {
741 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200742 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200743
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200744 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100745 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200746 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100747 tt_global_entry = container_of(tt_common_entry,
748 struct tt_global_entry,
749 common);
750 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200751 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100752 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800753 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200754 continue;
755
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200756 bat_dbg(DBG_TT, bat_priv, "Deleting global "
757 "tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100758 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200759 atomic_dec(&tt_global_entry->orig_node->tt_size);
760 hlist_del_rcu(node);
761 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200762 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200763 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200764 }
765
Antonio Quartullicc47f662011-04-27 14:27:57 +0200766}
767
Antonio Quartullia73105b2011-04-27 14:27:44 +0200768static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200770 struct hashtable_t *hash;
771 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100772 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200773 struct tt_global_entry *tt_global_entry;
774 struct hlist_node *node, *node_tmp;
775 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200776 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200777
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200778 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 return;
780
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200781 hash = bat_priv->tt_global_hash;
782
783 for (i = 0; i < hash->size; i++) {
784 head = &hash->table[i];
785 list_lock = &hash->list_locks[i];
786
787 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100788 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200789 head, hash_entry) {
790 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100791 tt_global_entry = container_of(tt_common_entry,
792 struct tt_global_entry,
793 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200794 tt_global_entry_free_ref(tt_global_entry);
795 }
796 spin_unlock_bh(list_lock);
797 }
798
799 hash_destroy(hash);
800
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200801 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802}
803
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200804static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
805 struct tt_global_entry *tt_global_entry)
806{
807 bool ret = false;
808
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100809 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
810 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200811 ret = true;
812
813 return ret;
814}
815
Sven Eckelmann747e4222011-05-14 23:14:50 +0200816struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200817 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000818{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200819 struct tt_local_entry *tt_local_entry = NULL;
820 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000821 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200823 if (src && atomic_read(&bat_priv->ap_isolation)) {
824 tt_local_entry = tt_local_hash_find(bat_priv, src);
825 if (!tt_local_entry)
826 goto out;
827 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000828
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200829 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200830 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000831 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200833 /* check whether the clients should not communicate due to AP
834 * isolation */
835 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
836 goto out;
837
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200838 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200839 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000840
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200841 /* A global client marked as PENDING has already moved from that
842 * originator */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100843 if (tt_global_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200844 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200845
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200846 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000847
848out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200849 if (tt_global_entry)
850 tt_global_entry_free_ref(tt_global_entry);
851 if (tt_local_entry)
852 tt_local_entry_free_ref(tt_local_entry);
853
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000854 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200856
857/* Calculates the checksum of the local table of a given orig_node */
858uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
859{
860 uint16_t total = 0, total_one;
861 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100862 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200863 struct tt_global_entry *tt_global_entry;
864 struct hlist_node *node;
865 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200866 uint32_t i;
867 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200868
869 for (i = 0; i < hash->size; i++) {
870 head = &hash->table[i];
871
872 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100873 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200874 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100875 tt_global_entry = container_of(tt_common_entry,
876 struct tt_global_entry,
877 common);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200878 if (compare_eth(tt_global_entry->orig_node,
879 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200880 /* Roaming clients are in the global table for
881 * consistency only. They don't have to be
882 * taken into account while computing the
883 * global crc */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100884 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200885 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200886 total_one = 0;
887 for (j = 0; j < ETH_ALEN; j++)
888 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100889 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200890 total ^= total_one;
891 }
892 }
893 rcu_read_unlock();
894 }
895
896 return total;
897}
898
899/* Calculates the checksum of the local table */
900uint16_t tt_local_crc(struct bat_priv *bat_priv)
901{
902 uint16_t total = 0, total_one;
903 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100904 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200905 struct hlist_node *node;
906 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200907 uint32_t i;
908 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200909
910 for (i = 0; i < hash->size; i++) {
911 head = &hash->table[i];
912
913 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100914 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200915 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200916 /* not yet committed clients have not to be taken into
917 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100918 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200919 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200920 total_one = 0;
921 for (j = 0; j < ETH_ALEN; j++)
922 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100923 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200924 total ^= total_one;
925 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200926 rcu_read_unlock();
927 }
928
929 return total;
930}
931
932static void tt_req_list_free(struct bat_priv *bat_priv)
933{
934 struct tt_req_node *node, *safe;
935
936 spin_lock_bh(&bat_priv->tt_req_list_lock);
937
938 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
939 list_del(&node->list);
940 kfree(node);
941 }
942
943 spin_unlock_bh(&bat_priv->tt_req_list_lock);
944}
945
946void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
947 const unsigned char *tt_buff, uint8_t tt_num_changes)
948{
949 uint16_t tt_buff_len = tt_len(tt_num_changes);
950
951 /* Replace the old buffer only if I received something in the
952 * last OGM (the OGM could carry no changes) */
953 spin_lock_bh(&orig_node->tt_buff_lock);
954 if (tt_buff_len > 0) {
955 kfree(orig_node->tt_buff);
956 orig_node->tt_buff_len = 0;
957 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
958 if (orig_node->tt_buff) {
959 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
960 orig_node->tt_buff_len = tt_buff_len;
961 }
962 }
963 spin_unlock_bh(&orig_node->tt_buff_lock);
964}
965
966static void tt_req_purge(struct bat_priv *bat_priv)
967{
968 struct tt_req_node *node, *safe;
969
970 spin_lock_bh(&bat_priv->tt_req_list_lock);
971 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +0800972 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200973 list_del(&node->list);
974 kfree(node);
975 }
976 }
977 spin_unlock_bh(&bat_priv->tt_req_list_lock);
978}
979
980/* returns the pointer to the new tt_req_node struct if no request
981 * has already been issued for this orig_node, NULL otherwise */
982static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
983 struct orig_node *orig_node)
984{
985 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
986
987 spin_lock_bh(&bat_priv->tt_req_list_lock);
988 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
989 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100990 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800991 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200992 goto unlock;
993 }
994
995 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
996 if (!tt_req_node)
997 goto unlock;
998
999 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1000 tt_req_node->issued_at = jiffies;
1001
1002 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1003unlock:
1004 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1005 return tt_req_node;
1006}
1007
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001008/* data_ptr is useless here, but has to be kept to respect the prototype */
1009static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1010{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001011 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001012
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001013 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001014 return 0;
1015 return 1;
1016}
1017
Antonio Quartullia73105b2011-04-27 14:27:44 +02001018static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1019{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001020 const struct tt_common_entry *tt_common_entry = entry_ptr;
1021 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001022 const struct orig_node *orig_node = data_ptr;
1023
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001024 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001025 return 0;
1026
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001027 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1028 common);
1029
Antonio Quartullia73105b2011-04-27 14:27:44 +02001030 return (tt_global_entry->orig_node == orig_node);
1031}
1032
1033static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1034 struct hashtable_t *hash,
1035 struct hard_iface *primary_if,
1036 int (*valid_cb)(const void *,
1037 const void *),
1038 void *cb_data)
1039{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001040 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001041 struct tt_query_packet *tt_response;
1042 struct tt_change *tt_change;
1043 struct hlist_node *node;
1044 struct hlist_head *head;
1045 struct sk_buff *skb = NULL;
1046 uint16_t tt_tot, tt_count;
1047 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001048 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001049
1050 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1051 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1052 tt_len -= tt_len % sizeof(struct tt_change);
1053 }
1054 tt_tot = tt_len / sizeof(struct tt_change);
1055
1056 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1057 if (!skb)
1058 goto out;
1059
1060 skb_reserve(skb, ETH_HLEN);
1061 tt_response = (struct tt_query_packet *)skb_put(skb,
1062 tt_query_size + tt_len);
1063 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001064
1065 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1066 tt_count = 0;
1067
1068 rcu_read_lock();
1069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1071
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001072 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001073 head, hash_entry) {
1074 if (tt_count == tt_tot)
1075 break;
1076
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001077 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001078 continue;
1079
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001080 memcpy(tt_change->addr, tt_common_entry->addr,
1081 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001082 tt_change->flags = NO_FLAGS;
1083
1084 tt_count++;
1085 tt_change++;
1086 }
1087 }
1088 rcu_read_unlock();
1089
Antonio Quartulli9d852392011-10-17 14:25:13 +02001090 /* store in the message the number of entries we have successfully
1091 * copied */
1092 tt_response->tt_data = htons(tt_count);
1093
Antonio Quartullia73105b2011-04-27 14:27:44 +02001094out:
1095 return skb;
1096}
1097
Marek Lindnera943cac2011-07-30 13:10:18 +02001098static int send_tt_request(struct bat_priv *bat_priv,
1099 struct orig_node *dst_orig_node,
1100 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001101{
1102 struct sk_buff *skb = NULL;
1103 struct tt_query_packet *tt_request;
1104 struct neigh_node *neigh_node = NULL;
1105 struct hard_iface *primary_if;
1106 struct tt_req_node *tt_req_node = NULL;
1107 int ret = 1;
1108
1109 primary_if = primary_if_get_selected(bat_priv);
1110 if (!primary_if)
1111 goto out;
1112
1113 /* The new tt_req will be issued only if I'm not waiting for a
1114 * reply from the same orig_node yet */
1115 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1116 if (!tt_req_node)
1117 goto out;
1118
1119 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1120 if (!skb)
1121 goto out;
1122
1123 skb_reserve(skb, ETH_HLEN);
1124
1125 tt_request = (struct tt_query_packet *)skb_put(skb,
1126 sizeof(struct tt_query_packet));
1127
Sven Eckelmann76543d12011-11-20 15:47:38 +01001128 tt_request->header.packet_type = BAT_TT_QUERY;
1129 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001130 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1131 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001132 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001133 tt_request->ttvn = ttvn;
1134 tt_request->tt_data = tt_crc;
1135 tt_request->flags = TT_REQUEST;
1136
1137 if (full_table)
1138 tt_request->flags |= TT_FULL_TABLE;
1139
1140 neigh_node = orig_node_get_router(dst_orig_node);
1141 if (!neigh_node)
1142 goto out;
1143
1144 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1145 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1146 (full_table ? 'F' : '.'));
1147
1148 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1149 ret = 0;
1150
1151out:
1152 if (neigh_node)
1153 neigh_node_free_ref(neigh_node);
1154 if (primary_if)
1155 hardif_free_ref(primary_if);
1156 if (ret)
1157 kfree_skb(skb);
1158 if (ret && tt_req_node) {
1159 spin_lock_bh(&bat_priv->tt_req_list_lock);
1160 list_del(&tt_req_node->list);
1161 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1162 kfree(tt_req_node);
1163 }
1164 return ret;
1165}
1166
1167static bool send_other_tt_response(struct bat_priv *bat_priv,
1168 struct tt_query_packet *tt_request)
1169{
1170 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1171 struct neigh_node *neigh_node = NULL;
1172 struct hard_iface *primary_if = NULL;
1173 uint8_t orig_ttvn, req_ttvn, ttvn;
1174 int ret = false;
1175 unsigned char *tt_buff;
1176 bool full_table;
1177 uint16_t tt_len, tt_tot;
1178 struct sk_buff *skb = NULL;
1179 struct tt_query_packet *tt_response;
1180
1181 bat_dbg(DBG_TT, bat_priv,
1182 "Received TT_REQUEST from %pM for "
1183 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1184 tt_request->ttvn, tt_request->dst,
1185 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1186
1187 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001188 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001189 if (!req_dst_orig_node)
1190 goto out;
1191
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001192 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001193 if (!res_dst_orig_node)
1194 goto out;
1195
1196 neigh_node = orig_node_get_router(res_dst_orig_node);
1197 if (!neigh_node)
1198 goto out;
1199
1200 primary_if = primary_if_get_selected(bat_priv);
1201 if (!primary_if)
1202 goto out;
1203
1204 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1205 req_ttvn = tt_request->ttvn;
1206
Antonio Quartulli015758d2011-07-09 17:52:13 +02001207 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001208 if (orig_ttvn != req_ttvn ||
1209 tt_request->tt_data != req_dst_orig_node->tt_crc)
1210 goto out;
1211
Antonio Quartulli015758d2011-07-09 17:52:13 +02001212 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001213 if (tt_request->flags & TT_FULL_TABLE ||
1214 !req_dst_orig_node->tt_buff)
1215 full_table = true;
1216 else
1217 full_table = false;
1218
1219 /* In this version, fragmentation is not implemented, then
1220 * I'll send only one packet with as much TT entries as I can */
1221 if (!full_table) {
1222 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1223 tt_len = req_dst_orig_node->tt_buff_len;
1224 tt_tot = tt_len / sizeof(struct tt_change);
1225
1226 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1227 tt_len + ETH_HLEN);
1228 if (!skb)
1229 goto unlock;
1230
1231 skb_reserve(skb, ETH_HLEN);
1232 tt_response = (struct tt_query_packet *)skb_put(skb,
1233 sizeof(struct tt_query_packet) + tt_len);
1234 tt_response->ttvn = req_ttvn;
1235 tt_response->tt_data = htons(tt_tot);
1236
1237 tt_buff = skb->data + sizeof(struct tt_query_packet);
1238 /* Copy the last orig_node's OGM buffer */
1239 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1240 req_dst_orig_node->tt_buff_len);
1241
1242 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1243 } else {
1244 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1245 sizeof(struct tt_change);
1246 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1247
1248 skb = tt_response_fill_table(tt_len, ttvn,
1249 bat_priv->tt_global_hash,
1250 primary_if, tt_global_valid_entry,
1251 req_dst_orig_node);
1252 if (!skb)
1253 goto out;
1254
1255 tt_response = (struct tt_query_packet *)skb->data;
1256 }
1257
Sven Eckelmann76543d12011-11-20 15:47:38 +01001258 tt_response->header.packet_type = BAT_TT_QUERY;
1259 tt_response->header.version = COMPAT_VERSION;
1260 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001261 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1262 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1263 tt_response->flags = TT_RESPONSE;
1264
1265 if (full_table)
1266 tt_response->flags |= TT_FULL_TABLE;
1267
1268 bat_dbg(DBG_TT, bat_priv,
1269 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1270 res_dst_orig_node->orig, neigh_node->addr,
1271 req_dst_orig_node->orig, req_ttvn);
1272
1273 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1274 ret = true;
1275 goto out;
1276
1277unlock:
1278 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1279
1280out:
1281 if (res_dst_orig_node)
1282 orig_node_free_ref(res_dst_orig_node);
1283 if (req_dst_orig_node)
1284 orig_node_free_ref(req_dst_orig_node);
1285 if (neigh_node)
1286 neigh_node_free_ref(neigh_node);
1287 if (primary_if)
1288 hardif_free_ref(primary_if);
1289 if (!ret)
1290 kfree_skb(skb);
1291 return ret;
1292
1293}
1294static bool send_my_tt_response(struct bat_priv *bat_priv,
1295 struct tt_query_packet *tt_request)
1296{
1297 struct orig_node *orig_node = NULL;
1298 struct neigh_node *neigh_node = NULL;
1299 struct hard_iface *primary_if = NULL;
1300 uint8_t my_ttvn, req_ttvn, ttvn;
1301 int ret = false;
1302 unsigned char *tt_buff;
1303 bool full_table;
1304 uint16_t tt_len, tt_tot;
1305 struct sk_buff *skb = NULL;
1306 struct tt_query_packet *tt_response;
1307
1308 bat_dbg(DBG_TT, bat_priv,
1309 "Received TT_REQUEST from %pM for "
1310 "ttvn: %u (me) [%c]\n", tt_request->src,
1311 tt_request->ttvn,
1312 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1313
1314
1315 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1316 req_ttvn = tt_request->ttvn;
1317
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001318 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001319 if (!orig_node)
1320 goto out;
1321
1322 neigh_node = orig_node_get_router(orig_node);
1323 if (!neigh_node)
1324 goto out;
1325
1326 primary_if = primary_if_get_selected(bat_priv);
1327 if (!primary_if)
1328 goto out;
1329
1330 /* If the full table has been explicitly requested or the gap
1331 * is too big send the whole local translation table */
1332 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1333 !bat_priv->tt_buff)
1334 full_table = true;
1335 else
1336 full_table = false;
1337
1338 /* In this version, fragmentation is not implemented, then
1339 * I'll send only one packet with as much TT entries as I can */
1340 if (!full_table) {
1341 spin_lock_bh(&bat_priv->tt_buff_lock);
1342 tt_len = bat_priv->tt_buff_len;
1343 tt_tot = tt_len / sizeof(struct tt_change);
1344
1345 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1346 tt_len + ETH_HLEN);
1347 if (!skb)
1348 goto unlock;
1349
1350 skb_reserve(skb, ETH_HLEN);
1351 tt_response = (struct tt_query_packet *)skb_put(skb,
1352 sizeof(struct tt_query_packet) + tt_len);
1353 tt_response->ttvn = req_ttvn;
1354 tt_response->tt_data = htons(tt_tot);
1355
1356 tt_buff = skb->data + sizeof(struct tt_query_packet);
1357 memcpy(tt_buff, bat_priv->tt_buff,
1358 bat_priv->tt_buff_len);
1359 spin_unlock_bh(&bat_priv->tt_buff_lock);
1360 } else {
1361 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1362 sizeof(struct tt_change);
1363 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1364
1365 skb = tt_response_fill_table(tt_len, ttvn,
1366 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001367 primary_if, tt_local_valid_entry,
1368 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001369 if (!skb)
1370 goto out;
1371
1372 tt_response = (struct tt_query_packet *)skb->data;
1373 }
1374
Sven Eckelmann76543d12011-11-20 15:47:38 +01001375 tt_response->header.packet_type = BAT_TT_QUERY;
1376 tt_response->header.version = COMPAT_VERSION;
1377 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001378 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1379 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1380 tt_response->flags = TT_RESPONSE;
1381
1382 if (full_table)
1383 tt_response->flags |= TT_FULL_TABLE;
1384
1385 bat_dbg(DBG_TT, bat_priv,
1386 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1387 orig_node->orig, neigh_node->addr,
1388 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1389
1390 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1391 ret = true;
1392 goto out;
1393
1394unlock:
1395 spin_unlock_bh(&bat_priv->tt_buff_lock);
1396out:
1397 if (orig_node)
1398 orig_node_free_ref(orig_node);
1399 if (neigh_node)
1400 neigh_node_free_ref(neigh_node);
1401 if (primary_if)
1402 hardif_free_ref(primary_if);
1403 if (!ret)
1404 kfree_skb(skb);
1405 /* This packet was for me, so it doesn't need to be re-routed */
1406 return true;
1407}
1408
1409bool send_tt_response(struct bat_priv *bat_priv,
1410 struct tt_query_packet *tt_request)
1411{
1412 if (is_my_mac(tt_request->dst))
1413 return send_my_tt_response(bat_priv, tt_request);
1414 else
1415 return send_other_tt_response(bat_priv, tt_request);
1416}
1417
1418static void _tt_update_changes(struct bat_priv *bat_priv,
1419 struct orig_node *orig_node,
1420 struct tt_change *tt_change,
1421 uint16_t tt_num_changes, uint8_t ttvn)
1422{
1423 int i;
1424
1425 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001426 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001427 tt_global_del(bat_priv, orig_node,
1428 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001429 "tt removed by changes",
1430 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001431 else
1432 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001433 (tt_change + i)->addr, ttvn, false,
1434 (tt_change + i)->flags &
1435 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001436 /* In case of problem while storing a
1437 * global_entry, we stop the updating
1438 * procedure without committing the
1439 * ttvn change. This will avoid to send
1440 * corrupted data on tt_request
1441 */
1442 return;
1443 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001444 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445}
1446
1447static void tt_fill_gtable(struct bat_priv *bat_priv,
1448 struct tt_query_packet *tt_response)
1449{
1450 struct orig_node *orig_node = NULL;
1451
1452 orig_node = orig_hash_find(bat_priv, tt_response->src);
1453 if (!orig_node)
1454 goto out;
1455
1456 /* Purge the old table first.. */
1457 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1458
1459 _tt_update_changes(bat_priv, orig_node,
1460 (struct tt_change *)(tt_response + 1),
1461 tt_response->tt_data, tt_response->ttvn);
1462
1463 spin_lock_bh(&orig_node->tt_buff_lock);
1464 kfree(orig_node->tt_buff);
1465 orig_node->tt_buff_len = 0;
1466 orig_node->tt_buff = NULL;
1467 spin_unlock_bh(&orig_node->tt_buff_lock);
1468
1469 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1470
1471out:
1472 if (orig_node)
1473 orig_node_free_ref(orig_node);
1474}
1475
Marek Lindnera943cac2011-07-30 13:10:18 +02001476static void tt_update_changes(struct bat_priv *bat_priv,
1477 struct orig_node *orig_node,
1478 uint16_t tt_num_changes, uint8_t ttvn,
1479 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480{
1481 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1482 ttvn);
1483
1484 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1485 tt_num_changes);
1486 atomic_set(&orig_node->last_ttvn, ttvn);
1487}
1488
1489bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1490{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001491 struct tt_local_entry *tt_local_entry = NULL;
1492 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001493
Antonio Quartullia73105b2011-04-27 14:27:44 +02001494 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001495 if (!tt_local_entry)
1496 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001497 /* Check if the client has been logically deleted (but is kept for
1498 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001499 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001500 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001501 ret = true;
1502out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001504 tt_local_entry_free_ref(tt_local_entry);
1505 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506}
1507
1508void handle_tt_response(struct bat_priv *bat_priv,
1509 struct tt_query_packet *tt_response)
1510{
1511 struct tt_req_node *node, *safe;
1512 struct orig_node *orig_node = NULL;
1513
1514 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1515 "ttvn %d t_size: %d [%c]\n",
1516 tt_response->src, tt_response->ttvn,
1517 tt_response->tt_data,
1518 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1519
1520 orig_node = orig_hash_find(bat_priv, tt_response->src);
1521 if (!orig_node)
1522 goto out;
1523
1524 if (tt_response->flags & TT_FULL_TABLE)
1525 tt_fill_gtable(bat_priv, tt_response);
1526 else
1527 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1528 tt_response->ttvn,
1529 (struct tt_change *)(tt_response + 1));
1530
1531 /* Delete the tt_req_node from pending tt_requests list */
1532 spin_lock_bh(&bat_priv->tt_req_list_lock);
1533 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1534 if (!compare_eth(node->addr, tt_response->src))
1535 continue;
1536 list_del(&node->list);
1537 kfree(node);
1538 }
1539 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1540
1541 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001543 /* Roaming phase is over: tables are in sync again. I can
1544 * unset the flag */
1545 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001546out:
1547 if (orig_node)
1548 orig_node_free_ref(orig_node);
1549}
1550
1551int tt_init(struct bat_priv *bat_priv)
1552{
1553 if (!tt_local_init(bat_priv))
1554 return 0;
1555
1556 if (!tt_global_init(bat_priv))
1557 return 0;
1558
1559 tt_start_timer(bat_priv);
1560
1561 return 1;
1562}
1563
Antonio Quartullicc47f662011-04-27 14:27:57 +02001564static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001565{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001566 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001567
Antonio Quartullicc47f662011-04-27 14:27:57 +02001568 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569
Antonio Quartullicc47f662011-04-27 14:27:57 +02001570 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1571 list_del(&node->list);
1572 kfree(node);
1573 }
1574
1575 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1576}
1577
1578static void tt_roam_purge(struct bat_priv *bat_priv)
1579{
1580 struct tt_roam_node *node, *safe;
1581
1582 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1583 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001584 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001585 continue;
1586
1587 list_del(&node->list);
1588 kfree(node);
1589 }
1590 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1591}
1592
1593/* This function checks whether the client already reached the
1594 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1595 * will not be sent.
1596 *
1597 * returns true if the ROAMING_ADV can be sent, false otherwise */
1598static bool tt_check_roam_count(struct bat_priv *bat_priv,
1599 uint8_t *client)
1600{
1601 struct tt_roam_node *tt_roam_node;
1602 bool ret = false;
1603
1604 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1605 /* The new tt_req will be issued only if I'm not waiting for a
1606 * reply from the same orig_node yet */
1607 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1608 if (!compare_eth(tt_roam_node->addr, client))
1609 continue;
1610
Marek Lindner032b7962011-12-20 19:30:40 +08001611 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001612 continue;
1613
1614 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1615 /* Sorry, you roamed too many times! */
1616 goto unlock;
1617 ret = true;
1618 break;
1619 }
1620
1621 if (!ret) {
1622 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1623 if (!tt_roam_node)
1624 goto unlock;
1625
1626 tt_roam_node->first_time = jiffies;
1627 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1628 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1629
1630 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1631 ret = true;
1632 }
1633
1634unlock:
1635 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1636 return ret;
1637}
1638
1639void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1640 struct orig_node *orig_node)
1641{
1642 struct neigh_node *neigh_node = NULL;
1643 struct sk_buff *skb = NULL;
1644 struct roam_adv_packet *roam_adv_packet;
1645 int ret = 1;
1646 struct hard_iface *primary_if;
1647
1648 /* before going on we have to check whether the client has
1649 * already roamed to us too many times */
1650 if (!tt_check_roam_count(bat_priv, client))
1651 goto out;
1652
1653 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1654 if (!skb)
1655 goto out;
1656
1657 skb_reserve(skb, ETH_HLEN);
1658
1659 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1660 sizeof(struct roam_adv_packet));
1661
Sven Eckelmann76543d12011-11-20 15:47:38 +01001662 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1663 roam_adv_packet->header.version = COMPAT_VERSION;
1664 roam_adv_packet->header.ttl = TTL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001665 primary_if = primary_if_get_selected(bat_priv);
1666 if (!primary_if)
1667 goto out;
1668 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1669 hardif_free_ref(primary_if);
1670 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1671 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1672
1673 neigh_node = orig_node_get_router(orig_node);
1674 if (!neigh_node)
1675 goto out;
1676
1677 bat_dbg(DBG_TT, bat_priv,
1678 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1679 orig_node->orig, client, neigh_node->addr);
1680
1681 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1682 ret = 0;
1683
1684out:
1685 if (neigh_node)
1686 neigh_node_free_ref(neigh_node);
1687 if (ret)
1688 kfree_skb(skb);
1689 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001690}
1691
1692static void tt_purge(struct work_struct *work)
1693{
1694 struct delayed_work *delayed_work =
1695 container_of(work, struct delayed_work, work);
1696 struct bat_priv *bat_priv =
1697 container_of(delayed_work, struct bat_priv, tt_work);
1698
1699 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001700 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001702 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
1704 tt_start_timer(bat_priv);
1705}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001706
1707void tt_free(struct bat_priv *bat_priv)
1708{
1709 cancel_delayed_work_sync(&bat_priv->tt_work);
1710
1711 tt_local_table_free(bat_priv);
1712 tt_global_table_free(bat_priv);
1713 tt_req_list_free(bat_priv);
1714 tt_changes_list_free(bat_priv);
1715 tt_roam_list_free(bat_priv);
1716
1717 kfree(bat_priv->tt_buff);
1718}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001719
Antonio Quartulli697f2532011-11-07 16:47:01 +01001720/* This function will enable or disable the specified flags for all the entries
1721 * in the given hash table and returns the number of modified entries */
1722static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1723 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001724{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001725 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001726 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001727 struct hlist_head *head;
1728 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001729 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001730
1731 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001732 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001733
1734 for (i = 0; i < hash->size; i++) {
1735 head = &hash->table[i];
1736
1737 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001738 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001739 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001740 if (enable) {
1741 if ((tt_common_entry->flags & flags) == flags)
1742 continue;
1743 tt_common_entry->flags |= flags;
1744 } else {
1745 if (!(tt_common_entry->flags & flags))
1746 continue;
1747 tt_common_entry->flags &= ~flags;
1748 }
1749 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001750 }
1751 rcu_read_unlock();
1752 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001753out:
1754 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001755}
1756
1757/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1758static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1759{
1760 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001761 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001762 struct tt_local_entry *tt_local_entry;
1763 struct hlist_node *node, *node_tmp;
1764 struct hlist_head *head;
1765 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001766 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001767
1768 if (!hash)
1769 return;
1770
1771 for (i = 0; i < hash->size; i++) {
1772 head = &hash->table[i];
1773 list_lock = &hash->list_locks[i];
1774
1775 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001776 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001777 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001778 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001779 continue;
1780
1781 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001782 "(%pM): pending\n", tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001783
1784 atomic_dec(&bat_priv->num_local_tt);
1785 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001786 tt_local_entry = container_of(tt_common_entry,
1787 struct tt_local_entry,
1788 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001789 tt_local_entry_free_ref(tt_local_entry);
1790 }
1791 spin_unlock_bh(list_lock);
1792 }
1793
1794}
1795
1796void tt_commit_changes(struct bat_priv *bat_priv)
1797{
Antonio Quartulli697f2532011-11-07 16:47:01 +01001798 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
1799 TT_CLIENT_NEW, false);
1800 /* all the reset entries have now to be effectively counted as local
1801 * entries */
1802 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001803 tt_local_purge_pending_clients(bat_priv);
1804
1805 /* Increment the TTVN only once per OGM interval */
1806 atomic_inc(&bat_priv->ttvn);
1807 bat_priv->tt_poss_change = false;
1808}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001809
1810bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1811{
1812 struct tt_local_entry *tt_local_entry = NULL;
1813 struct tt_global_entry *tt_global_entry = NULL;
1814 bool ret = true;
1815
1816 if (!atomic_read(&bat_priv->ap_isolation))
1817 return false;
1818
1819 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1820 if (!tt_local_entry)
1821 goto out;
1822
1823 tt_global_entry = tt_global_hash_find(bat_priv, src);
1824 if (!tt_global_entry)
1825 goto out;
1826
1827 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1828 goto out;
1829
1830 ret = false;
1831
1832out:
1833 if (tt_global_entry)
1834 tt_global_entry_free_ref(tt_global_entry);
1835 if (tt_local_entry)
1836 tt_local_entry_free_ref(tt_local_entry);
1837 return ret;
1838}
Marek Lindnera943cac2011-07-30 13:10:18 +02001839
1840void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1841 const unsigned char *tt_buff, uint8_t tt_num_changes,
1842 uint8_t ttvn, uint16_t tt_crc)
1843{
1844 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1845 bool full_table = true;
1846
Antonio Quartulli17071572011-11-07 16:36:40 +01001847 /* orig table not initialised AND first diff is in the OGM OR the ttvn
1848 * increased by one -> we can apply the attached changes */
1849 if ((!orig_node->tt_initialised && ttvn == 1) ||
1850 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001851 /* the OGM could not contain the changes due to their size or
1852 * because they have already been sent TT_OGM_APPEND_MAX times.
1853 * In this case send a tt request */
1854 if (!tt_num_changes) {
1855 full_table = false;
1856 goto request_table;
1857 }
1858
1859 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1860 (struct tt_change *)tt_buff);
1861
1862 /* Even if we received the precomputed crc with the OGM, we
1863 * prefer to recompute it to spot any possible inconsistency
1864 * in the global table */
1865 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1866
1867 /* The ttvn alone is not enough to guarantee consistency
1868 * because a single value could represent different states
1869 * (due to the wrap around). Thus a node has to check whether
1870 * the resulting table (after applying the changes) is still
1871 * consistent or not. E.g. a node could disconnect while its
1872 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1873 * checking the CRC value is mandatory to detect the
1874 * inconsistency */
1875 if (orig_node->tt_crc != tt_crc)
1876 goto request_table;
1877
1878 /* Roaming phase is over: tables are in sync again. I can
1879 * unset the flag */
1880 orig_node->tt_poss_change = false;
1881 } else {
1882 /* if we missed more than one change or our tables are not
1883 * in sync anymore -> request fresh tt data */
Antonio Quartulli17071572011-11-07 16:36:40 +01001884 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
1885 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001886request_table:
1887 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1888 "Need to retrieve the correct information "
1889 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1890 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1891 orig_ttvn, tt_crc, orig_node->tt_crc,
1892 tt_num_changes);
1893 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1894 full_table);
1895 return;
1896 }
1897 }
1898}