blob: 06d361d4ac4b4d93cec09be668236c46e4b6df17 [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 Quartulli2dafb492011-05-05 08:42:45 +0200218 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
219 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200220
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221 atomic_inc(&bat_priv->num_local_tt);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
223 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200224 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
Antonio Quartullicc47f662011-04-27 14:27:57 +0200226 /* Check whether it is a roaming! */
227 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200228 /* This node is probably going to update its tt table */
229 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200230 _tt_global_del(bat_priv, tt_global_entry,
231 "local tt received");
Antonio Quartullicc47f662011-04-27 14:27:57 +0200232 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200233 tt_global_entry->orig_node);
234 }
235out:
236 if (tt_local_entry)
237 tt_local_entry_free_ref(tt_local_entry);
238 if (tt_global_entry)
239 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240}
241
Antonio Quartullia73105b2011-04-27 14:27:44 +0200242int tt_changes_fill_buffer(struct bat_priv *bat_priv,
243 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200245 int count = 0, tot_changes = 0;
246 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Antonio Quartullia73105b2011-04-27 14:27:44 +0200248 if (buff_len > 0)
249 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Antonio Quartullia73105b2011-04-27 14:27:44 +0200251 spin_lock_bh(&bat_priv->tt_changes_list_lock);
252 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartullia73105b2011-04-27 14:27:44 +0200254 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
255 list) {
256 if (count < tot_changes) {
257 memcpy(buff + tt_len(count),
258 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 count++;
260 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200261 list_del(&entry->list);
262 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
Antonio Quartullia73105b2011-04-27 14:27:44 +0200266 /* Keep the buffer for possible tt_request */
267 spin_lock_bh(&bat_priv->tt_buff_lock);
268 kfree(bat_priv->tt_buff);
269 bat_priv->tt_buff_len = 0;
270 bat_priv->tt_buff = NULL;
271 /* We check whether this new OGM has no changes due to size
272 * problems */
273 if (buff_len > 0) {
274 /**
275 * if kmalloc() fails we will reply with the full table
276 * instead of providing the diff
277 */
278 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
279 if (bat_priv->tt_buff) {
280 memcpy(bat_priv->tt_buff, buff, buff_len);
281 bat_priv->tt_buff_len = buff_len;
282 }
283 }
284 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Antonio Quartullia73105b2011-04-27 14:27:44 +0200286 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287}
288
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200289int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290{
291 struct net_device *net_dev = (struct net_device *)seq->private;
292 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200293 struct hashtable_t *hash = bat_priv->tt_local_hash;
294 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200295 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000296 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 size_t buf_size, pos;
299 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200300 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301
Marek Lindner32ae9b22011-04-20 15:40:58 +0200302 primary_if = primary_if_get_selected(bat_priv);
303 if (!primary_if) {
304 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
305 "please specify interfaces to enable it\n",
306 net_dev->name);
307 goto out;
308 }
309
310 if (primary_if->if_status != IF_ACTIVE) {
311 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
312 "primary interface not active\n",
313 net_dev->name);
314 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 }
316
317 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200318 "announced via TT (TTVN: %u):\n",
319 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 buf_size = 1;
322 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
323 for (i = 0; i < hash->size; i++) {
324 head = &hash->table[i];
325
Marek Lindner7aadf882011-02-18 12:28:09 +0000326 rcu_read_lock();
327 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000329 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 }
331
332 buff = kmalloc(buf_size, GFP_ATOMIC);
333 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200334 ret = -ENOMEM;
335 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000337
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 buff[0] = '\0';
339 pos = 0;
340
341 for (i = 0; i < hash->size; i++) {
342 head = &hash->table[i];
343
Marek Lindner7aadf882011-02-18 12:28:09 +0000344 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200345 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000346 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200348 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000350 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 }
352
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 seq_printf(seq, "%s", buff);
354 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200355out:
356 if (primary_if)
357 hardif_free_ref(primary_if);
358 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359}
360
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200361static void tt_local_del(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200362 struct tt_local_entry *tt_local_entry,
363 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200365 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry (%pM): %s\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200366 tt_local_entry->addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Antonio Quartullia73105b2011-04-27 14:27:44 +0200368 atomic_dec(&bat_priv->num_local_tt);
369
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200370 hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
371 tt_local_entry->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200372
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200373 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374}
375
Antonio Quartullia73105b2011-04-27 14:27:44 +0200376void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200377 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200379 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200381 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200383 if (!tt_local_entry)
384 goto out;
385
Antonio Quartulliff66c972011-06-30 01:14:00 +0200386 tt_local_event(bat_priv, tt_local_entry->addr,
387 tt_local_entry->flags | TT_CLIENT_DEL |
388 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200389 tt_local_del(bat_priv, tt_local_entry, message);
390out:
391 if (tt_local_entry)
392 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393}
394
Antonio Quartullia73105b2011-04-27 14:27:44 +0200395static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200397 struct hashtable_t *hash = bat_priv->tt_local_hash;
398 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000399 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200401 spinlock_t *list_lock; /* protects write access to the hash lists */
Marek Lindner7aadf882011-02-18 12:28:09 +0000402 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404 for (i = 0; i < hash->size; i++) {
405 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200406 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200408 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200409 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000410 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200411 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000412 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Antonio Quartullia73105b2011-04-27 14:27:44 +0200414 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200415 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000416 continue;
417
Antonio Quartulliff66c972011-06-30 01:14:00 +0200418 tt_local_event(bat_priv, tt_local_entry->addr,
419 tt_local_entry->flags | TT_CLIENT_DEL);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200420 atomic_dec(&bat_priv->num_local_tt);
421 bat_dbg(DBG_TT, bat_priv, "Deleting local "
422 "tt entry (%pM): timed out\n",
423 tt_local_entry->addr);
424 hlist_del_rcu(node);
425 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200427 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 }
429
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430}
431
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200434 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200435 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200437 struct hlist_node *node, *node_tmp;
438 struct hlist_head *head;
439 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200441 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 return;
443
Antonio Quartullia73105b2011-04-27 14:27:44 +0200444 hash = bat_priv->tt_local_hash;
445
446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448 list_lock = &hash->list_locks[i];
449
450 spin_lock_bh(list_lock);
451 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
452 head, hash_entry) {
453 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200454 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200455 }
456 spin_unlock_bh(list_lock);
457 }
458
459 hash_destroy(hash);
460
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200461 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462}
463
Antonio Quartullia73105b2011-04-27 14:27:44 +0200464static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200466 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 return 1;
468
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200469 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200471 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472 return 0;
473
474 return 1;
475}
476
Antonio Quartullia73105b2011-04-27 14:27:44 +0200477static void tt_changes_list_free(struct bat_priv *bat_priv)
478{
479 struct tt_change_node *entry, *safe;
480
481 spin_lock_bh(&bat_priv->tt_changes_list_lock);
482
483 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
484 list) {
485 list_del(&entry->list);
486 kfree(entry);
487 }
488
489 atomic_set(&bat_priv->tt_local_changes, 0);
490 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
491}
492
493/* caller must hold orig_node refcount */
494int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200495 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200497 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200498 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200499 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500
Antonio Quartullia73105b2011-04-27 14:27:44 +0200501 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Antonio Quartullia73105b2011-04-27 14:27:44 +0200503 if (!tt_global_entry) {
504 tt_global_entry =
505 kmalloc(sizeof(*tt_global_entry),
506 GFP_ATOMIC);
507 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200508 goto out;
509
Antonio Quartullia73105b2011-04-27 14:27:44 +0200510 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
511 /* Assign the new orig_node */
512 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200513 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200514 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200515 tt_global_entry->flags = NO_FLAGS;
516 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200517 atomic_set(&tt_global_entry->refcount, 2);
518
Antonio Quartullia73105b2011-04-27 14:27:44 +0200519 hash_add(bat_priv->tt_global_hash, compare_gtt,
520 choose_orig, tt_global_entry,
521 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200522 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200523 } else {
524 if (tt_global_entry->orig_node != orig_node) {
525 atomic_dec(&tt_global_entry->orig_node->tt_size);
526 orig_node_tmp = tt_global_entry->orig_node;
527 atomic_inc(&orig_node->refcount);
528 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200529 orig_node_free_ref(orig_node_tmp);
530 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200532 tt_global_entry->ttvn = ttvn;
533 tt_global_entry->flags = NO_FLAGS;
534 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200536
Antonio Quartullia73105b2011-04-27 14:27:44 +0200537 bat_dbg(DBG_TT, bat_priv,
538 "Creating new global tt entry: %pM (via %pM)\n",
539 tt_global_entry->addr, orig_node->orig);
540
541 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200542 tt_local_remove(bat_priv, tt_global_entry->addr,
543 "global tt received", roaming);
544 ret = 1;
545out:
546 if (tt_global_entry)
547 tt_global_entry_free_ref(tt_global_entry);
548 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549}
550
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200551int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552{
553 struct net_device *net_dev = (struct net_device *)seq->private;
554 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200555 struct hashtable_t *hash = bat_priv->tt_global_hash;
556 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200557 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000558 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 size_t buf_size, pos;
561 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200562 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563
Marek Lindner32ae9b22011-04-20 15:40:58 +0200564 primary_if = primary_if_get_selected(bat_priv);
565 if (!primary_if) {
566 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
567 "specify interfaces to enable it\n",
568 net_dev->name);
569 goto out;
570 }
571
572 if (primary_if->if_status != IF_ACTIVE) {
573 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
574 "primary interface not active\n",
575 net_dev->name);
576 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577 }
578
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200579 seq_printf(seq,
580 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200582 seq_printf(seq, " %-13s %s %-15s %s\n",
583 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200586 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
587 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 for (i = 0; i < hash->size; i++) {
589 head = &hash->table[i];
590
Marek Lindner7aadf882011-02-18 12:28:09 +0000591 rcu_read_lock();
592 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200593 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000594 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 }
596
597 buff = kmalloc(buf_size, GFP_ATOMIC);
598 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200599 ret = -ENOMEM;
600 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200602
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603 buff[0] = '\0';
604 pos = 0;
605
606 for (i = 0; i < hash->size; i++) {
607 head = &hash->table[i];
608
Marek Lindner7aadf882011-02-18 12:28:09 +0000609 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200610 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000611 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200612 pos += snprintf(buff + pos, 61,
613 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200614 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200615 tt_global_entry->ttvn,
616 tt_global_entry->orig_node->orig,
617 (uint8_t) atomic_read(
618 &tt_global_entry->orig_node->
619 last_ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000621 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 }
623
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 seq_printf(seq, "%s", buff);
625 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200626out:
627 if (primary_if)
628 hardif_free_ref(primary_if);
629 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630}
631
Antonio Quartullia73105b2011-04-27 14:27:44 +0200632static void _tt_global_del(struct bat_priv *bat_priv,
633 struct tt_global_entry *tt_global_entry,
634 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200636 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200637 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638
639 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200640 "Deleting global tt entry %pM (via %pM): %s\n",
641 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642 message);
643
Antonio Quartullia73105b2011-04-27 14:27:44 +0200644 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200645
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200646 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
647 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648out:
649 if (tt_global_entry)
650 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651}
652
Antonio Quartullia73105b2011-04-27 14:27:44 +0200653void tt_global_del(struct bat_priv *bat_priv,
654 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200655 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200657 struct tt_global_entry *tt_global_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) {
665 tt_global_entry->flags |= TT_CLIENT_ROAM;
666 tt_global_entry->roam_at = jiffies;
667 goto out;
668 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200669 _tt_global_del(bat_priv, tt_global_entry, message);
670 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200671out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200672 if (tt_global_entry)
673 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200674}
675
676void tt_global_del_orig(struct bat_priv *bat_priv,
677 struct orig_node *orig_node, const char *message)
678{
679 struct tt_global_entry *tt_global_entry;
680 int i;
681 struct hashtable_t *hash = bat_priv->tt_global_hash;
682 struct hlist_node *node, *safe;
683 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200684 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200685
Antonio Quartullia73105b2011-04-27 14:27:44 +0200686 for (i = 0; i < hash->size; i++) {
687 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200688 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200690 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200691 hlist_for_each_entry_safe(tt_global_entry, node, safe,
692 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200693 if (tt_global_entry->orig_node == orig_node) {
694 bat_dbg(DBG_TT, bat_priv,
695 "Deleting global tt entry %pM "
696 "(via %pM): originator time out\n",
697 tt_global_entry->addr,
698 tt_global_entry->orig_node->orig);
699 hlist_del_rcu(node);
700 tt_global_entry_free_ref(tt_global_entry);
701 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200702 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200703 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706}
707
Antonio Quartullicc47f662011-04-27 14:27:57 +0200708static void tt_global_roam_purge(struct bat_priv *bat_priv)
709{
710 struct hashtable_t *hash = bat_priv->tt_global_hash;
711 struct tt_global_entry *tt_global_entry;
712 struct hlist_node *node, *node_tmp;
713 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200715 int i;
716
Antonio Quartullicc47f662011-04-27 14:27:57 +0200717 for (i = 0; i < hash->size; i++) {
718 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200719 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200720
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200721 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200722 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
723 head, hash_entry) {
724 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
725 continue;
726 if (!is_out_of_time(tt_global_entry->roam_at,
727 TT_CLIENT_ROAM_TIMEOUT * 1000))
728 continue;
729
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200730 bat_dbg(DBG_TT, bat_priv, "Deleting global "
731 "tt entry (%pM): Roaming timeout\n",
732 tt_global_entry->addr);
733 atomic_dec(&tt_global_entry->orig_node->tt_size);
734 hlist_del_rcu(node);
735 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200736 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200737 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200738 }
739
Antonio Quartullicc47f662011-04-27 14:27:57 +0200740}
741
Antonio Quartullia73105b2011-04-27 14:27:44 +0200742static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200744 struct hashtable_t *hash;
745 spinlock_t *list_lock; /* protects write access to the hash lists */
746 struct tt_global_entry *tt_global_entry;
747 struct hlist_node *node, *node_tmp;
748 struct hlist_head *head;
749 int i;
750
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200751 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 return;
753
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200754 hash = bat_priv->tt_global_hash;
755
756 for (i = 0; i < hash->size; i++) {
757 head = &hash->table[i];
758 list_lock = &hash->list_locks[i];
759
760 spin_lock_bh(list_lock);
761 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
762 head, hash_entry) {
763 hlist_del_rcu(node);
764 tt_global_entry_free_ref(tt_global_entry);
765 }
766 spin_unlock_bh(list_lock);
767 }
768
769 hash_destroy(hash);
770
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200771 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772}
773
Sven Eckelmann747e4222011-05-14 23:14:50 +0200774struct orig_node *transtable_search(struct bat_priv *bat_priv,
775 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200777 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000778 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200780 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000781
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200782 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000783 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200785 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200786 goto free_tt;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000787
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200788 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000789
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200790free_tt:
791 tt_global_entry_free_ref(tt_global_entry);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000792out:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000793 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200795
796/* Calculates the checksum of the local table of a given orig_node */
797uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
798{
799 uint16_t total = 0, total_one;
800 struct hashtable_t *hash = bat_priv->tt_global_hash;
801 struct tt_global_entry *tt_global_entry;
802 struct hlist_node *node;
803 struct hlist_head *head;
804 int i, j;
805
806 for (i = 0; i < hash->size; i++) {
807 head = &hash->table[i];
808
809 rcu_read_lock();
810 hlist_for_each_entry_rcu(tt_global_entry, node,
811 head, hash_entry) {
812 if (compare_eth(tt_global_entry->orig_node,
813 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200814 /* Roaming clients are in the global table for
815 * consistency only. They don't have to be
816 * taken into account while computing the
817 * global crc */
818 if (tt_global_entry->flags & TT_CLIENT_ROAM)
819 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200820 total_one = 0;
821 for (j = 0; j < ETH_ALEN; j++)
822 total_one = crc16_byte(total_one,
823 tt_global_entry->addr[j]);
824 total ^= total_one;
825 }
826 }
827 rcu_read_unlock();
828 }
829
830 return total;
831}
832
833/* Calculates the checksum of the local table */
834uint16_t tt_local_crc(struct bat_priv *bat_priv)
835{
836 uint16_t total = 0, total_one;
837 struct hashtable_t *hash = bat_priv->tt_local_hash;
838 struct tt_local_entry *tt_local_entry;
839 struct hlist_node *node;
840 struct hlist_head *head;
841 int i, j;
842
843 for (i = 0; i < hash->size; i++) {
844 head = &hash->table[i];
845
846 rcu_read_lock();
847 hlist_for_each_entry_rcu(tt_local_entry, node,
848 head, hash_entry) {
849 total_one = 0;
850 for (j = 0; j < ETH_ALEN; j++)
851 total_one = crc16_byte(total_one,
852 tt_local_entry->addr[j]);
853 total ^= total_one;
854 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200855 rcu_read_unlock();
856 }
857
858 return total;
859}
860
861static void tt_req_list_free(struct bat_priv *bat_priv)
862{
863 struct tt_req_node *node, *safe;
864
865 spin_lock_bh(&bat_priv->tt_req_list_lock);
866
867 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
868 list_del(&node->list);
869 kfree(node);
870 }
871
872 spin_unlock_bh(&bat_priv->tt_req_list_lock);
873}
874
875void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
876 const unsigned char *tt_buff, uint8_t tt_num_changes)
877{
878 uint16_t tt_buff_len = tt_len(tt_num_changes);
879
880 /* Replace the old buffer only if I received something in the
881 * last OGM (the OGM could carry no changes) */
882 spin_lock_bh(&orig_node->tt_buff_lock);
883 if (tt_buff_len > 0) {
884 kfree(orig_node->tt_buff);
885 orig_node->tt_buff_len = 0;
886 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
887 if (orig_node->tt_buff) {
888 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
889 orig_node->tt_buff_len = tt_buff_len;
890 }
891 }
892 spin_unlock_bh(&orig_node->tt_buff_lock);
893}
894
895static void tt_req_purge(struct bat_priv *bat_priv)
896{
897 struct tt_req_node *node, *safe;
898
899 spin_lock_bh(&bat_priv->tt_req_list_lock);
900 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
901 if (is_out_of_time(node->issued_at,
902 TT_REQUEST_TIMEOUT * 1000)) {
903 list_del(&node->list);
904 kfree(node);
905 }
906 }
907 spin_unlock_bh(&bat_priv->tt_req_list_lock);
908}
909
910/* returns the pointer to the new tt_req_node struct if no request
911 * has already been issued for this orig_node, NULL otherwise */
912static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
913 struct orig_node *orig_node)
914{
915 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
916
917 spin_lock_bh(&bat_priv->tt_req_list_lock);
918 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
919 if (compare_eth(tt_req_node_tmp, orig_node) &&
920 !is_out_of_time(tt_req_node_tmp->issued_at,
921 TT_REQUEST_TIMEOUT * 1000))
922 goto unlock;
923 }
924
925 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
926 if (!tt_req_node)
927 goto unlock;
928
929 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
930 tt_req_node->issued_at = jiffies;
931
932 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
933unlock:
934 spin_unlock_bh(&bat_priv->tt_req_list_lock);
935 return tt_req_node;
936}
937
938static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
939{
940 const struct tt_global_entry *tt_global_entry = entry_ptr;
941 const struct orig_node *orig_node = data_ptr;
942
Antonio Quartullicc47f662011-04-27 14:27:57 +0200943 if (tt_global_entry->flags & TT_CLIENT_ROAM)
944 return 0;
945
Antonio Quartullia73105b2011-04-27 14:27:44 +0200946 return (tt_global_entry->orig_node == orig_node);
947}
948
949static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
950 struct hashtable_t *hash,
951 struct hard_iface *primary_if,
952 int (*valid_cb)(const void *,
953 const void *),
954 void *cb_data)
955{
956 struct tt_local_entry *tt_local_entry;
957 struct tt_query_packet *tt_response;
958 struct tt_change *tt_change;
959 struct hlist_node *node;
960 struct hlist_head *head;
961 struct sk_buff *skb = NULL;
962 uint16_t tt_tot, tt_count;
963 ssize_t tt_query_size = sizeof(struct tt_query_packet);
964 int i;
965
966 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
967 tt_len = primary_if->soft_iface->mtu - tt_query_size;
968 tt_len -= tt_len % sizeof(struct tt_change);
969 }
970 tt_tot = tt_len / sizeof(struct tt_change);
971
972 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
973 if (!skb)
974 goto out;
975
976 skb_reserve(skb, ETH_HLEN);
977 tt_response = (struct tt_query_packet *)skb_put(skb,
978 tt_query_size + tt_len);
979 tt_response->ttvn = ttvn;
980 tt_response->tt_data = htons(tt_tot);
981
982 tt_change = (struct tt_change *)(skb->data + tt_query_size);
983 tt_count = 0;
984
985 rcu_read_lock();
986 for (i = 0; i < hash->size; i++) {
987 head = &hash->table[i];
988
989 hlist_for_each_entry_rcu(tt_local_entry, node,
990 head, hash_entry) {
991 if (tt_count == tt_tot)
992 break;
993
994 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
995 continue;
996
997 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
998 tt_change->flags = NO_FLAGS;
999
1000 tt_count++;
1001 tt_change++;
1002 }
1003 }
1004 rcu_read_unlock();
1005
1006out:
1007 return skb;
1008}
1009
1010int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1011 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1012{
1013 struct sk_buff *skb = NULL;
1014 struct tt_query_packet *tt_request;
1015 struct neigh_node *neigh_node = NULL;
1016 struct hard_iface *primary_if;
1017 struct tt_req_node *tt_req_node = NULL;
1018 int ret = 1;
1019
1020 primary_if = primary_if_get_selected(bat_priv);
1021 if (!primary_if)
1022 goto out;
1023
1024 /* The new tt_req will be issued only if I'm not waiting for a
1025 * reply from the same orig_node yet */
1026 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1027 if (!tt_req_node)
1028 goto out;
1029
1030 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1031 if (!skb)
1032 goto out;
1033
1034 skb_reserve(skb, ETH_HLEN);
1035
1036 tt_request = (struct tt_query_packet *)skb_put(skb,
1037 sizeof(struct tt_query_packet));
1038
1039 tt_request->packet_type = BAT_TT_QUERY;
1040 tt_request->version = COMPAT_VERSION;
1041 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1042 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1043 tt_request->ttl = TTL;
1044 tt_request->ttvn = ttvn;
1045 tt_request->tt_data = tt_crc;
1046 tt_request->flags = TT_REQUEST;
1047
1048 if (full_table)
1049 tt_request->flags |= TT_FULL_TABLE;
1050
1051 neigh_node = orig_node_get_router(dst_orig_node);
1052 if (!neigh_node)
1053 goto out;
1054
1055 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1056 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1057 (full_table ? 'F' : '.'));
1058
1059 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1060 ret = 0;
1061
1062out:
1063 if (neigh_node)
1064 neigh_node_free_ref(neigh_node);
1065 if (primary_if)
1066 hardif_free_ref(primary_if);
1067 if (ret)
1068 kfree_skb(skb);
1069 if (ret && tt_req_node) {
1070 spin_lock_bh(&bat_priv->tt_req_list_lock);
1071 list_del(&tt_req_node->list);
1072 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1073 kfree(tt_req_node);
1074 }
1075 return ret;
1076}
1077
1078static bool send_other_tt_response(struct bat_priv *bat_priv,
1079 struct tt_query_packet *tt_request)
1080{
1081 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1082 struct neigh_node *neigh_node = NULL;
1083 struct hard_iface *primary_if = NULL;
1084 uint8_t orig_ttvn, req_ttvn, ttvn;
1085 int ret = false;
1086 unsigned char *tt_buff;
1087 bool full_table;
1088 uint16_t tt_len, tt_tot;
1089 struct sk_buff *skb = NULL;
1090 struct tt_query_packet *tt_response;
1091
1092 bat_dbg(DBG_TT, bat_priv,
1093 "Received TT_REQUEST from %pM for "
1094 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1095 tt_request->ttvn, tt_request->dst,
1096 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1097
1098 /* Let's get the orig node of the REAL destination */
1099 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1100 if (!req_dst_orig_node)
1101 goto out;
1102
1103 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1104 if (!res_dst_orig_node)
1105 goto out;
1106
1107 neigh_node = orig_node_get_router(res_dst_orig_node);
1108 if (!neigh_node)
1109 goto out;
1110
1111 primary_if = primary_if_get_selected(bat_priv);
1112 if (!primary_if)
1113 goto out;
1114
1115 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1116 req_ttvn = tt_request->ttvn;
1117
1118 /* I have not the requested data */
1119 if (orig_ttvn != req_ttvn ||
1120 tt_request->tt_data != req_dst_orig_node->tt_crc)
1121 goto out;
1122
1123 /* If it has explicitly been requested the full table */
1124 if (tt_request->flags & TT_FULL_TABLE ||
1125 !req_dst_orig_node->tt_buff)
1126 full_table = true;
1127 else
1128 full_table = false;
1129
1130 /* In this version, fragmentation is not implemented, then
1131 * I'll send only one packet with as much TT entries as I can */
1132 if (!full_table) {
1133 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1134 tt_len = req_dst_orig_node->tt_buff_len;
1135 tt_tot = tt_len / sizeof(struct tt_change);
1136
1137 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1138 tt_len + ETH_HLEN);
1139 if (!skb)
1140 goto unlock;
1141
1142 skb_reserve(skb, ETH_HLEN);
1143 tt_response = (struct tt_query_packet *)skb_put(skb,
1144 sizeof(struct tt_query_packet) + tt_len);
1145 tt_response->ttvn = req_ttvn;
1146 tt_response->tt_data = htons(tt_tot);
1147
1148 tt_buff = skb->data + sizeof(struct tt_query_packet);
1149 /* Copy the last orig_node's OGM buffer */
1150 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1151 req_dst_orig_node->tt_buff_len);
1152
1153 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1154 } else {
1155 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1156 sizeof(struct tt_change);
1157 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1158
1159 skb = tt_response_fill_table(tt_len, ttvn,
1160 bat_priv->tt_global_hash,
1161 primary_if, tt_global_valid_entry,
1162 req_dst_orig_node);
1163 if (!skb)
1164 goto out;
1165
1166 tt_response = (struct tt_query_packet *)skb->data;
1167 }
1168
1169 tt_response->packet_type = BAT_TT_QUERY;
1170 tt_response->version = COMPAT_VERSION;
1171 tt_response->ttl = TTL;
1172 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1173 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1174 tt_response->flags = TT_RESPONSE;
1175
1176 if (full_table)
1177 tt_response->flags |= TT_FULL_TABLE;
1178
1179 bat_dbg(DBG_TT, bat_priv,
1180 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1181 res_dst_orig_node->orig, neigh_node->addr,
1182 req_dst_orig_node->orig, req_ttvn);
1183
1184 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1185 ret = true;
1186 goto out;
1187
1188unlock:
1189 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1190
1191out:
1192 if (res_dst_orig_node)
1193 orig_node_free_ref(res_dst_orig_node);
1194 if (req_dst_orig_node)
1195 orig_node_free_ref(req_dst_orig_node);
1196 if (neigh_node)
1197 neigh_node_free_ref(neigh_node);
1198 if (primary_if)
1199 hardif_free_ref(primary_if);
1200 if (!ret)
1201 kfree_skb(skb);
1202 return ret;
1203
1204}
1205static bool send_my_tt_response(struct bat_priv *bat_priv,
1206 struct tt_query_packet *tt_request)
1207{
1208 struct orig_node *orig_node = NULL;
1209 struct neigh_node *neigh_node = NULL;
1210 struct hard_iface *primary_if = NULL;
1211 uint8_t my_ttvn, req_ttvn, ttvn;
1212 int ret = false;
1213 unsigned char *tt_buff;
1214 bool full_table;
1215 uint16_t tt_len, tt_tot;
1216 struct sk_buff *skb = NULL;
1217 struct tt_query_packet *tt_response;
1218
1219 bat_dbg(DBG_TT, bat_priv,
1220 "Received TT_REQUEST from %pM for "
1221 "ttvn: %u (me) [%c]\n", tt_request->src,
1222 tt_request->ttvn,
1223 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1224
1225
1226 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1227 req_ttvn = tt_request->ttvn;
1228
1229 orig_node = get_orig_node(bat_priv, tt_request->src);
1230 if (!orig_node)
1231 goto out;
1232
1233 neigh_node = orig_node_get_router(orig_node);
1234 if (!neigh_node)
1235 goto out;
1236
1237 primary_if = primary_if_get_selected(bat_priv);
1238 if (!primary_if)
1239 goto out;
1240
1241 /* If the full table has been explicitly requested or the gap
1242 * is too big send the whole local translation table */
1243 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1244 !bat_priv->tt_buff)
1245 full_table = true;
1246 else
1247 full_table = false;
1248
1249 /* In this version, fragmentation is not implemented, then
1250 * I'll send only one packet with as much TT entries as I can */
1251 if (!full_table) {
1252 spin_lock_bh(&bat_priv->tt_buff_lock);
1253 tt_len = bat_priv->tt_buff_len;
1254 tt_tot = tt_len / sizeof(struct tt_change);
1255
1256 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1257 tt_len + ETH_HLEN);
1258 if (!skb)
1259 goto unlock;
1260
1261 skb_reserve(skb, ETH_HLEN);
1262 tt_response = (struct tt_query_packet *)skb_put(skb,
1263 sizeof(struct tt_query_packet) + tt_len);
1264 tt_response->ttvn = req_ttvn;
1265 tt_response->tt_data = htons(tt_tot);
1266
1267 tt_buff = skb->data + sizeof(struct tt_query_packet);
1268 memcpy(tt_buff, bat_priv->tt_buff,
1269 bat_priv->tt_buff_len);
1270 spin_unlock_bh(&bat_priv->tt_buff_lock);
1271 } else {
1272 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1273 sizeof(struct tt_change);
1274 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1275
1276 skb = tt_response_fill_table(tt_len, ttvn,
1277 bat_priv->tt_local_hash,
1278 primary_if, NULL, NULL);
1279 if (!skb)
1280 goto out;
1281
1282 tt_response = (struct tt_query_packet *)skb->data;
1283 }
1284
1285 tt_response->packet_type = BAT_TT_QUERY;
1286 tt_response->version = COMPAT_VERSION;
1287 tt_response->ttl = TTL;
1288 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1289 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1290 tt_response->flags = TT_RESPONSE;
1291
1292 if (full_table)
1293 tt_response->flags |= TT_FULL_TABLE;
1294
1295 bat_dbg(DBG_TT, bat_priv,
1296 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1297 orig_node->orig, neigh_node->addr,
1298 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1299
1300 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1301 ret = true;
1302 goto out;
1303
1304unlock:
1305 spin_unlock_bh(&bat_priv->tt_buff_lock);
1306out:
1307 if (orig_node)
1308 orig_node_free_ref(orig_node);
1309 if (neigh_node)
1310 neigh_node_free_ref(neigh_node);
1311 if (primary_if)
1312 hardif_free_ref(primary_if);
1313 if (!ret)
1314 kfree_skb(skb);
1315 /* This packet was for me, so it doesn't need to be re-routed */
1316 return true;
1317}
1318
1319bool send_tt_response(struct bat_priv *bat_priv,
1320 struct tt_query_packet *tt_request)
1321{
1322 if (is_my_mac(tt_request->dst))
1323 return send_my_tt_response(bat_priv, tt_request);
1324 else
1325 return send_other_tt_response(bat_priv, tt_request);
1326}
1327
1328static void _tt_update_changes(struct bat_priv *bat_priv,
1329 struct orig_node *orig_node,
1330 struct tt_change *tt_change,
1331 uint16_t tt_num_changes, uint8_t ttvn)
1332{
1333 int i;
1334
1335 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001336 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001337 tt_global_del(bat_priv, orig_node,
1338 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001339 "tt removed by changes",
1340 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341 else
1342 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001343 (tt_change + i)->addr, ttvn, false))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001344 /* In case of problem while storing a
1345 * global_entry, we stop the updating
1346 * procedure without committing the
1347 * ttvn change. This will avoid to send
1348 * corrupted data on tt_request
1349 */
1350 return;
1351 }
1352}
1353
1354static void tt_fill_gtable(struct bat_priv *bat_priv,
1355 struct tt_query_packet *tt_response)
1356{
1357 struct orig_node *orig_node = NULL;
1358
1359 orig_node = orig_hash_find(bat_priv, tt_response->src);
1360 if (!orig_node)
1361 goto out;
1362
1363 /* Purge the old table first.. */
1364 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1365
1366 _tt_update_changes(bat_priv, orig_node,
1367 (struct tt_change *)(tt_response + 1),
1368 tt_response->tt_data, tt_response->ttvn);
1369
1370 spin_lock_bh(&orig_node->tt_buff_lock);
1371 kfree(orig_node->tt_buff);
1372 orig_node->tt_buff_len = 0;
1373 orig_node->tt_buff = NULL;
1374 spin_unlock_bh(&orig_node->tt_buff_lock);
1375
1376 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1377
1378out:
1379 if (orig_node)
1380 orig_node_free_ref(orig_node);
1381}
1382
1383void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1384 uint16_t tt_num_changes, uint8_t ttvn,
1385 struct tt_change *tt_change)
1386{
1387 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1388 ttvn);
1389
1390 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1391 tt_num_changes);
1392 atomic_set(&orig_node->last_ttvn, ttvn);
1393}
1394
1395bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1396{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001397 struct tt_local_entry *tt_local_entry = NULL;
1398 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001401 if (!tt_local_entry)
1402 goto out;
1403 ret = true;
1404out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001405 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001406 tt_local_entry_free_ref(tt_local_entry);
1407 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001408}
1409
1410void handle_tt_response(struct bat_priv *bat_priv,
1411 struct tt_query_packet *tt_response)
1412{
1413 struct tt_req_node *node, *safe;
1414 struct orig_node *orig_node = NULL;
1415
1416 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1417 "ttvn %d t_size: %d [%c]\n",
1418 tt_response->src, tt_response->ttvn,
1419 tt_response->tt_data,
1420 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1421
1422 orig_node = orig_hash_find(bat_priv, tt_response->src);
1423 if (!orig_node)
1424 goto out;
1425
1426 if (tt_response->flags & TT_FULL_TABLE)
1427 tt_fill_gtable(bat_priv, tt_response);
1428 else
1429 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1430 tt_response->ttvn,
1431 (struct tt_change *)(tt_response + 1));
1432
1433 /* Delete the tt_req_node from pending tt_requests list */
1434 spin_lock_bh(&bat_priv->tt_req_list_lock);
1435 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1436 if (!compare_eth(node->addr, tt_response->src))
1437 continue;
1438 list_del(&node->list);
1439 kfree(node);
1440 }
1441 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1442
1443 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001444 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001445 /* Roaming phase is over: tables are in sync again. I can
1446 * unset the flag */
1447 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448out:
1449 if (orig_node)
1450 orig_node_free_ref(orig_node);
1451}
1452
1453int tt_init(struct bat_priv *bat_priv)
1454{
1455 if (!tt_local_init(bat_priv))
1456 return 0;
1457
1458 if (!tt_global_init(bat_priv))
1459 return 0;
1460
1461 tt_start_timer(bat_priv);
1462
1463 return 1;
1464}
1465
Antonio Quartullicc47f662011-04-27 14:27:57 +02001466static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001468 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001469
Antonio Quartullicc47f662011-04-27 14:27:57 +02001470 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471
Antonio Quartullicc47f662011-04-27 14:27:57 +02001472 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1473 list_del(&node->list);
1474 kfree(node);
1475 }
1476
1477 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1478}
1479
1480static void tt_roam_purge(struct bat_priv *bat_priv)
1481{
1482 struct tt_roam_node *node, *safe;
1483
1484 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1485 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1486 if (!is_out_of_time(node->first_time,
1487 ROAMING_MAX_TIME * 1000))
1488 continue;
1489
1490 list_del(&node->list);
1491 kfree(node);
1492 }
1493 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1494}
1495
1496/* This function checks whether the client already reached the
1497 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1498 * will not be sent.
1499 *
1500 * returns true if the ROAMING_ADV can be sent, false otherwise */
1501static bool tt_check_roam_count(struct bat_priv *bat_priv,
1502 uint8_t *client)
1503{
1504 struct tt_roam_node *tt_roam_node;
1505 bool ret = false;
1506
1507 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1508 /* The new tt_req will be issued only if I'm not waiting for a
1509 * reply from the same orig_node yet */
1510 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1511 if (!compare_eth(tt_roam_node->addr, client))
1512 continue;
1513
1514 if (is_out_of_time(tt_roam_node->first_time,
1515 ROAMING_MAX_TIME * 1000))
1516 continue;
1517
1518 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1519 /* Sorry, you roamed too many times! */
1520 goto unlock;
1521 ret = true;
1522 break;
1523 }
1524
1525 if (!ret) {
1526 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1527 if (!tt_roam_node)
1528 goto unlock;
1529
1530 tt_roam_node->first_time = jiffies;
1531 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1532 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1533
1534 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1535 ret = true;
1536 }
1537
1538unlock:
1539 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1540 return ret;
1541}
1542
1543void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1544 struct orig_node *orig_node)
1545{
1546 struct neigh_node *neigh_node = NULL;
1547 struct sk_buff *skb = NULL;
1548 struct roam_adv_packet *roam_adv_packet;
1549 int ret = 1;
1550 struct hard_iface *primary_if;
1551
1552 /* before going on we have to check whether the client has
1553 * already roamed to us too many times */
1554 if (!tt_check_roam_count(bat_priv, client))
1555 goto out;
1556
1557 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1558 if (!skb)
1559 goto out;
1560
1561 skb_reserve(skb, ETH_HLEN);
1562
1563 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1564 sizeof(struct roam_adv_packet));
1565
1566 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1567 roam_adv_packet->version = COMPAT_VERSION;
1568 roam_adv_packet->ttl = TTL;
1569 primary_if = primary_if_get_selected(bat_priv);
1570 if (!primary_if)
1571 goto out;
1572 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1573 hardif_free_ref(primary_if);
1574 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1575 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1576
1577 neigh_node = orig_node_get_router(orig_node);
1578 if (!neigh_node)
1579 goto out;
1580
1581 bat_dbg(DBG_TT, bat_priv,
1582 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1583 orig_node->orig, client, neigh_node->addr);
1584
1585 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1586 ret = 0;
1587
1588out:
1589 if (neigh_node)
1590 neigh_node_free_ref(neigh_node);
1591 if (ret)
1592 kfree_skb(skb);
1593 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001594}
1595
1596static void tt_purge(struct work_struct *work)
1597{
1598 struct delayed_work *delayed_work =
1599 container_of(work, struct delayed_work, work);
1600 struct bat_priv *bat_priv =
1601 container_of(delayed_work, struct bat_priv, tt_work);
1602
1603 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001604 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001605 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001606 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607
1608 tt_start_timer(bat_priv);
1609}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001610
1611void tt_free(struct bat_priv *bat_priv)
1612{
1613 cancel_delayed_work_sync(&bat_priv->tt_work);
1614
1615 tt_local_table_free(bat_priv);
1616 tt_global_table_free(bat_priv);
1617 tt_req_list_free(bat_priv);
1618 tt_changes_list_free(bat_priv);
1619 tt_roam_list_free(bat_priv);
1620
1621 kfree(bat_priv->tt_buff);
1622}