blob: 2ab83d7fb1f84c864d5bb2a49295e9b4f8071811 [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 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01004 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 *
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"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010030#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Antonio Quartullia73105b2011-04-27 14:27:44 +020032#include <linux/crc16.h>
33
Sven Eckelmannde7aae62012-02-05 18:55:22 +010034static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
35 struct orig_node *orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +020036static void tt_purge(struct work_struct *work);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +020037static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
Marek Lindner7aadf882011-02-18 12:28:09 +000039/* returns 1 if they are the same mac addr */
Antonio Quartulli48100ba2011-10-30 12:17:33 +010040static int compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000041{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010042 const void *data1 = container_of(node, struct tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020043 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000044
45 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
46}
47
Antonio Quartullia73105b2011-04-27 14:27:44 +020048static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049{
Antonio Quartullia73105b2011-04-27 14:27:44 +020050 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
51 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
52 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053}
54
Antonio Quartulli48100ba2011-10-30 12:17:33 +010055static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
56 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000057{
Marek Lindner7aadf882011-02-18 12:28:09 +000058 struct hlist_head *head;
59 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010060 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020061 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000062
63 if (!hash)
64 return NULL;
65
66 index = choose_orig(data, hash->size);
67 head = &hash->table[index];
68
69 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010070 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
71 if (!compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000072 continue;
73
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020075 continue;
76
Antonio Quartulli48100ba2011-10-30 12:17:33 +010077 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000078 break;
79 }
80 rcu_read_unlock();
81
Antonio Quartulli48100ba2011-10-30 12:17:33 +010082 return tt_common_entry_tmp;
83}
84
85static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
86 const void *data)
87{
88 struct tt_common_entry *tt_common_entry;
89 struct tt_local_entry *tt_local_entry = NULL;
90
91 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
92 if (tt_common_entry)
93 tt_local_entry = container_of(tt_common_entry,
94 struct tt_local_entry, common);
95 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000096}
97
Antonio Quartulli2dafb492011-05-05 08:42:45 +020098static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020099 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000100{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100101 struct tt_common_entry *tt_common_entry;
102 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000103
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100104 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
105 if (tt_common_entry)
106 tt_global_entry = container_of(tt_common_entry,
107 struct tt_global_entry, common);
108 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000109
Marek Lindner7aadf882011-02-18 12:28:09 +0000110}
111
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200112static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
113{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
115 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200116}
117
Simon Wunderlich531027f2011-10-19 11:02:25 +0200118static void tt_global_entry_free_rcu(struct rcu_head *rcu)
119{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100120 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200121 struct tt_global_entry *tt_global_entry;
122
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100123 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
124 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
125 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126
Simon Wunderlich531027f2011-10-19 11:02:25 +0200127 kfree(tt_global_entry);
128}
129
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200130static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
131{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200132 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
133 tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100134 call_rcu(&tt_global_entry->common.rcu,
135 tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200136 }
137}
138
139static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
140{
141 struct tt_orig_list_entry *orig_entry;
142
143 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 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{
Antonio Quartulli8b8e4bc2012-06-20 14:12:56 +0200150 /* to avoid race conditions, immediately decrease the tt counter */
151 atomic_dec(&orig_entry->orig_node->tt_size);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200152 call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200153}
154
Antonio Quartulliff66c972011-06-30 01:14:00 +0200155static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
156 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200157{
158 struct tt_change_node *tt_change_node;
159
160 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
161
162 if (!tt_change_node)
163 return;
164
Antonio Quartulliff66c972011-06-30 01:14:00 +0200165 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200166 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
167
168 spin_lock_bh(&bat_priv->tt_changes_list_lock);
169 /* track the change in the OGMinterval list */
170 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
171 atomic_inc(&bat_priv->tt_local_changes);
172 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
173
174 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
175}
176
177int tt_len(int changes_num)
178{
179 return changes_num * sizeof(struct tt_change);
180}
181
182static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200184 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 return 1;
186
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200189 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 return 0;
191
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192 return 1;
193}
194
Antonio Quartullibc279082011-07-07 15:35:35 +0200195void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
196 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197{
198 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199 struct tt_local_entry *tt_local_entry = NULL;
200 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200201 struct hlist_head *head;
202 struct hlist_node *node;
203 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100204 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200206 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200208 if (tt_local_entry) {
209 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100210 /* possibly unset the TT_CLIENT_PENDING flag */
211 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200212 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 }
214
Sven Eckelmann704509b2011-05-14 23:14:54 +0200215 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200216 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200217 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200218
Antonio Quartullia73105b2011-04-27 14:27:44 +0200219 bat_dbg(DBG_TT, bat_priv,
220 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
221 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100223 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
224 tt_local_entry->common.flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200225 if (is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100226 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
227 atomic_set(&tt_local_entry->common.refcount, 2);
228 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
230 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000231 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100232 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100234 /* The local entry has to be marked as NEW to avoid to send it in
235 * a full table response going out before the next ttvn increment
236 * (consistency check) */
237 tt_local_entry->common.flags |= TT_CLIENT_NEW;
238
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100239 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
240 &tt_local_entry->common,
241 &tt_local_entry->common.hash_entry);
242
243 if (unlikely(hash_added != 0)) {
244 /* remove the reference for the hash */
245 tt_local_entry_free_ref(tt_local_entry);
246 goto out;
247 }
248
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100249 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200250
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200252 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartullicc47f662011-04-27 14:27:57 +0200254 /* Check whether it is a roaming! */
255 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200256 /* These node are probably going to update their tt table */
257 head = &tt_global_entry->orig_list;
258 rcu_read_lock();
259 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
260 orig_entry->orig_node->tt_poss_change = true;
261
262 send_roam_adv(bat_priv, tt_global_entry->common.addr,
263 orig_entry->orig_node);
264 }
265 rcu_read_unlock();
266 /* The global entry has to be marked as ROAMING and
267 * has to be kept for consistency purpose
268 */
David S. Miller220b07e2011-12-16 15:07:28 -0500269 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100270 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200271 }
272out:
273 if (tt_local_entry)
274 tt_local_entry_free_ref(tt_local_entry);
275 if (tt_global_entry)
276 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277}
278
Antonio Quartullia73105b2011-04-27 14:27:44 +0200279int tt_changes_fill_buffer(struct bat_priv *bat_priv,
280 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200282 int count = 0, tot_changes = 0;
283 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
Antonio Quartullia73105b2011-04-27 14:27:44 +0200285 if (buff_len > 0)
286 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
Antonio Quartullia73105b2011-04-27 14:27:44 +0200288 spin_lock_bh(&bat_priv->tt_changes_list_lock);
289 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290
Antonio Quartullia73105b2011-04-27 14:27:44 +0200291 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100292 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200293 if (count < tot_changes) {
294 memcpy(buff + tt_len(count),
295 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 count++;
297 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200298 list_del(&entry->list);
299 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200301 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartullia73105b2011-04-27 14:27:44 +0200303 /* Keep the buffer for possible tt_request */
304 spin_lock_bh(&bat_priv->tt_buff_lock);
305 kfree(bat_priv->tt_buff);
306 bat_priv->tt_buff_len = 0;
307 bat_priv->tt_buff = NULL;
308 /* We check whether this new OGM has no changes due to size
309 * problems */
310 if (buff_len > 0) {
311 /**
312 * if kmalloc() fails we will reply with the full table
313 * instead of providing the diff
314 */
315 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
316 if (bat_priv->tt_buff) {
317 memcpy(bat_priv->tt_buff, buff, buff_len);
318 bat_priv->tt_buff_len = buff_len;
319 }
320 }
321 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322
Antonio Quartullia73105b2011-04-27 14:27:44 +0200323 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324}
325
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200326int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327{
328 struct net_device *net_dev = (struct net_device *)seq->private;
329 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200330 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100331 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200332 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000333 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200335 uint32_t i;
336 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
Marek Lindner32ae9b22011-04-20 15:40:58 +0200338 primary_if = primary_if_get_selected(bat_priv);
339 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100340 ret = seq_printf(seq,
341 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200342 net_dev->name);
343 goto out;
344 }
345
346 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100347 ret = seq_printf(seq,
348 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200349 net_dev->name);
350 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 }
352
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100353 seq_printf(seq,
354 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200355 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 for (i = 0; i < hash->size; i++) {
358 head = &hash->table[i];
359
Marek Lindner7aadf882011-02-18 12:28:09 +0000360 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100361 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000362 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200363 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100364 tt_common_entry->addr,
365 (tt_common_entry->flags &
366 TT_CLIENT_ROAM ? 'R' : '.'),
367 (tt_common_entry->flags &
368 TT_CLIENT_NOPURGE ? 'P' : '.'),
369 (tt_common_entry->flags &
370 TT_CLIENT_NEW ? 'N' : '.'),
371 (tt_common_entry->flags &
372 TT_CLIENT_PENDING ? 'X' : '.'),
373 (tt_common_entry->flags &
374 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000376 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200378out:
379 if (primary_if)
380 hardif_free_ref(primary_if);
381 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382}
383
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200384static void tt_local_set_pending(struct bat_priv *bat_priv,
385 struct tt_local_entry *tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100386 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100388 tt_local_event(bat_priv, tt_local_entry->common.addr,
389 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
Antonio Quartulli015758d2011-07-09 17:52:13 +0200391 /* The local client has to be marked as "pending to be removed" but has
392 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200393 * response issued before the net ttvn increment (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100394 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100395
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100396 bat_dbg(DBG_TT, bat_priv,
397 "Local tt entry (%pM) pending to be removed: %s\n",
398 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399}
400
Antonio Quartullia73105b2011-04-27 14:27:44 +0200401void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200402 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200404 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200406 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200407 if (!tt_local_entry)
408 goto out;
409
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200410 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100411 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200412out:
413 if (tt_local_entry)
414 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415}
416
Antonio Quartullia73105b2011-04-27 14:27:44 +0200417static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200419 struct hashtable_t *hash = bat_priv->tt_local_hash;
420 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100421 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000422 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200424 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200425 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 for (i = 0; i < hash->size; i++) {
428 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200429 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200431 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100432 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000433 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100434 tt_local_entry = container_of(tt_common_entry,
435 struct tt_local_entry,
436 common);
437 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000438 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200440 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100441 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200442 continue;
443
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100444 if (!has_timed_out(tt_local_entry->last_seen,
Marek Lindner032b7962011-12-20 19:30:40 +0800445 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000446 continue;
447
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200448 tt_local_set_pending(bat_priv, tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100449 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200451 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 }
453
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454}
455
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200458 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200459 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100460 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200461 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200462 struct hlist_node *node, *node_tmp;
463 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200464 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200465
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200466 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 return;
468
Antonio Quartullia73105b2011-04-27 14:27:44 +0200469 hash = bat_priv->tt_local_hash;
470
471 for (i = 0; i < hash->size; i++) {
472 head = &hash->table[i];
473 list_lock = &hash->list_locks[i];
474
475 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100476 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200477 head, hash_entry) {
478 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100479 tt_local_entry = container_of(tt_common_entry,
480 struct tt_local_entry,
481 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200482 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200483 }
484 spin_unlock_bh(list_lock);
485 }
486
487 hash_destroy(hash);
488
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200489 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Antonio Quartullia73105b2011-04-27 14:27:44 +0200492static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200494 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495 return 1;
496
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200497 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200499 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500 return 0;
501
502 return 1;
503}
504
Antonio Quartullia73105b2011-04-27 14:27:44 +0200505static void tt_changes_list_free(struct bat_priv *bat_priv)
506{
507 struct tt_change_node *entry, *safe;
508
509 spin_lock_bh(&bat_priv->tt_changes_list_lock);
510
511 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
512 list) {
513 list_del(&entry->list);
514 kfree(entry);
515 }
516
517 atomic_set(&bat_priv->tt_local_changes, 0);
518 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
519}
520
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200521/* find out if an orig_node is already in the list of a tt_global_entry.
522 * returns 1 if found, 0 otherwise
523 */
524static bool tt_global_entry_has_orig(const struct tt_global_entry *entry,
525 const struct orig_node *orig_node)
526{
527 struct tt_orig_list_entry *tmp_orig_entry;
528 const struct hlist_head *head;
529 struct hlist_node *node;
530 bool found = false;
531
532 rcu_read_lock();
533 head = &entry->orig_list;
534 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
535 if (tmp_orig_entry->orig_node == orig_node) {
536 found = true;
537 break;
538 }
539 }
540 rcu_read_unlock();
541 return found;
542}
543
544static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
545 struct orig_node *orig_node,
546 int ttvn)
547{
548 struct tt_orig_list_entry *orig_entry;
549
550 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
551 if (!orig_entry)
552 return;
553
554 INIT_HLIST_NODE(&orig_entry->list);
555 atomic_inc(&orig_node->refcount);
556 atomic_inc(&orig_node->tt_size);
557 orig_entry->orig_node = orig_node;
558 orig_entry->ttvn = ttvn;
559
560 spin_lock_bh(&tt_global_entry->list_lock);
561 hlist_add_head_rcu(&orig_entry->list,
562 &tt_global_entry->orig_list);
563 spin_unlock_bh(&tt_global_entry->list_lock);
564}
565
Antonio Quartullia73105b2011-04-27 14:27:44 +0200566/* caller must hold orig_node refcount */
567int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200568 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
569 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200571 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200572 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100573 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
Antonio Quartullia73105b2011-04-27 14:27:44 +0200575 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
Antonio Quartullia73105b2011-04-27 14:27:44 +0200577 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200578 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
579 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200580 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200581 goto out;
582
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100583 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200584
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100585 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200586 tt_global_entry->roam_at = 0;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200587 atomic_set(&tt_global_entry->common.refcount, 2);
588
589 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
590 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200591
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100592 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
593 choose_orig, &tt_global_entry->common,
594 &tt_global_entry->common.hash_entry);
595
596 if (unlikely(hash_added != 0)) {
597 /* remove the reference for the hash */
598 tt_global_entry_free_ref(tt_global_entry);
599 goto out_remove;
600 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200601
602 tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200603 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200604 /* there is already a global entry, use this one. */
605
606 /* If there is the TT_CLIENT_ROAM flag set, there is only one
607 * originator left in the list and we previously received a
608 * delete + roaming change for this originator.
609 *
610 * We should first delete the old originator before adding the
611 * new one.
612 */
613 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
614 tt_global_del_orig_list(tt_global_entry);
615 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
616 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200618
619 if (!tt_global_entry_has_orig(tt_global_entry, orig_node))
620 tt_global_add_orig_entry(tt_global_entry, orig_node,
621 ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200623
Antonio Quartullibc279082011-07-07 15:35:35 +0200624 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100625 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200626
Antonio Quartullia73105b2011-04-27 14:27:44 +0200627 bat_dbg(DBG_TT, bat_priv,
628 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100629 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200630
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100631out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200632 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100633 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200634 "global tt received", roaming);
635 ret = 1;
636out:
637 if (tt_global_entry)
638 tt_global_entry_free_ref(tt_global_entry);
639 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640}
641
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200642/* print all orig nodes who announce the address for this global entry.
643 * it is assumed that the caller holds rcu_read_lock();
644 */
645static void tt_global_print_entry(struct tt_global_entry *tt_global_entry,
646 struct seq_file *seq)
647{
648 struct hlist_head *head;
649 struct hlist_node *node;
650 struct tt_orig_list_entry *orig_entry;
651 struct tt_common_entry *tt_common_entry;
652 uint16_t flags;
653 uint8_t last_ttvn;
654
655 tt_common_entry = &tt_global_entry->common;
656
657 head = &tt_global_entry->orig_list;
658
659 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
660 flags = tt_common_entry->flags;
661 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
662 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
663 tt_global_entry->common.addr, orig_entry->ttvn,
664 orig_entry->orig_node->orig, last_ttvn,
665 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
666 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
667 }
668}
669
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200670int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000671{
672 struct net_device *net_dev = (struct net_device *)seq->private;
673 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200674 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100675 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200676 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200677 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000678 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200680 uint32_t i;
681 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682
Marek Lindner32ae9b22011-04-20 15:40:58 +0200683 primary_if = primary_if_get_selected(bat_priv);
684 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100685 ret = seq_printf(seq,
686 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200687 net_dev->name);
688 goto out;
689 }
690
691 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100692 ret = seq_printf(seq,
693 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200694 net_dev->name);
695 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 }
697
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200698 seq_printf(seq,
699 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200701 seq_printf(seq, " %-13s %s %-15s %s %s\n",
702 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704 for (i = 0; i < hash->size; i++) {
705 head = &hash->table[i];
706
Marek Lindner7aadf882011-02-18 12:28:09 +0000707 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100708 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000709 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100710 tt_global_entry = container_of(tt_common_entry,
711 struct tt_global_entry,
712 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200713 tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000715 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200717out:
718 if (primary_if)
719 hardif_free_ref(primary_if);
720 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721}
722
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200723/* deletes the orig list of a tt_global_entry */
724static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200726 struct hlist_head *head;
727 struct hlist_node *node, *safe;
728 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200729
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200730 spin_lock_bh(&tt_global_entry->list_lock);
731 head = &tt_global_entry->orig_list;
732 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
733 hlist_del_rcu(node);
734 tt_orig_list_entry_free_ref(orig_entry);
735 }
736 spin_unlock_bh(&tt_global_entry->list_lock);
737
738}
739
740static void tt_global_del_orig_entry(struct bat_priv *bat_priv,
741 struct tt_global_entry *tt_global_entry,
742 struct orig_node *orig_node,
743 const char *message)
744{
745 struct hlist_head *head;
746 struct hlist_node *node, *safe;
747 struct tt_orig_list_entry *orig_entry;
748
749 spin_lock_bh(&tt_global_entry->list_lock);
750 head = &tt_global_entry->orig_list;
751 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
752 if (orig_entry->orig_node == orig_node) {
753 bat_dbg(DBG_TT, bat_priv,
754 "Deleting %pM from global tt entry %pM: %s\n",
755 orig_node->orig, tt_global_entry->common.addr,
756 message);
757 hlist_del_rcu(node);
758 tt_orig_list_entry_free_ref(orig_entry);
759 }
760 }
761 spin_unlock_bh(&tt_global_entry->list_lock);
762}
763
764static void tt_global_del_struct(struct bat_priv *bat_priv,
765 struct tt_global_entry *tt_global_entry,
766 const char *message)
767{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200768 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200769 "Deleting global tt entry %pM: %s\n",
770 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200771
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100772 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
773 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200774 tt_global_entry_free_ref(tt_global_entry);
775
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776}
777
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200778/* If the client is to be deleted, we check if it is the last origantor entry
779 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
780 * otherwise we simply remove the originator scheduled for deletion.
781 */
782static void tt_global_del_roaming(struct bat_priv *bat_priv,
783 struct tt_global_entry *tt_global_entry,
784 struct orig_node *orig_node,
785 const char *message)
786{
787 bool last_entry = true;
788 struct hlist_head *head;
789 struct hlist_node *node;
790 struct tt_orig_list_entry *orig_entry;
791
792 /* no local entry exists, case 1:
793 * Check if this is the last one or if other entries exist.
794 */
795
796 rcu_read_lock();
797 head = &tt_global_entry->orig_list;
798 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
799 if (orig_entry->orig_node != orig_node) {
800 last_entry = false;
801 break;
802 }
803 }
804 rcu_read_unlock();
805
806 if (last_entry) {
807 /* its the last one, mark for roaming. */
808 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
809 tt_global_entry->roam_at = jiffies;
810 } else
811 /* there is another entry, we can simply delete this
812 * one and can still use the other one.
813 */
814 tt_global_del_orig_entry(bat_priv, tt_global_entry,
815 orig_node, message);
816}
817
818
819
Sven Eckelmannde7aae62012-02-05 18:55:22 +0100820static void tt_global_del(struct bat_priv *bat_priv,
821 struct orig_node *orig_node,
822 const unsigned char *addr,
823 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200825 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100826 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827
Antonio Quartullia73105b2011-04-27 14:27:44 +0200828 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200829 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200830 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200831
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200832 if (!roaming) {
833 tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node,
834 message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800835
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200836 if (hlist_empty(&tt_global_entry->orig_list))
837 tt_global_del_struct(bat_priv, tt_global_entry,
838 message);
839
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800840 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200841 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800842
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200843 /* if we are deleting a global entry due to a roam
844 * event, there are two possibilities:
845 * 1) the client roamed from node A to node B => if there
846 * is only one originator left for this client, we mark
847 * it with TT_CLIENT_ROAM, we start a timer and we
848 * wait for node B to claim it. In case of timeout
849 * the entry is purged.
850 *
851 * If there are other originators left, we directly delete
852 * the originator.
853 * 2) the client roamed to us => we can directly delete
854 * the global entry, since it is useless now. */
855
856 tt_local_entry = tt_local_hash_find(bat_priv,
857 tt_global_entry->common.addr);
858 if (tt_local_entry) {
859 /* local entry exists, case 2: client roamed to us. */
860 tt_global_del_orig_list(tt_global_entry);
861 tt_global_del_struct(bat_priv, tt_global_entry, message);
862 } else
863 /* no local entry exists, case 1: check for roaming */
864 tt_global_del_roaming(bat_priv, tt_global_entry, orig_node,
865 message);
866
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800867
Antonio Quartullicc47f662011-04-27 14:27:57 +0200868out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200869 if (tt_global_entry)
870 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100871 if (tt_local_entry)
872 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200873}
874
875void tt_global_del_orig(struct bat_priv *bat_priv,
876 struct orig_node *orig_node, const char *message)
877{
878 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100879 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200880 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200881 struct hashtable_t *hash = bat_priv->tt_global_hash;
882 struct hlist_node *node, *safe;
883 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200884 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200885
Simon Wunderlich6e801492011-10-19 10:28:26 +0200886 if (!hash)
887 return;
888
Antonio Quartullia73105b2011-04-27 14:27:44 +0200889 for (i = 0; i < hash->size; i++) {
890 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200891 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200893 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100894 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100895 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100896 tt_global_entry = container_of(tt_common_entry,
897 struct tt_global_entry,
898 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200899
900 tt_global_del_orig_entry(bat_priv, tt_global_entry,
901 orig_node, message);
902
903 if (hlist_empty(&tt_global_entry->orig_list)) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200904 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200905 "Deleting global tt entry %pM: %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100906 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200907 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200908 hlist_del_rcu(node);
909 tt_global_entry_free_ref(tt_global_entry);
910 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200911 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200912 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913 }
Antonio Quartulli17071572011-11-07 16:36:40 +0100914 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915}
916
Antonio Quartullicc47f662011-04-27 14:27:57 +0200917static void tt_global_roam_purge(struct bat_priv *bat_priv)
918{
919 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100920 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200921 struct tt_global_entry *tt_global_entry;
922 struct hlist_node *node, *node_tmp;
923 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200924 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200925 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200926
Antonio Quartullicc47f662011-04-27 14:27:57 +0200927 for (i = 0; i < hash->size; i++) {
928 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200929 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200930
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200931 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100932 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200933 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100934 tt_global_entry = container_of(tt_common_entry,
935 struct tt_global_entry,
936 common);
937 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200938 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100939 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800940 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200941 continue;
942
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100943 bat_dbg(DBG_TT, bat_priv,
944 "Deleting global tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100945 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200946
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200947 hlist_del_rcu(node);
948 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200949 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200950 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200951 }
952
Antonio Quartullicc47f662011-04-27 14:27:57 +0200953}
954
Antonio Quartullia73105b2011-04-27 14:27:44 +0200955static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000956{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200957 struct hashtable_t *hash;
958 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100959 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200960 struct tt_global_entry *tt_global_entry;
961 struct hlist_node *node, *node_tmp;
962 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200963 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200964
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200965 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966 return;
967
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200968 hash = bat_priv->tt_global_hash;
969
970 for (i = 0; i < hash->size; i++) {
971 head = &hash->table[i];
972 list_lock = &hash->list_locks[i];
973
974 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100975 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200976 head, hash_entry) {
977 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100978 tt_global_entry = container_of(tt_common_entry,
979 struct tt_global_entry,
980 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200981 tt_global_entry_free_ref(tt_global_entry);
982 }
983 spin_unlock_bh(list_lock);
984 }
985
986 hash_destroy(hash);
987
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200988 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000989}
990
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200991static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
992 struct tt_global_entry *tt_global_entry)
993{
994 bool ret = false;
995
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100996 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
997 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200998 ret = true;
999
1000 return ret;
1001}
1002
Sven Eckelmann747e4222011-05-14 23:14:50 +02001003struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001004 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001005{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001006 struct tt_local_entry *tt_local_entry = NULL;
1007 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001008 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001009 struct neigh_node *router = NULL;
1010 struct hlist_head *head;
1011 struct hlist_node *node;
1012 struct tt_orig_list_entry *orig_entry;
1013 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001015 if (src && atomic_read(&bat_priv->ap_isolation)) {
1016 tt_local_entry = tt_local_hash_find(bat_priv, src);
1017 if (!tt_local_entry)
1018 goto out;
1019 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001020
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001021 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001022 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001023 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001025 /* check whether the clients should not communicate due to AP
1026 * isolation */
1027 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
1028 goto out;
1029
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001030 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001031
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001032 rcu_read_lock();
1033 head = &tt_global_entry->orig_list;
1034 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1035 router = orig_node_get_router(orig_entry->orig_node);
1036 if (!router)
1037 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001038
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001039 if (router->tq_avg > best_tq) {
1040 orig_node = orig_entry->orig_node;
1041 best_tq = router->tq_avg;
1042 }
1043 neigh_node_free_ref(router);
1044 }
1045 /* found anything? */
1046 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1047 orig_node = NULL;
1048 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001049out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001050 if (tt_global_entry)
1051 tt_global_entry_free_ref(tt_global_entry);
1052 if (tt_local_entry)
1053 tt_local_entry_free_ref(tt_local_entry);
1054
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001055 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001056}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001057
1058/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001059static uint16_t tt_global_crc(struct bat_priv *bat_priv,
1060 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001061{
1062 uint16_t total = 0, total_one;
1063 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001064 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001065 struct tt_global_entry *tt_global_entry;
1066 struct hlist_node *node;
1067 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001068 uint32_t i;
1069 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001070
1071 for (i = 0; i < hash->size; i++) {
1072 head = &hash->table[i];
1073
1074 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001075 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001076 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001077 tt_global_entry = container_of(tt_common_entry,
1078 struct tt_global_entry,
1079 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001080 /* Roaming clients are in the global table for
1081 * consistency only. They don't have to be
1082 * taken into account while computing the
1083 * global crc
1084 */
1085 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1086 continue;
1087
1088 /* find out if this global entry is announced by this
1089 * originator
1090 */
1091 if (!tt_global_entry_has_orig(tt_global_entry,
1092 orig_node))
1093 continue;
1094
1095 total_one = 0;
1096 for (j = 0; j < ETH_ALEN; j++)
1097 total_one = crc16_byte(total_one,
1098 tt_global_entry->common.addr[j]);
1099 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001100 }
1101 rcu_read_unlock();
1102 }
1103
1104 return total;
1105}
1106
1107/* Calculates the checksum of the local table */
1108uint16_t tt_local_crc(struct bat_priv *bat_priv)
1109{
1110 uint16_t total = 0, total_one;
1111 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001112 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001113 struct hlist_node *node;
1114 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001115 uint32_t i;
1116 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001117
1118 for (i = 0; i < hash->size; i++) {
1119 head = &hash->table[i];
1120
1121 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001122 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001123 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001124 /* not yet committed clients have not to be taken into
1125 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001126 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001127 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001128 total_one = 0;
1129 for (j = 0; j < ETH_ALEN; j++)
1130 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001131 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001132 total ^= total_one;
1133 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001134 rcu_read_unlock();
1135 }
1136
1137 return total;
1138}
1139
1140static void tt_req_list_free(struct bat_priv *bat_priv)
1141{
1142 struct tt_req_node *node, *safe;
1143
1144 spin_lock_bh(&bat_priv->tt_req_list_lock);
1145
1146 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1147 list_del(&node->list);
1148 kfree(node);
1149 }
1150
1151 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1152}
1153
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001154static void tt_save_orig_buffer(struct bat_priv *bat_priv,
1155 struct orig_node *orig_node,
1156 const unsigned char *tt_buff,
1157 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001158{
1159 uint16_t tt_buff_len = tt_len(tt_num_changes);
1160
1161 /* Replace the old buffer only if I received something in the
1162 * last OGM (the OGM could carry no changes) */
1163 spin_lock_bh(&orig_node->tt_buff_lock);
1164 if (tt_buff_len > 0) {
1165 kfree(orig_node->tt_buff);
1166 orig_node->tt_buff_len = 0;
1167 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1168 if (orig_node->tt_buff) {
1169 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1170 orig_node->tt_buff_len = tt_buff_len;
1171 }
1172 }
1173 spin_unlock_bh(&orig_node->tt_buff_lock);
1174}
1175
1176static void tt_req_purge(struct bat_priv *bat_priv)
1177{
1178 struct tt_req_node *node, *safe;
1179
1180 spin_lock_bh(&bat_priv->tt_req_list_lock);
1181 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001182 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001183 list_del(&node->list);
1184 kfree(node);
1185 }
1186 }
1187 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1188}
1189
1190/* returns the pointer to the new tt_req_node struct if no request
1191 * has already been issued for this orig_node, NULL otherwise */
1192static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
1193 struct orig_node *orig_node)
1194{
1195 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1196
1197 spin_lock_bh(&bat_priv->tt_req_list_lock);
1198 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1199 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +01001200 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +08001201 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001202 goto unlock;
1203 }
1204
1205 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1206 if (!tt_req_node)
1207 goto unlock;
1208
1209 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1210 tt_req_node->issued_at = jiffies;
1211
1212 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1213unlock:
1214 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1215 return tt_req_node;
1216}
1217
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001218/* data_ptr is useless here, but has to be kept to respect the prototype */
1219static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1220{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001221 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001222
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001223 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001224 return 0;
1225 return 1;
1226}
1227
Antonio Quartullia73105b2011-04-27 14:27:44 +02001228static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1229{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001230 const struct tt_common_entry *tt_common_entry = entry_ptr;
1231 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232 const struct orig_node *orig_node = data_ptr;
1233
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001234 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001235 return 0;
1236
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001237 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1238 common);
1239
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001240 return tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001241}
1242
1243static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1244 struct hashtable_t *hash,
1245 struct hard_iface *primary_if,
1246 int (*valid_cb)(const void *,
1247 const void *),
1248 void *cb_data)
1249{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001250 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001251 struct tt_query_packet *tt_response;
1252 struct tt_change *tt_change;
1253 struct hlist_node *node;
1254 struct hlist_head *head;
1255 struct sk_buff *skb = NULL;
1256 uint16_t tt_tot, tt_count;
1257 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001258 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259
1260 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1261 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1262 tt_len -= tt_len % sizeof(struct tt_change);
1263 }
1264 tt_tot = tt_len / sizeof(struct tt_change);
1265
1266 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1267 if (!skb)
1268 goto out;
1269
1270 skb_reserve(skb, ETH_HLEN);
1271 tt_response = (struct tt_query_packet *)skb_put(skb,
1272 tt_query_size + tt_len);
1273 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001274
1275 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1276 tt_count = 0;
1277
1278 rcu_read_lock();
1279 for (i = 0; i < hash->size; i++) {
1280 head = &hash->table[i];
1281
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001282 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001283 head, hash_entry) {
1284 if (tt_count == tt_tot)
1285 break;
1286
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001287 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001288 continue;
1289
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001290 memcpy(tt_change->addr, tt_common_entry->addr,
1291 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001292 tt_change->flags = NO_FLAGS;
1293
1294 tt_count++;
1295 tt_change++;
1296 }
1297 }
1298 rcu_read_unlock();
1299
Antonio Quartulli9d852392011-10-17 14:25:13 +02001300 /* store in the message the number of entries we have successfully
1301 * copied */
1302 tt_response->tt_data = htons(tt_count);
1303
Antonio Quartullia73105b2011-04-27 14:27:44 +02001304out:
1305 return skb;
1306}
1307
Marek Lindnera943cac2011-07-30 13:10:18 +02001308static int send_tt_request(struct bat_priv *bat_priv,
1309 struct orig_node *dst_orig_node,
1310 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001311{
1312 struct sk_buff *skb = NULL;
1313 struct tt_query_packet *tt_request;
1314 struct neigh_node *neigh_node = NULL;
1315 struct hard_iface *primary_if;
1316 struct tt_req_node *tt_req_node = NULL;
1317 int ret = 1;
1318
1319 primary_if = primary_if_get_selected(bat_priv);
1320 if (!primary_if)
1321 goto out;
1322
1323 /* The new tt_req will be issued only if I'm not waiting for a
1324 * reply from the same orig_node yet */
1325 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1326 if (!tt_req_node)
1327 goto out;
1328
1329 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1330 if (!skb)
1331 goto out;
1332
1333 skb_reserve(skb, ETH_HLEN);
1334
1335 tt_request = (struct tt_query_packet *)skb_put(skb,
1336 sizeof(struct tt_query_packet));
1337
Sven Eckelmann76543d12011-11-20 15:47:38 +01001338 tt_request->header.packet_type = BAT_TT_QUERY;
1339 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1341 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001342 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001343 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001344 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001345 tt_request->flags = TT_REQUEST;
1346
1347 if (full_table)
1348 tt_request->flags |= TT_FULL_TABLE;
1349
1350 neigh_node = orig_node_get_router(dst_orig_node);
1351 if (!neigh_node)
1352 goto out;
1353
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001354 bat_dbg(DBG_TT, bat_priv,
1355 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1356 dst_orig_node->orig, neigh_node->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001357 (full_table ? 'F' : '.'));
1358
1359 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1360 ret = 0;
1361
1362out:
1363 if (neigh_node)
1364 neigh_node_free_ref(neigh_node);
1365 if (primary_if)
1366 hardif_free_ref(primary_if);
1367 if (ret)
1368 kfree_skb(skb);
1369 if (ret && tt_req_node) {
1370 spin_lock_bh(&bat_priv->tt_req_list_lock);
1371 list_del(&tt_req_node->list);
1372 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1373 kfree(tt_req_node);
1374 }
1375 return ret;
1376}
1377
1378static bool send_other_tt_response(struct bat_priv *bat_priv,
1379 struct tt_query_packet *tt_request)
1380{
1381 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1382 struct neigh_node *neigh_node = NULL;
1383 struct hard_iface *primary_if = NULL;
1384 uint8_t orig_ttvn, req_ttvn, ttvn;
1385 int ret = false;
1386 unsigned char *tt_buff;
1387 bool full_table;
1388 uint16_t tt_len, tt_tot;
1389 struct sk_buff *skb = NULL;
1390 struct tt_query_packet *tt_response;
1391
1392 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001393 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1394 tt_request->src, tt_request->ttvn, tt_request->dst,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1396
1397 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001398 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399 if (!req_dst_orig_node)
1400 goto out;
1401
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001402 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001403 if (!res_dst_orig_node)
1404 goto out;
1405
1406 neigh_node = orig_node_get_router(res_dst_orig_node);
1407 if (!neigh_node)
1408 goto out;
1409
1410 primary_if = primary_if_get_selected(bat_priv);
1411 if (!primary_if)
1412 goto out;
1413
1414 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1415 req_ttvn = tt_request->ttvn;
1416
Antonio Quartulli015758d2011-07-09 17:52:13 +02001417 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001418 if (orig_ttvn != req_ttvn ||
1419 tt_request->tt_data != req_dst_orig_node->tt_crc)
1420 goto out;
1421
Antonio Quartulli015758d2011-07-09 17:52:13 +02001422 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001423 if (tt_request->flags & TT_FULL_TABLE ||
1424 !req_dst_orig_node->tt_buff)
1425 full_table = true;
1426 else
1427 full_table = false;
1428
1429 /* In this version, fragmentation is not implemented, then
1430 * I'll send only one packet with as much TT entries as I can */
1431 if (!full_table) {
1432 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1433 tt_len = req_dst_orig_node->tt_buff_len;
1434 tt_tot = tt_len / sizeof(struct tt_change);
1435
1436 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1437 tt_len + ETH_HLEN);
1438 if (!skb)
1439 goto unlock;
1440
1441 skb_reserve(skb, ETH_HLEN);
1442 tt_response = (struct tt_query_packet *)skb_put(skb,
1443 sizeof(struct tt_query_packet) + tt_len);
1444 tt_response->ttvn = req_ttvn;
1445 tt_response->tt_data = htons(tt_tot);
1446
1447 tt_buff = skb->data + sizeof(struct tt_query_packet);
1448 /* Copy the last orig_node's OGM buffer */
1449 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1450 req_dst_orig_node->tt_buff_len);
1451
1452 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1453 } else {
1454 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1455 sizeof(struct tt_change);
1456 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1457
1458 skb = tt_response_fill_table(tt_len, ttvn,
1459 bat_priv->tt_global_hash,
1460 primary_if, tt_global_valid_entry,
1461 req_dst_orig_node);
1462 if (!skb)
1463 goto out;
1464
1465 tt_response = (struct tt_query_packet *)skb->data;
1466 }
1467
Sven Eckelmann76543d12011-11-20 15:47:38 +01001468 tt_response->header.packet_type = BAT_TT_QUERY;
1469 tt_response->header.version = COMPAT_VERSION;
1470 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1472 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1473 tt_response->flags = TT_RESPONSE;
1474
1475 if (full_table)
1476 tt_response->flags |= TT_FULL_TABLE;
1477
1478 bat_dbg(DBG_TT, bat_priv,
1479 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1480 res_dst_orig_node->orig, neigh_node->addr,
1481 req_dst_orig_node->orig, req_ttvn);
1482
1483 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1484 ret = true;
1485 goto out;
1486
1487unlock:
1488 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1489
1490out:
1491 if (res_dst_orig_node)
1492 orig_node_free_ref(res_dst_orig_node);
1493 if (req_dst_orig_node)
1494 orig_node_free_ref(req_dst_orig_node);
1495 if (neigh_node)
1496 neigh_node_free_ref(neigh_node);
1497 if (primary_if)
1498 hardif_free_ref(primary_if);
1499 if (!ret)
1500 kfree_skb(skb);
1501 return ret;
1502
1503}
1504static bool send_my_tt_response(struct bat_priv *bat_priv,
1505 struct tt_query_packet *tt_request)
1506{
1507 struct orig_node *orig_node = NULL;
1508 struct neigh_node *neigh_node = NULL;
1509 struct hard_iface *primary_if = NULL;
1510 uint8_t my_ttvn, req_ttvn, ttvn;
1511 int ret = false;
1512 unsigned char *tt_buff;
1513 bool full_table;
1514 uint16_t tt_len, tt_tot;
1515 struct sk_buff *skb = NULL;
1516 struct tt_query_packet *tt_response;
1517
1518 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001519 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1520 tt_request->src, tt_request->ttvn,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1522
1523
1524 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1525 req_ttvn = tt_request->ttvn;
1526
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001527 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528 if (!orig_node)
1529 goto out;
1530
1531 neigh_node = orig_node_get_router(orig_node);
1532 if (!neigh_node)
1533 goto out;
1534
1535 primary_if = primary_if_get_selected(bat_priv);
1536 if (!primary_if)
1537 goto out;
1538
1539 /* If the full table has been explicitly requested or the gap
1540 * is too big send the whole local translation table */
1541 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1542 !bat_priv->tt_buff)
1543 full_table = true;
1544 else
1545 full_table = false;
1546
1547 /* In this version, fragmentation is not implemented, then
1548 * I'll send only one packet with as much TT entries as I can */
1549 if (!full_table) {
1550 spin_lock_bh(&bat_priv->tt_buff_lock);
1551 tt_len = bat_priv->tt_buff_len;
1552 tt_tot = tt_len / sizeof(struct tt_change);
1553
1554 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1555 tt_len + ETH_HLEN);
1556 if (!skb)
1557 goto unlock;
1558
1559 skb_reserve(skb, ETH_HLEN);
1560 tt_response = (struct tt_query_packet *)skb_put(skb,
1561 sizeof(struct tt_query_packet) + tt_len);
1562 tt_response->ttvn = req_ttvn;
1563 tt_response->tt_data = htons(tt_tot);
1564
1565 tt_buff = skb->data + sizeof(struct tt_query_packet);
1566 memcpy(tt_buff, bat_priv->tt_buff,
1567 bat_priv->tt_buff_len);
1568 spin_unlock_bh(&bat_priv->tt_buff_lock);
1569 } else {
1570 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1571 sizeof(struct tt_change);
1572 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1573
1574 skb = tt_response_fill_table(tt_len, ttvn,
1575 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001576 primary_if, tt_local_valid_entry,
1577 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578 if (!skb)
1579 goto out;
1580
1581 tt_response = (struct tt_query_packet *)skb->data;
1582 }
1583
Sven Eckelmann76543d12011-11-20 15:47:38 +01001584 tt_response->header.packet_type = BAT_TT_QUERY;
1585 tt_response->header.version = COMPAT_VERSION;
1586 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001587 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1588 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1589 tt_response->flags = TT_RESPONSE;
1590
1591 if (full_table)
1592 tt_response->flags |= TT_FULL_TABLE;
1593
1594 bat_dbg(DBG_TT, bat_priv,
1595 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1596 orig_node->orig, neigh_node->addr,
1597 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1598
1599 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1600 ret = true;
1601 goto out;
1602
1603unlock:
1604 spin_unlock_bh(&bat_priv->tt_buff_lock);
1605out:
1606 if (orig_node)
1607 orig_node_free_ref(orig_node);
1608 if (neigh_node)
1609 neigh_node_free_ref(neigh_node);
1610 if (primary_if)
1611 hardif_free_ref(primary_if);
1612 if (!ret)
1613 kfree_skb(skb);
1614 /* This packet was for me, so it doesn't need to be re-routed */
1615 return true;
1616}
1617
1618bool send_tt_response(struct bat_priv *bat_priv,
1619 struct tt_query_packet *tt_request)
1620{
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001621 if (is_my_mac(tt_request->dst)) {
1622 /* don't answer backbone gws! */
1623 if (bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1624 return true;
1625
Antonio Quartullia73105b2011-04-27 14:27:44 +02001626 return send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001627 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628 return send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001629 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630}
1631
1632static void _tt_update_changes(struct bat_priv *bat_priv,
1633 struct orig_node *orig_node,
1634 struct tt_change *tt_change,
1635 uint16_t tt_num_changes, uint8_t ttvn)
1636{
1637 int i;
1638
1639 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001640 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641 tt_global_del(bat_priv, orig_node,
1642 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001643 "tt removed by changes",
1644 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001645 else
1646 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001647 (tt_change + i)->addr, ttvn, false,
1648 (tt_change + i)->flags &
1649 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001650 /* In case of problem while storing a
1651 * global_entry, we stop the updating
1652 * procedure without committing the
1653 * ttvn change. This will avoid to send
1654 * corrupted data on tt_request
1655 */
1656 return;
1657 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001658 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659}
1660
1661static void tt_fill_gtable(struct bat_priv *bat_priv,
1662 struct tt_query_packet *tt_response)
1663{
1664 struct orig_node *orig_node = NULL;
1665
1666 orig_node = orig_hash_find(bat_priv, tt_response->src);
1667 if (!orig_node)
1668 goto out;
1669
1670 /* Purge the old table first.. */
1671 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1672
1673 _tt_update_changes(bat_priv, orig_node,
1674 (struct tt_change *)(tt_response + 1),
1675 tt_response->tt_data, tt_response->ttvn);
1676
1677 spin_lock_bh(&orig_node->tt_buff_lock);
1678 kfree(orig_node->tt_buff);
1679 orig_node->tt_buff_len = 0;
1680 orig_node->tt_buff = NULL;
1681 spin_unlock_bh(&orig_node->tt_buff_lock);
1682
1683 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1684
1685out:
1686 if (orig_node)
1687 orig_node_free_ref(orig_node);
1688}
1689
Marek Lindnera943cac2011-07-30 13:10:18 +02001690static void tt_update_changes(struct bat_priv *bat_priv,
1691 struct orig_node *orig_node,
1692 uint16_t tt_num_changes, uint8_t ttvn,
1693 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694{
1695 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1696 ttvn);
1697
1698 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1699 tt_num_changes);
1700 atomic_set(&orig_node->last_ttvn, ttvn);
1701}
1702
1703bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1704{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001705 struct tt_local_entry *tt_local_entry = NULL;
1706 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001709 if (!tt_local_entry)
1710 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001711 /* Check if the client has been logically deleted (but is kept for
1712 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001713 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001714 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001715 ret = true;
1716out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001718 tt_local_entry_free_ref(tt_local_entry);
1719 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001720}
1721
1722void handle_tt_response(struct bat_priv *bat_priv,
1723 struct tt_query_packet *tt_response)
1724{
1725 struct tt_req_node *node, *safe;
1726 struct orig_node *orig_node = NULL;
1727
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001728 bat_dbg(DBG_TT, bat_priv,
1729 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1730 tt_response->src, tt_response->ttvn, tt_response->tt_data,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001731 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1732
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001733 /* we should have never asked a backbone gw */
1734 if (bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1735 goto out;
1736
Antonio Quartullia73105b2011-04-27 14:27:44 +02001737 orig_node = orig_hash_find(bat_priv, tt_response->src);
1738 if (!orig_node)
1739 goto out;
1740
1741 if (tt_response->flags & TT_FULL_TABLE)
1742 tt_fill_gtable(bat_priv, tt_response);
1743 else
1744 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1745 tt_response->ttvn,
1746 (struct tt_change *)(tt_response + 1));
1747
1748 /* Delete the tt_req_node from pending tt_requests list */
1749 spin_lock_bh(&bat_priv->tt_req_list_lock);
1750 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1751 if (!compare_eth(node->addr, tt_response->src))
1752 continue;
1753 list_del(&node->list);
1754 kfree(node);
1755 }
1756 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1757
1758 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001760 /* Roaming phase is over: tables are in sync again. I can
1761 * unset the flag */
1762 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763out:
1764 if (orig_node)
1765 orig_node_free_ref(orig_node);
1766}
1767
1768int tt_init(struct bat_priv *bat_priv)
1769{
1770 if (!tt_local_init(bat_priv))
1771 return 0;
1772
1773 if (!tt_global_init(bat_priv))
1774 return 0;
1775
1776 tt_start_timer(bat_priv);
1777
1778 return 1;
1779}
1780
Antonio Quartullicc47f662011-04-27 14:27:57 +02001781static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001783 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784
Antonio Quartullicc47f662011-04-27 14:27:57 +02001785 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786
Antonio Quartullicc47f662011-04-27 14:27:57 +02001787 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1788 list_del(&node->list);
1789 kfree(node);
1790 }
1791
1792 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1793}
1794
1795static void tt_roam_purge(struct bat_priv *bat_priv)
1796{
1797 struct tt_roam_node *node, *safe;
1798
1799 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1800 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001801 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001802 continue;
1803
1804 list_del(&node->list);
1805 kfree(node);
1806 }
1807 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1808}
1809
1810/* This function checks whether the client already reached the
1811 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1812 * will not be sent.
1813 *
1814 * returns true if the ROAMING_ADV can be sent, false otherwise */
1815static bool tt_check_roam_count(struct bat_priv *bat_priv,
1816 uint8_t *client)
1817{
1818 struct tt_roam_node *tt_roam_node;
1819 bool ret = false;
1820
1821 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1822 /* The new tt_req will be issued only if I'm not waiting for a
1823 * reply from the same orig_node yet */
1824 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1825 if (!compare_eth(tt_roam_node->addr, client))
1826 continue;
1827
Marek Lindner032b7962011-12-20 19:30:40 +08001828 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001829 continue;
1830
1831 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1832 /* Sorry, you roamed too many times! */
1833 goto unlock;
1834 ret = true;
1835 break;
1836 }
1837
1838 if (!ret) {
1839 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1840 if (!tt_roam_node)
1841 goto unlock;
1842
1843 tt_roam_node->first_time = jiffies;
1844 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1845 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1846
1847 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1848 ret = true;
1849 }
1850
1851unlock:
1852 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1853 return ret;
1854}
1855
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001856static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1857 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001858{
1859 struct neigh_node *neigh_node = NULL;
1860 struct sk_buff *skb = NULL;
1861 struct roam_adv_packet *roam_adv_packet;
1862 int ret = 1;
1863 struct hard_iface *primary_if;
1864
1865 /* before going on we have to check whether the client has
1866 * already roamed to us too many times */
1867 if (!tt_check_roam_count(bat_priv, client))
1868 goto out;
1869
1870 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1871 if (!skb)
1872 goto out;
1873
1874 skb_reserve(skb, ETH_HLEN);
1875
1876 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1877 sizeof(struct roam_adv_packet));
1878
Sven Eckelmann76543d12011-11-20 15:47:38 +01001879 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1880 roam_adv_packet->header.version = COMPAT_VERSION;
1881 roam_adv_packet->header.ttl = TTL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001882 primary_if = primary_if_get_selected(bat_priv);
1883 if (!primary_if)
1884 goto out;
1885 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1886 hardif_free_ref(primary_if);
1887 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1888 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1889
1890 neigh_node = orig_node_get_router(orig_node);
1891 if (!neigh_node)
1892 goto out;
1893
1894 bat_dbg(DBG_TT, bat_priv,
1895 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1896 orig_node->orig, client, neigh_node->addr);
1897
1898 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1899 ret = 0;
1900
1901out:
1902 if (neigh_node)
1903 neigh_node_free_ref(neigh_node);
1904 if (ret)
1905 kfree_skb(skb);
1906 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001907}
1908
1909static void tt_purge(struct work_struct *work)
1910{
1911 struct delayed_work *delayed_work =
1912 container_of(work, struct delayed_work, work);
1913 struct bat_priv *bat_priv =
1914 container_of(delayed_work, struct bat_priv, tt_work);
1915
1916 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001917 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001919 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920
1921 tt_start_timer(bat_priv);
1922}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001923
1924void tt_free(struct bat_priv *bat_priv)
1925{
1926 cancel_delayed_work_sync(&bat_priv->tt_work);
1927
1928 tt_local_table_free(bat_priv);
1929 tt_global_table_free(bat_priv);
1930 tt_req_list_free(bat_priv);
1931 tt_changes_list_free(bat_priv);
1932 tt_roam_list_free(bat_priv);
1933
1934 kfree(bat_priv->tt_buff);
1935}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001936
Antonio Quartulli697f2532011-11-07 16:47:01 +01001937/* This function will enable or disable the specified flags for all the entries
1938 * in the given hash table and returns the number of modified entries */
1939static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1940 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001941{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001942 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001943 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001944 struct hlist_head *head;
1945 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001946 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001947
1948 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001949 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001950
1951 for (i = 0; i < hash->size; i++) {
1952 head = &hash->table[i];
1953
1954 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001955 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001956 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001957 if (enable) {
1958 if ((tt_common_entry->flags & flags) == flags)
1959 continue;
1960 tt_common_entry->flags |= flags;
1961 } else {
1962 if (!(tt_common_entry->flags & flags))
1963 continue;
1964 tt_common_entry->flags &= ~flags;
1965 }
1966 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001967 }
1968 rcu_read_unlock();
1969 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001970out:
1971 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001972}
1973
1974/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1975static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1976{
1977 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001978 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001979 struct tt_local_entry *tt_local_entry;
1980 struct hlist_node *node, *node_tmp;
1981 struct hlist_head *head;
1982 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001983 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001984
1985 if (!hash)
1986 return;
1987
1988 for (i = 0; i < hash->size; i++) {
1989 head = &hash->table[i];
1990 list_lock = &hash->list_locks[i];
1991
1992 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001993 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001994 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001995 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001996 continue;
1997
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001998 bat_dbg(DBG_TT, bat_priv,
1999 "Deleting local tt entry (%pM): pending\n",
2000 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002001
2002 atomic_dec(&bat_priv->num_local_tt);
2003 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002004 tt_local_entry = container_of(tt_common_entry,
2005 struct tt_local_entry,
2006 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002007 tt_local_entry_free_ref(tt_local_entry);
2008 }
2009 spin_unlock_bh(list_lock);
2010 }
2011
2012}
2013
2014void tt_commit_changes(struct bat_priv *bat_priv)
2015{
Antonio Quartulli697f2532011-11-07 16:47:01 +01002016 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
2017 TT_CLIENT_NEW, false);
2018 /* all the reset entries have now to be effectively counted as local
2019 * entries */
2020 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002021 tt_local_purge_pending_clients(bat_priv);
2022
2023 /* Increment the TTVN only once per OGM interval */
2024 atomic_inc(&bat_priv->ttvn);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002025 bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
2026 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002027 bat_priv->tt_poss_change = false;
2028}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002029
2030bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
2031{
2032 struct tt_local_entry *tt_local_entry = NULL;
2033 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002034 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002035
2036 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002037 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002038
2039 tt_local_entry = tt_local_hash_find(bat_priv, dst);
2040 if (!tt_local_entry)
2041 goto out;
2042
2043 tt_global_entry = tt_global_hash_find(bat_priv, src);
2044 if (!tt_global_entry)
2045 goto out;
2046
Marek Lindner5870adc2012-06-20 17:16:05 +02002047 if (!_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002048 goto out;
2049
Marek Lindner5870adc2012-06-20 17:16:05 +02002050 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002051
2052out:
2053 if (tt_global_entry)
2054 tt_global_entry_free_ref(tt_global_entry);
2055 if (tt_local_entry)
2056 tt_local_entry_free_ref(tt_local_entry);
2057 return ret;
2058}
Marek Lindnera943cac2011-07-30 13:10:18 +02002059
2060void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
2061 const unsigned char *tt_buff, uint8_t tt_num_changes,
2062 uint8_t ttvn, uint16_t tt_crc)
2063{
2064 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2065 bool full_table = true;
2066
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002067 /* don't care about a backbone gateways updates. */
2068 if (bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2069 return;
2070
Antonio Quartulli17071572011-11-07 16:36:40 +01002071 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2072 * increased by one -> we can apply the attached changes */
2073 if ((!orig_node->tt_initialised && ttvn == 1) ||
2074 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002075 /* the OGM could not contain the changes due to their size or
2076 * because they have already been sent TT_OGM_APPEND_MAX times.
2077 * In this case send a tt request */
2078 if (!tt_num_changes) {
2079 full_table = false;
2080 goto request_table;
2081 }
2082
2083 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
2084 (struct tt_change *)tt_buff);
2085
2086 /* Even if we received the precomputed crc with the OGM, we
2087 * prefer to recompute it to spot any possible inconsistency
2088 * in the global table */
2089 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
2090
2091 /* The ttvn alone is not enough to guarantee consistency
2092 * because a single value could represent different states
2093 * (due to the wrap around). Thus a node has to check whether
2094 * the resulting table (after applying the changes) is still
2095 * consistent or not. E.g. a node could disconnect while its
2096 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2097 * checking the CRC value is mandatory to detect the
2098 * inconsistency */
2099 if (orig_node->tt_crc != tt_crc)
2100 goto request_table;
2101
2102 /* Roaming phase is over: tables are in sync again. I can
2103 * unset the flag */
2104 orig_node->tt_poss_change = false;
2105 } else {
2106 /* if we missed more than one change or our tables are not
2107 * in sync anymore -> request fresh tt data */
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002108
Antonio Quartulli17071572011-11-07 16:36:40 +01002109 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2110 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002111request_table:
Sven Eckelmann86ceb362012-03-07 09:07:45 +01002112 bat_dbg(DBG_TT, bat_priv,
2113 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2114 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2115 orig_node->tt_crc, tt_num_changes);
Marek Lindnera943cac2011-07-30 13:10:18 +02002116 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
2117 full_table);
2118 return;
2119 }
2120 }
2121}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002122
2123/* returns true whether we know that the client has moved from its old
2124 * originator to another one. This entry is kept is still kept for consistency
2125 * purposes
2126 */
2127bool tt_global_client_is_roaming(struct bat_priv *bat_priv, uint8_t *addr)
2128{
2129 struct tt_global_entry *tt_global_entry;
2130 bool ret = false;
2131
2132 tt_global_entry = tt_global_hash_find(bat_priv, addr);
2133 if (!tt_global_entry)
2134 goto out;
2135
2136 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
2137 tt_global_entry_free_ref(tt_global_entry);
2138out:
2139 return ret;
2140}