blob: 9648b0dc57ef959a2d13e7e6b2f5d8e027bc8669 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 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
Sven Eckelmannde7aae62012-02-05 18:55:22 +010033static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
34 struct orig_node *orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +020035static void tt_purge(struct work_struct *work);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +020036static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Marek Lindner7aadf882011-02-18 12:28:09 +000038/* returns 1 if they are the same mac addr */
Antonio Quartulli48100ba2011-10-30 12:17:33 +010039static int compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010041 const void *data1 = container_of(node, struct tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020042 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
Antonio Quartullia73105b2011-04-27 14:27:44 +020047static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048{
Antonio Quartullia73105b2011-04-27 14:27:44 +020049 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
50 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
51 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
Antonio Quartulli48100ba2011-10-30 12:17:33 +010054static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
55 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000056{
Marek Lindner7aadf882011-02-18 12:28:09 +000057 struct hlist_head *head;
58 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010059 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020060 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000061
62 if (!hash)
63 return NULL;
64
65 index = choose_orig(data, hash->size);
66 head = &hash->table[index];
67
68 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010069 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
70 if (!compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000071 continue;
72
Antonio Quartulli48100ba2011-10-30 12:17:33 +010073 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020074 continue;
75
Antonio Quartulli48100ba2011-10-30 12:17:33 +010076 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000077 break;
78 }
79 rcu_read_unlock();
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 return tt_common_entry_tmp;
82}
83
84static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
85 const void *data)
86{
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
89
90 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
91 if (tt_common_entry)
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000095}
96
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020098 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000099{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100103 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
104 if (tt_common_entry)
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Marek Lindner7aadf882011-02-18 12:28:09 +0000109}
110
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200111static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
112{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100113 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
114 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115}
116
Simon Wunderlich531027f2011-10-19 11:02:25 +0200117static void tt_global_entry_free_rcu(struct rcu_head *rcu)
118{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100119 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200120 struct tt_global_entry *tt_global_entry;
121
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100122 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
123 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
124 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126 kfree(tt_global_entry);
127}
128
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200129static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
130{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200131 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
132 tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100133 call_rcu(&tt_global_entry->common.rcu,
134 tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200135 }
136}
137
138static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
139{
140 struct tt_orig_list_entry *orig_entry;
141
142 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
143 atomic_dec(&orig_entry->orig_node->tt_size);
144 orig_node_free_ref(orig_entry->orig_node);
145 kfree(orig_entry);
146}
147
148static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
149{
150 call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200151}
152
Antonio Quartulliff66c972011-06-30 01:14:00 +0200153static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
154 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200155{
156 struct tt_change_node *tt_change_node;
157
158 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
159
160 if (!tt_change_node)
161 return;
162
Antonio Quartulliff66c972011-06-30 01:14:00 +0200163 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200164 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
165
166 spin_lock_bh(&bat_priv->tt_changes_list_lock);
167 /* track the change in the OGMinterval list */
168 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
169 atomic_inc(&bat_priv->tt_local_changes);
170 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
171
172 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
173}
174
175int tt_len(int changes_num)
176{
177 return changes_num * sizeof(struct tt_change);
178}
179
180static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200182 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 return 1;
184
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200185 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 return 0;
189
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 return 1;
191}
192
Antonio Quartullibc279082011-07-07 15:35:35 +0200193void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
194 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195{
196 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200197 struct tt_local_entry *tt_local_entry = NULL;
198 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200199 struct hlist_head *head;
200 struct hlist_node *node;
201 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100202 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200204 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200206 if (tt_local_entry) {
207 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200208 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 }
210
Sven Eckelmann704509b2011-05-14 23:14:54 +0200211 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200212 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200213 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200214
Antonio Quartullia73105b2011-04-27 14:27:44 +0200215 bat_dbg(DBG_TT, bat_priv,
216 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
217 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100219 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
220 tt_local_entry->common.flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200221 if (is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100222 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
223 atomic_set(&tt_local_entry->common.refcount, 2);
224 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
226 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000227 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100228 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100230 /* The local entry has to be marked as NEW to avoid to send it in
231 * a full table response going out before the next ttvn increment
232 * (consistency check) */
233 tt_local_entry->common.flags |= TT_CLIENT_NEW;
234
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100235 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
236 &tt_local_entry->common,
237 &tt_local_entry->common.hash_entry);
238
239 if (unlikely(hash_added != 0)) {
240 /* remove the reference for the hash */
241 tt_local_entry_free_ref(tt_local_entry);
242 goto out;
243 }
244
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100245 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200246
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200248 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Antonio Quartullicc47f662011-04-27 14:27:57 +0200250 /* Check whether it is a roaming! */
251 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200252 /* These node are probably going to update their tt table */
253 head = &tt_global_entry->orig_list;
254 rcu_read_lock();
255 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
256 orig_entry->orig_node->tt_poss_change = true;
257
258 send_roam_adv(bat_priv, tt_global_entry->common.addr,
259 orig_entry->orig_node);
260 }
261 rcu_read_unlock();
262 /* The global entry has to be marked as ROAMING and
263 * has to be kept for consistency purpose
264 */
David S. Miller220b07e2011-12-16 15:07:28 -0500265 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100266 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200267 }
268out:
269 if (tt_local_entry)
270 tt_local_entry_free_ref(tt_local_entry);
271 if (tt_global_entry)
272 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273}
274
Antonio Quartullia73105b2011-04-27 14:27:44 +0200275int tt_changes_fill_buffer(struct bat_priv *bat_priv,
276 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200278 int count = 0, tot_changes = 0;
279 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280
Antonio Quartullia73105b2011-04-27 14:27:44 +0200281 if (buff_len > 0)
282 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Antonio Quartullia73105b2011-04-27 14:27:44 +0200284 spin_lock_bh(&bat_priv->tt_changes_list_lock);
285 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286
Antonio Quartullia73105b2011-04-27 14:27:44 +0200287 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100288 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200289 if (count < tot_changes) {
290 memcpy(buff + tt_len(count),
291 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 count++;
293 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200294 list_del(&entry->list);
295 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200297 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298
Antonio Quartullia73105b2011-04-27 14:27:44 +0200299 /* Keep the buffer for possible tt_request */
300 spin_lock_bh(&bat_priv->tt_buff_lock);
301 kfree(bat_priv->tt_buff);
302 bat_priv->tt_buff_len = 0;
303 bat_priv->tt_buff = NULL;
304 /* We check whether this new OGM has no changes due to size
305 * problems */
306 if (buff_len > 0) {
307 /**
308 * if kmalloc() fails we will reply with the full table
309 * instead of providing the diff
310 */
311 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
312 if (bat_priv->tt_buff) {
313 memcpy(bat_priv->tt_buff, buff, buff_len);
314 bat_priv->tt_buff_len = buff_len;
315 }
316 }
317 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
Antonio Quartullia73105b2011-04-27 14:27:44 +0200319 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320}
321
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200322int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323{
324 struct net_device *net_dev = (struct net_device *)seq->private;
325 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200326 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100327 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200328 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000329 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200331 uint32_t i;
332 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
Marek Lindner32ae9b22011-04-20 15:40:58 +0200334 primary_if = primary_if_get_selected(bat_priv);
335 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100336 ret = seq_printf(seq,
337 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200338 net_dev->name);
339 goto out;
340 }
341
342 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100343 ret = seq_printf(seq,
344 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200345 net_dev->name);
346 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 }
348
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100349 seq_printf(seq,
350 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200351 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 for (i = 0; i < hash->size; i++) {
354 head = &hash->table[i];
355
Marek Lindner7aadf882011-02-18 12:28:09 +0000356 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100357 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000358 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200359 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100360 tt_common_entry->addr,
361 (tt_common_entry->flags &
362 TT_CLIENT_ROAM ? 'R' : '.'),
363 (tt_common_entry->flags &
364 TT_CLIENT_NOPURGE ? 'P' : '.'),
365 (tt_common_entry->flags &
366 TT_CLIENT_NEW ? 'N' : '.'),
367 (tt_common_entry->flags &
368 TT_CLIENT_PENDING ? 'X' : '.'),
369 (tt_common_entry->flags &
370 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000372 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200374out:
375 if (primary_if)
376 hardif_free_ref(primary_if);
377 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378}
379
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200380static void tt_local_set_pending(struct bat_priv *bat_priv,
381 struct tt_local_entry *tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100382 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100384 tt_local_event(bat_priv, tt_local_entry->common.addr,
385 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Antonio Quartulli015758d2011-07-09 17:52:13 +0200387 /* The local client has to be marked as "pending to be removed" but has
388 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200389 * response issued before the net ttvn increment (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100390 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100391
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100392 bat_dbg(DBG_TT, bat_priv,
393 "Local tt entry (%pM) pending to be removed: %s\n",
394 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395}
396
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200398 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200400 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200402 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200403 if (!tt_local_entry)
404 goto out;
405
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200406 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100407 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200408out:
409 if (tt_local_entry)
410 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411}
412
Antonio Quartullia73105b2011-04-27 14:27:44 +0200413static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200415 struct hashtable_t *hash = bat_priv->tt_local_hash;
416 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100417 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000418 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200420 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200421 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 for (i = 0; i < hash->size; i++) {
424 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200425 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200427 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100428 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000429 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100430 tt_local_entry = container_of(tt_common_entry,
431 struct tt_local_entry,
432 common);
433 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000434 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200436 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100437 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200438 continue;
439
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100440 if (!has_timed_out(tt_local_entry->last_seen,
Marek Lindner032b7962011-12-20 19:30:40 +0800441 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000442 continue;
443
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200444 tt_local_set_pending(bat_priv, tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100445 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200447 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 }
449
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450}
451
Antonio Quartullia73105b2011-04-27 14:27:44 +0200452static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200454 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200455 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100456 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200457 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200458 struct hlist_node *node, *node_tmp;
459 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200460 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200461
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200462 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 return;
464
Antonio Quartullia73105b2011-04-27 14:27:44 +0200465 hash = bat_priv->tt_local_hash;
466
467 for (i = 0; i < hash->size; i++) {
468 head = &hash->table[i];
469 list_lock = &hash->list_locks[i];
470
471 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100472 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200473 head, hash_entry) {
474 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100475 tt_local_entry = container_of(tt_common_entry,
476 struct tt_local_entry,
477 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200478 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200479 }
480 spin_unlock_bh(list_lock);
481 }
482
483 hash_destroy(hash);
484
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200485 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486}
487
Antonio Quartullia73105b2011-04-27 14:27:44 +0200488static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200490 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 return 1;
492
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200493 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200495 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496 return 0;
497
498 return 1;
499}
500
Antonio Quartullia73105b2011-04-27 14:27:44 +0200501static void tt_changes_list_free(struct bat_priv *bat_priv)
502{
503 struct tt_change_node *entry, *safe;
504
505 spin_lock_bh(&bat_priv->tt_changes_list_lock);
506
507 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
508 list) {
509 list_del(&entry->list);
510 kfree(entry);
511 }
512
513 atomic_set(&bat_priv->tt_local_changes, 0);
514 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
515}
516
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200517/* find out if an orig_node is already in the list of a tt_global_entry.
518 * returns 1 if found, 0 otherwise
519 */
520static bool tt_global_entry_has_orig(const struct tt_global_entry *entry,
521 const struct orig_node *orig_node)
522{
523 struct tt_orig_list_entry *tmp_orig_entry;
524 const struct hlist_head *head;
525 struct hlist_node *node;
526 bool found = false;
527
528 rcu_read_lock();
529 head = &entry->orig_list;
530 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
531 if (tmp_orig_entry->orig_node == orig_node) {
532 found = true;
533 break;
534 }
535 }
536 rcu_read_unlock();
537 return found;
538}
539
540static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
541 struct orig_node *orig_node,
542 int ttvn)
543{
544 struct tt_orig_list_entry *orig_entry;
545
546 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
547 if (!orig_entry)
548 return;
549
550 INIT_HLIST_NODE(&orig_entry->list);
551 atomic_inc(&orig_node->refcount);
552 atomic_inc(&orig_node->tt_size);
553 orig_entry->orig_node = orig_node;
554 orig_entry->ttvn = ttvn;
555
556 spin_lock_bh(&tt_global_entry->list_lock);
557 hlist_add_head_rcu(&orig_entry->list,
558 &tt_global_entry->orig_list);
559 spin_unlock_bh(&tt_global_entry->list_lock);
560}
561
Antonio Quartullia73105b2011-04-27 14:27:44 +0200562/* caller must hold orig_node refcount */
563int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200564 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
565 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200567 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200568 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100569 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
Antonio Quartullia73105b2011-04-27 14:27:44 +0200571 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572
Antonio Quartullia73105b2011-04-27 14:27:44 +0200573 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200574 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
575 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200576 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200577 goto out;
578
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100579 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200580
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100581 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200582 tt_global_entry->roam_at = 0;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200583 atomic_set(&tt_global_entry->common.refcount, 2);
584
585 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
586 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200587
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100588 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
589 choose_orig, &tt_global_entry->common,
590 &tt_global_entry->common.hash_entry);
591
592 if (unlikely(hash_added != 0)) {
593 /* remove the reference for the hash */
594 tt_global_entry_free_ref(tt_global_entry);
595 goto out_remove;
596 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200597
598 tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200599 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200600 /* there is already a global entry, use this one. */
601
602 /* If there is the TT_CLIENT_ROAM flag set, there is only one
603 * originator left in the list and we previously received a
604 * delete + roaming change for this originator.
605 *
606 * We should first delete the old originator before adding the
607 * new one.
608 */
609 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
610 tt_global_del_orig_list(tt_global_entry);
611 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
612 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200614
615 if (!tt_global_entry_has_orig(tt_global_entry, orig_node))
616 tt_global_add_orig_entry(tt_global_entry, orig_node,
617 ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200619
Antonio Quartullibc279082011-07-07 15:35:35 +0200620 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100621 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200622
Antonio Quartullia73105b2011-04-27 14:27:44 +0200623 bat_dbg(DBG_TT, bat_priv,
624 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100625 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200626
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100627out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100629 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200630 "global tt received", roaming);
631 ret = 1;
632out:
633 if (tt_global_entry)
634 tt_global_entry_free_ref(tt_global_entry);
635 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636}
637
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200638/* print all orig nodes who announce the address for this global entry.
639 * it is assumed that the caller holds rcu_read_lock();
640 */
641static void tt_global_print_entry(struct tt_global_entry *tt_global_entry,
642 struct seq_file *seq)
643{
644 struct hlist_head *head;
645 struct hlist_node *node;
646 struct tt_orig_list_entry *orig_entry;
647 struct tt_common_entry *tt_common_entry;
648 uint16_t flags;
649 uint8_t last_ttvn;
650
651 tt_common_entry = &tt_global_entry->common;
652
653 head = &tt_global_entry->orig_list;
654
655 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
656 flags = tt_common_entry->flags;
657 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
658 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
659 tt_global_entry->common.addr, orig_entry->ttvn,
660 orig_entry->orig_node->orig, last_ttvn,
661 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
662 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
663 }
664}
665
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200666int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667{
668 struct net_device *net_dev = (struct net_device *)seq->private;
669 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200670 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100671 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200672 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200673 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000674 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200676 uint32_t i;
677 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678
Marek Lindner32ae9b22011-04-20 15:40:58 +0200679 primary_if = primary_if_get_selected(bat_priv);
680 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100681 ret = seq_printf(seq,
682 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200683 net_dev->name);
684 goto out;
685 }
686
687 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100688 ret = seq_printf(seq,
689 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200690 net_dev->name);
691 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692 }
693
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200694 seq_printf(seq,
695 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200697 seq_printf(seq, " %-13s %s %-15s %s %s\n",
698 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 for (i = 0; i < hash->size; i++) {
701 head = &hash->table[i];
702
Marek Lindner7aadf882011-02-18 12:28:09 +0000703 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100704 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000705 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100706 tt_global_entry = container_of(tt_common_entry,
707 struct tt_global_entry,
708 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200709 tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000711 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200713out:
714 if (primary_if)
715 hardif_free_ref(primary_if);
716 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717}
718
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200719/* deletes the orig list of a tt_global_entry */
720static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200722 struct hlist_head *head;
723 struct hlist_node *node, *safe;
724 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200725
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200726 spin_lock_bh(&tt_global_entry->list_lock);
727 head = &tt_global_entry->orig_list;
728 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
729 hlist_del_rcu(node);
730 tt_orig_list_entry_free_ref(orig_entry);
731 }
732 spin_unlock_bh(&tt_global_entry->list_lock);
733
734}
735
736static void tt_global_del_orig_entry(struct bat_priv *bat_priv,
737 struct tt_global_entry *tt_global_entry,
738 struct orig_node *orig_node,
739 const char *message)
740{
741 struct hlist_head *head;
742 struct hlist_node *node, *safe;
743 struct tt_orig_list_entry *orig_entry;
744
745 spin_lock_bh(&tt_global_entry->list_lock);
746 head = &tt_global_entry->orig_list;
747 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
748 if (orig_entry->orig_node == orig_node) {
749 bat_dbg(DBG_TT, bat_priv,
750 "Deleting %pM from global tt entry %pM: %s\n",
751 orig_node->orig, tt_global_entry->common.addr,
752 message);
753 hlist_del_rcu(node);
754 tt_orig_list_entry_free_ref(orig_entry);
755 }
756 }
757 spin_unlock_bh(&tt_global_entry->list_lock);
758}
759
760static void tt_global_del_struct(struct bat_priv *bat_priv,
761 struct tt_global_entry *tt_global_entry,
762 const char *message)
763{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200764 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200765 "Deleting global tt entry %pM: %s\n",
766 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200767
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100768 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
769 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200770 tt_global_entry_free_ref(tt_global_entry);
771
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772}
773
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200774/* If the client is to be deleted, we check if it is the last origantor entry
775 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
776 * otherwise we simply remove the originator scheduled for deletion.
777 */
778static void tt_global_del_roaming(struct bat_priv *bat_priv,
779 struct tt_global_entry *tt_global_entry,
780 struct orig_node *orig_node,
781 const char *message)
782{
783 bool last_entry = true;
784 struct hlist_head *head;
785 struct hlist_node *node;
786 struct tt_orig_list_entry *orig_entry;
787
788 /* no local entry exists, case 1:
789 * Check if this is the last one or if other entries exist.
790 */
791
792 rcu_read_lock();
793 head = &tt_global_entry->orig_list;
794 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
795 if (orig_entry->orig_node != orig_node) {
796 last_entry = false;
797 break;
798 }
799 }
800 rcu_read_unlock();
801
802 if (last_entry) {
803 /* its the last one, mark for roaming. */
804 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
805 tt_global_entry->roam_at = jiffies;
806 } else
807 /* there is another entry, we can simply delete this
808 * one and can still use the other one.
809 */
810 tt_global_del_orig_entry(bat_priv, tt_global_entry,
811 orig_node, message);
812}
813
814
815
Sven Eckelmannde7aae62012-02-05 18:55:22 +0100816static void tt_global_del(struct bat_priv *bat_priv,
817 struct orig_node *orig_node,
818 const unsigned char *addr,
819 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200821 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100822 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
Antonio Quartullia73105b2011-04-27 14:27:44 +0200824 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200826 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200827
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200828 if (!roaming) {
829 tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node,
830 message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800831
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200832 if (hlist_empty(&tt_global_entry->orig_list))
833 tt_global_del_struct(bat_priv, tt_global_entry,
834 message);
835
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800836 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200837 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800838
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839 /* if we are deleting a global entry due to a roam
840 * event, there are two possibilities:
841 * 1) the client roamed from node A to node B => if there
842 * is only one originator left for this client, we mark
843 * it with TT_CLIENT_ROAM, we start a timer and we
844 * wait for node B to claim it. In case of timeout
845 * the entry is purged.
846 *
847 * If there are other originators left, we directly delete
848 * the originator.
849 * 2) the client roamed to us => we can directly delete
850 * the global entry, since it is useless now. */
851
852 tt_local_entry = tt_local_hash_find(bat_priv,
853 tt_global_entry->common.addr);
854 if (tt_local_entry) {
855 /* local entry exists, case 2: client roamed to us. */
856 tt_global_del_orig_list(tt_global_entry);
857 tt_global_del_struct(bat_priv, tt_global_entry, message);
858 } else
859 /* no local entry exists, case 1: check for roaming */
860 tt_global_del_roaming(bat_priv, tt_global_entry, orig_node,
861 message);
862
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800863
Antonio Quartullicc47f662011-04-27 14:27:57 +0200864out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200865 if (tt_global_entry)
866 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100867 if (tt_local_entry)
868 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200869}
870
871void tt_global_del_orig(struct bat_priv *bat_priv,
872 struct orig_node *orig_node, const char *message)
873{
874 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100875 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200876 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200877 struct hashtable_t *hash = bat_priv->tt_global_hash;
878 struct hlist_node *node, *safe;
879 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200880 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200881
Simon Wunderlich6e801492011-10-19 10:28:26 +0200882 if (!hash)
883 return;
884
Antonio Quartullia73105b2011-04-27 14:27:44 +0200885 for (i = 0; i < hash->size; i++) {
886 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200887 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000888
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200889 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100890 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100891 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100892 tt_global_entry = container_of(tt_common_entry,
893 struct tt_global_entry,
894 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200895
896 tt_global_del_orig_entry(bat_priv, tt_global_entry,
897 orig_node, message);
898
899 if (hlist_empty(&tt_global_entry->orig_list)) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200900 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200901 "Deleting global tt entry %pM: %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100902 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200903 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200904 hlist_del_rcu(node);
905 tt_global_entry_free_ref(tt_global_entry);
906 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200908 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000909 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200910 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100911 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912}
913
Antonio Quartullicc47f662011-04-27 14:27:57 +0200914static void tt_global_roam_purge(struct bat_priv *bat_priv)
915{
916 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100917 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200918 struct tt_global_entry *tt_global_entry;
919 struct hlist_node *node, *node_tmp;
920 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200921 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200922 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200923
Antonio Quartullicc47f662011-04-27 14:27:57 +0200924 for (i = 0; i < hash->size; i++) {
925 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200926 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200927
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200928 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100929 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200930 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100931 tt_global_entry = container_of(tt_common_entry,
932 struct tt_global_entry,
933 common);
934 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200935 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100936 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800937 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200938 continue;
939
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100940 bat_dbg(DBG_TT, bat_priv,
941 "Deleting global tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100942 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200943
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200944 hlist_del_rcu(node);
945 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200946 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200947 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200948 }
949
Antonio Quartullicc47f662011-04-27 14:27:57 +0200950}
951
Antonio Quartullia73105b2011-04-27 14:27:44 +0200952static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000953{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200954 struct hashtable_t *hash;
955 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100956 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200957 struct tt_global_entry *tt_global_entry;
958 struct hlist_node *node, *node_tmp;
959 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200960 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200961
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200962 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963 return;
964
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200965 hash = bat_priv->tt_global_hash;
966
967 for (i = 0; i < hash->size; i++) {
968 head = &hash->table[i];
969 list_lock = &hash->list_locks[i];
970
971 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100972 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200973 head, hash_entry) {
974 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100975 tt_global_entry = container_of(tt_common_entry,
976 struct tt_global_entry,
977 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200978 tt_global_entry_free_ref(tt_global_entry);
979 }
980 spin_unlock_bh(list_lock);
981 }
982
983 hash_destroy(hash);
984
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200985 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000986}
987
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200988static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
989 struct tt_global_entry *tt_global_entry)
990{
991 bool ret = false;
992
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100993 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
994 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200995 ret = true;
996
997 return ret;
998}
999
Sven Eckelmann747e4222011-05-14 23:14:50 +02001000struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001001 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001002{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001003 struct tt_local_entry *tt_local_entry = NULL;
1004 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001005 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001006 struct neigh_node *router = NULL;
1007 struct hlist_head *head;
1008 struct hlist_node *node;
1009 struct tt_orig_list_entry *orig_entry;
1010 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001012 if (src && atomic_read(&bat_priv->ap_isolation)) {
1013 tt_local_entry = tt_local_hash_find(bat_priv, src);
1014 if (!tt_local_entry)
1015 goto out;
1016 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001017
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001018 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001019 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001020 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001022 /* check whether the clients should not communicate due to AP
1023 * isolation */
1024 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
1025 goto out;
1026
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001027 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001028
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001029 rcu_read_lock();
1030 head = &tt_global_entry->orig_list;
1031 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1032 router = orig_node_get_router(orig_entry->orig_node);
1033 if (!router)
1034 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001035
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001036 if (router->tq_avg > best_tq) {
1037 orig_node = orig_entry->orig_node;
1038 best_tq = router->tq_avg;
1039 }
1040 neigh_node_free_ref(router);
1041 }
1042 /* found anything? */
1043 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1044 orig_node = NULL;
1045 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001046out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001047 if (tt_global_entry)
1048 tt_global_entry_free_ref(tt_global_entry);
1049 if (tt_local_entry)
1050 tt_local_entry_free_ref(tt_local_entry);
1051
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001052 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001054
1055/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001056static uint16_t tt_global_crc(struct bat_priv *bat_priv,
1057 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001058{
1059 uint16_t total = 0, total_one;
1060 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001061 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001062 struct tt_global_entry *tt_global_entry;
1063 struct hlist_node *node;
1064 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001065 uint32_t i;
1066 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001067
1068 for (i = 0; i < hash->size; i++) {
1069 head = &hash->table[i];
1070
1071 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001072 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001073 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001074 tt_global_entry = container_of(tt_common_entry,
1075 struct tt_global_entry,
1076 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001077 /* Roaming clients are in the global table for
1078 * consistency only. They don't have to be
1079 * taken into account while computing the
1080 * global crc
1081 */
1082 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1083 continue;
1084
1085 /* find out if this global entry is announced by this
1086 * originator
1087 */
1088 if (!tt_global_entry_has_orig(tt_global_entry,
1089 orig_node))
1090 continue;
1091
1092 total_one = 0;
1093 for (j = 0; j < ETH_ALEN; j++)
1094 total_one = crc16_byte(total_one,
1095 tt_global_entry->common.addr[j]);
1096 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001097 }
1098 rcu_read_unlock();
1099 }
1100
1101 return total;
1102}
1103
1104/* Calculates the checksum of the local table */
1105uint16_t tt_local_crc(struct bat_priv *bat_priv)
1106{
1107 uint16_t total = 0, total_one;
1108 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001109 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001110 struct hlist_node *node;
1111 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001112 uint32_t i;
1113 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001114
1115 for (i = 0; i < hash->size; i++) {
1116 head = &hash->table[i];
1117
1118 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001119 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001120 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001121 /* not yet committed clients have not to be taken into
1122 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001123 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001124 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001125 total_one = 0;
1126 for (j = 0; j < ETH_ALEN; j++)
1127 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001128 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001129 total ^= total_one;
1130 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001131 rcu_read_unlock();
1132 }
1133
1134 return total;
1135}
1136
1137static void tt_req_list_free(struct bat_priv *bat_priv)
1138{
1139 struct tt_req_node *node, *safe;
1140
1141 spin_lock_bh(&bat_priv->tt_req_list_lock);
1142
1143 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1144 list_del(&node->list);
1145 kfree(node);
1146 }
1147
1148 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1149}
1150
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001151static void tt_save_orig_buffer(struct bat_priv *bat_priv,
1152 struct orig_node *orig_node,
1153 const unsigned char *tt_buff,
1154 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001155{
1156 uint16_t tt_buff_len = tt_len(tt_num_changes);
1157
1158 /* Replace the old buffer only if I received something in the
1159 * last OGM (the OGM could carry no changes) */
1160 spin_lock_bh(&orig_node->tt_buff_lock);
1161 if (tt_buff_len > 0) {
1162 kfree(orig_node->tt_buff);
1163 orig_node->tt_buff_len = 0;
1164 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1165 if (orig_node->tt_buff) {
1166 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1167 orig_node->tt_buff_len = tt_buff_len;
1168 }
1169 }
1170 spin_unlock_bh(&orig_node->tt_buff_lock);
1171}
1172
1173static void tt_req_purge(struct bat_priv *bat_priv)
1174{
1175 struct tt_req_node *node, *safe;
1176
1177 spin_lock_bh(&bat_priv->tt_req_list_lock);
1178 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001179 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001180 list_del(&node->list);
1181 kfree(node);
1182 }
1183 }
1184 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1185}
1186
1187/* returns the pointer to the new tt_req_node struct if no request
1188 * has already been issued for this orig_node, NULL otherwise */
1189static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
1190 struct orig_node *orig_node)
1191{
1192 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1193
1194 spin_lock_bh(&bat_priv->tt_req_list_lock);
1195 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1196 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +01001197 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +08001198 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001199 goto unlock;
1200 }
1201
1202 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1203 if (!tt_req_node)
1204 goto unlock;
1205
1206 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1207 tt_req_node->issued_at = jiffies;
1208
1209 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1210unlock:
1211 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1212 return tt_req_node;
1213}
1214
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001215/* data_ptr is useless here, but has to be kept to respect the prototype */
1216static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1217{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001218 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001219
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001220 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001221 return 0;
1222 return 1;
1223}
1224
Antonio Quartullia73105b2011-04-27 14:27:44 +02001225static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1226{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001227 const struct tt_common_entry *tt_common_entry = entry_ptr;
1228 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001229 const struct orig_node *orig_node = data_ptr;
1230
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001231 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001232 return 0;
1233
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001234 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1235 common);
1236
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001237 return tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001238}
1239
1240static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1241 struct hashtable_t *hash,
1242 struct hard_iface *primary_if,
1243 int (*valid_cb)(const void *,
1244 const void *),
1245 void *cb_data)
1246{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001247 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001248 struct tt_query_packet *tt_response;
1249 struct tt_change *tt_change;
1250 struct hlist_node *node;
1251 struct hlist_head *head;
1252 struct sk_buff *skb = NULL;
1253 uint16_t tt_tot, tt_count;
1254 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001255 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001256
1257 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1258 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1259 tt_len -= tt_len % sizeof(struct tt_change);
1260 }
1261 tt_tot = tt_len / sizeof(struct tt_change);
1262
1263 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1264 if (!skb)
1265 goto out;
1266
1267 skb_reserve(skb, ETH_HLEN);
1268 tt_response = (struct tt_query_packet *)skb_put(skb,
1269 tt_query_size + tt_len);
1270 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001271
1272 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1273 tt_count = 0;
1274
1275 rcu_read_lock();
1276 for (i = 0; i < hash->size; i++) {
1277 head = &hash->table[i];
1278
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001279 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001280 head, hash_entry) {
1281 if (tt_count == tt_tot)
1282 break;
1283
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001284 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001285 continue;
1286
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001287 memcpy(tt_change->addr, tt_common_entry->addr,
1288 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001289 tt_change->flags = NO_FLAGS;
1290
1291 tt_count++;
1292 tt_change++;
1293 }
1294 }
1295 rcu_read_unlock();
1296
Antonio Quartulli9d852392011-10-17 14:25:13 +02001297 /* store in the message the number of entries we have successfully
1298 * copied */
1299 tt_response->tt_data = htons(tt_count);
1300
Antonio Quartullia73105b2011-04-27 14:27:44 +02001301out:
1302 return skb;
1303}
1304
Marek Lindnera943cac2011-07-30 13:10:18 +02001305static int send_tt_request(struct bat_priv *bat_priv,
1306 struct orig_node *dst_orig_node,
1307 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001308{
1309 struct sk_buff *skb = NULL;
1310 struct tt_query_packet *tt_request;
1311 struct neigh_node *neigh_node = NULL;
1312 struct hard_iface *primary_if;
1313 struct tt_req_node *tt_req_node = NULL;
1314 int ret = 1;
1315
1316 primary_if = primary_if_get_selected(bat_priv);
1317 if (!primary_if)
1318 goto out;
1319
1320 /* The new tt_req will be issued only if I'm not waiting for a
1321 * reply from the same orig_node yet */
1322 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1323 if (!tt_req_node)
1324 goto out;
1325
1326 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1327 if (!skb)
1328 goto out;
1329
1330 skb_reserve(skb, ETH_HLEN);
1331
1332 tt_request = (struct tt_query_packet *)skb_put(skb,
1333 sizeof(struct tt_query_packet));
1334
Sven Eckelmann76543d12011-11-20 15:47:38 +01001335 tt_request->header.packet_type = BAT_TT_QUERY;
1336 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001337 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1338 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001339 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340 tt_request->ttvn = ttvn;
1341 tt_request->tt_data = tt_crc;
1342 tt_request->flags = TT_REQUEST;
1343
1344 if (full_table)
1345 tt_request->flags |= TT_FULL_TABLE;
1346
1347 neigh_node = orig_node_get_router(dst_orig_node);
1348 if (!neigh_node)
1349 goto out;
1350
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001351 bat_dbg(DBG_TT, bat_priv,
1352 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1353 dst_orig_node->orig, neigh_node->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001354 (full_table ? 'F' : '.'));
1355
1356 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1357 ret = 0;
1358
1359out:
1360 if (neigh_node)
1361 neigh_node_free_ref(neigh_node);
1362 if (primary_if)
1363 hardif_free_ref(primary_if);
1364 if (ret)
1365 kfree_skb(skb);
1366 if (ret && tt_req_node) {
1367 spin_lock_bh(&bat_priv->tt_req_list_lock);
1368 list_del(&tt_req_node->list);
1369 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1370 kfree(tt_req_node);
1371 }
1372 return ret;
1373}
1374
1375static bool send_other_tt_response(struct bat_priv *bat_priv,
1376 struct tt_query_packet *tt_request)
1377{
1378 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1379 struct neigh_node *neigh_node = NULL;
1380 struct hard_iface *primary_if = NULL;
1381 uint8_t orig_ttvn, req_ttvn, ttvn;
1382 int ret = false;
1383 unsigned char *tt_buff;
1384 bool full_table;
1385 uint16_t tt_len, tt_tot;
1386 struct sk_buff *skb = NULL;
1387 struct tt_query_packet *tt_response;
1388
1389 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001390 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1391 tt_request->src, tt_request->ttvn, tt_request->dst,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001392 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1393
1394 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001395 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396 if (!req_dst_orig_node)
1397 goto out;
1398
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001399 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 if (!res_dst_orig_node)
1401 goto out;
1402
1403 neigh_node = orig_node_get_router(res_dst_orig_node);
1404 if (!neigh_node)
1405 goto out;
1406
1407 primary_if = primary_if_get_selected(bat_priv);
1408 if (!primary_if)
1409 goto out;
1410
1411 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1412 req_ttvn = tt_request->ttvn;
1413
Antonio Quartulli015758d2011-07-09 17:52:13 +02001414 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415 if (orig_ttvn != req_ttvn ||
1416 tt_request->tt_data != req_dst_orig_node->tt_crc)
1417 goto out;
1418
Antonio Quartulli015758d2011-07-09 17:52:13 +02001419 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001420 if (tt_request->flags & TT_FULL_TABLE ||
1421 !req_dst_orig_node->tt_buff)
1422 full_table = true;
1423 else
1424 full_table = false;
1425
1426 /* In this version, fragmentation is not implemented, then
1427 * I'll send only one packet with as much TT entries as I can */
1428 if (!full_table) {
1429 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1430 tt_len = req_dst_orig_node->tt_buff_len;
1431 tt_tot = tt_len / sizeof(struct tt_change);
1432
1433 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1434 tt_len + ETH_HLEN);
1435 if (!skb)
1436 goto unlock;
1437
1438 skb_reserve(skb, ETH_HLEN);
1439 tt_response = (struct tt_query_packet *)skb_put(skb,
1440 sizeof(struct tt_query_packet) + tt_len);
1441 tt_response->ttvn = req_ttvn;
1442 tt_response->tt_data = htons(tt_tot);
1443
1444 tt_buff = skb->data + sizeof(struct tt_query_packet);
1445 /* Copy the last orig_node's OGM buffer */
1446 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1447 req_dst_orig_node->tt_buff_len);
1448
1449 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1450 } else {
1451 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1452 sizeof(struct tt_change);
1453 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1454
1455 skb = tt_response_fill_table(tt_len, ttvn,
1456 bat_priv->tt_global_hash,
1457 primary_if, tt_global_valid_entry,
1458 req_dst_orig_node);
1459 if (!skb)
1460 goto out;
1461
1462 tt_response = (struct tt_query_packet *)skb->data;
1463 }
1464
Sven Eckelmann76543d12011-11-20 15:47:38 +01001465 tt_response->header.packet_type = BAT_TT_QUERY;
1466 tt_response->header.version = COMPAT_VERSION;
1467 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1469 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1470 tt_response->flags = TT_RESPONSE;
1471
1472 if (full_table)
1473 tt_response->flags |= TT_FULL_TABLE;
1474
1475 bat_dbg(DBG_TT, bat_priv,
1476 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1477 res_dst_orig_node->orig, neigh_node->addr,
1478 req_dst_orig_node->orig, req_ttvn);
1479
1480 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1481 ret = true;
1482 goto out;
1483
1484unlock:
1485 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1486
1487out:
1488 if (res_dst_orig_node)
1489 orig_node_free_ref(res_dst_orig_node);
1490 if (req_dst_orig_node)
1491 orig_node_free_ref(req_dst_orig_node);
1492 if (neigh_node)
1493 neigh_node_free_ref(neigh_node);
1494 if (primary_if)
1495 hardif_free_ref(primary_if);
1496 if (!ret)
1497 kfree_skb(skb);
1498 return ret;
1499
1500}
1501static bool send_my_tt_response(struct bat_priv *bat_priv,
1502 struct tt_query_packet *tt_request)
1503{
1504 struct orig_node *orig_node = NULL;
1505 struct neigh_node *neigh_node = NULL;
1506 struct hard_iface *primary_if = NULL;
1507 uint8_t my_ttvn, req_ttvn, ttvn;
1508 int ret = false;
1509 unsigned char *tt_buff;
1510 bool full_table;
1511 uint16_t tt_len, tt_tot;
1512 struct sk_buff *skb = NULL;
1513 struct tt_query_packet *tt_response;
1514
1515 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001516 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1517 tt_request->src, tt_request->ttvn,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1519
1520
1521 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1522 req_ttvn = tt_request->ttvn;
1523
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001524 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001525 if (!orig_node)
1526 goto out;
1527
1528 neigh_node = orig_node_get_router(orig_node);
1529 if (!neigh_node)
1530 goto out;
1531
1532 primary_if = primary_if_get_selected(bat_priv);
1533 if (!primary_if)
1534 goto out;
1535
1536 /* If the full table has been explicitly requested or the gap
1537 * is too big send the whole local translation table */
1538 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1539 !bat_priv->tt_buff)
1540 full_table = true;
1541 else
1542 full_table = false;
1543
1544 /* In this version, fragmentation is not implemented, then
1545 * I'll send only one packet with as much TT entries as I can */
1546 if (!full_table) {
1547 spin_lock_bh(&bat_priv->tt_buff_lock);
1548 tt_len = bat_priv->tt_buff_len;
1549 tt_tot = tt_len / sizeof(struct tt_change);
1550
1551 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1552 tt_len + ETH_HLEN);
1553 if (!skb)
1554 goto unlock;
1555
1556 skb_reserve(skb, ETH_HLEN);
1557 tt_response = (struct tt_query_packet *)skb_put(skb,
1558 sizeof(struct tt_query_packet) + tt_len);
1559 tt_response->ttvn = req_ttvn;
1560 tt_response->tt_data = htons(tt_tot);
1561
1562 tt_buff = skb->data + sizeof(struct tt_query_packet);
1563 memcpy(tt_buff, bat_priv->tt_buff,
1564 bat_priv->tt_buff_len);
1565 spin_unlock_bh(&bat_priv->tt_buff_lock);
1566 } else {
1567 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1568 sizeof(struct tt_change);
1569 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1570
1571 skb = tt_response_fill_table(tt_len, ttvn,
1572 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001573 primary_if, tt_local_valid_entry,
1574 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575 if (!skb)
1576 goto out;
1577
1578 tt_response = (struct tt_query_packet *)skb->data;
1579 }
1580
Sven Eckelmann76543d12011-11-20 15:47:38 +01001581 tt_response->header.packet_type = BAT_TT_QUERY;
1582 tt_response->header.version = COMPAT_VERSION;
1583 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001584 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1585 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1586 tt_response->flags = TT_RESPONSE;
1587
1588 if (full_table)
1589 tt_response->flags |= TT_FULL_TABLE;
1590
1591 bat_dbg(DBG_TT, bat_priv,
1592 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1593 orig_node->orig, neigh_node->addr,
1594 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1595
1596 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1597 ret = true;
1598 goto out;
1599
1600unlock:
1601 spin_unlock_bh(&bat_priv->tt_buff_lock);
1602out:
1603 if (orig_node)
1604 orig_node_free_ref(orig_node);
1605 if (neigh_node)
1606 neigh_node_free_ref(neigh_node);
1607 if (primary_if)
1608 hardif_free_ref(primary_if);
1609 if (!ret)
1610 kfree_skb(skb);
1611 /* This packet was for me, so it doesn't need to be re-routed */
1612 return true;
1613}
1614
1615bool send_tt_response(struct bat_priv *bat_priv,
1616 struct tt_query_packet *tt_request)
1617{
1618 if (is_my_mac(tt_request->dst))
1619 return send_my_tt_response(bat_priv, tt_request);
1620 else
1621 return send_other_tt_response(bat_priv, tt_request);
1622}
1623
1624static void _tt_update_changes(struct bat_priv *bat_priv,
1625 struct orig_node *orig_node,
1626 struct tt_change *tt_change,
1627 uint16_t tt_num_changes, uint8_t ttvn)
1628{
1629 int i;
1630
1631 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001632 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001633 tt_global_del(bat_priv, orig_node,
1634 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001635 "tt removed by changes",
1636 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637 else
1638 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001639 (tt_change + i)->addr, ttvn, false,
1640 (tt_change + i)->flags &
1641 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642 /* In case of problem while storing a
1643 * global_entry, we stop the updating
1644 * procedure without committing the
1645 * ttvn change. This will avoid to send
1646 * corrupted data on tt_request
1647 */
1648 return;
1649 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001650 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651}
1652
1653static void tt_fill_gtable(struct bat_priv *bat_priv,
1654 struct tt_query_packet *tt_response)
1655{
1656 struct orig_node *orig_node = NULL;
1657
1658 orig_node = orig_hash_find(bat_priv, tt_response->src);
1659 if (!orig_node)
1660 goto out;
1661
1662 /* Purge the old table first.. */
1663 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1664
1665 _tt_update_changes(bat_priv, orig_node,
1666 (struct tt_change *)(tt_response + 1),
1667 tt_response->tt_data, tt_response->ttvn);
1668
1669 spin_lock_bh(&orig_node->tt_buff_lock);
1670 kfree(orig_node->tt_buff);
1671 orig_node->tt_buff_len = 0;
1672 orig_node->tt_buff = NULL;
1673 spin_unlock_bh(&orig_node->tt_buff_lock);
1674
1675 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1676
1677out:
1678 if (orig_node)
1679 orig_node_free_ref(orig_node);
1680}
1681
Marek Lindnera943cac2011-07-30 13:10:18 +02001682static void tt_update_changes(struct bat_priv *bat_priv,
1683 struct orig_node *orig_node,
1684 uint16_t tt_num_changes, uint8_t ttvn,
1685 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686{
1687 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1688 ttvn);
1689
1690 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1691 tt_num_changes);
1692 atomic_set(&orig_node->last_ttvn, ttvn);
1693}
1694
1695bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1696{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001697 struct tt_local_entry *tt_local_entry = NULL;
1698 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001699
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001701 if (!tt_local_entry)
1702 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001703 /* Check if the client has been logically deleted (but is kept for
1704 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001705 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001706 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001707 ret = true;
1708out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001709 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001710 tt_local_entry_free_ref(tt_local_entry);
1711 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712}
1713
1714void handle_tt_response(struct bat_priv *bat_priv,
1715 struct tt_query_packet *tt_response)
1716{
1717 struct tt_req_node *node, *safe;
1718 struct orig_node *orig_node = NULL;
1719
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001720 bat_dbg(DBG_TT, bat_priv,
1721 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1722 tt_response->src, tt_response->ttvn, tt_response->tt_data,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001723 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1724
1725 orig_node = orig_hash_find(bat_priv, tt_response->src);
1726 if (!orig_node)
1727 goto out;
1728
1729 if (tt_response->flags & TT_FULL_TABLE)
1730 tt_fill_gtable(bat_priv, tt_response);
1731 else
1732 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1733 tt_response->ttvn,
1734 (struct tt_change *)(tt_response + 1));
1735
1736 /* Delete the tt_req_node from pending tt_requests list */
1737 spin_lock_bh(&bat_priv->tt_req_list_lock);
1738 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1739 if (!compare_eth(node->addr, tt_response->src))
1740 continue;
1741 list_del(&node->list);
1742 kfree(node);
1743 }
1744 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1745
1746 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001748 /* Roaming phase is over: tables are in sync again. I can
1749 * unset the flag */
1750 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751out:
1752 if (orig_node)
1753 orig_node_free_ref(orig_node);
1754}
1755
1756int tt_init(struct bat_priv *bat_priv)
1757{
1758 if (!tt_local_init(bat_priv))
1759 return 0;
1760
1761 if (!tt_global_init(bat_priv))
1762 return 0;
1763
1764 tt_start_timer(bat_priv);
1765
1766 return 1;
1767}
1768
Antonio Quartullicc47f662011-04-27 14:27:57 +02001769static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001770{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001771 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772
Antonio Quartullicc47f662011-04-27 14:27:57 +02001773 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774
Antonio Quartullicc47f662011-04-27 14:27:57 +02001775 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1776 list_del(&node->list);
1777 kfree(node);
1778 }
1779
1780 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1781}
1782
1783static void tt_roam_purge(struct bat_priv *bat_priv)
1784{
1785 struct tt_roam_node *node, *safe;
1786
1787 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1788 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001789 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001790 continue;
1791
1792 list_del(&node->list);
1793 kfree(node);
1794 }
1795 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1796}
1797
1798/* This function checks whether the client already reached the
1799 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1800 * will not be sent.
1801 *
1802 * returns true if the ROAMING_ADV can be sent, false otherwise */
1803static bool tt_check_roam_count(struct bat_priv *bat_priv,
1804 uint8_t *client)
1805{
1806 struct tt_roam_node *tt_roam_node;
1807 bool ret = false;
1808
1809 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1810 /* The new tt_req will be issued only if I'm not waiting for a
1811 * reply from the same orig_node yet */
1812 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1813 if (!compare_eth(tt_roam_node->addr, client))
1814 continue;
1815
Marek Lindner032b7962011-12-20 19:30:40 +08001816 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001817 continue;
1818
1819 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1820 /* Sorry, you roamed too many times! */
1821 goto unlock;
1822 ret = true;
1823 break;
1824 }
1825
1826 if (!ret) {
1827 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1828 if (!tt_roam_node)
1829 goto unlock;
1830
1831 tt_roam_node->first_time = jiffies;
1832 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1833 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1834
1835 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1836 ret = true;
1837 }
1838
1839unlock:
1840 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1841 return ret;
1842}
1843
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001844static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1845 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001846{
1847 struct neigh_node *neigh_node = NULL;
1848 struct sk_buff *skb = NULL;
1849 struct roam_adv_packet *roam_adv_packet;
1850 int ret = 1;
1851 struct hard_iface *primary_if;
1852
1853 /* before going on we have to check whether the client has
1854 * already roamed to us too many times */
1855 if (!tt_check_roam_count(bat_priv, client))
1856 goto out;
1857
1858 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1859 if (!skb)
1860 goto out;
1861
1862 skb_reserve(skb, ETH_HLEN);
1863
1864 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1865 sizeof(struct roam_adv_packet));
1866
Sven Eckelmann76543d12011-11-20 15:47:38 +01001867 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1868 roam_adv_packet->header.version = COMPAT_VERSION;
1869 roam_adv_packet->header.ttl = TTL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001870 primary_if = primary_if_get_selected(bat_priv);
1871 if (!primary_if)
1872 goto out;
1873 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1874 hardif_free_ref(primary_if);
1875 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1876 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1877
1878 neigh_node = orig_node_get_router(orig_node);
1879 if (!neigh_node)
1880 goto out;
1881
1882 bat_dbg(DBG_TT, bat_priv,
1883 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1884 orig_node->orig, client, neigh_node->addr);
1885
1886 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1887 ret = 0;
1888
1889out:
1890 if (neigh_node)
1891 neigh_node_free_ref(neigh_node);
1892 if (ret)
1893 kfree_skb(skb);
1894 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895}
1896
1897static void tt_purge(struct work_struct *work)
1898{
1899 struct delayed_work *delayed_work =
1900 container_of(work, struct delayed_work, work);
1901 struct bat_priv *bat_priv =
1902 container_of(delayed_work, struct bat_priv, tt_work);
1903
1904 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001905 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001906 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001907 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908
1909 tt_start_timer(bat_priv);
1910}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001911
1912void tt_free(struct bat_priv *bat_priv)
1913{
1914 cancel_delayed_work_sync(&bat_priv->tt_work);
1915
1916 tt_local_table_free(bat_priv);
1917 tt_global_table_free(bat_priv);
1918 tt_req_list_free(bat_priv);
1919 tt_changes_list_free(bat_priv);
1920 tt_roam_list_free(bat_priv);
1921
1922 kfree(bat_priv->tt_buff);
1923}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001924
Antonio Quartulli697f2532011-11-07 16:47:01 +01001925/* This function will enable or disable the specified flags for all the entries
1926 * in the given hash table and returns the number of modified entries */
1927static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1928 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001929{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001930 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001931 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001932 struct hlist_head *head;
1933 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001934 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001935
1936 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001937 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001938
1939 for (i = 0; i < hash->size; i++) {
1940 head = &hash->table[i];
1941
1942 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001943 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001944 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001945 if (enable) {
1946 if ((tt_common_entry->flags & flags) == flags)
1947 continue;
1948 tt_common_entry->flags |= flags;
1949 } else {
1950 if (!(tt_common_entry->flags & flags))
1951 continue;
1952 tt_common_entry->flags &= ~flags;
1953 }
1954 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001955 }
1956 rcu_read_unlock();
1957 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001958out:
1959 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001960}
1961
1962/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1963static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1964{
1965 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001966 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001967 struct tt_local_entry *tt_local_entry;
1968 struct hlist_node *node, *node_tmp;
1969 struct hlist_head *head;
1970 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001971 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001972
1973 if (!hash)
1974 return;
1975
1976 for (i = 0; i < hash->size; i++) {
1977 head = &hash->table[i];
1978 list_lock = &hash->list_locks[i];
1979
1980 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001981 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001982 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001983 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001984 continue;
1985
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001986 bat_dbg(DBG_TT, bat_priv,
1987 "Deleting local tt entry (%pM): pending\n",
1988 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001989
1990 atomic_dec(&bat_priv->num_local_tt);
1991 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001992 tt_local_entry = container_of(tt_common_entry,
1993 struct tt_local_entry,
1994 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001995 tt_local_entry_free_ref(tt_local_entry);
1996 }
1997 spin_unlock_bh(list_lock);
1998 }
1999
2000}
2001
2002void tt_commit_changes(struct bat_priv *bat_priv)
2003{
Antonio Quartulli697f2532011-11-07 16:47:01 +01002004 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
2005 TT_CLIENT_NEW, false);
2006 /* all the reset entries have now to be effectively counted as local
2007 * entries */
2008 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002009 tt_local_purge_pending_clients(bat_priv);
2010
2011 /* Increment the TTVN only once per OGM interval */
2012 atomic_inc(&bat_priv->ttvn);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002013 bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
2014 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002015 bat_priv->tt_poss_change = false;
2016}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002017
2018bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
2019{
2020 struct tt_local_entry *tt_local_entry = NULL;
2021 struct tt_global_entry *tt_global_entry = NULL;
2022 bool ret = true;
2023
2024 if (!atomic_read(&bat_priv->ap_isolation))
2025 return false;
2026
2027 tt_local_entry = tt_local_hash_find(bat_priv, dst);
2028 if (!tt_local_entry)
2029 goto out;
2030
2031 tt_global_entry = tt_global_hash_find(bat_priv, src);
2032 if (!tt_global_entry)
2033 goto out;
2034
2035 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
2036 goto out;
2037
2038 ret = false;
2039
2040out:
2041 if (tt_global_entry)
2042 tt_global_entry_free_ref(tt_global_entry);
2043 if (tt_local_entry)
2044 tt_local_entry_free_ref(tt_local_entry);
2045 return ret;
2046}
Marek Lindnera943cac2011-07-30 13:10:18 +02002047
2048void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
2049 const unsigned char *tt_buff, uint8_t tt_num_changes,
2050 uint8_t ttvn, uint16_t tt_crc)
2051{
2052 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2053 bool full_table = true;
2054
Antonio Quartulli17071572011-11-07 16:36:40 +01002055 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2056 * increased by one -> we can apply the attached changes */
2057 if ((!orig_node->tt_initialised && ttvn == 1) ||
2058 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002059 /* the OGM could not contain the changes due to their size or
2060 * because they have already been sent TT_OGM_APPEND_MAX times.
2061 * In this case send a tt request */
2062 if (!tt_num_changes) {
2063 full_table = false;
2064 goto request_table;
2065 }
2066
2067 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
2068 (struct tt_change *)tt_buff);
2069
2070 /* Even if we received the precomputed crc with the OGM, we
2071 * prefer to recompute it to spot any possible inconsistency
2072 * in the global table */
2073 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
2074
2075 /* The ttvn alone is not enough to guarantee consistency
2076 * because a single value could represent different states
2077 * (due to the wrap around). Thus a node has to check whether
2078 * the resulting table (after applying the changes) is still
2079 * consistent or not. E.g. a node could disconnect while its
2080 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2081 * checking the CRC value is mandatory to detect the
2082 * inconsistency */
2083 if (orig_node->tt_crc != tt_crc)
2084 goto request_table;
2085
2086 /* Roaming phase is over: tables are in sync again. I can
2087 * unset the flag */
2088 orig_node->tt_poss_change = false;
2089 } else {
2090 /* if we missed more than one change or our tables are not
2091 * in sync anymore -> request fresh tt data */
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002092
Antonio Quartulli17071572011-11-07 16:36:40 +01002093 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2094 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002095request_table:
Sven Eckelmann86ceb362012-03-07 09:07:45 +01002096 bat_dbg(DBG_TT, bat_priv,
2097 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2098 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2099 orig_node->tt_crc, tt_num_changes);
Marek Lindnera943cac2011-07-30 13:10:18 +02002100 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
2101 full_table);
2102 return;
2103 }
2104 }
2105}