blob: 4208dc71257b728c4222f12ca773138ca6258b1d [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 Quartullia73105b2011-04-27 14:27:44 +0200146static void tt_local_event(struct bat_priv *bat_priv, uint8_t op,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200147 const uint8_t *addr, bool roaming)
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
156 tt_change_node->change.flags = op;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200157 if (roaming)
158 tt_change_node->change.flags |= TT_CLIENT_ROAM;
159
Antonio Quartullia73105b2011-04-27 14:27:44 +0200160 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
161
162 spin_lock_bh(&bat_priv->tt_changes_list_lock);
163 /* track the change in the OGMinterval list */
164 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
165 atomic_inc(&bat_priv->tt_local_changes);
166 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
167
168 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
169}
170
171int tt_len(int changes_num)
172{
173 return changes_num * sizeof(struct tt_change);
174}
175
176static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179 return 1;
180
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200181 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200183 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 return 0;
185
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186 return 1;
187}
188
Sven Eckelmann747e4222011-05-14 23:14:50 +0200189void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190{
191 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200192 struct tt_local_entry *tt_local_entry = NULL;
193 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200195 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200197 if (tt_local_entry) {
198 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 }
201
Sven Eckelmann704509b2011-05-14 23:14:54 +0200202 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200203 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200204 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200205
Antonio Quartullicc47f662011-04-27 14:27:57 +0200206 tt_local_event(bat_priv, NO_FLAGS, addr, false);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200207
208 bat_dbg(DBG_TT, bat_priv,
209 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
210 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200212 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
213 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200214 tt_local_entry->flags = NO_FLAGS;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200215 atomic_set(&tt_local_entry->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
217 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000218 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200219 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200221 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
222 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200223
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224 atomic_inc(&bat_priv->num_local_tt);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
226 /* 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 Quartulli2dafb492011-05-05 08:42:45 +0200364static void tt_local_del(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200365 struct tt_local_entry *tt_local_entry,
366 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200368 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry (%pM): %s\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200369 tt_local_entry->addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Antonio Quartullia73105b2011-04-27 14:27:44 +0200371 atomic_dec(&bat_priv->num_local_tt);
372
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200373 hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
374 tt_local_entry->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200375
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200376 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377}
378
Antonio Quartullia73105b2011-04-27 14:27:44 +0200379void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200380 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200382 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200384 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200386 if (!tt_local_entry)
387 goto out;
388
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200389 tt_local_event(bat_priv, TT_CLIENT_DEL, tt_local_entry->addr, roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200390 tt_local_del(bat_priv, tt_local_entry, message);
391out:
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 Quartullia73105b2011-04-27 14:27:44 +0200415 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200416 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000417 continue;
418
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200419 tt_local_event(bat_priv, TT_CLIENT_DEL,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200420 tt_local_entry->addr, false);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200421 atomic_dec(&bat_priv->num_local_tt);
422 bat_dbg(DBG_TT, bat_priv, "Deleting local "
423 "tt entry (%pM): timed out\n",
424 tt_local_entry->addr);
425 hlist_del_rcu(node);
426 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200428 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 }
430
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431}
432
Antonio Quartullia73105b2011-04-27 14:27:44 +0200433static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200435 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200438 struct hlist_node *node, *node_tmp;
439 struct hlist_head *head;
440 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200442 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443 return;
444
Antonio Quartullia73105b2011-04-27 14:27:44 +0200445 hash = bat_priv->tt_local_hash;
446
447 for (i = 0; i < hash->size; i++) {
448 head = &hash->table[i];
449 list_lock = &hash->list_locks[i];
450
451 spin_lock_bh(list_lock);
452 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
453 head, hash_entry) {
454 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200455 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 }
457 spin_unlock_bh(list_lock);
458 }
459
460 hash_destroy(hash);
461
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200462 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463}
464
Antonio Quartullia73105b2011-04-27 14:27:44 +0200465static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200467 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 return 1;
469
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200470 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200472 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 return 0;
474
475 return 1;
476}
477
Antonio Quartullia73105b2011-04-27 14:27:44 +0200478static void tt_changes_list_free(struct bat_priv *bat_priv)
479{
480 struct tt_change_node *entry, *safe;
481
482 spin_lock_bh(&bat_priv->tt_changes_list_lock);
483
484 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
485 list) {
486 list_del(&entry->list);
487 kfree(entry);
488 }
489
490 atomic_set(&bat_priv->tt_local_changes, 0);
491 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
492}
493
494/* caller must hold orig_node refcount */
495int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200496 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200498 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200499 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200500 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Antonio Quartullia73105b2011-04-27 14:27:44 +0200502 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Antonio Quartullia73105b2011-04-27 14:27:44 +0200504 if (!tt_global_entry) {
505 tt_global_entry =
506 kmalloc(sizeof(*tt_global_entry),
507 GFP_ATOMIC);
508 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200509 goto out;
510
Antonio Quartullia73105b2011-04-27 14:27:44 +0200511 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
512 /* Assign the new orig_node */
513 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200514 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200516 tt_global_entry->flags = NO_FLAGS;
517 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200518 atomic_set(&tt_global_entry->refcount, 2);
519
Antonio Quartullia73105b2011-04-27 14:27:44 +0200520 hash_add(bat_priv->tt_global_hash, compare_gtt,
521 choose_orig, tt_global_entry,
522 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200523 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200524 } else {
525 if (tt_global_entry->orig_node != orig_node) {
526 atomic_dec(&tt_global_entry->orig_node->tt_size);
527 orig_node_tmp = tt_global_entry->orig_node;
528 atomic_inc(&orig_node->refcount);
529 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200530 orig_node_free_ref(orig_node_tmp);
531 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200533 tt_global_entry->ttvn = ttvn;
534 tt_global_entry->flags = NO_FLAGS;
535 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200537
Antonio Quartullia73105b2011-04-27 14:27:44 +0200538 bat_dbg(DBG_TT, bat_priv,
539 "Creating new global tt entry: %pM (via %pM)\n",
540 tt_global_entry->addr, orig_node->orig);
541
542 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200543 tt_local_remove(bat_priv, tt_global_entry->addr,
544 "global tt received", roaming);
545 ret = 1;
546out:
547 if (tt_global_entry)
548 tt_global_entry_free_ref(tt_global_entry);
549 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550}
551
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200552int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553{
554 struct net_device *net_dev = (struct net_device *)seq->private;
555 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200556 struct hashtable_t *hash = bat_priv->tt_global_hash;
557 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200558 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000559 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 size_t buf_size, pos;
562 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200563 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
Marek Lindner32ae9b22011-04-20 15:40:58 +0200565 primary_if = primary_if_get_selected(bat_priv);
566 if (!primary_if) {
567 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
568 "specify interfaces to enable it\n",
569 net_dev->name);
570 goto out;
571 }
572
573 if (primary_if->if_status != IF_ACTIVE) {
574 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
575 "primary interface not active\n",
576 net_dev->name);
577 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578 }
579
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200580 seq_printf(seq,
581 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200583 seq_printf(seq, " %-13s %s %-15s %s\n",
584 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200587 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
588 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589 for (i = 0; i < hash->size; i++) {
590 head = &hash->table[i];
591
Marek Lindner7aadf882011-02-18 12:28:09 +0000592 rcu_read_lock();
593 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200594 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000595 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 }
597
598 buff = kmalloc(buf_size, GFP_ATOMIC);
599 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200600 ret = -ENOMEM;
601 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200603
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 buff[0] = '\0';
605 pos = 0;
606
607 for (i = 0; i < hash->size; i++) {
608 head = &hash->table[i];
609
Marek Lindner7aadf882011-02-18 12:28:09 +0000610 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200611 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000612 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200613 pos += snprintf(buff + pos, 61,
614 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200615 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200616 tt_global_entry->ttvn,
617 tt_global_entry->orig_node->orig,
618 (uint8_t) atomic_read(
619 &tt_global_entry->orig_node->
620 last_ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000622 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623 }
624
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 seq_printf(seq, "%s", buff);
626 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200627out:
628 if (primary_if)
629 hardif_free_ref(primary_if);
630 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631}
632
Antonio Quartullia73105b2011-04-27 14:27:44 +0200633static void _tt_global_del(struct bat_priv *bat_priv,
634 struct tt_global_entry *tt_global_entry,
635 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200637 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200639
640 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200641 "Deleting global tt entry %pM (via %pM): %s\n",
642 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 message);
644
Antonio Quartullia73105b2011-04-27 14:27:44 +0200645 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200646
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200647 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
648 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200649out:
650 if (tt_global_entry)
651 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652}
653
Antonio Quartullia73105b2011-04-27 14:27:44 +0200654void tt_global_del(struct bat_priv *bat_priv,
655 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200656 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200658 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659
Antonio Quartullia73105b2011-04-27 14:27:44 +0200660 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200661 if (!tt_global_entry)
662 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200663
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200664 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200665 if (roaming) {
666 tt_global_entry->flags |= TT_CLIENT_ROAM;
667 tt_global_entry->roam_at = jiffies;
668 goto out;
669 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200670 _tt_global_del(bat_priv, tt_global_entry, message);
671 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200672out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200673 if (tt_global_entry)
674 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200675}
676
677void tt_global_del_orig(struct bat_priv *bat_priv,
678 struct orig_node *orig_node, const char *message)
679{
680 struct tt_global_entry *tt_global_entry;
681 int i;
682 struct hashtable_t *hash = bat_priv->tt_global_hash;
683 struct hlist_node *node, *safe;
684 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200685 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200686
Antonio Quartullia73105b2011-04-27 14:27:44 +0200687 for (i = 0; i < hash->size; i++) {
688 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200689 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200691 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200692 hlist_for_each_entry_safe(tt_global_entry, node, safe,
693 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200694 if (tt_global_entry->orig_node == orig_node) {
695 bat_dbg(DBG_TT, bat_priv,
696 "Deleting global tt entry %pM "
697 "(via %pM): originator time out\n",
698 tt_global_entry->addr,
699 tt_global_entry->orig_node->orig);
700 hlist_del_rcu(node);
701 tt_global_entry_free_ref(tt_global_entry);
702 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200704 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000705 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200706 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707}
708
Antonio Quartullicc47f662011-04-27 14:27:57 +0200709static void tt_global_roam_purge(struct bat_priv *bat_priv)
710{
711 struct hashtable_t *hash = bat_priv->tt_global_hash;
712 struct tt_global_entry *tt_global_entry;
713 struct hlist_node *node, *node_tmp;
714 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200715 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200716 int i;
717
Antonio Quartullicc47f662011-04-27 14:27:57 +0200718 for (i = 0; i < hash->size; i++) {
719 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200720 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200721
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200722 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200723 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
724 head, hash_entry) {
725 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
726 continue;
727 if (!is_out_of_time(tt_global_entry->roam_at,
728 TT_CLIENT_ROAM_TIMEOUT * 1000))
729 continue;
730
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200731 bat_dbg(DBG_TT, bat_priv, "Deleting global "
732 "tt entry (%pM): Roaming timeout\n",
733 tt_global_entry->addr);
734 atomic_dec(&tt_global_entry->orig_node->tt_size);
735 hlist_del_rcu(node);
736 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200737 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200738 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200739 }
740
Antonio Quartullicc47f662011-04-27 14:27:57 +0200741}
742
Antonio Quartullia73105b2011-04-27 14:27:44 +0200743static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200745 struct hashtable_t *hash;
746 spinlock_t *list_lock; /* protects write access to the hash lists */
747 struct tt_global_entry *tt_global_entry;
748 struct hlist_node *node, *node_tmp;
749 struct hlist_head *head;
750 int i;
751
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200752 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753 return;
754
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200755 hash = bat_priv->tt_global_hash;
756
757 for (i = 0; i < hash->size; i++) {
758 head = &hash->table[i];
759 list_lock = &hash->list_locks[i];
760
761 spin_lock_bh(list_lock);
762 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
763 head, hash_entry) {
764 hlist_del_rcu(node);
765 tt_global_entry_free_ref(tt_global_entry);
766 }
767 spin_unlock_bh(list_lock);
768 }
769
770 hash_destroy(hash);
771
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200772 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773}
774
Sven Eckelmann747e4222011-05-14 23:14:50 +0200775struct orig_node *transtable_search(struct bat_priv *bat_priv,
776 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200778 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000779 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200781 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000782
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200783 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000784 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200786 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200787 goto free_tt;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000788
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200789 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000790
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200791free_tt:
792 tt_global_entry_free_ref(tt_global_entry);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000793out:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000794 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200796
797/* Calculates the checksum of the local table of a given orig_node */
798uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
799{
800 uint16_t total = 0, total_one;
801 struct hashtable_t *hash = bat_priv->tt_global_hash;
802 struct tt_global_entry *tt_global_entry;
803 struct hlist_node *node;
804 struct hlist_head *head;
805 int i, j;
806
807 for (i = 0; i < hash->size; i++) {
808 head = &hash->table[i];
809
810 rcu_read_lock();
811 hlist_for_each_entry_rcu(tt_global_entry, node,
812 head, hash_entry) {
813 if (compare_eth(tt_global_entry->orig_node,
814 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200815 /* Roaming clients are in the global table for
816 * consistency only. They don't have to be
817 * taken into account while computing the
818 * global crc */
819 if (tt_global_entry->flags & TT_CLIENT_ROAM)
820 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200821 total_one = 0;
822 for (j = 0; j < ETH_ALEN; j++)
823 total_one = crc16_byte(total_one,
824 tt_global_entry->addr[j]);
825 total ^= total_one;
826 }
827 }
828 rcu_read_unlock();
829 }
830
831 return total;
832}
833
834/* Calculates the checksum of the local table */
835uint16_t tt_local_crc(struct bat_priv *bat_priv)
836{
837 uint16_t total = 0, total_one;
838 struct hashtable_t *hash = bat_priv->tt_local_hash;
839 struct tt_local_entry *tt_local_entry;
840 struct hlist_node *node;
841 struct hlist_head *head;
842 int i, j;
843
844 for (i = 0; i < hash->size; i++) {
845 head = &hash->table[i];
846
847 rcu_read_lock();
848 hlist_for_each_entry_rcu(tt_local_entry, node,
849 head, hash_entry) {
850 total_one = 0;
851 for (j = 0; j < ETH_ALEN; j++)
852 total_one = crc16_byte(total_one,
853 tt_local_entry->addr[j]);
854 total ^= total_one;
855 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200856 rcu_read_unlock();
857 }
858
859 return total;
860}
861
862static void tt_req_list_free(struct bat_priv *bat_priv)
863{
864 struct tt_req_node *node, *safe;
865
866 spin_lock_bh(&bat_priv->tt_req_list_lock);
867
868 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
869 list_del(&node->list);
870 kfree(node);
871 }
872
873 spin_unlock_bh(&bat_priv->tt_req_list_lock);
874}
875
876void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
877 const unsigned char *tt_buff, uint8_t tt_num_changes)
878{
879 uint16_t tt_buff_len = tt_len(tt_num_changes);
880
881 /* Replace the old buffer only if I received something in the
882 * last OGM (the OGM could carry no changes) */
883 spin_lock_bh(&orig_node->tt_buff_lock);
884 if (tt_buff_len > 0) {
885 kfree(orig_node->tt_buff);
886 orig_node->tt_buff_len = 0;
887 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
888 if (orig_node->tt_buff) {
889 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
890 orig_node->tt_buff_len = tt_buff_len;
891 }
892 }
893 spin_unlock_bh(&orig_node->tt_buff_lock);
894}
895
896static void tt_req_purge(struct bat_priv *bat_priv)
897{
898 struct tt_req_node *node, *safe;
899
900 spin_lock_bh(&bat_priv->tt_req_list_lock);
901 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
902 if (is_out_of_time(node->issued_at,
903 TT_REQUEST_TIMEOUT * 1000)) {
904 list_del(&node->list);
905 kfree(node);
906 }
907 }
908 spin_unlock_bh(&bat_priv->tt_req_list_lock);
909}
910
911/* returns the pointer to the new tt_req_node struct if no request
912 * has already been issued for this orig_node, NULL otherwise */
913static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
914 struct orig_node *orig_node)
915{
916 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
917
918 spin_lock_bh(&bat_priv->tt_req_list_lock);
919 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
920 if (compare_eth(tt_req_node_tmp, orig_node) &&
921 !is_out_of_time(tt_req_node_tmp->issued_at,
922 TT_REQUEST_TIMEOUT * 1000))
923 goto unlock;
924 }
925
926 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
927 if (!tt_req_node)
928 goto unlock;
929
930 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
931 tt_req_node->issued_at = jiffies;
932
933 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
934unlock:
935 spin_unlock_bh(&bat_priv->tt_req_list_lock);
936 return tt_req_node;
937}
938
939static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
940{
941 const struct tt_global_entry *tt_global_entry = entry_ptr;
942 const struct orig_node *orig_node = data_ptr;
943
Antonio Quartullicc47f662011-04-27 14:27:57 +0200944 if (tt_global_entry->flags & TT_CLIENT_ROAM)
945 return 0;
946
Antonio Quartullia73105b2011-04-27 14:27:44 +0200947 return (tt_global_entry->orig_node == orig_node);
948}
949
950static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
951 struct hashtable_t *hash,
952 struct hard_iface *primary_if,
953 int (*valid_cb)(const void *,
954 const void *),
955 void *cb_data)
956{
957 struct tt_local_entry *tt_local_entry;
958 struct tt_query_packet *tt_response;
959 struct tt_change *tt_change;
960 struct hlist_node *node;
961 struct hlist_head *head;
962 struct sk_buff *skb = NULL;
963 uint16_t tt_tot, tt_count;
964 ssize_t tt_query_size = sizeof(struct tt_query_packet);
965 int i;
966
967 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
968 tt_len = primary_if->soft_iface->mtu - tt_query_size;
969 tt_len -= tt_len % sizeof(struct tt_change);
970 }
971 tt_tot = tt_len / sizeof(struct tt_change);
972
973 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
974 if (!skb)
975 goto out;
976
977 skb_reserve(skb, ETH_HLEN);
978 tt_response = (struct tt_query_packet *)skb_put(skb,
979 tt_query_size + tt_len);
980 tt_response->ttvn = ttvn;
981 tt_response->tt_data = htons(tt_tot);
982
983 tt_change = (struct tt_change *)(skb->data + tt_query_size);
984 tt_count = 0;
985
986 rcu_read_lock();
987 for (i = 0; i < hash->size; i++) {
988 head = &hash->table[i];
989
990 hlist_for_each_entry_rcu(tt_local_entry, node,
991 head, hash_entry) {
992 if (tt_count == tt_tot)
993 break;
994
995 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
996 continue;
997
998 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
999 tt_change->flags = NO_FLAGS;
1000
1001 tt_count++;
1002 tt_change++;
1003 }
1004 }
1005 rcu_read_unlock();
1006
1007out:
1008 return skb;
1009}
1010
1011int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1012 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1013{
1014 struct sk_buff *skb = NULL;
1015 struct tt_query_packet *tt_request;
1016 struct neigh_node *neigh_node = NULL;
1017 struct hard_iface *primary_if;
1018 struct tt_req_node *tt_req_node = NULL;
1019 int ret = 1;
1020
1021 primary_if = primary_if_get_selected(bat_priv);
1022 if (!primary_if)
1023 goto out;
1024
1025 /* The new tt_req will be issued only if I'm not waiting for a
1026 * reply from the same orig_node yet */
1027 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1028 if (!tt_req_node)
1029 goto out;
1030
1031 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1032 if (!skb)
1033 goto out;
1034
1035 skb_reserve(skb, ETH_HLEN);
1036
1037 tt_request = (struct tt_query_packet *)skb_put(skb,
1038 sizeof(struct tt_query_packet));
1039
1040 tt_request->packet_type = BAT_TT_QUERY;
1041 tt_request->version = COMPAT_VERSION;
1042 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1043 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1044 tt_request->ttl = TTL;
1045 tt_request->ttvn = ttvn;
1046 tt_request->tt_data = tt_crc;
1047 tt_request->flags = TT_REQUEST;
1048
1049 if (full_table)
1050 tt_request->flags |= TT_FULL_TABLE;
1051
1052 neigh_node = orig_node_get_router(dst_orig_node);
1053 if (!neigh_node)
1054 goto out;
1055
1056 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1057 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1058 (full_table ? 'F' : '.'));
1059
1060 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1061 ret = 0;
1062
1063out:
1064 if (neigh_node)
1065 neigh_node_free_ref(neigh_node);
1066 if (primary_if)
1067 hardif_free_ref(primary_if);
1068 if (ret)
1069 kfree_skb(skb);
1070 if (ret && tt_req_node) {
1071 spin_lock_bh(&bat_priv->tt_req_list_lock);
1072 list_del(&tt_req_node->list);
1073 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1074 kfree(tt_req_node);
1075 }
1076 return ret;
1077}
1078
1079static bool send_other_tt_response(struct bat_priv *bat_priv,
1080 struct tt_query_packet *tt_request)
1081{
1082 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1083 struct neigh_node *neigh_node = NULL;
1084 struct hard_iface *primary_if = NULL;
1085 uint8_t orig_ttvn, req_ttvn, ttvn;
1086 int ret = false;
1087 unsigned char *tt_buff;
1088 bool full_table;
1089 uint16_t tt_len, tt_tot;
1090 struct sk_buff *skb = NULL;
1091 struct tt_query_packet *tt_response;
1092
1093 bat_dbg(DBG_TT, bat_priv,
1094 "Received TT_REQUEST from %pM for "
1095 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1096 tt_request->ttvn, tt_request->dst,
1097 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1098
1099 /* Let's get the orig node of the REAL destination */
1100 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1101 if (!req_dst_orig_node)
1102 goto out;
1103
1104 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1105 if (!res_dst_orig_node)
1106 goto out;
1107
1108 neigh_node = orig_node_get_router(res_dst_orig_node);
1109 if (!neigh_node)
1110 goto out;
1111
1112 primary_if = primary_if_get_selected(bat_priv);
1113 if (!primary_if)
1114 goto out;
1115
1116 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1117 req_ttvn = tt_request->ttvn;
1118
1119 /* I have not the requested data */
1120 if (orig_ttvn != req_ttvn ||
1121 tt_request->tt_data != req_dst_orig_node->tt_crc)
1122 goto out;
1123
1124 /* If it has explicitly been requested the full table */
1125 if (tt_request->flags & TT_FULL_TABLE ||
1126 !req_dst_orig_node->tt_buff)
1127 full_table = true;
1128 else
1129 full_table = false;
1130
1131 /* In this version, fragmentation is not implemented, then
1132 * I'll send only one packet with as much TT entries as I can */
1133 if (!full_table) {
1134 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1135 tt_len = req_dst_orig_node->tt_buff_len;
1136 tt_tot = tt_len / sizeof(struct tt_change);
1137
1138 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1139 tt_len + ETH_HLEN);
1140 if (!skb)
1141 goto unlock;
1142
1143 skb_reserve(skb, ETH_HLEN);
1144 tt_response = (struct tt_query_packet *)skb_put(skb,
1145 sizeof(struct tt_query_packet) + tt_len);
1146 tt_response->ttvn = req_ttvn;
1147 tt_response->tt_data = htons(tt_tot);
1148
1149 tt_buff = skb->data + sizeof(struct tt_query_packet);
1150 /* Copy the last orig_node's OGM buffer */
1151 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1152 req_dst_orig_node->tt_buff_len);
1153
1154 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1155 } else {
1156 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1157 sizeof(struct tt_change);
1158 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1159
1160 skb = tt_response_fill_table(tt_len, ttvn,
1161 bat_priv->tt_global_hash,
1162 primary_if, tt_global_valid_entry,
1163 req_dst_orig_node);
1164 if (!skb)
1165 goto out;
1166
1167 tt_response = (struct tt_query_packet *)skb->data;
1168 }
1169
1170 tt_response->packet_type = BAT_TT_QUERY;
1171 tt_response->version = COMPAT_VERSION;
1172 tt_response->ttl = TTL;
1173 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1174 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1175 tt_response->flags = TT_RESPONSE;
1176
1177 if (full_table)
1178 tt_response->flags |= TT_FULL_TABLE;
1179
1180 bat_dbg(DBG_TT, bat_priv,
1181 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1182 res_dst_orig_node->orig, neigh_node->addr,
1183 req_dst_orig_node->orig, req_ttvn);
1184
1185 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1186 ret = true;
1187 goto out;
1188
1189unlock:
1190 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1191
1192out:
1193 if (res_dst_orig_node)
1194 orig_node_free_ref(res_dst_orig_node);
1195 if (req_dst_orig_node)
1196 orig_node_free_ref(req_dst_orig_node);
1197 if (neigh_node)
1198 neigh_node_free_ref(neigh_node);
1199 if (primary_if)
1200 hardif_free_ref(primary_if);
1201 if (!ret)
1202 kfree_skb(skb);
1203 return ret;
1204
1205}
1206static bool send_my_tt_response(struct bat_priv *bat_priv,
1207 struct tt_query_packet *tt_request)
1208{
1209 struct orig_node *orig_node = NULL;
1210 struct neigh_node *neigh_node = NULL;
1211 struct hard_iface *primary_if = NULL;
1212 uint8_t my_ttvn, req_ttvn, ttvn;
1213 int ret = false;
1214 unsigned char *tt_buff;
1215 bool full_table;
1216 uint16_t tt_len, tt_tot;
1217 struct sk_buff *skb = NULL;
1218 struct tt_query_packet *tt_response;
1219
1220 bat_dbg(DBG_TT, bat_priv,
1221 "Received TT_REQUEST from %pM for "
1222 "ttvn: %u (me) [%c]\n", tt_request->src,
1223 tt_request->ttvn,
1224 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1225
1226
1227 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1228 req_ttvn = tt_request->ttvn;
1229
1230 orig_node = get_orig_node(bat_priv, tt_request->src);
1231 if (!orig_node)
1232 goto out;
1233
1234 neigh_node = orig_node_get_router(orig_node);
1235 if (!neigh_node)
1236 goto out;
1237
1238 primary_if = primary_if_get_selected(bat_priv);
1239 if (!primary_if)
1240 goto out;
1241
1242 /* If the full table has been explicitly requested or the gap
1243 * is too big send the whole local translation table */
1244 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1245 !bat_priv->tt_buff)
1246 full_table = true;
1247 else
1248 full_table = false;
1249
1250 /* In this version, fragmentation is not implemented, then
1251 * I'll send only one packet with as much TT entries as I can */
1252 if (!full_table) {
1253 spin_lock_bh(&bat_priv->tt_buff_lock);
1254 tt_len = bat_priv->tt_buff_len;
1255 tt_tot = tt_len / sizeof(struct tt_change);
1256
1257 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1258 tt_len + ETH_HLEN);
1259 if (!skb)
1260 goto unlock;
1261
1262 skb_reserve(skb, ETH_HLEN);
1263 tt_response = (struct tt_query_packet *)skb_put(skb,
1264 sizeof(struct tt_query_packet) + tt_len);
1265 tt_response->ttvn = req_ttvn;
1266 tt_response->tt_data = htons(tt_tot);
1267
1268 tt_buff = skb->data + sizeof(struct tt_query_packet);
1269 memcpy(tt_buff, bat_priv->tt_buff,
1270 bat_priv->tt_buff_len);
1271 spin_unlock_bh(&bat_priv->tt_buff_lock);
1272 } else {
1273 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1274 sizeof(struct tt_change);
1275 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1276
1277 skb = tt_response_fill_table(tt_len, ttvn,
1278 bat_priv->tt_local_hash,
1279 primary_if, NULL, NULL);
1280 if (!skb)
1281 goto out;
1282
1283 tt_response = (struct tt_query_packet *)skb->data;
1284 }
1285
1286 tt_response->packet_type = BAT_TT_QUERY;
1287 tt_response->version = COMPAT_VERSION;
1288 tt_response->ttl = TTL;
1289 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1290 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1291 tt_response->flags = TT_RESPONSE;
1292
1293 if (full_table)
1294 tt_response->flags |= TT_FULL_TABLE;
1295
1296 bat_dbg(DBG_TT, bat_priv,
1297 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1298 orig_node->orig, neigh_node->addr,
1299 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1300
1301 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1302 ret = true;
1303 goto out;
1304
1305unlock:
1306 spin_unlock_bh(&bat_priv->tt_buff_lock);
1307out:
1308 if (orig_node)
1309 orig_node_free_ref(orig_node);
1310 if (neigh_node)
1311 neigh_node_free_ref(neigh_node);
1312 if (primary_if)
1313 hardif_free_ref(primary_if);
1314 if (!ret)
1315 kfree_skb(skb);
1316 /* This packet was for me, so it doesn't need to be re-routed */
1317 return true;
1318}
1319
1320bool send_tt_response(struct bat_priv *bat_priv,
1321 struct tt_query_packet *tt_request)
1322{
1323 if (is_my_mac(tt_request->dst))
1324 return send_my_tt_response(bat_priv, tt_request);
1325 else
1326 return send_other_tt_response(bat_priv, tt_request);
1327}
1328
1329static void _tt_update_changes(struct bat_priv *bat_priv,
1330 struct orig_node *orig_node,
1331 struct tt_change *tt_change,
1332 uint16_t tt_num_changes, uint8_t ttvn)
1333{
1334 int i;
1335
1336 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001337 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001338 tt_global_del(bat_priv, orig_node,
1339 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001340 "tt removed by changes",
1341 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001342 else
1343 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001344 (tt_change + i)->addr, ttvn, false))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001345 /* In case of problem while storing a
1346 * global_entry, we stop the updating
1347 * procedure without committing the
1348 * ttvn change. This will avoid to send
1349 * corrupted data on tt_request
1350 */
1351 return;
1352 }
1353}
1354
1355static void tt_fill_gtable(struct bat_priv *bat_priv,
1356 struct tt_query_packet *tt_response)
1357{
1358 struct orig_node *orig_node = NULL;
1359
1360 orig_node = orig_hash_find(bat_priv, tt_response->src);
1361 if (!orig_node)
1362 goto out;
1363
1364 /* Purge the old table first.. */
1365 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1366
1367 _tt_update_changes(bat_priv, orig_node,
1368 (struct tt_change *)(tt_response + 1),
1369 tt_response->tt_data, tt_response->ttvn);
1370
1371 spin_lock_bh(&orig_node->tt_buff_lock);
1372 kfree(orig_node->tt_buff);
1373 orig_node->tt_buff_len = 0;
1374 orig_node->tt_buff = NULL;
1375 spin_unlock_bh(&orig_node->tt_buff_lock);
1376
1377 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1378
1379out:
1380 if (orig_node)
1381 orig_node_free_ref(orig_node);
1382}
1383
1384void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1385 uint16_t tt_num_changes, uint8_t ttvn,
1386 struct tt_change *tt_change)
1387{
1388 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1389 ttvn);
1390
1391 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1392 tt_num_changes);
1393 atomic_set(&orig_node->last_ttvn, ttvn);
1394}
1395
1396bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1397{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001398 struct tt_local_entry *tt_local_entry = NULL;
1399 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001402 if (!tt_local_entry)
1403 goto out;
1404 ret = true;
1405out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001406 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001407 tt_local_entry_free_ref(tt_local_entry);
1408 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001409}
1410
1411void handle_tt_response(struct bat_priv *bat_priv,
1412 struct tt_query_packet *tt_response)
1413{
1414 struct tt_req_node *node, *safe;
1415 struct orig_node *orig_node = NULL;
1416
1417 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1418 "ttvn %d t_size: %d [%c]\n",
1419 tt_response->src, tt_response->ttvn,
1420 tt_response->tt_data,
1421 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1422
1423 orig_node = orig_hash_find(bat_priv, tt_response->src);
1424 if (!orig_node)
1425 goto out;
1426
1427 if (tt_response->flags & TT_FULL_TABLE)
1428 tt_fill_gtable(bat_priv, tt_response);
1429 else
1430 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1431 tt_response->ttvn,
1432 (struct tt_change *)(tt_response + 1));
1433
1434 /* Delete the tt_req_node from pending tt_requests list */
1435 spin_lock_bh(&bat_priv->tt_req_list_lock);
1436 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1437 if (!compare_eth(node->addr, tt_response->src))
1438 continue;
1439 list_del(&node->list);
1440 kfree(node);
1441 }
1442 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1443
1444 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001445 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001446 /* Roaming phase is over: tables are in sync again. I can
1447 * unset the flag */
1448 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001449out:
1450 if (orig_node)
1451 orig_node_free_ref(orig_node);
1452}
1453
1454int tt_init(struct bat_priv *bat_priv)
1455{
1456 if (!tt_local_init(bat_priv))
1457 return 0;
1458
1459 if (!tt_global_init(bat_priv))
1460 return 0;
1461
1462 tt_start_timer(bat_priv);
1463
1464 return 1;
1465}
1466
Antonio Quartullicc47f662011-04-27 14:27:57 +02001467static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001469 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470
Antonio Quartullicc47f662011-04-27 14:27:57 +02001471 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472
Antonio Quartullicc47f662011-04-27 14:27:57 +02001473 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1474 list_del(&node->list);
1475 kfree(node);
1476 }
1477
1478 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1479}
1480
1481static void tt_roam_purge(struct bat_priv *bat_priv)
1482{
1483 struct tt_roam_node *node, *safe;
1484
1485 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1486 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1487 if (!is_out_of_time(node->first_time,
1488 ROAMING_MAX_TIME * 1000))
1489 continue;
1490
1491 list_del(&node->list);
1492 kfree(node);
1493 }
1494 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1495}
1496
1497/* This function checks whether the client already reached the
1498 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1499 * will not be sent.
1500 *
1501 * returns true if the ROAMING_ADV can be sent, false otherwise */
1502static bool tt_check_roam_count(struct bat_priv *bat_priv,
1503 uint8_t *client)
1504{
1505 struct tt_roam_node *tt_roam_node;
1506 bool ret = false;
1507
1508 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1509 /* The new tt_req will be issued only if I'm not waiting for a
1510 * reply from the same orig_node yet */
1511 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1512 if (!compare_eth(tt_roam_node->addr, client))
1513 continue;
1514
1515 if (is_out_of_time(tt_roam_node->first_time,
1516 ROAMING_MAX_TIME * 1000))
1517 continue;
1518
1519 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1520 /* Sorry, you roamed too many times! */
1521 goto unlock;
1522 ret = true;
1523 break;
1524 }
1525
1526 if (!ret) {
1527 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1528 if (!tt_roam_node)
1529 goto unlock;
1530
1531 tt_roam_node->first_time = jiffies;
1532 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1533 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1534
1535 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1536 ret = true;
1537 }
1538
1539unlock:
1540 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1541 return ret;
1542}
1543
1544void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1545 struct orig_node *orig_node)
1546{
1547 struct neigh_node *neigh_node = NULL;
1548 struct sk_buff *skb = NULL;
1549 struct roam_adv_packet *roam_adv_packet;
1550 int ret = 1;
1551 struct hard_iface *primary_if;
1552
1553 /* before going on we have to check whether the client has
1554 * already roamed to us too many times */
1555 if (!tt_check_roam_count(bat_priv, client))
1556 goto out;
1557
1558 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1559 if (!skb)
1560 goto out;
1561
1562 skb_reserve(skb, ETH_HLEN);
1563
1564 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1565 sizeof(struct roam_adv_packet));
1566
1567 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1568 roam_adv_packet->version = COMPAT_VERSION;
1569 roam_adv_packet->ttl = TTL;
1570 primary_if = primary_if_get_selected(bat_priv);
1571 if (!primary_if)
1572 goto out;
1573 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1574 hardif_free_ref(primary_if);
1575 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1576 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1577
1578 neigh_node = orig_node_get_router(orig_node);
1579 if (!neigh_node)
1580 goto out;
1581
1582 bat_dbg(DBG_TT, bat_priv,
1583 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1584 orig_node->orig, client, neigh_node->addr);
1585
1586 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1587 ret = 0;
1588
1589out:
1590 if (neigh_node)
1591 neigh_node_free_ref(neigh_node);
1592 if (ret)
1593 kfree_skb(skb);
1594 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595}
1596
1597static void tt_purge(struct work_struct *work)
1598{
1599 struct delayed_work *delayed_work =
1600 container_of(work, struct delayed_work, work);
1601 struct bat_priv *bat_priv =
1602 container_of(delayed_work, struct bat_priv, tt_work);
1603
1604 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001605 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001607 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608
1609 tt_start_timer(bat_priv);
1610}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001611
1612void tt_free(struct bat_priv *bat_priv)
1613{
1614 cancel_delayed_work_sync(&bat_priv->tt_work);
1615
1616 tt_local_table_free(bat_priv);
1617 tt_global_table_free(bat_priv);
1618 tt_req_list_free(bat_priv);
1619 tt_changes_list_free(bat_priv);
1620 tt_roam_list_free(bat_priv);
1621
1622 kfree(bat_priv->tt_buff);
1623}