blob: ff9a1b33c13677ca5e7bc866028c2fad4a270dab [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 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
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100213 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
214 &tt_local_entry->common,
215 &tt_local_entry->common.hash_entry);
216
217 if (unlikely(hash_added != 0)) {
218 /* remove the reference for the hash */
219 tt_local_entry_free_ref(tt_local_entry);
220 goto out;
221 }
222
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100223 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200224
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200225 /* The local entry has to be marked as NEW to avoid to send it in
226 * a full table response going out before the next ttvn increment
227 * (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100228 tt_local_entry->common.flags |= TT_CLIENT_NEW;
Antonio Quartulli058d0e22011-07-07 01:40:58 +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,
358 uint16_t flags)
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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367}
368
Antonio Quartullia73105b2011-04-27 14:27:44 +0200369void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200370 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200372 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200374 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200375 if (!tt_local_entry)
376 goto out;
377
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200378 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
379 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
380
381 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100382 "%s\n", tt_local_entry->common.addr, 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,
420 TT_CLIENT_DEL);
421 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
422 "pending to be removed: timed out\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100423 tt_local_entry->common.addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200425 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 }
427
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428}
429
Antonio Quartullia73105b2011-04-27 14:27:44 +0200430static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200433 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100434 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200435 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200436 struct hlist_node *node, *node_tmp;
437 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200438 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200439
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200440 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 return;
442
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443 hash = bat_priv->tt_local_hash;
444
445 for (i = 0; i < hash->size; i++) {
446 head = &hash->table[i];
447 list_lock = &hash->list_locks[i];
448
449 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100450 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200451 head, hash_entry) {
452 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100453 tt_local_entry = container_of(tt_common_entry,
454 struct tt_local_entry,
455 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200456 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200457 }
458 spin_unlock_bh(list_lock);
459 }
460
461 hash_destroy(hash);
462
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200463 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464}
465
Antonio Quartullia73105b2011-04-27 14:27:44 +0200466static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200468 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 return 1;
470
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200471 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200473 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 return 0;
475
476 return 1;
477}
478
Antonio Quartullia73105b2011-04-27 14:27:44 +0200479static void tt_changes_list_free(struct bat_priv *bat_priv)
480{
481 struct tt_change_node *entry, *safe;
482
483 spin_lock_bh(&bat_priv->tt_changes_list_lock);
484
485 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
486 list) {
487 list_del(&entry->list);
488 kfree(entry);
489 }
490
491 atomic_set(&bat_priv->tt_local_changes, 0);
492 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
493}
494
495/* caller must hold orig_node refcount */
496int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200497 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
498 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200500 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200501 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200502 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100503 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Antonio Quartullia73105b2011-04-27 14:27:44 +0200505 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506
Antonio Quartullia73105b2011-04-27 14:27:44 +0200507 if (!tt_global_entry) {
508 tt_global_entry =
509 kmalloc(sizeof(*tt_global_entry),
510 GFP_ATOMIC);
511 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200512 goto out;
513
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100514 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
515 tt_global_entry->common.flags = NO_FLAGS;
516 atomic_set(&tt_global_entry->common.refcount, 2);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 /* Assign the new orig_node */
518 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200519 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200520 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200521 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200522
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100523 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
524 choose_orig, &tt_global_entry->common,
525 &tt_global_entry->common.hash_entry);
526
527 if (unlikely(hash_added != 0)) {
528 /* remove the reference for the hash */
529 tt_global_entry_free_ref(tt_global_entry);
530 goto out_remove;
531 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200532 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200533 } else {
534 if (tt_global_entry->orig_node != orig_node) {
535 atomic_dec(&tt_global_entry->orig_node->tt_size);
536 orig_node_tmp = tt_global_entry->orig_node;
537 atomic_inc(&orig_node->refcount);
538 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539 orig_node_free_ref(orig_node_tmp);
540 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541 }
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100542 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200543 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200544 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200546
Antonio Quartullibc279082011-07-07 15:35:35 +0200547 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100548 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200549
Antonio Quartullia73105b2011-04-27 14:27:44 +0200550 bat_dbg(DBG_TT, bat_priv,
551 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100552 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200553
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100554out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200555 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100556 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200557 "global tt received", roaming);
558 ret = 1;
559out:
560 if (tt_global_entry)
561 tt_global_entry_free_ref(tt_global_entry);
562 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563}
564
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200565int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566{
567 struct net_device *net_dev = (struct net_device *)seq->private;
568 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200569 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100570 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200571 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200572 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000573 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200575 uint32_t i;
576 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577
Marek Lindner32ae9b22011-04-20 15:40:58 +0200578 primary_if = primary_if_get_selected(bat_priv);
579 if (!primary_if) {
580 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
581 "specify interfaces to enable it\n",
582 net_dev->name);
583 goto out;
584 }
585
586 if (primary_if->if_status != IF_ACTIVE) {
587 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
588 "primary interface not active\n",
589 net_dev->name);
590 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 }
592
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200593 seq_printf(seq,
594 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200596 seq_printf(seq, " %-13s %s %-15s %s %s\n",
597 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 for (i = 0; i < hash->size; i++) {
600 head = &hash->table[i];
601
Marek Lindner7aadf882011-02-18 12:28:09 +0000602 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100603 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000604 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100605 tt_global_entry = container_of(tt_common_entry,
606 struct tt_global_entry,
607 common);
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200608 seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100609 "[%c%c%c]\n",
610 tt_global_entry->common.addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200611 tt_global_entry->ttvn,
612 tt_global_entry->orig_node->orig,
613 (uint8_t) atomic_read(
614 &tt_global_entry->orig_node->
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200615 last_ttvn),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100616 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200617 TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100618 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200619 TT_CLIENT_PENDING ? 'X' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100620 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200621 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000623 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200625out:
626 if (primary_if)
627 hardif_free_ref(primary_if);
628 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629}
630
Antonio Quartullia73105b2011-04-27 14:27:44 +0200631static void _tt_global_del(struct bat_priv *bat_priv,
632 struct tt_global_entry *tt_global_entry,
633 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200635 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200636 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200637
638 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200639 "Deleting global tt entry %pM (via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100640 tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 message);
642
Antonio Quartullia73105b2011-04-27 14:27:44 +0200643 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200644
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100645 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
646 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647out:
648 if (tt_global_entry)
649 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650}
651
Antonio Quartullia73105b2011-04-27 14:27:44 +0200652void tt_global_del(struct bat_priv *bat_priv,
653 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200654 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200656 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100657 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Antonio Quartullia73105b2011-04-27 14:27:44 +0200659 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200660 if (!tt_global_entry)
661 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200663 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200664 if (roaming) {
Antonio Quartulli797399b2011-12-04 22:38:27 +0100665 /* if we are deleting a global entry due to a roam
666 * event, there are two possibilities:
667 * 1) the client roamed from node A to node B => we mark
668 * it with TT_CLIENT_ROAM, we start a timer and we
669 * wait for node B to claim it. In case of timeout
670 * the entry is purged.
671 * 2) the client roamed to us => we can directly delete
672 * the global entry, since it is useless now. */
673 tt_local_entry = tt_local_hash_find(bat_priv,
David S. Millerb26e4782011-12-16 02:11:14 -0500674 tt_global_entry->common.addr);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100675 if (!tt_local_entry) {
David S. Millerb26e4782011-12-16 02:11:14 -0500676 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100677 tt_global_entry->roam_at = jiffies;
678 goto out;
679 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200680 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200681 _tt_global_del(bat_priv, tt_global_entry, message);
682 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200683out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200684 if (tt_global_entry)
685 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100686 if (tt_local_entry)
687 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200688}
689
690void tt_global_del_orig(struct bat_priv *bat_priv,
691 struct orig_node *orig_node, const char *message)
692{
693 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100694 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200695 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696 struct hashtable_t *hash = bat_priv->tt_global_hash;
697 struct hlist_node *node, *safe;
698 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200699 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200700
Simon Wunderlich6e801492011-10-19 10:28:26 +0200701 if (!hash)
702 return;
703
Antonio Quartullia73105b2011-04-27 14:27:44 +0200704 for (i = 0; i < hash->size; i++) {
705 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200706 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200708 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100709 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200710 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100711 tt_global_entry = container_of(tt_common_entry,
712 struct tt_global_entry,
713 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 if (tt_global_entry->orig_node == orig_node) {
715 bat_dbg(DBG_TT, bat_priv,
716 "Deleting global tt entry %pM "
Antonio Quartulli87944972011-09-19 12:29:19 +0200717 "(via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100718 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200719 tt_global_entry->orig_node->orig,
720 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200721 hlist_del_rcu(node);
722 tt_global_entry_free_ref(tt_global_entry);
723 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200724 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200725 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200727 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100728 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729}
730
Antonio Quartullicc47f662011-04-27 14:27:57 +0200731static void tt_global_roam_purge(struct bat_priv *bat_priv)
732{
733 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100734 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200735 struct tt_global_entry *tt_global_entry;
736 struct hlist_node *node, *node_tmp;
737 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200738 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200739 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200740
Antonio Quartullicc47f662011-04-27 14:27:57 +0200741 for (i = 0; i < hash->size; i++) {
742 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200743 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200744
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200745 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100746 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200747 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100748 tt_global_entry = container_of(tt_common_entry,
749 struct tt_global_entry,
750 common);
751 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200752 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100753 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800754 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200755 continue;
756
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200757 bat_dbg(DBG_TT, bat_priv, "Deleting global "
758 "tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100759 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200760 atomic_dec(&tt_global_entry->orig_node->tt_size);
761 hlist_del_rcu(node);
762 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200763 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200764 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200765 }
766
Antonio Quartullicc47f662011-04-27 14:27:57 +0200767}
768
Antonio Quartullia73105b2011-04-27 14:27:44 +0200769static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200771 struct hashtable_t *hash;
772 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100773 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200774 struct tt_global_entry *tt_global_entry;
775 struct hlist_node *node, *node_tmp;
776 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200777 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200778
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200779 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780 return;
781
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200782 hash = bat_priv->tt_global_hash;
783
784 for (i = 0; i < hash->size; i++) {
785 head = &hash->table[i];
786 list_lock = &hash->list_locks[i];
787
788 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100789 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200790 head, hash_entry) {
791 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100792 tt_global_entry = container_of(tt_common_entry,
793 struct tt_global_entry,
794 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200795 tt_global_entry_free_ref(tt_global_entry);
796 }
797 spin_unlock_bh(list_lock);
798 }
799
800 hash_destroy(hash);
801
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200802 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803}
804
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200805static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
806 struct tt_global_entry *tt_global_entry)
807{
808 bool ret = false;
809
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100810 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
811 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200812 ret = true;
813
814 return ret;
815}
816
Sven Eckelmann747e4222011-05-14 23:14:50 +0200817struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200818 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200820 struct tt_local_entry *tt_local_entry = NULL;
821 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000822 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200824 if (src && atomic_read(&bat_priv->ap_isolation)) {
825 tt_local_entry = tt_local_hash_find(bat_priv, src);
826 if (!tt_local_entry)
827 goto out;
828 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000829
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200830 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200831 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000832 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200834 /* check whether the clients should not communicate due to AP
835 * isolation */
836 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
837 goto out;
838
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200839 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200840 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000841
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200842 /* A global client marked as PENDING has already moved from that
843 * originator */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100844 if (tt_global_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200845 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200846
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200847 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000848
849out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200850 if (tt_global_entry)
851 tt_global_entry_free_ref(tt_global_entry);
852 if (tt_local_entry)
853 tt_local_entry_free_ref(tt_local_entry);
854
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000855 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200857
858/* Calculates the checksum of the local table of a given orig_node */
859uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
860{
861 uint16_t total = 0, total_one;
862 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100863 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200864 struct tt_global_entry *tt_global_entry;
865 struct hlist_node *node;
866 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200867 uint32_t i;
868 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200869
870 for (i = 0; i < hash->size; i++) {
871 head = &hash->table[i];
872
873 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100874 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200875 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100876 tt_global_entry = container_of(tt_common_entry,
877 struct tt_global_entry,
878 common);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200879 if (compare_eth(tt_global_entry->orig_node,
880 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200881 /* Roaming clients are in the global table for
882 * consistency only. They don't have to be
883 * taken into account while computing the
884 * global crc */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100885 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200886 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200887 total_one = 0;
888 for (j = 0; j < ETH_ALEN; j++)
889 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100890 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200891 total ^= total_one;
892 }
893 }
894 rcu_read_unlock();
895 }
896
897 return total;
898}
899
900/* Calculates the checksum of the local table */
901uint16_t tt_local_crc(struct bat_priv *bat_priv)
902{
903 uint16_t total = 0, total_one;
904 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100905 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200906 struct hlist_node *node;
907 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200908 uint32_t i;
909 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200910
911 for (i = 0; i < hash->size; i++) {
912 head = &hash->table[i];
913
914 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100915 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200916 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200917 /* not yet committed clients have not to be taken into
918 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100919 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200920 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200921 total_one = 0;
922 for (j = 0; j < ETH_ALEN; j++)
923 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100924 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200925 total ^= total_one;
926 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200927 rcu_read_unlock();
928 }
929
930 return total;
931}
932
933static void tt_req_list_free(struct bat_priv *bat_priv)
934{
935 struct tt_req_node *node, *safe;
936
937 spin_lock_bh(&bat_priv->tt_req_list_lock);
938
939 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
940 list_del(&node->list);
941 kfree(node);
942 }
943
944 spin_unlock_bh(&bat_priv->tt_req_list_lock);
945}
946
947void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
948 const unsigned char *tt_buff, uint8_t tt_num_changes)
949{
950 uint16_t tt_buff_len = tt_len(tt_num_changes);
951
952 /* Replace the old buffer only if I received something in the
953 * last OGM (the OGM could carry no changes) */
954 spin_lock_bh(&orig_node->tt_buff_lock);
955 if (tt_buff_len > 0) {
956 kfree(orig_node->tt_buff);
957 orig_node->tt_buff_len = 0;
958 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
959 if (orig_node->tt_buff) {
960 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
961 orig_node->tt_buff_len = tt_buff_len;
962 }
963 }
964 spin_unlock_bh(&orig_node->tt_buff_lock);
965}
966
967static void tt_req_purge(struct bat_priv *bat_priv)
968{
969 struct tt_req_node *node, *safe;
970
971 spin_lock_bh(&bat_priv->tt_req_list_lock);
972 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +0800973 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200974 list_del(&node->list);
975 kfree(node);
976 }
977 }
978 spin_unlock_bh(&bat_priv->tt_req_list_lock);
979}
980
981/* returns the pointer to the new tt_req_node struct if no request
982 * has already been issued for this orig_node, NULL otherwise */
983static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
984 struct orig_node *orig_node)
985{
986 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
987
988 spin_lock_bh(&bat_priv->tt_req_list_lock);
989 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
990 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100991 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800992 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200993 goto unlock;
994 }
995
996 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
997 if (!tt_req_node)
998 goto unlock;
999
1000 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1001 tt_req_node->issued_at = jiffies;
1002
1003 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1004unlock:
1005 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1006 return tt_req_node;
1007}
1008
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001009/* data_ptr is useless here, but has to be kept to respect the prototype */
1010static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1011{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001012 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001013
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001014 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001015 return 0;
1016 return 1;
1017}
1018
Antonio Quartullia73105b2011-04-27 14:27:44 +02001019static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1020{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001021 const struct tt_common_entry *tt_common_entry = entry_ptr;
1022 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001023 const struct orig_node *orig_node = data_ptr;
1024
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001025 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001026 return 0;
1027
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001028 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1029 common);
1030
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031 return (tt_global_entry->orig_node == orig_node);
1032}
1033
1034static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1035 struct hashtable_t *hash,
1036 struct hard_iface *primary_if,
1037 int (*valid_cb)(const void *,
1038 const void *),
1039 void *cb_data)
1040{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001041 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001042 struct tt_query_packet *tt_response;
1043 struct tt_change *tt_change;
1044 struct hlist_node *node;
1045 struct hlist_head *head;
1046 struct sk_buff *skb = NULL;
1047 uint16_t tt_tot, tt_count;
1048 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001049 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001050
1051 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1052 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1053 tt_len -= tt_len % sizeof(struct tt_change);
1054 }
1055 tt_tot = tt_len / sizeof(struct tt_change);
1056
1057 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1058 if (!skb)
1059 goto out;
1060
1061 skb_reserve(skb, ETH_HLEN);
1062 tt_response = (struct tt_query_packet *)skb_put(skb,
1063 tt_query_size + tt_len);
1064 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001065
1066 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1067 tt_count = 0;
1068
1069 rcu_read_lock();
1070 for (i = 0; i < hash->size; i++) {
1071 head = &hash->table[i];
1072
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001073 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001074 head, hash_entry) {
1075 if (tt_count == tt_tot)
1076 break;
1077
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001078 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001079 continue;
1080
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001081 memcpy(tt_change->addr, tt_common_entry->addr,
1082 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001083 tt_change->flags = NO_FLAGS;
1084
1085 tt_count++;
1086 tt_change++;
1087 }
1088 }
1089 rcu_read_unlock();
1090
Antonio Quartulli9d852392011-10-17 14:25:13 +02001091 /* store in the message the number of entries we have successfully
1092 * copied */
1093 tt_response->tt_data = htons(tt_count);
1094
Antonio Quartullia73105b2011-04-27 14:27:44 +02001095out:
1096 return skb;
1097}
1098
Marek Lindnera943cac2011-07-30 13:10:18 +02001099static int send_tt_request(struct bat_priv *bat_priv,
1100 struct orig_node *dst_orig_node,
1101 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001102{
1103 struct sk_buff *skb = NULL;
1104 struct tt_query_packet *tt_request;
1105 struct neigh_node *neigh_node = NULL;
1106 struct hard_iface *primary_if;
1107 struct tt_req_node *tt_req_node = NULL;
1108 int ret = 1;
1109
1110 primary_if = primary_if_get_selected(bat_priv);
1111 if (!primary_if)
1112 goto out;
1113
1114 /* The new tt_req will be issued only if I'm not waiting for a
1115 * reply from the same orig_node yet */
1116 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1117 if (!tt_req_node)
1118 goto out;
1119
1120 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1121 if (!skb)
1122 goto out;
1123
1124 skb_reserve(skb, ETH_HLEN);
1125
1126 tt_request = (struct tt_query_packet *)skb_put(skb,
1127 sizeof(struct tt_query_packet));
1128
Sven Eckelmann76543d12011-11-20 15:47:38 +01001129 tt_request->header.packet_type = BAT_TT_QUERY;
1130 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001131 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1132 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001133 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001134 tt_request->ttvn = ttvn;
1135 tt_request->tt_data = tt_crc;
1136 tt_request->flags = TT_REQUEST;
1137
1138 if (full_table)
1139 tt_request->flags |= TT_FULL_TABLE;
1140
1141 neigh_node = orig_node_get_router(dst_orig_node);
1142 if (!neigh_node)
1143 goto out;
1144
1145 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1146 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1147 (full_table ? 'F' : '.'));
1148
1149 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1150 ret = 0;
1151
1152out:
1153 if (neigh_node)
1154 neigh_node_free_ref(neigh_node);
1155 if (primary_if)
1156 hardif_free_ref(primary_if);
1157 if (ret)
1158 kfree_skb(skb);
1159 if (ret && tt_req_node) {
1160 spin_lock_bh(&bat_priv->tt_req_list_lock);
1161 list_del(&tt_req_node->list);
1162 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1163 kfree(tt_req_node);
1164 }
1165 return ret;
1166}
1167
1168static bool send_other_tt_response(struct bat_priv *bat_priv,
1169 struct tt_query_packet *tt_request)
1170{
1171 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1172 struct neigh_node *neigh_node = NULL;
1173 struct hard_iface *primary_if = NULL;
1174 uint8_t orig_ttvn, req_ttvn, ttvn;
1175 int ret = false;
1176 unsigned char *tt_buff;
1177 bool full_table;
1178 uint16_t tt_len, tt_tot;
1179 struct sk_buff *skb = NULL;
1180 struct tt_query_packet *tt_response;
1181
1182 bat_dbg(DBG_TT, bat_priv,
1183 "Received TT_REQUEST from %pM for "
1184 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1185 tt_request->ttvn, tt_request->dst,
1186 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1187
1188 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001189 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001190 if (!req_dst_orig_node)
1191 goto out;
1192
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001193 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194 if (!res_dst_orig_node)
1195 goto out;
1196
1197 neigh_node = orig_node_get_router(res_dst_orig_node);
1198 if (!neigh_node)
1199 goto out;
1200
1201 primary_if = primary_if_get_selected(bat_priv);
1202 if (!primary_if)
1203 goto out;
1204
1205 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1206 req_ttvn = tt_request->ttvn;
1207
Antonio Quartulli015758d2011-07-09 17:52:13 +02001208 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001209 if (orig_ttvn != req_ttvn ||
1210 tt_request->tt_data != req_dst_orig_node->tt_crc)
1211 goto out;
1212
Antonio Quartulli015758d2011-07-09 17:52:13 +02001213 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001214 if (tt_request->flags & TT_FULL_TABLE ||
1215 !req_dst_orig_node->tt_buff)
1216 full_table = true;
1217 else
1218 full_table = false;
1219
1220 /* In this version, fragmentation is not implemented, then
1221 * I'll send only one packet with as much TT entries as I can */
1222 if (!full_table) {
1223 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1224 tt_len = req_dst_orig_node->tt_buff_len;
1225 tt_tot = tt_len / sizeof(struct tt_change);
1226
1227 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1228 tt_len + ETH_HLEN);
1229 if (!skb)
1230 goto unlock;
1231
1232 skb_reserve(skb, ETH_HLEN);
1233 tt_response = (struct tt_query_packet *)skb_put(skb,
1234 sizeof(struct tt_query_packet) + tt_len);
1235 tt_response->ttvn = req_ttvn;
1236 tt_response->tt_data = htons(tt_tot);
1237
1238 tt_buff = skb->data + sizeof(struct tt_query_packet);
1239 /* Copy the last orig_node's OGM buffer */
1240 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1241 req_dst_orig_node->tt_buff_len);
1242
1243 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1244 } else {
1245 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1246 sizeof(struct tt_change);
1247 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1248
1249 skb = tt_response_fill_table(tt_len, ttvn,
1250 bat_priv->tt_global_hash,
1251 primary_if, tt_global_valid_entry,
1252 req_dst_orig_node);
1253 if (!skb)
1254 goto out;
1255
1256 tt_response = (struct tt_query_packet *)skb->data;
1257 }
1258
Sven Eckelmann76543d12011-11-20 15:47:38 +01001259 tt_response->header.packet_type = BAT_TT_QUERY;
1260 tt_response->header.version = COMPAT_VERSION;
1261 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1263 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1264 tt_response->flags = TT_RESPONSE;
1265
1266 if (full_table)
1267 tt_response->flags |= TT_FULL_TABLE;
1268
1269 bat_dbg(DBG_TT, bat_priv,
1270 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1271 res_dst_orig_node->orig, neigh_node->addr,
1272 req_dst_orig_node->orig, req_ttvn);
1273
1274 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1275 ret = true;
1276 goto out;
1277
1278unlock:
1279 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1280
1281out:
1282 if (res_dst_orig_node)
1283 orig_node_free_ref(res_dst_orig_node);
1284 if (req_dst_orig_node)
1285 orig_node_free_ref(req_dst_orig_node);
1286 if (neigh_node)
1287 neigh_node_free_ref(neigh_node);
1288 if (primary_if)
1289 hardif_free_ref(primary_if);
1290 if (!ret)
1291 kfree_skb(skb);
1292 return ret;
1293
1294}
1295static bool send_my_tt_response(struct bat_priv *bat_priv,
1296 struct tt_query_packet *tt_request)
1297{
1298 struct orig_node *orig_node = NULL;
1299 struct neigh_node *neigh_node = NULL;
1300 struct hard_iface *primary_if = NULL;
1301 uint8_t my_ttvn, req_ttvn, ttvn;
1302 int ret = false;
1303 unsigned char *tt_buff;
1304 bool full_table;
1305 uint16_t tt_len, tt_tot;
1306 struct sk_buff *skb = NULL;
1307 struct tt_query_packet *tt_response;
1308
1309 bat_dbg(DBG_TT, bat_priv,
1310 "Received TT_REQUEST from %pM for "
1311 "ttvn: %u (me) [%c]\n", tt_request->src,
1312 tt_request->ttvn,
1313 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1314
1315
1316 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1317 req_ttvn = tt_request->ttvn;
1318
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001319 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001320 if (!orig_node)
1321 goto out;
1322
1323 neigh_node = orig_node_get_router(orig_node);
1324 if (!neigh_node)
1325 goto out;
1326
1327 primary_if = primary_if_get_selected(bat_priv);
1328 if (!primary_if)
1329 goto out;
1330
1331 /* If the full table has been explicitly requested or the gap
1332 * is too big send the whole local translation table */
1333 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1334 !bat_priv->tt_buff)
1335 full_table = true;
1336 else
1337 full_table = false;
1338
1339 /* In this version, fragmentation is not implemented, then
1340 * I'll send only one packet with as much TT entries as I can */
1341 if (!full_table) {
1342 spin_lock_bh(&bat_priv->tt_buff_lock);
1343 tt_len = bat_priv->tt_buff_len;
1344 tt_tot = tt_len / sizeof(struct tt_change);
1345
1346 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1347 tt_len + ETH_HLEN);
1348 if (!skb)
1349 goto unlock;
1350
1351 skb_reserve(skb, ETH_HLEN);
1352 tt_response = (struct tt_query_packet *)skb_put(skb,
1353 sizeof(struct tt_query_packet) + tt_len);
1354 tt_response->ttvn = req_ttvn;
1355 tt_response->tt_data = htons(tt_tot);
1356
1357 tt_buff = skb->data + sizeof(struct tt_query_packet);
1358 memcpy(tt_buff, bat_priv->tt_buff,
1359 bat_priv->tt_buff_len);
1360 spin_unlock_bh(&bat_priv->tt_buff_lock);
1361 } else {
1362 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1363 sizeof(struct tt_change);
1364 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1365
1366 skb = tt_response_fill_table(tt_len, ttvn,
1367 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001368 primary_if, tt_local_valid_entry,
1369 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001370 if (!skb)
1371 goto out;
1372
1373 tt_response = (struct tt_query_packet *)skb->data;
1374 }
1375
Sven Eckelmann76543d12011-11-20 15:47:38 +01001376 tt_response->header.packet_type = BAT_TT_QUERY;
1377 tt_response->header.version = COMPAT_VERSION;
1378 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001379 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1380 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1381 tt_response->flags = TT_RESPONSE;
1382
1383 if (full_table)
1384 tt_response->flags |= TT_FULL_TABLE;
1385
1386 bat_dbg(DBG_TT, bat_priv,
1387 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1388 orig_node->orig, neigh_node->addr,
1389 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1390
1391 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1392 ret = true;
1393 goto out;
1394
1395unlock:
1396 spin_unlock_bh(&bat_priv->tt_buff_lock);
1397out:
1398 if (orig_node)
1399 orig_node_free_ref(orig_node);
1400 if (neigh_node)
1401 neigh_node_free_ref(neigh_node);
1402 if (primary_if)
1403 hardif_free_ref(primary_if);
1404 if (!ret)
1405 kfree_skb(skb);
1406 /* This packet was for me, so it doesn't need to be re-routed */
1407 return true;
1408}
1409
1410bool send_tt_response(struct bat_priv *bat_priv,
1411 struct tt_query_packet *tt_request)
1412{
1413 if (is_my_mac(tt_request->dst))
1414 return send_my_tt_response(bat_priv, tt_request);
1415 else
1416 return send_other_tt_response(bat_priv, tt_request);
1417}
1418
1419static void _tt_update_changes(struct bat_priv *bat_priv,
1420 struct orig_node *orig_node,
1421 struct tt_change *tt_change,
1422 uint16_t tt_num_changes, uint8_t ttvn)
1423{
1424 int i;
1425
1426 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001427 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001428 tt_global_del(bat_priv, orig_node,
1429 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001430 "tt removed by changes",
1431 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001432 else
1433 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001434 (tt_change + i)->addr, ttvn, false,
1435 (tt_change + i)->flags &
1436 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437 /* In case of problem while storing a
1438 * global_entry, we stop the updating
1439 * procedure without committing the
1440 * ttvn change. This will avoid to send
1441 * corrupted data on tt_request
1442 */
1443 return;
1444 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001445 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446}
1447
1448static void tt_fill_gtable(struct bat_priv *bat_priv,
1449 struct tt_query_packet *tt_response)
1450{
1451 struct orig_node *orig_node = NULL;
1452
1453 orig_node = orig_hash_find(bat_priv, tt_response->src);
1454 if (!orig_node)
1455 goto out;
1456
1457 /* Purge the old table first.. */
1458 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1459
1460 _tt_update_changes(bat_priv, orig_node,
1461 (struct tt_change *)(tt_response + 1),
1462 tt_response->tt_data, tt_response->ttvn);
1463
1464 spin_lock_bh(&orig_node->tt_buff_lock);
1465 kfree(orig_node->tt_buff);
1466 orig_node->tt_buff_len = 0;
1467 orig_node->tt_buff = NULL;
1468 spin_unlock_bh(&orig_node->tt_buff_lock);
1469
1470 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1471
1472out:
1473 if (orig_node)
1474 orig_node_free_ref(orig_node);
1475}
1476
Marek Lindnera943cac2011-07-30 13:10:18 +02001477static void tt_update_changes(struct bat_priv *bat_priv,
1478 struct orig_node *orig_node,
1479 uint16_t tt_num_changes, uint8_t ttvn,
1480 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481{
1482 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1483 ttvn);
1484
1485 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1486 tt_num_changes);
1487 atomic_set(&orig_node->last_ttvn, ttvn);
1488}
1489
1490bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1491{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001492 struct tt_local_entry *tt_local_entry = NULL;
1493 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001494
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001496 if (!tt_local_entry)
1497 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001498 /* Check if the client has been logically deleted (but is kept for
1499 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001500 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001501 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001502 ret = true;
1503out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001505 tt_local_entry_free_ref(tt_local_entry);
1506 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507}
1508
1509void handle_tt_response(struct bat_priv *bat_priv,
1510 struct tt_query_packet *tt_response)
1511{
1512 struct tt_req_node *node, *safe;
1513 struct orig_node *orig_node = NULL;
1514
1515 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1516 "ttvn %d t_size: %d [%c]\n",
1517 tt_response->src, tt_response->ttvn,
1518 tt_response->tt_data,
1519 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1520
1521 orig_node = orig_hash_find(bat_priv, tt_response->src);
1522 if (!orig_node)
1523 goto out;
1524
1525 if (tt_response->flags & TT_FULL_TABLE)
1526 tt_fill_gtable(bat_priv, tt_response);
1527 else
1528 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1529 tt_response->ttvn,
1530 (struct tt_change *)(tt_response + 1));
1531
1532 /* Delete the tt_req_node from pending tt_requests list */
1533 spin_lock_bh(&bat_priv->tt_req_list_lock);
1534 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1535 if (!compare_eth(node->addr, tt_response->src))
1536 continue;
1537 list_del(&node->list);
1538 kfree(node);
1539 }
1540 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1541
1542 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001543 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001544 /* Roaming phase is over: tables are in sync again. I can
1545 * unset the flag */
1546 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001547out:
1548 if (orig_node)
1549 orig_node_free_ref(orig_node);
1550}
1551
1552int tt_init(struct bat_priv *bat_priv)
1553{
1554 if (!tt_local_init(bat_priv))
1555 return 0;
1556
1557 if (!tt_global_init(bat_priv))
1558 return 0;
1559
1560 tt_start_timer(bat_priv);
1561
1562 return 1;
1563}
1564
Antonio Quartullicc47f662011-04-27 14:27:57 +02001565static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001566{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001567 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001568
Antonio Quartullicc47f662011-04-27 14:27:57 +02001569 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001570
Antonio Quartullicc47f662011-04-27 14:27:57 +02001571 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1572 list_del(&node->list);
1573 kfree(node);
1574 }
1575
1576 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1577}
1578
1579static void tt_roam_purge(struct bat_priv *bat_priv)
1580{
1581 struct tt_roam_node *node, *safe;
1582
1583 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1584 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001585 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001586 continue;
1587
1588 list_del(&node->list);
1589 kfree(node);
1590 }
1591 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1592}
1593
1594/* This function checks whether the client already reached the
1595 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1596 * will not be sent.
1597 *
1598 * returns true if the ROAMING_ADV can be sent, false otherwise */
1599static bool tt_check_roam_count(struct bat_priv *bat_priv,
1600 uint8_t *client)
1601{
1602 struct tt_roam_node *tt_roam_node;
1603 bool ret = false;
1604
1605 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1606 /* The new tt_req will be issued only if I'm not waiting for a
1607 * reply from the same orig_node yet */
1608 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1609 if (!compare_eth(tt_roam_node->addr, client))
1610 continue;
1611
Marek Lindner032b7962011-12-20 19:30:40 +08001612 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001613 continue;
1614
1615 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1616 /* Sorry, you roamed too many times! */
1617 goto unlock;
1618 ret = true;
1619 break;
1620 }
1621
1622 if (!ret) {
1623 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1624 if (!tt_roam_node)
1625 goto unlock;
1626
1627 tt_roam_node->first_time = jiffies;
1628 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1629 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1630
1631 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1632 ret = true;
1633 }
1634
1635unlock:
1636 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1637 return ret;
1638}
1639
1640void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1641 struct orig_node *orig_node)
1642{
1643 struct neigh_node *neigh_node = NULL;
1644 struct sk_buff *skb = NULL;
1645 struct roam_adv_packet *roam_adv_packet;
1646 int ret = 1;
1647 struct hard_iface *primary_if;
1648
1649 /* before going on we have to check whether the client has
1650 * already roamed to us too many times */
1651 if (!tt_check_roam_count(bat_priv, client))
1652 goto out;
1653
1654 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1655 if (!skb)
1656 goto out;
1657
1658 skb_reserve(skb, ETH_HLEN);
1659
1660 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1661 sizeof(struct roam_adv_packet));
1662
Sven Eckelmann76543d12011-11-20 15:47:38 +01001663 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1664 roam_adv_packet->header.version = COMPAT_VERSION;
1665 roam_adv_packet->header.ttl = TTL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001666 primary_if = primary_if_get_selected(bat_priv);
1667 if (!primary_if)
1668 goto out;
1669 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1670 hardif_free_ref(primary_if);
1671 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1672 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1673
1674 neigh_node = orig_node_get_router(orig_node);
1675 if (!neigh_node)
1676 goto out;
1677
1678 bat_dbg(DBG_TT, bat_priv,
1679 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1680 orig_node->orig, client, neigh_node->addr);
1681
1682 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1683 ret = 0;
1684
1685out:
1686 if (neigh_node)
1687 neigh_node_free_ref(neigh_node);
1688 if (ret)
1689 kfree_skb(skb);
1690 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691}
1692
1693static void tt_purge(struct work_struct *work)
1694{
1695 struct delayed_work *delayed_work =
1696 container_of(work, struct delayed_work, work);
1697 struct bat_priv *bat_priv =
1698 container_of(delayed_work, struct bat_priv, tt_work);
1699
1700 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001701 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001703 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704
1705 tt_start_timer(bat_priv);
1706}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001707
1708void tt_free(struct bat_priv *bat_priv)
1709{
1710 cancel_delayed_work_sync(&bat_priv->tt_work);
1711
1712 tt_local_table_free(bat_priv);
1713 tt_global_table_free(bat_priv);
1714 tt_req_list_free(bat_priv);
1715 tt_changes_list_free(bat_priv);
1716 tt_roam_list_free(bat_priv);
1717
1718 kfree(bat_priv->tt_buff);
1719}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001720
Antonio Quartulli697f2532011-11-07 16:47:01 +01001721/* This function will enable or disable the specified flags for all the entries
1722 * in the given hash table and returns the number of modified entries */
1723static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1724 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001725{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001726 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001727 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001728 struct hlist_head *head;
1729 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001730 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001731
1732 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001733 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001734
1735 for (i = 0; i < hash->size; i++) {
1736 head = &hash->table[i];
1737
1738 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001739 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001740 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001741 if (enable) {
1742 if ((tt_common_entry->flags & flags) == flags)
1743 continue;
1744 tt_common_entry->flags |= flags;
1745 } else {
1746 if (!(tt_common_entry->flags & flags))
1747 continue;
1748 tt_common_entry->flags &= ~flags;
1749 }
1750 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001751 }
1752 rcu_read_unlock();
1753 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001754out:
1755 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001756}
1757
1758/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1759static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1760{
1761 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001762 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001763 struct tt_local_entry *tt_local_entry;
1764 struct hlist_node *node, *node_tmp;
1765 struct hlist_head *head;
1766 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001767 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001768
1769 if (!hash)
1770 return;
1771
1772 for (i = 0; i < hash->size; i++) {
1773 head = &hash->table[i];
1774 list_lock = &hash->list_locks[i];
1775
1776 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001777 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001778 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001779 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001780 continue;
1781
1782 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001783 "(%pM): pending\n", tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001784
1785 atomic_dec(&bat_priv->num_local_tt);
1786 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001787 tt_local_entry = container_of(tt_common_entry,
1788 struct tt_local_entry,
1789 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001790 tt_local_entry_free_ref(tt_local_entry);
1791 }
1792 spin_unlock_bh(list_lock);
1793 }
1794
1795}
1796
1797void tt_commit_changes(struct bat_priv *bat_priv)
1798{
Antonio Quartulli697f2532011-11-07 16:47:01 +01001799 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
1800 TT_CLIENT_NEW, false);
1801 /* all the reset entries have now to be effectively counted as local
1802 * entries */
1803 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001804 tt_local_purge_pending_clients(bat_priv);
1805
1806 /* Increment the TTVN only once per OGM interval */
1807 atomic_inc(&bat_priv->ttvn);
1808 bat_priv->tt_poss_change = false;
1809}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001810
1811bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1812{
1813 struct tt_local_entry *tt_local_entry = NULL;
1814 struct tt_global_entry *tt_global_entry = NULL;
1815 bool ret = true;
1816
1817 if (!atomic_read(&bat_priv->ap_isolation))
1818 return false;
1819
1820 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1821 if (!tt_local_entry)
1822 goto out;
1823
1824 tt_global_entry = tt_global_hash_find(bat_priv, src);
1825 if (!tt_global_entry)
1826 goto out;
1827
1828 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1829 goto out;
1830
1831 ret = false;
1832
1833out:
1834 if (tt_global_entry)
1835 tt_global_entry_free_ref(tt_global_entry);
1836 if (tt_local_entry)
1837 tt_local_entry_free_ref(tt_local_entry);
1838 return ret;
1839}
Marek Lindnera943cac2011-07-30 13:10:18 +02001840
1841void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1842 const unsigned char *tt_buff, uint8_t tt_num_changes,
1843 uint8_t ttvn, uint16_t tt_crc)
1844{
1845 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1846 bool full_table = true;
1847
Antonio Quartulli17071572011-11-07 16:36:40 +01001848 /* orig table not initialised AND first diff is in the OGM OR the ttvn
1849 * increased by one -> we can apply the attached changes */
1850 if ((!orig_node->tt_initialised && ttvn == 1) ||
1851 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001852 /* the OGM could not contain the changes due to their size or
1853 * because they have already been sent TT_OGM_APPEND_MAX times.
1854 * In this case send a tt request */
1855 if (!tt_num_changes) {
1856 full_table = false;
1857 goto request_table;
1858 }
1859
1860 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1861 (struct tt_change *)tt_buff);
1862
1863 /* Even if we received the precomputed crc with the OGM, we
1864 * prefer to recompute it to spot any possible inconsistency
1865 * in the global table */
1866 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1867
1868 /* The ttvn alone is not enough to guarantee consistency
1869 * because a single value could represent different states
1870 * (due to the wrap around). Thus a node has to check whether
1871 * the resulting table (after applying the changes) is still
1872 * consistent or not. E.g. a node could disconnect while its
1873 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1874 * checking the CRC value is mandatory to detect the
1875 * inconsistency */
1876 if (orig_node->tt_crc != tt_crc)
1877 goto request_table;
1878
1879 /* Roaming phase is over: tables are in sync again. I can
1880 * unset the flag */
1881 orig_node->tt_poss_change = false;
1882 } else {
1883 /* if we missed more than one change or our tables are not
1884 * in sync anymore -> request fresh tt data */
Antonio Quartulli17071572011-11-07 16:36:40 +01001885 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
1886 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001887request_table:
1888 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1889 "Need to retrieve the correct information "
1890 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1891 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1892 orig_ttvn, tt_crc, orig_node->tt_crc,
1893 tt_num_changes);
1894 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1895 full_table);
1896 return;
1897 }
1898 }
1899}