blob: 7cc67c0f491519b6d9bcf3ca0a7b76e2bfb7def8 [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 */
Sven Eckelmann747e4222011-05-14 23:14:50 +020039static int compare_ltt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Sven Eckelmann747e4222011-05-14 23:14:50 +020041 const void *data1 = container_of(node, struct tt_local_entry,
42 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
47/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020048static int compare_gtt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000049{
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 const void *data1 = container_of(node, struct tt_global_entry,
51 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000052
53 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
54}
55
Antonio Quartullia73105b2011-04-27 14:27:44 +020056static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Antonio Quartullia73105b2011-04-27 14:27:44 +020058 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
59 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
60 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061}
62
Antonio Quartulli2dafb492011-05-05 08:42:45 +020063static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020064 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000065{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020066 struct hashtable_t *hash = bat_priv->tt_local_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000067 struct hlist_head *head;
68 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020069 struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000070 int index;
71
72 if (!hash)
73 return NULL;
74
75 index = choose_orig(data, hash->size);
76 head = &hash->table[index];
77
78 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +020079 hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
80 if (!compare_eth(tt_local_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000081 continue;
82
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020083 if (!atomic_inc_not_zero(&tt_local_entry->refcount))
84 continue;
85
Antonio Quartulli2dafb492011-05-05 08:42:45 +020086 tt_local_entry_tmp = tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000087 break;
88 }
89 rcu_read_unlock();
90
Antonio Quartulli2dafb492011-05-05 08:42:45 +020091 return tt_local_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +000092}
93
Antonio Quartulli2dafb492011-05-05 08:42:45 +020094static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020095 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000096{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097 struct hashtable_t *hash = bat_priv->tt_global_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000098 struct hlist_head *head;
99 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200100 struct tt_global_entry *tt_global_entry;
101 struct tt_global_entry *tt_global_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102 int index;
103
104 if (!hash)
105 return NULL;
106
107 index = choose_orig(data, hash->size);
108 head = &hash->table[index];
109
110 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200111 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
112 if (!compare_eth(tt_global_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000113 continue;
114
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115 if (!atomic_inc_not_zero(&tt_global_entry->refcount))
116 continue;
117
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200118 tt_global_entry_tmp = tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000119 break;
120 }
121 rcu_read_unlock();
122
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200123 return tt_global_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +0000124}
125
Antonio Quartullia73105b2011-04-27 14:27:44 +0200126static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
127{
128 unsigned long deadline;
129 deadline = starting_time + msecs_to_jiffies(timeout);
130
131 return time_after(jiffies, deadline);
132}
133
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
135{
136 if (atomic_dec_and_test(&tt_local_entry->refcount))
137 kfree_rcu(tt_local_entry, rcu);
138}
139
140static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
141{
142 if (atomic_dec_and_test(&tt_global_entry->refcount))
143 kfree_rcu(tt_global_entry, rcu);
144}
145
Antonio Quartulliff66c972011-06-30 01:14:00 +0200146static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
147 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200148{
149 struct tt_change_node *tt_change_node;
150
151 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
152
153 if (!tt_change_node)
154 return;
155
Antonio Quartulliff66c972011-06-30 01:14:00 +0200156 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200157 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
158
159 spin_lock_bh(&bat_priv->tt_changes_list_lock);
160 /* track the change in the OGMinterval list */
161 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
162 atomic_inc(&bat_priv->tt_local_changes);
163 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
164
165 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
166}
167
168int tt_len(int changes_num)
169{
170 return changes_num * sizeof(struct tt_change);
171}
172
173static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200175 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 return 1;
177
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200180 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 return 0;
182
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 return 1;
184}
185
Sven Eckelmann747e4222011-05-14 23:14:50 +0200186void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187{
188 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200189 struct tt_local_entry *tt_local_entry = NULL;
190 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200192 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200194 if (tt_local_entry) {
195 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200196 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 }
198
Sven Eckelmann704509b2011-05-14 23:14:54 +0200199 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200200 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200201 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200202
Antonio Quartullia73105b2011-04-27 14:27:44 +0200203 bat_dbg(DBG_TT, bat_priv,
204 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
205 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200207 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
208 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200209 tt_local_entry->flags = NO_FLAGS;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200210 atomic_set(&tt_local_entry->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
212 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000213 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200214 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215
Antonio Quartulliff66c972011-06-30 01:14:00 +0200216 tt_local_event(bat_priv, addr, tt_local_entry->flags);
217
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200218 /* The local entry has to be marked as NEW to avoid to send it in
219 * a full table response going out before the next ttvn increment
220 * (consistency check) */
221 tt_local_entry->flags |= TT_CLIENT_NEW;
222
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200223 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
224 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200225
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200227 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Antonio Quartullicc47f662011-04-27 14:27:57 +0200229 /* Check whether it is a roaming! */
230 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200231 /* This node is probably going to update its tt table */
232 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200233 _tt_global_del(bat_priv, tt_global_entry,
234 "local tt received");
Antonio Quartullicc47f662011-04-27 14:27:57 +0200235 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200236 tt_global_entry->orig_node);
237 }
238out:
239 if (tt_local_entry)
240 tt_local_entry_free_ref(tt_local_entry);
241 if (tt_global_entry)
242 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243}
244
Antonio Quartullia73105b2011-04-27 14:27:44 +0200245int tt_changes_fill_buffer(struct bat_priv *bat_priv,
246 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200248 int count = 0, tot_changes = 0;
249 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Antonio Quartullia73105b2011-04-27 14:27:44 +0200251 if (buff_len > 0)
252 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartullia73105b2011-04-27 14:27:44 +0200254 spin_lock_bh(&bat_priv->tt_changes_list_lock);
255 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Antonio Quartullia73105b2011-04-27 14:27:44 +0200257 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
258 list) {
259 if (count < tot_changes) {
260 memcpy(buff + tt_len(count),
261 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 count++;
263 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264 list_del(&entry->list);
265 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200267 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Antonio Quartullia73105b2011-04-27 14:27:44 +0200269 /* Keep the buffer for possible tt_request */
270 spin_lock_bh(&bat_priv->tt_buff_lock);
271 kfree(bat_priv->tt_buff);
272 bat_priv->tt_buff_len = 0;
273 bat_priv->tt_buff = NULL;
274 /* We check whether this new OGM has no changes due to size
275 * problems */
276 if (buff_len > 0) {
277 /**
278 * if kmalloc() fails we will reply with the full table
279 * instead of providing the diff
280 */
281 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
282 if (bat_priv->tt_buff) {
283 memcpy(bat_priv->tt_buff, buff, buff_len);
284 bat_priv->tt_buff_len = buff_len;
285 }
286 }
287 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
Antonio Quartullia73105b2011-04-27 14:27:44 +0200289 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290}
291
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200292int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293{
294 struct net_device *net_dev = (struct net_device *)seq->private;
295 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200296 struct hashtable_t *hash = bat_priv->tt_local_hash;
297 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200298 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000299 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 size_t buf_size, pos;
302 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200303 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Marek Lindner32ae9b22011-04-20 15:40:58 +0200305 primary_if = primary_if_get_selected(bat_priv);
306 if (!primary_if) {
307 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
308 "please specify interfaces to enable it\n",
309 net_dev->name);
310 goto out;
311 }
312
313 if (primary_if->if_status != IF_ACTIVE) {
314 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
315 "primary interface not active\n",
316 net_dev->name);
317 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 }
319
320 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200321 "announced via TT (TTVN: %u):\n",
322 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 buf_size = 1;
325 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
326 for (i = 0; i < hash->size; i++) {
327 head = &hash->table[i];
328
Marek Lindner7aadf882011-02-18 12:28:09 +0000329 rcu_read_lock();
330 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000332 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 }
334
335 buff = kmalloc(buf_size, GFP_ATOMIC);
336 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200337 ret = -ENOMEM;
338 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000340
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341 buff[0] = '\0';
342 pos = 0;
343
344 for (i = 0; i < hash->size; i++) {
345 head = &hash->table[i];
346
Marek Lindner7aadf882011-02-18 12:28:09 +0000347 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200348 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000349 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200351 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000353 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 }
355
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 seq_printf(seq, "%s", buff);
357 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200358out:
359 if (primary_if)
360 hardif_free_ref(primary_if);
361 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362}
363
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200364static void tt_local_set_pending(struct bat_priv *bat_priv,
365 struct tt_local_entry *tt_local_entry,
366 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367{
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200368 tt_local_event(bat_priv, tt_local_entry->addr,
369 tt_local_entry->flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200371 /* The local client has to be merked as "pending to be removed" but has
372 * to be kept in the table in order to send it in an full tables
373 * response issued before the net ttvn increment (consistency check) */
374 tt_local_entry->flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375}
376
Antonio Quartullia73105b2011-04-27 14:27:44 +0200377void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200378 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200380 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200382 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200383 if (!tt_local_entry)
384 goto out;
385
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200386 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
387 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
388
389 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
390 "%s\n", tt_local_entry->addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200391out:
392 if (tt_local_entry)
393 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394}
395
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200398 struct hashtable_t *hash = bat_priv->tt_local_hash;
399 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000400 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200402 spinlock_t *list_lock; /* protects write access to the hash lists */
Marek Lindner7aadf882011-02-18 12:28:09 +0000403 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 for (i = 0; i < hash->size; i++) {
406 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200407 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200409 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200410 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000411 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200412 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000413 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200415 /* entry already marked for deletion */
416 if (tt_local_entry->flags & TT_CLIENT_PENDING)
417 continue;
418
Antonio Quartullia73105b2011-04-27 14:27:44 +0200419 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200420 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000421 continue;
422
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200423 tt_local_set_pending(bat_priv, tt_local_entry,
424 TT_CLIENT_DEL);
425 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
426 "pending to be removed: timed out\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200427 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200429 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 }
431
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432}
433
Antonio Quartullia73105b2011-04-27 14:27:44 +0200434static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200439 struct hlist_node *node, *node_tmp;
440 struct hlist_head *head;
441 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200442
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200443 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 return;
445
Antonio Quartullia73105b2011-04-27 14:27:44 +0200446 hash = bat_priv->tt_local_hash;
447
448 for (i = 0; i < hash->size; i++) {
449 head = &hash->table[i];
450 list_lock = &hash->list_locks[i];
451
452 spin_lock_bh(list_lock);
453 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
454 head, hash_entry) {
455 hlist_del_rcu(node);
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 Quartullicc47f662011-04-27 14:27:57 +0200497 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200499 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200500 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200501 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Antonio Quartullia73105b2011-04-27 14:27:44 +0200503 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Antonio Quartullia73105b2011-04-27 14:27:44 +0200505 if (!tt_global_entry) {
506 tt_global_entry =
507 kmalloc(sizeof(*tt_global_entry),
508 GFP_ATOMIC);
509 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200510 goto out;
511
Antonio Quartullia73105b2011-04-27 14:27:44 +0200512 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
513 /* Assign the new orig_node */
514 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200515 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200516 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200517 tt_global_entry->flags = NO_FLAGS;
518 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200519 atomic_set(&tt_global_entry->refcount, 2);
520
Antonio Quartullia73105b2011-04-27 14:27:44 +0200521 hash_add(bat_priv->tt_global_hash, compare_gtt,
522 choose_orig, tt_global_entry,
523 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200524 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200525 } else {
526 if (tt_global_entry->orig_node != orig_node) {
527 atomic_dec(&tt_global_entry->orig_node->tt_size);
528 orig_node_tmp = tt_global_entry->orig_node;
529 atomic_inc(&orig_node->refcount);
530 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200531 orig_node_free_ref(orig_node_tmp);
532 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200534 tt_global_entry->ttvn = ttvn;
535 tt_global_entry->flags = NO_FLAGS;
536 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200538
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539 bat_dbg(DBG_TT, bat_priv,
540 "Creating new global tt entry: %pM (via %pM)\n",
541 tt_global_entry->addr, orig_node->orig);
542
543 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200544 tt_local_remove(bat_priv, tt_global_entry->addr,
545 "global tt received", roaming);
546 ret = 1;
547out:
548 if (tt_global_entry)
549 tt_global_entry_free_ref(tt_global_entry);
550 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551}
552
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200553int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554{
555 struct net_device *net_dev = (struct net_device *)seq->private;
556 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200557 struct hashtable_t *hash = bat_priv->tt_global_hash;
558 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000560 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562 size_t buf_size, pos;
563 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200564 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Marek Lindner32ae9b22011-04-20 15:40:58 +0200566 primary_if = primary_if_get_selected(bat_priv);
567 if (!primary_if) {
568 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
569 "specify interfaces to enable it\n",
570 net_dev->name);
571 goto out;
572 }
573
574 if (primary_if->if_status != IF_ACTIVE) {
575 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
576 "primary interface not active\n",
577 net_dev->name);
578 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 }
580
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200581 seq_printf(seq,
582 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200584 seq_printf(seq, " %-13s %s %-15s %s\n",
585 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200588 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
589 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590 for (i = 0; i < hash->size; i++) {
591 head = &hash->table[i];
592
Marek Lindner7aadf882011-02-18 12:28:09 +0000593 rcu_read_lock();
594 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200595 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000596 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597 }
598
599 buff = kmalloc(buf_size, GFP_ATOMIC);
600 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200601 ret = -ENOMEM;
602 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200604
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 buff[0] = '\0';
606 pos = 0;
607
608 for (i = 0; i < hash->size; i++) {
609 head = &hash->table[i];
610
Marek Lindner7aadf882011-02-18 12:28:09 +0000611 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200612 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000613 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200614 pos += snprintf(buff + pos, 61,
615 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200616 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200617 tt_global_entry->ttvn,
618 tt_global_entry->orig_node->orig,
619 (uint8_t) atomic_read(
620 &tt_global_entry->orig_node->
621 last_ttvn));
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 }
625
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626 seq_printf(seq, "%s", buff);
627 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200628out:
629 if (primary_if)
630 hardif_free_ref(primary_if);
631 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632}
633
Antonio Quartullia73105b2011-04-27 14:27:44 +0200634static void _tt_global_del(struct bat_priv *bat_priv,
635 struct tt_global_entry *tt_global_entry,
636 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200640
641 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200642 "Deleting global tt entry %pM (via %pM): %s\n",
643 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644 message);
645
Antonio Quartullia73105b2011-04-27 14:27:44 +0200646 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200648 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
649 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200650out:
651 if (tt_global_entry)
652 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653}
654
Antonio Quartullia73105b2011-04-27 14:27:44 +0200655void tt_global_del(struct bat_priv *bat_priv,
656 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200657 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200659 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660
Antonio Quartullia73105b2011-04-27 14:27:44 +0200661 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200662 if (!tt_global_entry)
663 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200664
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200665 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200666 if (roaming) {
667 tt_global_entry->flags |= TT_CLIENT_ROAM;
668 tt_global_entry->roam_at = jiffies;
669 goto out;
670 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200671 _tt_global_del(bat_priv, tt_global_entry, message);
672 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200673out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200674 if (tt_global_entry)
675 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200676}
677
678void tt_global_del_orig(struct bat_priv *bat_priv,
679 struct orig_node *orig_node, const char *message)
680{
681 struct tt_global_entry *tt_global_entry;
682 int i;
683 struct hashtable_t *hash = bat_priv->tt_global_hash;
684 struct hlist_node *node, *safe;
685 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200686 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200687
Antonio Quartullia73105b2011-04-27 14:27:44 +0200688 for (i = 0; i < hash->size; i++) {
689 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200690 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200692 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200693 hlist_for_each_entry_safe(tt_global_entry, node, safe,
694 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200695 if (tt_global_entry->orig_node == orig_node) {
696 bat_dbg(DBG_TT, bat_priv,
697 "Deleting global tt entry %pM "
698 "(via %pM): originator time out\n",
699 tt_global_entry->addr,
700 tt_global_entry->orig_node->orig);
701 hlist_del_rcu(node);
702 tt_global_entry_free_ref(tt_global_entry);
703 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200704 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200705 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200707 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708}
709
Antonio Quartullicc47f662011-04-27 14:27:57 +0200710static void tt_global_roam_purge(struct bat_priv *bat_priv)
711{
712 struct hashtable_t *hash = bat_priv->tt_global_hash;
713 struct tt_global_entry *tt_global_entry;
714 struct hlist_node *node, *node_tmp;
715 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200716 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200717 int i;
718
Antonio Quartullicc47f662011-04-27 14:27:57 +0200719 for (i = 0; i < hash->size; i++) {
720 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200721 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200722
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200723 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200724 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
725 head, hash_entry) {
726 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
727 continue;
728 if (!is_out_of_time(tt_global_entry->roam_at,
729 TT_CLIENT_ROAM_TIMEOUT * 1000))
730 continue;
731
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200732 bat_dbg(DBG_TT, bat_priv, "Deleting global "
733 "tt entry (%pM): Roaming timeout\n",
734 tt_global_entry->addr);
735 atomic_dec(&tt_global_entry->orig_node->tt_size);
736 hlist_del_rcu(node);
737 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200738 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200739 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200740 }
741
Antonio Quartullicc47f662011-04-27 14:27:57 +0200742}
743
Antonio Quartullia73105b2011-04-27 14:27:44 +0200744static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200746 struct hashtable_t *hash;
747 spinlock_t *list_lock; /* protects write access to the hash lists */
748 struct tt_global_entry *tt_global_entry;
749 struct hlist_node *node, *node_tmp;
750 struct hlist_head *head;
751 int i;
752
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200753 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754 return;
755
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200756 hash = bat_priv->tt_global_hash;
757
758 for (i = 0; i < hash->size; i++) {
759 head = &hash->table[i];
760 list_lock = &hash->list_locks[i];
761
762 spin_lock_bh(list_lock);
763 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
764 head, hash_entry) {
765 hlist_del_rcu(node);
766 tt_global_entry_free_ref(tt_global_entry);
767 }
768 spin_unlock_bh(list_lock);
769 }
770
771 hash_destroy(hash);
772
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200773 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774}
775
Sven Eckelmann747e4222011-05-14 23:14:50 +0200776struct orig_node *transtable_search(struct bat_priv *bat_priv,
777 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200779 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000780 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200782 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000783
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200784 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000785 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200787 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200788 goto free_tt;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000789
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200790 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000791
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200792free_tt:
793 tt_global_entry_free_ref(tt_global_entry);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000794out:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000795 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200797
798/* Calculates the checksum of the local table of a given orig_node */
799uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
800{
801 uint16_t total = 0, total_one;
802 struct hashtable_t *hash = bat_priv->tt_global_hash;
803 struct tt_global_entry *tt_global_entry;
804 struct hlist_node *node;
805 struct hlist_head *head;
806 int i, j;
807
808 for (i = 0; i < hash->size; i++) {
809 head = &hash->table[i];
810
811 rcu_read_lock();
812 hlist_for_each_entry_rcu(tt_global_entry, node,
813 head, hash_entry) {
814 if (compare_eth(tt_global_entry->orig_node,
815 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200816 /* Roaming clients are in the global table for
817 * consistency only. They don't have to be
818 * taken into account while computing the
819 * global crc */
820 if (tt_global_entry->flags & TT_CLIENT_ROAM)
821 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200822 total_one = 0;
823 for (j = 0; j < ETH_ALEN; j++)
824 total_one = crc16_byte(total_one,
825 tt_global_entry->addr[j]);
826 total ^= total_one;
827 }
828 }
829 rcu_read_unlock();
830 }
831
832 return total;
833}
834
835/* Calculates the checksum of the local table */
836uint16_t tt_local_crc(struct bat_priv *bat_priv)
837{
838 uint16_t total = 0, total_one;
839 struct hashtable_t *hash = bat_priv->tt_local_hash;
840 struct tt_local_entry *tt_local_entry;
841 struct hlist_node *node;
842 struct hlist_head *head;
843 int i, j;
844
845 for (i = 0; i < hash->size; i++) {
846 head = &hash->table[i];
847
848 rcu_read_lock();
849 hlist_for_each_entry_rcu(tt_local_entry, node,
850 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200851 /* not yet committed clients have not to be taken into
852 * account while computing the CRC */
853 if (tt_local_entry->flags & TT_CLIENT_NEW)
854 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200855 total_one = 0;
856 for (j = 0; j < ETH_ALEN; j++)
857 total_one = crc16_byte(total_one,
858 tt_local_entry->addr[j]);
859 total ^= total_one;
860 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200861 rcu_read_unlock();
862 }
863
864 return total;
865}
866
867static void tt_req_list_free(struct bat_priv *bat_priv)
868{
869 struct tt_req_node *node, *safe;
870
871 spin_lock_bh(&bat_priv->tt_req_list_lock);
872
873 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
874 list_del(&node->list);
875 kfree(node);
876 }
877
878 spin_unlock_bh(&bat_priv->tt_req_list_lock);
879}
880
881void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
882 const unsigned char *tt_buff, uint8_t tt_num_changes)
883{
884 uint16_t tt_buff_len = tt_len(tt_num_changes);
885
886 /* Replace the old buffer only if I received something in the
887 * last OGM (the OGM could carry no changes) */
888 spin_lock_bh(&orig_node->tt_buff_lock);
889 if (tt_buff_len > 0) {
890 kfree(orig_node->tt_buff);
891 orig_node->tt_buff_len = 0;
892 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
893 if (orig_node->tt_buff) {
894 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
895 orig_node->tt_buff_len = tt_buff_len;
896 }
897 }
898 spin_unlock_bh(&orig_node->tt_buff_lock);
899}
900
901static void tt_req_purge(struct bat_priv *bat_priv)
902{
903 struct tt_req_node *node, *safe;
904
905 spin_lock_bh(&bat_priv->tt_req_list_lock);
906 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
907 if (is_out_of_time(node->issued_at,
908 TT_REQUEST_TIMEOUT * 1000)) {
909 list_del(&node->list);
910 kfree(node);
911 }
912 }
913 spin_unlock_bh(&bat_priv->tt_req_list_lock);
914}
915
916/* returns the pointer to the new tt_req_node struct if no request
917 * has already been issued for this orig_node, NULL otherwise */
918static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
919 struct orig_node *orig_node)
920{
921 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
922
923 spin_lock_bh(&bat_priv->tt_req_list_lock);
924 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
925 if (compare_eth(tt_req_node_tmp, orig_node) &&
926 !is_out_of_time(tt_req_node_tmp->issued_at,
927 TT_REQUEST_TIMEOUT * 1000))
928 goto unlock;
929 }
930
931 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
932 if (!tt_req_node)
933 goto unlock;
934
935 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
936 tt_req_node->issued_at = jiffies;
937
938 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
939unlock:
940 spin_unlock_bh(&bat_priv->tt_req_list_lock);
941 return tt_req_node;
942}
943
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200944/* data_ptr is useless here, but has to be kept to respect the prototype */
945static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
946{
947 const struct tt_local_entry *tt_local_entry = entry_ptr;
948
949 if (tt_local_entry->flags & TT_CLIENT_NEW)
950 return 0;
951 return 1;
952}
953
Antonio Quartullia73105b2011-04-27 14:27:44 +0200954static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
955{
956 const struct tt_global_entry *tt_global_entry = entry_ptr;
957 const struct orig_node *orig_node = data_ptr;
958
Antonio Quartullicc47f662011-04-27 14:27:57 +0200959 if (tt_global_entry->flags & TT_CLIENT_ROAM)
960 return 0;
961
Antonio Quartullia73105b2011-04-27 14:27:44 +0200962 return (tt_global_entry->orig_node == orig_node);
963}
964
965static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
966 struct hashtable_t *hash,
967 struct hard_iface *primary_if,
968 int (*valid_cb)(const void *,
969 const void *),
970 void *cb_data)
971{
972 struct tt_local_entry *tt_local_entry;
973 struct tt_query_packet *tt_response;
974 struct tt_change *tt_change;
975 struct hlist_node *node;
976 struct hlist_head *head;
977 struct sk_buff *skb = NULL;
978 uint16_t tt_tot, tt_count;
979 ssize_t tt_query_size = sizeof(struct tt_query_packet);
980 int i;
981
982 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
983 tt_len = primary_if->soft_iface->mtu - tt_query_size;
984 tt_len -= tt_len % sizeof(struct tt_change);
985 }
986 tt_tot = tt_len / sizeof(struct tt_change);
987
988 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
989 if (!skb)
990 goto out;
991
992 skb_reserve(skb, ETH_HLEN);
993 tt_response = (struct tt_query_packet *)skb_put(skb,
994 tt_query_size + tt_len);
995 tt_response->ttvn = ttvn;
996 tt_response->tt_data = htons(tt_tot);
997
998 tt_change = (struct tt_change *)(skb->data + tt_query_size);
999 tt_count = 0;
1000
1001 rcu_read_lock();
1002 for (i = 0; i < hash->size; i++) {
1003 head = &hash->table[i];
1004
1005 hlist_for_each_entry_rcu(tt_local_entry, node,
1006 head, hash_entry) {
1007 if (tt_count == tt_tot)
1008 break;
1009
1010 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
1011 continue;
1012
1013 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
1014 tt_change->flags = NO_FLAGS;
1015
1016 tt_count++;
1017 tt_change++;
1018 }
1019 }
1020 rcu_read_unlock();
1021
1022out:
1023 return skb;
1024}
1025
1026int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1027 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1028{
1029 struct sk_buff *skb = NULL;
1030 struct tt_query_packet *tt_request;
1031 struct neigh_node *neigh_node = NULL;
1032 struct hard_iface *primary_if;
1033 struct tt_req_node *tt_req_node = NULL;
1034 int ret = 1;
1035
1036 primary_if = primary_if_get_selected(bat_priv);
1037 if (!primary_if)
1038 goto out;
1039
1040 /* The new tt_req will be issued only if I'm not waiting for a
1041 * reply from the same orig_node yet */
1042 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1043 if (!tt_req_node)
1044 goto out;
1045
1046 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1047 if (!skb)
1048 goto out;
1049
1050 skb_reserve(skb, ETH_HLEN);
1051
1052 tt_request = (struct tt_query_packet *)skb_put(skb,
1053 sizeof(struct tt_query_packet));
1054
1055 tt_request->packet_type = BAT_TT_QUERY;
1056 tt_request->version = COMPAT_VERSION;
1057 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1058 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1059 tt_request->ttl = TTL;
1060 tt_request->ttvn = ttvn;
1061 tt_request->tt_data = tt_crc;
1062 tt_request->flags = TT_REQUEST;
1063
1064 if (full_table)
1065 tt_request->flags |= TT_FULL_TABLE;
1066
1067 neigh_node = orig_node_get_router(dst_orig_node);
1068 if (!neigh_node)
1069 goto out;
1070
1071 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1072 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1073 (full_table ? 'F' : '.'));
1074
1075 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1076 ret = 0;
1077
1078out:
1079 if (neigh_node)
1080 neigh_node_free_ref(neigh_node);
1081 if (primary_if)
1082 hardif_free_ref(primary_if);
1083 if (ret)
1084 kfree_skb(skb);
1085 if (ret && tt_req_node) {
1086 spin_lock_bh(&bat_priv->tt_req_list_lock);
1087 list_del(&tt_req_node->list);
1088 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1089 kfree(tt_req_node);
1090 }
1091 return ret;
1092}
1093
1094static bool send_other_tt_response(struct bat_priv *bat_priv,
1095 struct tt_query_packet *tt_request)
1096{
1097 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1098 struct neigh_node *neigh_node = NULL;
1099 struct hard_iface *primary_if = NULL;
1100 uint8_t orig_ttvn, req_ttvn, ttvn;
1101 int ret = false;
1102 unsigned char *tt_buff;
1103 bool full_table;
1104 uint16_t tt_len, tt_tot;
1105 struct sk_buff *skb = NULL;
1106 struct tt_query_packet *tt_response;
1107
1108 bat_dbg(DBG_TT, bat_priv,
1109 "Received TT_REQUEST from %pM for "
1110 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1111 tt_request->ttvn, tt_request->dst,
1112 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1113
1114 /* Let's get the orig node of the REAL destination */
1115 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1116 if (!req_dst_orig_node)
1117 goto out;
1118
1119 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1120 if (!res_dst_orig_node)
1121 goto out;
1122
1123 neigh_node = orig_node_get_router(res_dst_orig_node);
1124 if (!neigh_node)
1125 goto out;
1126
1127 primary_if = primary_if_get_selected(bat_priv);
1128 if (!primary_if)
1129 goto out;
1130
1131 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1132 req_ttvn = tt_request->ttvn;
1133
1134 /* I have not the requested data */
1135 if (orig_ttvn != req_ttvn ||
1136 tt_request->tt_data != req_dst_orig_node->tt_crc)
1137 goto out;
1138
1139 /* If it has explicitly been requested the full table */
1140 if (tt_request->flags & TT_FULL_TABLE ||
1141 !req_dst_orig_node->tt_buff)
1142 full_table = true;
1143 else
1144 full_table = false;
1145
1146 /* In this version, fragmentation is not implemented, then
1147 * I'll send only one packet with as much TT entries as I can */
1148 if (!full_table) {
1149 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1150 tt_len = req_dst_orig_node->tt_buff_len;
1151 tt_tot = tt_len / sizeof(struct tt_change);
1152
1153 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1154 tt_len + ETH_HLEN);
1155 if (!skb)
1156 goto unlock;
1157
1158 skb_reserve(skb, ETH_HLEN);
1159 tt_response = (struct tt_query_packet *)skb_put(skb,
1160 sizeof(struct tt_query_packet) + tt_len);
1161 tt_response->ttvn = req_ttvn;
1162 tt_response->tt_data = htons(tt_tot);
1163
1164 tt_buff = skb->data + sizeof(struct tt_query_packet);
1165 /* Copy the last orig_node's OGM buffer */
1166 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1167 req_dst_orig_node->tt_buff_len);
1168
1169 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1170 } else {
1171 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1172 sizeof(struct tt_change);
1173 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1174
1175 skb = tt_response_fill_table(tt_len, ttvn,
1176 bat_priv->tt_global_hash,
1177 primary_if, tt_global_valid_entry,
1178 req_dst_orig_node);
1179 if (!skb)
1180 goto out;
1181
1182 tt_response = (struct tt_query_packet *)skb->data;
1183 }
1184
1185 tt_response->packet_type = BAT_TT_QUERY;
1186 tt_response->version = COMPAT_VERSION;
1187 tt_response->ttl = TTL;
1188 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1189 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1190 tt_response->flags = TT_RESPONSE;
1191
1192 if (full_table)
1193 tt_response->flags |= TT_FULL_TABLE;
1194
1195 bat_dbg(DBG_TT, bat_priv,
1196 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1197 res_dst_orig_node->orig, neigh_node->addr,
1198 req_dst_orig_node->orig, req_ttvn);
1199
1200 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1201 ret = true;
1202 goto out;
1203
1204unlock:
1205 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1206
1207out:
1208 if (res_dst_orig_node)
1209 orig_node_free_ref(res_dst_orig_node);
1210 if (req_dst_orig_node)
1211 orig_node_free_ref(req_dst_orig_node);
1212 if (neigh_node)
1213 neigh_node_free_ref(neigh_node);
1214 if (primary_if)
1215 hardif_free_ref(primary_if);
1216 if (!ret)
1217 kfree_skb(skb);
1218 return ret;
1219
1220}
1221static bool send_my_tt_response(struct bat_priv *bat_priv,
1222 struct tt_query_packet *tt_request)
1223{
1224 struct orig_node *orig_node = NULL;
1225 struct neigh_node *neigh_node = NULL;
1226 struct hard_iface *primary_if = NULL;
1227 uint8_t my_ttvn, req_ttvn, ttvn;
1228 int ret = false;
1229 unsigned char *tt_buff;
1230 bool full_table;
1231 uint16_t tt_len, tt_tot;
1232 struct sk_buff *skb = NULL;
1233 struct tt_query_packet *tt_response;
1234
1235 bat_dbg(DBG_TT, bat_priv,
1236 "Received TT_REQUEST from %pM for "
1237 "ttvn: %u (me) [%c]\n", tt_request->src,
1238 tt_request->ttvn,
1239 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1240
1241
1242 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1243 req_ttvn = tt_request->ttvn;
1244
1245 orig_node = get_orig_node(bat_priv, tt_request->src);
1246 if (!orig_node)
1247 goto out;
1248
1249 neigh_node = orig_node_get_router(orig_node);
1250 if (!neigh_node)
1251 goto out;
1252
1253 primary_if = primary_if_get_selected(bat_priv);
1254 if (!primary_if)
1255 goto out;
1256
1257 /* If the full table has been explicitly requested or the gap
1258 * is too big send the whole local translation table */
1259 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1260 !bat_priv->tt_buff)
1261 full_table = true;
1262 else
1263 full_table = false;
1264
1265 /* In this version, fragmentation is not implemented, then
1266 * I'll send only one packet with as much TT entries as I can */
1267 if (!full_table) {
1268 spin_lock_bh(&bat_priv->tt_buff_lock);
1269 tt_len = bat_priv->tt_buff_len;
1270 tt_tot = tt_len / sizeof(struct tt_change);
1271
1272 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1273 tt_len + ETH_HLEN);
1274 if (!skb)
1275 goto unlock;
1276
1277 skb_reserve(skb, ETH_HLEN);
1278 tt_response = (struct tt_query_packet *)skb_put(skb,
1279 sizeof(struct tt_query_packet) + tt_len);
1280 tt_response->ttvn = req_ttvn;
1281 tt_response->tt_data = htons(tt_tot);
1282
1283 tt_buff = skb->data + sizeof(struct tt_query_packet);
1284 memcpy(tt_buff, bat_priv->tt_buff,
1285 bat_priv->tt_buff_len);
1286 spin_unlock_bh(&bat_priv->tt_buff_lock);
1287 } else {
1288 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1289 sizeof(struct tt_change);
1290 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1291
1292 skb = tt_response_fill_table(tt_len, ttvn,
1293 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001294 primary_if, tt_local_valid_entry,
1295 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001296 if (!skb)
1297 goto out;
1298
1299 tt_response = (struct tt_query_packet *)skb->data;
1300 }
1301
1302 tt_response->packet_type = BAT_TT_QUERY;
1303 tt_response->version = COMPAT_VERSION;
1304 tt_response->ttl = TTL;
1305 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1306 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1307 tt_response->flags = TT_RESPONSE;
1308
1309 if (full_table)
1310 tt_response->flags |= TT_FULL_TABLE;
1311
1312 bat_dbg(DBG_TT, bat_priv,
1313 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1314 orig_node->orig, neigh_node->addr,
1315 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1316
1317 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1318 ret = true;
1319 goto out;
1320
1321unlock:
1322 spin_unlock_bh(&bat_priv->tt_buff_lock);
1323out:
1324 if (orig_node)
1325 orig_node_free_ref(orig_node);
1326 if (neigh_node)
1327 neigh_node_free_ref(neigh_node);
1328 if (primary_if)
1329 hardif_free_ref(primary_if);
1330 if (!ret)
1331 kfree_skb(skb);
1332 /* This packet was for me, so it doesn't need to be re-routed */
1333 return true;
1334}
1335
1336bool send_tt_response(struct bat_priv *bat_priv,
1337 struct tt_query_packet *tt_request)
1338{
1339 if (is_my_mac(tt_request->dst))
1340 return send_my_tt_response(bat_priv, tt_request);
1341 else
1342 return send_other_tt_response(bat_priv, tt_request);
1343}
1344
1345static void _tt_update_changes(struct bat_priv *bat_priv,
1346 struct orig_node *orig_node,
1347 struct tt_change *tt_change,
1348 uint16_t tt_num_changes, uint8_t ttvn)
1349{
1350 int i;
1351
1352 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001353 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001354 tt_global_del(bat_priv, orig_node,
1355 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001356 "tt removed by changes",
1357 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358 else
1359 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001360 (tt_change + i)->addr, ttvn, false))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001361 /* In case of problem while storing a
1362 * global_entry, we stop the updating
1363 * procedure without committing the
1364 * ttvn change. This will avoid to send
1365 * corrupted data on tt_request
1366 */
1367 return;
1368 }
1369}
1370
1371static void tt_fill_gtable(struct bat_priv *bat_priv,
1372 struct tt_query_packet *tt_response)
1373{
1374 struct orig_node *orig_node = NULL;
1375
1376 orig_node = orig_hash_find(bat_priv, tt_response->src);
1377 if (!orig_node)
1378 goto out;
1379
1380 /* Purge the old table first.. */
1381 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1382
1383 _tt_update_changes(bat_priv, orig_node,
1384 (struct tt_change *)(tt_response + 1),
1385 tt_response->tt_data, tt_response->ttvn);
1386
1387 spin_lock_bh(&orig_node->tt_buff_lock);
1388 kfree(orig_node->tt_buff);
1389 orig_node->tt_buff_len = 0;
1390 orig_node->tt_buff = NULL;
1391 spin_unlock_bh(&orig_node->tt_buff_lock);
1392
1393 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1394
1395out:
1396 if (orig_node)
1397 orig_node_free_ref(orig_node);
1398}
1399
1400void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1401 uint16_t tt_num_changes, uint8_t ttvn,
1402 struct tt_change *tt_change)
1403{
1404 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1405 ttvn);
1406
1407 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1408 tt_num_changes);
1409 atomic_set(&orig_node->last_ttvn, ttvn);
1410}
1411
1412bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1413{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001414 struct tt_local_entry *tt_local_entry = NULL;
1415 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416
Antonio Quartullia73105b2011-04-27 14:27:44 +02001417 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001418 if (!tt_local_entry)
1419 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001420 /* Check if the client has been logically deleted (but is kept for
1421 * consistency purpose) */
1422 if (tt_local_entry->flags & TT_CLIENT_PENDING)
1423 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001424 ret = true;
1425out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001426 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001427 tt_local_entry_free_ref(tt_local_entry);
1428 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001429}
1430
1431void handle_tt_response(struct bat_priv *bat_priv,
1432 struct tt_query_packet *tt_response)
1433{
1434 struct tt_req_node *node, *safe;
1435 struct orig_node *orig_node = NULL;
1436
1437 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1438 "ttvn %d t_size: %d [%c]\n",
1439 tt_response->src, tt_response->ttvn,
1440 tt_response->tt_data,
1441 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1442
1443 orig_node = orig_hash_find(bat_priv, tt_response->src);
1444 if (!orig_node)
1445 goto out;
1446
1447 if (tt_response->flags & TT_FULL_TABLE)
1448 tt_fill_gtable(bat_priv, tt_response);
1449 else
1450 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1451 tt_response->ttvn,
1452 (struct tt_change *)(tt_response + 1));
1453
1454 /* Delete the tt_req_node from pending tt_requests list */
1455 spin_lock_bh(&bat_priv->tt_req_list_lock);
1456 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1457 if (!compare_eth(node->addr, tt_response->src))
1458 continue;
1459 list_del(&node->list);
1460 kfree(node);
1461 }
1462 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1463
1464 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001465 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001466 /* Roaming phase is over: tables are in sync again. I can
1467 * unset the flag */
1468 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001469out:
1470 if (orig_node)
1471 orig_node_free_ref(orig_node);
1472}
1473
1474int tt_init(struct bat_priv *bat_priv)
1475{
1476 if (!tt_local_init(bat_priv))
1477 return 0;
1478
1479 if (!tt_global_init(bat_priv))
1480 return 0;
1481
1482 tt_start_timer(bat_priv);
1483
1484 return 1;
1485}
1486
Antonio Quartullicc47f662011-04-27 14:27:57 +02001487static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001488{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001489 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490
Antonio Quartullicc47f662011-04-27 14:27:57 +02001491 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001492
Antonio Quartullicc47f662011-04-27 14:27:57 +02001493 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1494 list_del(&node->list);
1495 kfree(node);
1496 }
1497
1498 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1499}
1500
1501static void tt_roam_purge(struct bat_priv *bat_priv)
1502{
1503 struct tt_roam_node *node, *safe;
1504
1505 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1506 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1507 if (!is_out_of_time(node->first_time,
1508 ROAMING_MAX_TIME * 1000))
1509 continue;
1510
1511 list_del(&node->list);
1512 kfree(node);
1513 }
1514 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1515}
1516
1517/* This function checks whether the client already reached the
1518 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1519 * will not be sent.
1520 *
1521 * returns true if the ROAMING_ADV can be sent, false otherwise */
1522static bool tt_check_roam_count(struct bat_priv *bat_priv,
1523 uint8_t *client)
1524{
1525 struct tt_roam_node *tt_roam_node;
1526 bool ret = false;
1527
1528 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1529 /* The new tt_req will be issued only if I'm not waiting for a
1530 * reply from the same orig_node yet */
1531 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1532 if (!compare_eth(tt_roam_node->addr, client))
1533 continue;
1534
1535 if (is_out_of_time(tt_roam_node->first_time,
1536 ROAMING_MAX_TIME * 1000))
1537 continue;
1538
1539 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1540 /* Sorry, you roamed too many times! */
1541 goto unlock;
1542 ret = true;
1543 break;
1544 }
1545
1546 if (!ret) {
1547 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1548 if (!tt_roam_node)
1549 goto unlock;
1550
1551 tt_roam_node->first_time = jiffies;
1552 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1553 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1554
1555 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1556 ret = true;
1557 }
1558
1559unlock:
1560 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1561 return ret;
1562}
1563
1564void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1565 struct orig_node *orig_node)
1566{
1567 struct neigh_node *neigh_node = NULL;
1568 struct sk_buff *skb = NULL;
1569 struct roam_adv_packet *roam_adv_packet;
1570 int ret = 1;
1571 struct hard_iface *primary_if;
1572
1573 /* before going on we have to check whether the client has
1574 * already roamed to us too many times */
1575 if (!tt_check_roam_count(bat_priv, client))
1576 goto out;
1577
1578 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1579 if (!skb)
1580 goto out;
1581
1582 skb_reserve(skb, ETH_HLEN);
1583
1584 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1585 sizeof(struct roam_adv_packet));
1586
1587 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1588 roam_adv_packet->version = COMPAT_VERSION;
1589 roam_adv_packet->ttl = TTL;
1590 primary_if = primary_if_get_selected(bat_priv);
1591 if (!primary_if)
1592 goto out;
1593 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1594 hardif_free_ref(primary_if);
1595 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1596 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1597
1598 neigh_node = orig_node_get_router(orig_node);
1599 if (!neigh_node)
1600 goto out;
1601
1602 bat_dbg(DBG_TT, bat_priv,
1603 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1604 orig_node->orig, client, neigh_node->addr);
1605
1606 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1607 ret = 0;
1608
1609out:
1610 if (neigh_node)
1611 neigh_node_free_ref(neigh_node);
1612 if (ret)
1613 kfree_skb(skb);
1614 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615}
1616
1617static void tt_purge(struct work_struct *work)
1618{
1619 struct delayed_work *delayed_work =
1620 container_of(work, struct delayed_work, work);
1621 struct bat_priv *bat_priv =
1622 container_of(delayed_work, struct bat_priv, tt_work);
1623
1624 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001625 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001626 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001627 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628
1629 tt_start_timer(bat_priv);
1630}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001631
1632void tt_free(struct bat_priv *bat_priv)
1633{
1634 cancel_delayed_work_sync(&bat_priv->tt_work);
1635
1636 tt_local_table_free(bat_priv);
1637 tt_global_table_free(bat_priv);
1638 tt_req_list_free(bat_priv);
1639 tt_changes_list_free(bat_priv);
1640 tt_roam_list_free(bat_priv);
1641
1642 kfree(bat_priv->tt_buff);
1643}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001644
1645/* This function will reset the specified flags from all the entries in
1646 * the given hash table and will increment num_local_tt for each involved
1647 * entry */
1648static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
1649{
1650 int i;
1651 struct hashtable_t *hash = bat_priv->tt_local_hash;
1652 struct hlist_head *head;
1653 struct hlist_node *node;
1654 struct tt_local_entry *tt_local_entry;
1655
1656 if (!hash)
1657 return;
1658
1659 for (i = 0; i < hash->size; i++) {
1660 head = &hash->table[i];
1661
1662 rcu_read_lock();
1663 hlist_for_each_entry_rcu(tt_local_entry, node,
1664 head, hash_entry) {
1665 tt_local_entry->flags &= ~flags;
1666 atomic_inc(&bat_priv->num_local_tt);
1667 }
1668 rcu_read_unlock();
1669 }
1670
1671}
1672
1673/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1674static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1675{
1676 struct hashtable_t *hash = bat_priv->tt_local_hash;
1677 struct tt_local_entry *tt_local_entry;
1678 struct hlist_node *node, *node_tmp;
1679 struct hlist_head *head;
1680 spinlock_t *list_lock; /* protects write access to the hash lists */
1681 int i;
1682
1683 if (!hash)
1684 return;
1685
1686 for (i = 0; i < hash->size; i++) {
1687 head = &hash->table[i];
1688 list_lock = &hash->list_locks[i];
1689
1690 spin_lock_bh(list_lock);
1691 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
1692 head, hash_entry) {
1693 if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
1694 continue;
1695
1696 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
1697 "(%pM): pending\n", tt_local_entry->addr);
1698
1699 atomic_dec(&bat_priv->num_local_tt);
1700 hlist_del_rcu(node);
1701 tt_local_entry_free_ref(tt_local_entry);
1702 }
1703 spin_unlock_bh(list_lock);
1704 }
1705
1706}
1707
1708void tt_commit_changes(struct bat_priv *bat_priv)
1709{
1710 tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
1711 tt_local_purge_pending_clients(bat_priv);
1712
1713 /* Increment the TTVN only once per OGM interval */
1714 atomic_inc(&bat_priv->ttvn);
1715 bat_priv->tt_poss_change = false;
1716}