blob: d516d8591cfcb5af67bdf3d9e67580fb43167a7e [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 Quartulli2dafb492011-05-05 08:42:45 +020083 tt_local_entry_tmp = tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000084 break;
85 }
86 rcu_read_unlock();
87
Antonio Quartulli2dafb492011-05-05 08:42:45 +020088 return tt_local_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +000089}
90
Antonio Quartulli2dafb492011-05-05 08:42:45 +020091static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020092 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000093{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020094 struct hashtable_t *hash = bat_priv->tt_global_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000095 struct hlist_head *head;
96 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097 struct tt_global_entry *tt_global_entry;
98 struct tt_global_entry *tt_global_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000099 int index;
100
101 if (!hash)
102 return NULL;
103
104 index = choose_orig(data, hash->size);
105 head = &hash->table[index];
106
107 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200108 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
109 if (!compare_eth(tt_global_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000110 continue;
111
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200112 tt_global_entry_tmp = tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000113 break;
114 }
115 rcu_read_unlock();
116
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200117 return tt_global_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +0000118}
119
Antonio Quartullia73105b2011-04-27 14:27:44 +0200120static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
121{
122 unsigned long deadline;
123 deadline = starting_time + msecs_to_jiffies(timeout);
124
125 return time_after(jiffies, deadline);
126}
127
128static void tt_local_event(struct bat_priv *bat_priv, uint8_t op,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200129 const uint8_t *addr, uint8_t roaming)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200130{
131 struct tt_change_node *tt_change_node;
132
133 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
134
135 if (!tt_change_node)
136 return;
137
138 tt_change_node->change.flags = op;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200139 if (roaming)
140 tt_change_node->change.flags |= TT_CLIENT_ROAM;
141
Antonio Quartullia73105b2011-04-27 14:27:44 +0200142 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
143
144 spin_lock_bh(&bat_priv->tt_changes_list_lock);
145 /* track the change in the OGMinterval list */
146 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
147 atomic_inc(&bat_priv->tt_local_changes);
148 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
149
150 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
151}
152
153int tt_len(int changes_num)
154{
155 return changes_num * sizeof(struct tt_change);
156}
157
158static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200160 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161 return 1;
162
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200163 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200165 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166 return 0;
167
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168 return 1;
169}
170
Sven Eckelmann747e4222011-05-14 23:14:50 +0200171void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172{
173 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200174 struct tt_local_entry *tt_local_entry;
175 struct tt_global_entry *tt_global_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200176 uint8_t roam_addr[ETH_ALEN];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 spin_lock_bh(&bat_priv->tt_lhash_lock);
179 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200181 if (tt_local_entry) {
182 tt_local_entry->last_seen = jiffies;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200183 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 }
185
Sven Eckelmann704509b2011-05-14 23:14:54 +0200186 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 if (!tt_local_entry)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200188 goto unlock;
189
Antonio Quartullicc47f662011-04-27 14:27:57 +0200190 tt_local_event(bat_priv, NO_FLAGS, addr, false);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200191
192 bat_dbg(DBG_TT, bat_priv,
193 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
194 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200196 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
197 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198
199 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000200 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200201 tt_local_entry->never_purge = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202 else
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200203 tt_local_entry->never_purge = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200205 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
206 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200207 atomic_inc(&bat_priv->num_local_tt);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200208 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
210 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200211 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200213 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214
Antonio Quartullicc47f662011-04-27 14:27:57 +0200215 /* Check whether it is a roaming! */
216 if (tt_global_entry) {
217 memcpy(roam_addr, tt_global_entry->addr, ETH_ALEN);
218 /* This node is probably going to update its tt table */
219 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220 _tt_global_del(bat_priv, tt_global_entry,
221 "local tt received");
Antonio Quartullicc47f662011-04-27 14:27:57 +0200222 spin_unlock_bh(&bat_priv->tt_ghash_lock);
223 send_roam_adv(bat_priv, tt_global_entry->addr,
224 tt_global_entry->orig_node);
225 } else
226 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Antonio Quartullia73105b2011-04-27 14:27:44 +0200228 return;
229unlock:
230 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231}
232
Antonio Quartullia73105b2011-04-27 14:27:44 +0200233int tt_changes_fill_buffer(struct bat_priv *bat_priv,
234 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200236 int count = 0, tot_changes = 0;
237 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
Antonio Quartullia73105b2011-04-27 14:27:44 +0200239 if (buff_len > 0)
240 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Antonio Quartullia73105b2011-04-27 14:27:44 +0200242 spin_lock_bh(&bat_priv->tt_changes_list_lock);
243 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Antonio Quartullia73105b2011-04-27 14:27:44 +0200245 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
246 list) {
247 if (count < tot_changes) {
248 memcpy(buff + tt_len(count),
249 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 count++;
251 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200252 list_del(&entry->list);
253 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200255 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Antonio Quartullia73105b2011-04-27 14:27:44 +0200257 /* Keep the buffer for possible tt_request */
258 spin_lock_bh(&bat_priv->tt_buff_lock);
259 kfree(bat_priv->tt_buff);
260 bat_priv->tt_buff_len = 0;
261 bat_priv->tt_buff = NULL;
262 /* We check whether this new OGM has no changes due to size
263 * problems */
264 if (buff_len > 0) {
265 /**
266 * if kmalloc() fails we will reply with the full table
267 * instead of providing the diff
268 */
269 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
270 if (bat_priv->tt_buff) {
271 memcpy(bat_priv->tt_buff, buff, buff_len);
272 bat_priv->tt_buff_len = buff_len;
273 }
274 }
275 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Antonio Quartullia73105b2011-04-27 14:27:44 +0200277 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278}
279
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200280int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281{
282 struct net_device *net_dev = (struct net_device *)seq->private;
283 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200284 struct hashtable_t *hash = bat_priv->tt_local_hash;
285 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200286 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000287 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 size_t buf_size, pos;
290 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200291 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292
Marek Lindner32ae9b22011-04-20 15:40:58 +0200293 primary_if = primary_if_get_selected(bat_priv);
294 if (!primary_if) {
295 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
296 "please specify interfaces to enable it\n",
297 net_dev->name);
298 goto out;
299 }
300
301 if (primary_if->if_status != IF_ACTIVE) {
302 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
303 "primary interface not active\n",
304 net_dev->name);
305 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 }
307
308 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200309 "announced via TT (TTVN: %u):\n",
310 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200312 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
314 buf_size = 1;
315 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
316 for (i = 0; i < hash->size; i++) {
317 head = &hash->table[i];
318
Marek Lindner7aadf882011-02-18 12:28:09 +0000319 rcu_read_lock();
320 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000322 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 }
324
325 buff = kmalloc(buf_size, GFP_ATOMIC);
326 if (!buff) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200327 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200328 ret = -ENOMEM;
329 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000331
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 buff[0] = '\0';
333 pos = 0;
334
335 for (i = 0; i < hash->size; i++) {
336 head = &hash->table[i];
337
Marek Lindner7aadf882011-02-18 12:28:09 +0000338 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200339 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000340 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200342 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000344 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 }
346
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200347 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
349 seq_printf(seq, "%s", buff);
350 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200351out:
352 if (primary_if)
353 hardif_free_ref(primary_if);
354 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355}
356
Antonio Quartullia73105b2011-04-27 14:27:44 +0200357static void tt_local_entry_free(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200359 struct bat_priv *bat_priv = arg;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200360 void *data = container_of(node, struct tt_local_entry, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361
362 kfree(data);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200363 atomic_dec(&bat_priv->num_local_tt);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364}
365
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200366static void tt_local_del(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200367 struct tt_local_entry *tt_local_entry,
368 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200370 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry (%pM): %s\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200371 tt_local_entry->addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Antonio Quartullia73105b2011-04-27 14:27:44 +0200373 atomic_dec(&bat_priv->num_local_tt);
374
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200375 hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
376 tt_local_entry->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200377
378 tt_local_entry_free(&tt_local_entry->hash_entry, bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379}
380
Antonio Quartullia73105b2011-04-27 14:27:44 +0200381void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200382 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200384 struct tt_local_entry *tt_local_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200386 spin_lock_bh(&bat_priv->tt_lhash_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200387 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389 if (tt_local_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200390 tt_local_event(bat_priv, TT_CHANGE_DEL, tt_local_entry->addr,
391 roaming);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200392 tt_local_del(bat_priv, tt_local_entry, message);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200393 }
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200394 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395}
396
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200399 struct hashtable_t *hash = bat_priv->tt_local_hash;
400 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000401 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 struct hlist_head *head;
Marek Lindner7aadf882011-02-18 12:28:09 +0000403 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200405 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
407 for (i = 0; i < hash->size; i++) {
408 head = &hash->table[i];
409
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 Quartulli2dafb492011-05-05 08:42:45 +0200412 if (tt_local_entry->never_purge)
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,
416 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000417 continue;
418
Antonio Quartullia73105b2011-04-27 14:27:44 +0200419 tt_local_event(bat_priv, TT_CHANGE_DEL,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200420 tt_local_entry->addr, false);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200421 tt_local_del(bat_priv, tt_local_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200422 "address timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 }
424 }
425
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200426 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427}
428
Antonio Quartullia73105b2011-04-27 14:27:44 +0200429static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200431 struct hashtable_t *hash;
432 int i;
433 spinlock_t *list_lock; /* protects write access to the hash lists */
434 struct hlist_head *head;
435 struct hlist_node *node, *node_tmp;
436 struct tt_local_entry *tt_local_entry;
437
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200438 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439 return;
440
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441 hash = bat_priv->tt_local_hash;
442
443 for (i = 0; i < hash->size; i++) {
444 head = &hash->table[i];
445 list_lock = &hash->list_locks[i];
446
447 spin_lock_bh(list_lock);
448 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
449 head, hash_entry) {
450 hlist_del_rcu(node);
451 kfree(tt_local_entry);
452 }
453 spin_unlock_bh(list_lock);
454 }
455
456 hash_destroy(hash);
457
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200458 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459}
460
Antonio Quartullia73105b2011-04-27 14:27:44 +0200461static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200463 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 return 1;
465
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200466 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200468 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 return 0;
470
471 return 1;
472}
473
Antonio Quartullia73105b2011-04-27 14:27:44 +0200474static void tt_changes_list_free(struct bat_priv *bat_priv)
475{
476 struct tt_change_node *entry, *safe;
477
478 spin_lock_bh(&bat_priv->tt_changes_list_lock);
479
480 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
481 list) {
482 list_del(&entry->list);
483 kfree(entry);
484 }
485
486 atomic_set(&bat_priv->tt_local_changes, 0);
487 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
488}
489
490/* caller must hold orig_node refcount */
491int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200492 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200494 struct tt_global_entry *tt_global_entry;
495 struct tt_local_entry *tt_local_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200496 struct orig_node *orig_node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497
Antonio Quartullia73105b2011-04-27 14:27:44 +0200498 spin_lock_bh(&bat_priv->tt_ghash_lock);
499 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500
Antonio Quartullia73105b2011-04-27 14:27:44 +0200501 if (!tt_global_entry) {
502 tt_global_entry =
503 kmalloc(sizeof(*tt_global_entry),
504 GFP_ATOMIC);
505 if (!tt_global_entry)
506 goto unlock;
507 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
508 /* Assign the new orig_node */
509 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200510 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200511 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200512 tt_global_entry->flags = NO_FLAGS;
513 tt_global_entry->roam_at = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200514 atomic_inc(&orig_node->tt_size);
515 hash_add(bat_priv->tt_global_hash, compare_gtt,
516 choose_orig, tt_global_entry,
517 &tt_global_entry->hash_entry);
518 } else {
519 if (tt_global_entry->orig_node != orig_node) {
520 atomic_dec(&tt_global_entry->orig_node->tt_size);
521 orig_node_tmp = tt_global_entry->orig_node;
522 atomic_inc(&orig_node->refcount);
523 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200524 orig_node_free_ref(orig_node_tmp);
525 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200527 tt_global_entry->ttvn = ttvn;
528 tt_global_entry->flags = NO_FLAGS;
529 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200531
532 spin_unlock_bh(&bat_priv->tt_ghash_lock);
533
534 bat_dbg(DBG_TT, bat_priv,
535 "Creating new global tt entry: %pM (via %pM)\n",
536 tt_global_entry->addr, orig_node->orig);
537
538 /* remove address from local hash if present */
539 spin_lock_bh(&bat_priv->tt_lhash_lock);
540 tt_local_entry = tt_local_hash_find(bat_priv, tt_addr);
541
542 if (tt_local_entry)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200543 tt_local_remove(bat_priv, tt_global_entry->addr,
544 "global tt received", roaming);
545
Antonio Quartullia73105b2011-04-27 14:27:44 +0200546 spin_unlock_bh(&bat_priv->tt_lhash_lock);
547 return 1;
548unlock:
549 spin_unlock_bh(&bat_priv->tt_ghash_lock);
550 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551}
552
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200553int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554{
555 struct net_device *net_dev = (struct net_device *)seq->private;
556 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200557 struct hashtable_t *hash = bat_priv->tt_global_hash;
558 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000560 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562 size_t buf_size, pos;
563 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200564 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Marek Lindner32ae9b22011-04-20 15:40:58 +0200566 primary_if = primary_if_get_selected(bat_priv);
567 if (!primary_if) {
568 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
569 "specify interfaces to enable it\n",
570 net_dev->name);
571 goto out;
572 }
573
574 if (primary_if->if_status != IF_ACTIVE) {
575 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
576 "primary interface not active\n",
577 net_dev->name);
578 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 }
580
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200581 seq_printf(seq,
582 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200584 seq_printf(seq, " %-13s %s %-15s %s\n",
585 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200587 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
589 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200590 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
591 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 for (i = 0; i < hash->size; i++) {
593 head = &hash->table[i];
594
Marek Lindner7aadf882011-02-18 12:28:09 +0000595 rcu_read_lock();
596 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200597 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000598 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 }
600
601 buff = kmalloc(buf_size, GFP_ATOMIC);
602 if (!buff) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200603 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200604 ret = -ENOMEM;
605 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606 }
607 buff[0] = '\0';
608 pos = 0;
609
610 for (i = 0; i < hash->size; i++) {
611 head = &hash->table[i];
612
Marek Lindner7aadf882011-02-18 12:28:09 +0000613 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200614 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000615 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200616 pos += snprintf(buff + pos, 61,
617 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200618 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200619 tt_global_entry->ttvn,
620 tt_global_entry->orig_node->orig,
621 (uint8_t) atomic_read(
622 &tt_global_entry->orig_node->
623 last_ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000625 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626 }
627
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200628 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629
630 seq_printf(seq, "%s", buff);
631 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200632out:
633 if (primary_if)
634 hardif_free_ref(primary_if);
635 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636}
637
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638static void _tt_global_del(struct bat_priv *bat_priv,
639 struct tt_global_entry *tt_global_entry,
640 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200642 if (!tt_global_entry)
643 return;
644
645 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200646 "Deleting global tt entry %pM (via %pM): %s\n",
647 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648 message);
649
Antonio Quartullia73105b2011-04-27 14:27:44 +0200650 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200651 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
652 tt_global_entry->addr);
653 kfree(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654}
655
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656void tt_global_del(struct bat_priv *bat_priv,
657 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200658 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200660 struct tt_global_entry *tt_global_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662 spin_lock_bh(&bat_priv->tt_ghash_lock);
663 tt_global_entry = tt_global_hash_find(bat_priv, addr);
664
665 if (tt_global_entry && tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200666 if (roaming) {
667 tt_global_entry->flags |= TT_CLIENT_ROAM;
668 tt_global_entry->roam_at = jiffies;
669 goto out;
670 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200671 atomic_dec(&orig_node->tt_size);
672 _tt_global_del(bat_priv, tt_global_entry, message);
673 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200674out:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200675 spin_unlock_bh(&bat_priv->tt_ghash_lock);
676}
677
678void tt_global_del_orig(struct bat_priv *bat_priv,
679 struct orig_node *orig_node, const char *message)
680{
681 struct tt_global_entry *tt_global_entry;
682 int i;
683 struct hashtable_t *hash = bat_priv->tt_global_hash;
684 struct hlist_node *node, *safe;
685 struct hlist_head *head;
686
687 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688 return;
689
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200690 spin_lock_bh(&bat_priv->tt_ghash_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200691 for (i = 0; i < hash->size; i++) {
692 head = &hash->table[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694 hlist_for_each_entry_safe(tt_global_entry, node, safe,
695 head, hash_entry) {
696 if (tt_global_entry->orig_node == orig_node)
697 _tt_global_del(bat_priv, tt_global_entry,
698 message);
699 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200703 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704}
705
Antonio Quartullia73105b2011-04-27 14:27:44 +0200706static void tt_global_entry_free(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200708 void *data = container_of(node, struct tt_global_entry, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709 kfree(data);
710}
711
Antonio Quartullicc47f662011-04-27 14:27:57 +0200712static void tt_global_roam_purge(struct bat_priv *bat_priv)
713{
714 struct hashtable_t *hash = bat_priv->tt_global_hash;
715 struct tt_global_entry *tt_global_entry;
716 struct hlist_node *node, *node_tmp;
717 struct hlist_head *head;
718 int i;
719
720 spin_lock_bh(&bat_priv->tt_ghash_lock);
721
722 for (i = 0; i < hash->size; i++) {
723 head = &hash->table[i];
724
725 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
726 head, hash_entry) {
727 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
728 continue;
729 if (!is_out_of_time(tt_global_entry->roam_at,
730 TT_CLIENT_ROAM_TIMEOUT * 1000))
731 continue;
732
733 _tt_global_del(bat_priv, tt_global_entry,
734 "Roaming timeout");
735 }
736 }
737
738 spin_unlock_bh(&bat_priv->tt_ghash_lock);
739}
740
Antonio Quartullia73105b2011-04-27 14:27:44 +0200741static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200743 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 return;
745
Antonio Quartullia73105b2011-04-27 14:27:44 +0200746 hash_delete(bat_priv->tt_global_hash, tt_global_entry_free, NULL);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200747 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748}
749
Sven Eckelmann747e4222011-05-14 23:14:50 +0200750struct orig_node *transtable_search(struct bat_priv *bat_priv,
751 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200753 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000754 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200756 spin_lock_bh(&bat_priv->tt_ghash_lock);
757 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000758
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200759 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000760 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200762 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000763 goto out;
764
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200765 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000766
767out:
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200768 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000769 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200771
772/* Calculates the checksum of the local table of a given orig_node */
773uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
774{
775 uint16_t total = 0, total_one;
776 struct hashtable_t *hash = bat_priv->tt_global_hash;
777 struct tt_global_entry *tt_global_entry;
778 struct hlist_node *node;
779 struct hlist_head *head;
780 int i, j;
781
782 for (i = 0; i < hash->size; i++) {
783 head = &hash->table[i];
784
785 rcu_read_lock();
786 hlist_for_each_entry_rcu(tt_global_entry, node,
787 head, hash_entry) {
788 if (compare_eth(tt_global_entry->orig_node,
789 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200790 /* Roaming clients are in the global table for
791 * consistency only. They don't have to be
792 * taken into account while computing the
793 * global crc */
794 if (tt_global_entry->flags & TT_CLIENT_ROAM)
795 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200796 total_one = 0;
797 for (j = 0; j < ETH_ALEN; j++)
798 total_one = crc16_byte(total_one,
799 tt_global_entry->addr[j]);
800 total ^= total_one;
801 }
802 }
803 rcu_read_unlock();
804 }
805
806 return total;
807}
808
809/* Calculates the checksum of the local table */
810uint16_t tt_local_crc(struct bat_priv *bat_priv)
811{
812 uint16_t total = 0, total_one;
813 struct hashtable_t *hash = bat_priv->tt_local_hash;
814 struct tt_local_entry *tt_local_entry;
815 struct hlist_node *node;
816 struct hlist_head *head;
817 int i, j;
818
819 for (i = 0; i < hash->size; i++) {
820 head = &hash->table[i];
821
822 rcu_read_lock();
823 hlist_for_each_entry_rcu(tt_local_entry, node,
824 head, hash_entry) {
825 total_one = 0;
826 for (j = 0; j < ETH_ALEN; j++)
827 total_one = crc16_byte(total_one,
828 tt_local_entry->addr[j]);
829 total ^= total_one;
830 }
831
832 rcu_read_unlock();
833 }
834
835 return total;
836}
837
838static void tt_req_list_free(struct bat_priv *bat_priv)
839{
840 struct tt_req_node *node, *safe;
841
842 spin_lock_bh(&bat_priv->tt_req_list_lock);
843
844 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
845 list_del(&node->list);
846 kfree(node);
847 }
848
849 spin_unlock_bh(&bat_priv->tt_req_list_lock);
850}
851
852void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
853 const unsigned char *tt_buff, uint8_t tt_num_changes)
854{
855 uint16_t tt_buff_len = tt_len(tt_num_changes);
856
857 /* Replace the old buffer only if I received something in the
858 * last OGM (the OGM could carry no changes) */
859 spin_lock_bh(&orig_node->tt_buff_lock);
860 if (tt_buff_len > 0) {
861 kfree(orig_node->tt_buff);
862 orig_node->tt_buff_len = 0;
863 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
864 if (orig_node->tt_buff) {
865 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
866 orig_node->tt_buff_len = tt_buff_len;
867 }
868 }
869 spin_unlock_bh(&orig_node->tt_buff_lock);
870}
871
872static void tt_req_purge(struct bat_priv *bat_priv)
873{
874 struct tt_req_node *node, *safe;
875
876 spin_lock_bh(&bat_priv->tt_req_list_lock);
877 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
878 if (is_out_of_time(node->issued_at,
879 TT_REQUEST_TIMEOUT * 1000)) {
880 list_del(&node->list);
881 kfree(node);
882 }
883 }
884 spin_unlock_bh(&bat_priv->tt_req_list_lock);
885}
886
887/* returns the pointer to the new tt_req_node struct if no request
888 * has already been issued for this orig_node, NULL otherwise */
889static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
890 struct orig_node *orig_node)
891{
892 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
893
894 spin_lock_bh(&bat_priv->tt_req_list_lock);
895 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
896 if (compare_eth(tt_req_node_tmp, orig_node) &&
897 !is_out_of_time(tt_req_node_tmp->issued_at,
898 TT_REQUEST_TIMEOUT * 1000))
899 goto unlock;
900 }
901
902 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
903 if (!tt_req_node)
904 goto unlock;
905
906 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
907 tt_req_node->issued_at = jiffies;
908
909 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
910unlock:
911 spin_unlock_bh(&bat_priv->tt_req_list_lock);
912 return tt_req_node;
913}
914
915static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
916{
917 const struct tt_global_entry *tt_global_entry = entry_ptr;
918 const struct orig_node *orig_node = data_ptr;
919
Antonio Quartullicc47f662011-04-27 14:27:57 +0200920 if (tt_global_entry->flags & TT_CLIENT_ROAM)
921 return 0;
922
Antonio Quartullia73105b2011-04-27 14:27:44 +0200923 return (tt_global_entry->orig_node == orig_node);
924}
925
926static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
927 struct hashtable_t *hash,
928 struct hard_iface *primary_if,
929 int (*valid_cb)(const void *,
930 const void *),
931 void *cb_data)
932{
933 struct tt_local_entry *tt_local_entry;
934 struct tt_query_packet *tt_response;
935 struct tt_change *tt_change;
936 struct hlist_node *node;
937 struct hlist_head *head;
938 struct sk_buff *skb = NULL;
939 uint16_t tt_tot, tt_count;
940 ssize_t tt_query_size = sizeof(struct tt_query_packet);
941 int i;
942
943 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
944 tt_len = primary_if->soft_iface->mtu - tt_query_size;
945 tt_len -= tt_len % sizeof(struct tt_change);
946 }
947 tt_tot = tt_len / sizeof(struct tt_change);
948
949 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
950 if (!skb)
951 goto out;
952
953 skb_reserve(skb, ETH_HLEN);
954 tt_response = (struct tt_query_packet *)skb_put(skb,
955 tt_query_size + tt_len);
956 tt_response->ttvn = ttvn;
957 tt_response->tt_data = htons(tt_tot);
958
959 tt_change = (struct tt_change *)(skb->data + tt_query_size);
960 tt_count = 0;
961
962 rcu_read_lock();
963 for (i = 0; i < hash->size; i++) {
964 head = &hash->table[i];
965
966 hlist_for_each_entry_rcu(tt_local_entry, node,
967 head, hash_entry) {
968 if (tt_count == tt_tot)
969 break;
970
971 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
972 continue;
973
974 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
975 tt_change->flags = NO_FLAGS;
976
977 tt_count++;
978 tt_change++;
979 }
980 }
981 rcu_read_unlock();
982
983out:
984 return skb;
985}
986
987int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
988 uint8_t ttvn, uint16_t tt_crc, bool full_table)
989{
990 struct sk_buff *skb = NULL;
991 struct tt_query_packet *tt_request;
992 struct neigh_node *neigh_node = NULL;
993 struct hard_iface *primary_if;
994 struct tt_req_node *tt_req_node = NULL;
995 int ret = 1;
996
997 primary_if = primary_if_get_selected(bat_priv);
998 if (!primary_if)
999 goto out;
1000
1001 /* The new tt_req will be issued only if I'm not waiting for a
1002 * reply from the same orig_node yet */
1003 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1004 if (!tt_req_node)
1005 goto out;
1006
1007 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1008 if (!skb)
1009 goto out;
1010
1011 skb_reserve(skb, ETH_HLEN);
1012
1013 tt_request = (struct tt_query_packet *)skb_put(skb,
1014 sizeof(struct tt_query_packet));
1015
1016 tt_request->packet_type = BAT_TT_QUERY;
1017 tt_request->version = COMPAT_VERSION;
1018 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1019 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1020 tt_request->ttl = TTL;
1021 tt_request->ttvn = ttvn;
1022 tt_request->tt_data = tt_crc;
1023 tt_request->flags = TT_REQUEST;
1024
1025 if (full_table)
1026 tt_request->flags |= TT_FULL_TABLE;
1027
1028 neigh_node = orig_node_get_router(dst_orig_node);
1029 if (!neigh_node)
1030 goto out;
1031
1032 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1033 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1034 (full_table ? 'F' : '.'));
1035
1036 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1037 ret = 0;
1038
1039out:
1040 if (neigh_node)
1041 neigh_node_free_ref(neigh_node);
1042 if (primary_if)
1043 hardif_free_ref(primary_if);
1044 if (ret)
1045 kfree_skb(skb);
1046 if (ret && tt_req_node) {
1047 spin_lock_bh(&bat_priv->tt_req_list_lock);
1048 list_del(&tt_req_node->list);
1049 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1050 kfree(tt_req_node);
1051 }
1052 return ret;
1053}
1054
1055static bool send_other_tt_response(struct bat_priv *bat_priv,
1056 struct tt_query_packet *tt_request)
1057{
1058 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1059 struct neigh_node *neigh_node = NULL;
1060 struct hard_iface *primary_if = NULL;
1061 uint8_t orig_ttvn, req_ttvn, ttvn;
1062 int ret = false;
1063 unsigned char *tt_buff;
1064 bool full_table;
1065 uint16_t tt_len, tt_tot;
1066 struct sk_buff *skb = NULL;
1067 struct tt_query_packet *tt_response;
1068
1069 bat_dbg(DBG_TT, bat_priv,
1070 "Received TT_REQUEST from %pM for "
1071 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1072 tt_request->ttvn, tt_request->dst,
1073 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1074
1075 /* Let's get the orig node of the REAL destination */
1076 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1077 if (!req_dst_orig_node)
1078 goto out;
1079
1080 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1081 if (!res_dst_orig_node)
1082 goto out;
1083
1084 neigh_node = orig_node_get_router(res_dst_orig_node);
1085 if (!neigh_node)
1086 goto out;
1087
1088 primary_if = primary_if_get_selected(bat_priv);
1089 if (!primary_if)
1090 goto out;
1091
1092 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1093 req_ttvn = tt_request->ttvn;
1094
1095 /* I have not the requested data */
1096 if (orig_ttvn != req_ttvn ||
1097 tt_request->tt_data != req_dst_orig_node->tt_crc)
1098 goto out;
1099
1100 /* If it has explicitly been requested the full table */
1101 if (tt_request->flags & TT_FULL_TABLE ||
1102 !req_dst_orig_node->tt_buff)
1103 full_table = true;
1104 else
1105 full_table = false;
1106
1107 /* In this version, fragmentation is not implemented, then
1108 * I'll send only one packet with as much TT entries as I can */
1109 if (!full_table) {
1110 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1111 tt_len = req_dst_orig_node->tt_buff_len;
1112 tt_tot = tt_len / sizeof(struct tt_change);
1113
1114 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1115 tt_len + ETH_HLEN);
1116 if (!skb)
1117 goto unlock;
1118
1119 skb_reserve(skb, ETH_HLEN);
1120 tt_response = (struct tt_query_packet *)skb_put(skb,
1121 sizeof(struct tt_query_packet) + tt_len);
1122 tt_response->ttvn = req_ttvn;
1123 tt_response->tt_data = htons(tt_tot);
1124
1125 tt_buff = skb->data + sizeof(struct tt_query_packet);
1126 /* Copy the last orig_node's OGM buffer */
1127 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1128 req_dst_orig_node->tt_buff_len);
1129
1130 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1131 } else {
1132 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1133 sizeof(struct tt_change);
1134 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1135
1136 skb = tt_response_fill_table(tt_len, ttvn,
1137 bat_priv->tt_global_hash,
1138 primary_if, tt_global_valid_entry,
1139 req_dst_orig_node);
1140 if (!skb)
1141 goto out;
1142
1143 tt_response = (struct tt_query_packet *)skb->data;
1144 }
1145
1146 tt_response->packet_type = BAT_TT_QUERY;
1147 tt_response->version = COMPAT_VERSION;
1148 tt_response->ttl = TTL;
1149 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1150 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1151 tt_response->flags = TT_RESPONSE;
1152
1153 if (full_table)
1154 tt_response->flags |= TT_FULL_TABLE;
1155
1156 bat_dbg(DBG_TT, bat_priv,
1157 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1158 res_dst_orig_node->orig, neigh_node->addr,
1159 req_dst_orig_node->orig, req_ttvn);
1160
1161 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1162 ret = true;
1163 goto out;
1164
1165unlock:
1166 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1167
1168out:
1169 if (res_dst_orig_node)
1170 orig_node_free_ref(res_dst_orig_node);
1171 if (req_dst_orig_node)
1172 orig_node_free_ref(req_dst_orig_node);
1173 if (neigh_node)
1174 neigh_node_free_ref(neigh_node);
1175 if (primary_if)
1176 hardif_free_ref(primary_if);
1177 if (!ret)
1178 kfree_skb(skb);
1179 return ret;
1180
1181}
1182static bool send_my_tt_response(struct bat_priv *bat_priv,
1183 struct tt_query_packet *tt_request)
1184{
1185 struct orig_node *orig_node = NULL;
1186 struct neigh_node *neigh_node = NULL;
1187 struct hard_iface *primary_if = NULL;
1188 uint8_t my_ttvn, req_ttvn, ttvn;
1189 int ret = false;
1190 unsigned char *tt_buff;
1191 bool full_table;
1192 uint16_t tt_len, tt_tot;
1193 struct sk_buff *skb = NULL;
1194 struct tt_query_packet *tt_response;
1195
1196 bat_dbg(DBG_TT, bat_priv,
1197 "Received TT_REQUEST from %pM for "
1198 "ttvn: %u (me) [%c]\n", tt_request->src,
1199 tt_request->ttvn,
1200 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1201
1202
1203 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1204 req_ttvn = tt_request->ttvn;
1205
1206 orig_node = get_orig_node(bat_priv, tt_request->src);
1207 if (!orig_node)
1208 goto out;
1209
1210 neigh_node = orig_node_get_router(orig_node);
1211 if (!neigh_node)
1212 goto out;
1213
1214 primary_if = primary_if_get_selected(bat_priv);
1215 if (!primary_if)
1216 goto out;
1217
1218 /* If the full table has been explicitly requested or the gap
1219 * is too big send the whole local translation table */
1220 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1221 !bat_priv->tt_buff)
1222 full_table = true;
1223 else
1224 full_table = false;
1225
1226 /* In this version, fragmentation is not implemented, then
1227 * I'll send only one packet with as much TT entries as I can */
1228 if (!full_table) {
1229 spin_lock_bh(&bat_priv->tt_buff_lock);
1230 tt_len = bat_priv->tt_buff_len;
1231 tt_tot = tt_len / sizeof(struct tt_change);
1232
1233 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1234 tt_len + ETH_HLEN);
1235 if (!skb)
1236 goto unlock;
1237
1238 skb_reserve(skb, ETH_HLEN);
1239 tt_response = (struct tt_query_packet *)skb_put(skb,
1240 sizeof(struct tt_query_packet) + tt_len);
1241 tt_response->ttvn = req_ttvn;
1242 tt_response->tt_data = htons(tt_tot);
1243
1244 tt_buff = skb->data + sizeof(struct tt_query_packet);
1245 memcpy(tt_buff, bat_priv->tt_buff,
1246 bat_priv->tt_buff_len);
1247 spin_unlock_bh(&bat_priv->tt_buff_lock);
1248 } else {
1249 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1250 sizeof(struct tt_change);
1251 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1252
1253 skb = tt_response_fill_table(tt_len, ttvn,
1254 bat_priv->tt_local_hash,
1255 primary_if, NULL, NULL);
1256 if (!skb)
1257 goto out;
1258
1259 tt_response = (struct tt_query_packet *)skb->data;
1260 }
1261
1262 tt_response->packet_type = BAT_TT_QUERY;
1263 tt_response->version = COMPAT_VERSION;
1264 tt_response->ttl = TTL;
1265 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1266 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1267 tt_response->flags = TT_RESPONSE;
1268
1269 if (full_table)
1270 tt_response->flags |= TT_FULL_TABLE;
1271
1272 bat_dbg(DBG_TT, bat_priv,
1273 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1274 orig_node->orig, neigh_node->addr,
1275 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1276
1277 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1278 ret = true;
1279 goto out;
1280
1281unlock:
1282 spin_unlock_bh(&bat_priv->tt_buff_lock);
1283out:
1284 if (orig_node)
1285 orig_node_free_ref(orig_node);
1286 if (neigh_node)
1287 neigh_node_free_ref(neigh_node);
1288 if (primary_if)
1289 hardif_free_ref(primary_if);
1290 if (!ret)
1291 kfree_skb(skb);
1292 /* This packet was for me, so it doesn't need to be re-routed */
1293 return true;
1294}
1295
1296bool send_tt_response(struct bat_priv *bat_priv,
1297 struct tt_query_packet *tt_request)
1298{
1299 if (is_my_mac(tt_request->dst))
1300 return send_my_tt_response(bat_priv, tt_request);
1301 else
1302 return send_other_tt_response(bat_priv, tt_request);
1303}
1304
1305static void _tt_update_changes(struct bat_priv *bat_priv,
1306 struct orig_node *orig_node,
1307 struct tt_change *tt_change,
1308 uint16_t tt_num_changes, uint8_t ttvn)
1309{
1310 int i;
1311
1312 for (i = 0; i < tt_num_changes; i++) {
1313 if ((tt_change + i)->flags & TT_CHANGE_DEL)
1314 tt_global_del(bat_priv, orig_node,
1315 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001316 "tt removed by changes",
1317 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001318 else
1319 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001320 (tt_change + i)->addr, ttvn, false))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001321 /* In case of problem while storing a
1322 * global_entry, we stop the updating
1323 * procedure without committing the
1324 * ttvn change. This will avoid to send
1325 * corrupted data on tt_request
1326 */
1327 return;
1328 }
1329}
1330
1331static void tt_fill_gtable(struct bat_priv *bat_priv,
1332 struct tt_query_packet *tt_response)
1333{
1334 struct orig_node *orig_node = NULL;
1335
1336 orig_node = orig_hash_find(bat_priv, tt_response->src);
1337 if (!orig_node)
1338 goto out;
1339
1340 /* Purge the old table first.. */
1341 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1342
1343 _tt_update_changes(bat_priv, orig_node,
1344 (struct tt_change *)(tt_response + 1),
1345 tt_response->tt_data, tt_response->ttvn);
1346
1347 spin_lock_bh(&orig_node->tt_buff_lock);
1348 kfree(orig_node->tt_buff);
1349 orig_node->tt_buff_len = 0;
1350 orig_node->tt_buff = NULL;
1351 spin_unlock_bh(&orig_node->tt_buff_lock);
1352
1353 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1354
1355out:
1356 if (orig_node)
1357 orig_node_free_ref(orig_node);
1358}
1359
1360void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1361 uint16_t tt_num_changes, uint8_t ttvn,
1362 struct tt_change *tt_change)
1363{
1364 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1365 ttvn);
1366
1367 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1368 tt_num_changes);
1369 atomic_set(&orig_node->last_ttvn, ttvn);
1370}
1371
1372bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1373{
1374 struct tt_local_entry *tt_local_entry;
1375
1376 spin_lock_bh(&bat_priv->tt_lhash_lock);
1377 tt_local_entry = tt_local_hash_find(bat_priv, addr);
1378 spin_unlock_bh(&bat_priv->tt_lhash_lock);
1379
1380 if (tt_local_entry)
1381 return true;
1382 return false;
1383}
1384
1385void handle_tt_response(struct bat_priv *bat_priv,
1386 struct tt_query_packet *tt_response)
1387{
1388 struct tt_req_node *node, *safe;
1389 struct orig_node *orig_node = NULL;
1390
1391 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1392 "ttvn %d t_size: %d [%c]\n",
1393 tt_response->src, tt_response->ttvn,
1394 tt_response->tt_data,
1395 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1396
1397 orig_node = orig_hash_find(bat_priv, tt_response->src);
1398 if (!orig_node)
1399 goto out;
1400
1401 if (tt_response->flags & TT_FULL_TABLE)
1402 tt_fill_gtable(bat_priv, tt_response);
1403 else
1404 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1405 tt_response->ttvn,
1406 (struct tt_change *)(tt_response + 1));
1407
1408 /* Delete the tt_req_node from pending tt_requests list */
1409 spin_lock_bh(&bat_priv->tt_req_list_lock);
1410 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1411 if (!compare_eth(node->addr, tt_response->src))
1412 continue;
1413 list_del(&node->list);
1414 kfree(node);
1415 }
1416 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1417
1418 /* Recalculate the CRC for this orig_node and store it */
1419 spin_lock_bh(&bat_priv->tt_ghash_lock);
1420 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1421 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001422 /* Roaming phase is over: tables are in sync again. I can
1423 * unset the flag */
1424 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001425out:
1426 if (orig_node)
1427 orig_node_free_ref(orig_node);
1428}
1429
1430int tt_init(struct bat_priv *bat_priv)
1431{
1432 if (!tt_local_init(bat_priv))
1433 return 0;
1434
1435 if (!tt_global_init(bat_priv))
1436 return 0;
1437
1438 tt_start_timer(bat_priv);
1439
1440 return 1;
1441}
1442
Antonio Quartullicc47f662011-04-27 14:27:57 +02001443static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001444{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001445 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446
Antonio Quartullicc47f662011-04-27 14:27:57 +02001447 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448
Antonio Quartullicc47f662011-04-27 14:27:57 +02001449 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1450 list_del(&node->list);
1451 kfree(node);
1452 }
1453
1454 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1455}
1456
1457static void tt_roam_purge(struct bat_priv *bat_priv)
1458{
1459 struct tt_roam_node *node, *safe;
1460
1461 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1462 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1463 if (!is_out_of_time(node->first_time,
1464 ROAMING_MAX_TIME * 1000))
1465 continue;
1466
1467 list_del(&node->list);
1468 kfree(node);
1469 }
1470 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1471}
1472
1473/* This function checks whether the client already reached the
1474 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1475 * will not be sent.
1476 *
1477 * returns true if the ROAMING_ADV can be sent, false otherwise */
1478static bool tt_check_roam_count(struct bat_priv *bat_priv,
1479 uint8_t *client)
1480{
1481 struct tt_roam_node *tt_roam_node;
1482 bool ret = false;
1483
1484 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1485 /* The new tt_req will be issued only if I'm not waiting for a
1486 * reply from the same orig_node yet */
1487 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1488 if (!compare_eth(tt_roam_node->addr, client))
1489 continue;
1490
1491 if (is_out_of_time(tt_roam_node->first_time,
1492 ROAMING_MAX_TIME * 1000))
1493 continue;
1494
1495 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1496 /* Sorry, you roamed too many times! */
1497 goto unlock;
1498 ret = true;
1499 break;
1500 }
1501
1502 if (!ret) {
1503 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1504 if (!tt_roam_node)
1505 goto unlock;
1506
1507 tt_roam_node->first_time = jiffies;
1508 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1509 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1510
1511 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1512 ret = true;
1513 }
1514
1515unlock:
1516 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1517 return ret;
1518}
1519
1520void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1521 struct orig_node *orig_node)
1522{
1523 struct neigh_node *neigh_node = NULL;
1524 struct sk_buff *skb = NULL;
1525 struct roam_adv_packet *roam_adv_packet;
1526 int ret = 1;
1527 struct hard_iface *primary_if;
1528
1529 /* before going on we have to check whether the client has
1530 * already roamed to us too many times */
1531 if (!tt_check_roam_count(bat_priv, client))
1532 goto out;
1533
1534 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1535 if (!skb)
1536 goto out;
1537
1538 skb_reserve(skb, ETH_HLEN);
1539
1540 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1541 sizeof(struct roam_adv_packet));
1542
1543 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1544 roam_adv_packet->version = COMPAT_VERSION;
1545 roam_adv_packet->ttl = TTL;
1546 primary_if = primary_if_get_selected(bat_priv);
1547 if (!primary_if)
1548 goto out;
1549 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1550 hardif_free_ref(primary_if);
1551 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1552 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1553
1554 neigh_node = orig_node_get_router(orig_node);
1555 if (!neigh_node)
1556 goto out;
1557
1558 bat_dbg(DBG_TT, bat_priv,
1559 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1560 orig_node->orig, client, neigh_node->addr);
1561
1562 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1563 ret = 0;
1564
1565out:
1566 if (neigh_node)
1567 neigh_node_free_ref(neigh_node);
1568 if (ret)
1569 kfree_skb(skb);
1570 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001571}
1572
1573static void tt_purge(struct work_struct *work)
1574{
1575 struct delayed_work *delayed_work =
1576 container_of(work, struct delayed_work, work);
1577 struct bat_priv *bat_priv =
1578 container_of(delayed_work, struct bat_priv, tt_work);
1579
1580 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001581 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001582 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001583 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001584
1585 tt_start_timer(bat_priv);
1586}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001587
1588void tt_free(struct bat_priv *bat_priv)
1589{
1590 cancel_delayed_work_sync(&bat_priv->tt_work);
1591
1592 tt_local_table_free(bat_priv);
1593 tt_global_table_free(bat_priv);
1594 tt_req_list_free(bat_priv);
1595 tt_changes_list_free(bat_priv);
1596 tt_roam_list_free(bat_priv);
1597
1598 kfree(bat_priv->tt_buff);
1599}