blob: a38d315d3cd68a8be478a1f5192127353699aa1f [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020025#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020026#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020029#include "routing.h"
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);
144 atomic_dec(&orig_entry->orig_node->tt_size);
145 orig_node_free_ref(orig_entry->orig_node);
146 kfree(orig_entry);
147}
148
149static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
150{
151 call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200152}
153
Antonio Quartulliff66c972011-06-30 01:14:00 +0200154static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
155 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200156{
157 struct tt_change_node *tt_change_node;
158
159 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
160
161 if (!tt_change_node)
162 return;
163
Antonio Quartulliff66c972011-06-30 01:14:00 +0200164 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200165 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
166
167 spin_lock_bh(&bat_priv->tt_changes_list_lock);
168 /* track the change in the OGMinterval list */
169 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
170 atomic_inc(&bat_priv->tt_local_changes);
171 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
172
173 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
174}
175
176int tt_len(int changes_num)
177{
178 return changes_num * sizeof(struct tt_change);
179}
180
181static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200183 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 return 1;
185
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200186 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200188 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189 return 0;
190
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 return 1;
192}
193
Antonio Quartullibc279082011-07-07 15:35:35 +0200194void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
195 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196{
197 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200198 struct tt_local_entry *tt_local_entry = NULL;
199 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200200 struct hlist_head *head;
201 struct hlist_node *node;
202 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100203 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200205 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200207 if (tt_local_entry) {
208 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200209 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 }
211
Sven Eckelmann704509b2011-05-14 23:14:54 +0200212 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200213 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200214 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200215
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216 bat_dbg(DBG_TT, bat_priv,
217 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
218 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100220 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
221 tt_local_entry->common.flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200222 if (is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100223 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
224 atomic_set(&tt_local_entry->common.refcount, 2);
225 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
227 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000228 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100229 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100231 /* The local entry has to be marked as NEW to avoid to send it in
232 * a full table response going out before the next ttvn increment
233 * (consistency check) */
234 tt_local_entry->common.flags |= TT_CLIENT_NEW;
235
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100236 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
237 &tt_local_entry->common,
238 &tt_local_entry->common.hash_entry);
239
240 if (unlikely(hash_added != 0)) {
241 /* remove the reference for the hash */
242 tt_local_entry_free_ref(tt_local_entry);
243 goto out;
244 }
245
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100246 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200247
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200249 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Antonio Quartullicc47f662011-04-27 14:27:57 +0200251 /* Check whether it is a roaming! */
252 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200253 /* These node are probably going to update their tt table */
254 head = &tt_global_entry->orig_list;
255 rcu_read_lock();
256 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
257 orig_entry->orig_node->tt_poss_change = true;
258
259 send_roam_adv(bat_priv, tt_global_entry->common.addr,
260 orig_entry->orig_node);
261 }
262 rcu_read_unlock();
263 /* The global entry has to be marked as ROAMING and
264 * has to be kept for consistency purpose
265 */
David S. Miller220b07e2011-12-16 15:07:28 -0500266 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100267 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200268 }
269out:
270 if (tt_local_entry)
271 tt_local_entry_free_ref(tt_local_entry);
272 if (tt_global_entry)
273 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274}
275
Antonio Quartullia73105b2011-04-27 14:27:44 +0200276int tt_changes_fill_buffer(struct bat_priv *bat_priv,
277 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200279 int count = 0, tot_changes = 0;
280 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Antonio Quartullia73105b2011-04-27 14:27:44 +0200282 if (buff_len > 0)
283 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
Antonio Quartullia73105b2011-04-27 14:27:44 +0200285 spin_lock_bh(&bat_priv->tt_changes_list_lock);
286 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
Antonio Quartullia73105b2011-04-27 14:27:44 +0200288 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100289 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200290 if (count < tot_changes) {
291 memcpy(buff + tt_len(count),
292 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 count++;
294 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200295 list_del(&entry->list);
296 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200298 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299
Antonio Quartullia73105b2011-04-27 14:27:44 +0200300 /* Keep the buffer for possible tt_request */
301 spin_lock_bh(&bat_priv->tt_buff_lock);
302 kfree(bat_priv->tt_buff);
303 bat_priv->tt_buff_len = 0;
304 bat_priv->tt_buff = NULL;
305 /* We check whether this new OGM has no changes due to size
306 * problems */
307 if (buff_len > 0) {
308 /**
309 * if kmalloc() fails we will reply with the full table
310 * instead of providing the diff
311 */
312 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
313 if (bat_priv->tt_buff) {
314 memcpy(bat_priv->tt_buff, buff, buff_len);
315 bat_priv->tt_buff_len = buff_len;
316 }
317 }
318 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Antonio Quartullia73105b2011-04-27 14:27:44 +0200320 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321}
322
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200323int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324{
325 struct net_device *net_dev = (struct net_device *)seq->private;
326 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200327 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100328 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200329 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000330 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200332 uint32_t i;
333 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334
Marek Lindner32ae9b22011-04-20 15:40:58 +0200335 primary_if = primary_if_get_selected(bat_priv);
336 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100337 ret = seq_printf(seq,
338 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200339 net_dev->name);
340 goto out;
341 }
342
343 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100344 ret = seq_printf(seq,
345 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200346 net_dev->name);
347 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 }
349
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100350 seq_printf(seq,
351 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200352 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 for (i = 0; i < hash->size; i++) {
355 head = &hash->table[i];
356
Marek Lindner7aadf882011-02-18 12:28:09 +0000357 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100358 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000359 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200360 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100361 tt_common_entry->addr,
362 (tt_common_entry->flags &
363 TT_CLIENT_ROAM ? 'R' : '.'),
364 (tt_common_entry->flags &
365 TT_CLIENT_NOPURGE ? 'P' : '.'),
366 (tt_common_entry->flags &
367 TT_CLIENT_NEW ? 'N' : '.'),
368 (tt_common_entry->flags &
369 TT_CLIENT_PENDING ? 'X' : '.'),
370 (tt_common_entry->flags &
371 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000373 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200375out:
376 if (primary_if)
377 hardif_free_ref(primary_if);
378 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379}
380
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200381static void tt_local_set_pending(struct bat_priv *bat_priv,
382 struct tt_local_entry *tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100383 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100385 tt_local_event(bat_priv, tt_local_entry->common.addr,
386 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387
Antonio Quartulli015758d2011-07-09 17:52:13 +0200388 /* The local client has to be marked as "pending to be removed" but has
389 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200390 * response issued before the net ttvn increment (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100391 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100392
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100393 bat_dbg(DBG_TT, bat_priv,
394 "Local tt entry (%pM) pending to be removed: %s\n",
395 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396}
397
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200399 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200401 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200403 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200404 if (!tt_local_entry)
405 goto out;
406
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200407 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100408 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200409out:
410 if (tt_local_entry)
411 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412}
413
Antonio Quartullia73105b2011-04-27 14:27:44 +0200414static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200416 struct hashtable_t *hash = bat_priv->tt_local_hash;
417 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100418 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000419 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200421 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200422 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 for (i = 0; i < hash->size; i++) {
425 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200426 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200428 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100429 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000430 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100431 tt_local_entry = container_of(tt_common_entry,
432 struct tt_local_entry,
433 common);
434 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000435 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200437 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100438 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200439 continue;
440
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100441 if (!has_timed_out(tt_local_entry->last_seen,
Marek Lindner032b7962011-12-20 19:30:40 +0800442 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000443 continue;
444
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200445 tt_local_set_pending(bat_priv, tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100446 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200448 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449 }
450
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451}
452
Antonio Quartullia73105b2011-04-27 14:27:44 +0200453static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200455 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100457 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200458 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200459 struct hlist_node *node, *node_tmp;
460 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200461 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200462
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200463 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 return;
465
Antonio Quartullia73105b2011-04-27 14:27:44 +0200466 hash = bat_priv->tt_local_hash;
467
468 for (i = 0; i < hash->size; i++) {
469 head = &hash->table[i];
470 list_lock = &hash->list_locks[i];
471
472 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100473 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200474 head, hash_entry) {
475 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100476 tt_local_entry = container_of(tt_common_entry,
477 struct tt_local_entry,
478 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200479 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200480 }
481 spin_unlock_bh(list_lock);
482 }
483
484 hash_destroy(hash);
485
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200486 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487}
488
Antonio Quartullia73105b2011-04-27 14:27:44 +0200489static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200491 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492 return 1;
493
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200494 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200496 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 return 0;
498
499 return 1;
500}
501
Antonio Quartullia73105b2011-04-27 14:27:44 +0200502static void tt_changes_list_free(struct bat_priv *bat_priv)
503{
504 struct tt_change_node *entry, *safe;
505
506 spin_lock_bh(&bat_priv->tt_changes_list_lock);
507
508 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
509 list) {
510 list_del(&entry->list);
511 kfree(entry);
512 }
513
514 atomic_set(&bat_priv->tt_local_changes, 0);
515 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
516}
517
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200518/* find out if an orig_node is already in the list of a tt_global_entry.
519 * returns 1 if found, 0 otherwise
520 */
521static bool tt_global_entry_has_orig(const struct tt_global_entry *entry,
522 const struct orig_node *orig_node)
523{
524 struct tt_orig_list_entry *tmp_orig_entry;
525 const struct hlist_head *head;
526 struct hlist_node *node;
527 bool found = false;
528
529 rcu_read_lock();
530 head = &entry->orig_list;
531 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
532 if (tmp_orig_entry->orig_node == orig_node) {
533 found = true;
534 break;
535 }
536 }
537 rcu_read_unlock();
538 return found;
539}
540
541static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
542 struct orig_node *orig_node,
543 int ttvn)
544{
545 struct tt_orig_list_entry *orig_entry;
546
547 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
548 if (!orig_entry)
549 return;
550
551 INIT_HLIST_NODE(&orig_entry->list);
552 atomic_inc(&orig_node->refcount);
553 atomic_inc(&orig_node->tt_size);
554 orig_entry->orig_node = orig_node;
555 orig_entry->ttvn = ttvn;
556
557 spin_lock_bh(&tt_global_entry->list_lock);
558 hlist_add_head_rcu(&orig_entry->list,
559 &tt_global_entry->orig_list);
560 spin_unlock_bh(&tt_global_entry->list_lock);
561}
562
Antonio Quartullia73105b2011-04-27 14:27:44 +0200563/* caller must hold orig_node refcount */
564int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200565 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
566 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200568 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200569 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100570 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571
Antonio Quartullia73105b2011-04-27 14:27:44 +0200572 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573
Antonio Quartullia73105b2011-04-27 14:27:44 +0200574 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200575 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
576 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200577 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200578 goto out;
579
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100580 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200581
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100582 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200583 tt_global_entry->roam_at = 0;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200584 atomic_set(&tt_global_entry->common.refcount, 2);
585
586 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
587 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200588
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100589 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
590 choose_orig, &tt_global_entry->common,
591 &tt_global_entry->common.hash_entry);
592
593 if (unlikely(hash_added != 0)) {
594 /* remove the reference for the hash */
595 tt_global_entry_free_ref(tt_global_entry);
596 goto out_remove;
597 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200598
599 tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200600 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200601 /* there is already a global entry, use this one. */
602
603 /* If there is the TT_CLIENT_ROAM flag set, there is only one
604 * originator left in the list and we previously received a
605 * delete + roaming change for this originator.
606 *
607 * We should first delete the old originator before adding the
608 * new one.
609 */
610 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
611 tt_global_del_orig_list(tt_global_entry);
612 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
613 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200615
616 if (!tt_global_entry_has_orig(tt_global_entry, orig_node))
617 tt_global_add_orig_entry(tt_global_entry, orig_node,
618 ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200620
Antonio Quartullibc279082011-07-07 15:35:35 +0200621 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100622 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200623
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624 bat_dbg(DBG_TT, bat_priv,
625 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100626 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200627
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100628out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200629 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100630 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200631 "global tt received", roaming);
632 ret = 1;
633out:
634 if (tt_global_entry)
635 tt_global_entry_free_ref(tt_global_entry);
636 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637}
638
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200639/* print all orig nodes who announce the address for this global entry.
640 * it is assumed that the caller holds rcu_read_lock();
641 */
642static void tt_global_print_entry(struct tt_global_entry *tt_global_entry,
643 struct seq_file *seq)
644{
645 struct hlist_head *head;
646 struct hlist_node *node;
647 struct tt_orig_list_entry *orig_entry;
648 struct tt_common_entry *tt_common_entry;
649 uint16_t flags;
650 uint8_t last_ttvn;
651
652 tt_common_entry = &tt_global_entry->common;
653
654 head = &tt_global_entry->orig_list;
655
656 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
657 flags = tt_common_entry->flags;
658 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
659 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
660 tt_global_entry->common.addr, orig_entry->ttvn,
661 orig_entry->orig_node->orig, last_ttvn,
662 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
663 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
664 }
665}
666
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200667int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668{
669 struct net_device *net_dev = (struct net_device *)seq->private;
670 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200671 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100672 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200673 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200674 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000675 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200677 uint32_t i;
678 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679
Marek Lindner32ae9b22011-04-20 15:40:58 +0200680 primary_if = primary_if_get_selected(bat_priv);
681 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100682 ret = seq_printf(seq,
683 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200684 net_dev->name);
685 goto out;
686 }
687
688 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100689 ret = seq_printf(seq,
690 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200691 net_dev->name);
692 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693 }
694
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200695 seq_printf(seq,
696 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200698 seq_printf(seq, " %-13s %s %-15s %s %s\n",
699 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701 for (i = 0; i < hash->size; i++) {
702 head = &hash->table[i];
703
Marek Lindner7aadf882011-02-18 12:28:09 +0000704 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100705 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000706 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100707 tt_global_entry = container_of(tt_common_entry,
708 struct tt_global_entry,
709 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200710 tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000712 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200714out:
715 if (primary_if)
716 hardif_free_ref(primary_if);
717 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718}
719
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200720/* deletes the orig list of a tt_global_entry */
721static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200723 struct hlist_head *head;
724 struct hlist_node *node, *safe;
725 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200726
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200727 spin_lock_bh(&tt_global_entry->list_lock);
728 head = &tt_global_entry->orig_list;
729 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
730 hlist_del_rcu(node);
731 tt_orig_list_entry_free_ref(orig_entry);
732 }
733 spin_unlock_bh(&tt_global_entry->list_lock);
734
735}
736
737static void tt_global_del_orig_entry(struct bat_priv *bat_priv,
738 struct tt_global_entry *tt_global_entry,
739 struct orig_node *orig_node,
740 const char *message)
741{
742 struct hlist_head *head;
743 struct hlist_node *node, *safe;
744 struct tt_orig_list_entry *orig_entry;
745
746 spin_lock_bh(&tt_global_entry->list_lock);
747 head = &tt_global_entry->orig_list;
748 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
749 if (orig_entry->orig_node == orig_node) {
750 bat_dbg(DBG_TT, bat_priv,
751 "Deleting %pM from global tt entry %pM: %s\n",
752 orig_node->orig, tt_global_entry->common.addr,
753 message);
754 hlist_del_rcu(node);
755 tt_orig_list_entry_free_ref(orig_entry);
756 }
757 }
758 spin_unlock_bh(&tt_global_entry->list_lock);
759}
760
761static void tt_global_del_struct(struct bat_priv *bat_priv,
762 struct tt_global_entry *tt_global_entry,
763 const char *message)
764{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200765 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200766 "Deleting global tt entry %pM: %s\n",
767 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200768
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100769 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
770 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200771 tt_global_entry_free_ref(tt_global_entry);
772
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773}
774
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200775/* If the client is to be deleted, we check if it is the last origantor entry
776 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
777 * otherwise we simply remove the originator scheduled for deletion.
778 */
779static void tt_global_del_roaming(struct bat_priv *bat_priv,
780 struct tt_global_entry *tt_global_entry,
781 struct orig_node *orig_node,
782 const char *message)
783{
784 bool last_entry = true;
785 struct hlist_head *head;
786 struct hlist_node *node;
787 struct tt_orig_list_entry *orig_entry;
788
789 /* no local entry exists, case 1:
790 * Check if this is the last one or if other entries exist.
791 */
792
793 rcu_read_lock();
794 head = &tt_global_entry->orig_list;
795 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
796 if (orig_entry->orig_node != orig_node) {
797 last_entry = false;
798 break;
799 }
800 }
801 rcu_read_unlock();
802
803 if (last_entry) {
804 /* its the last one, mark for roaming. */
805 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
806 tt_global_entry->roam_at = jiffies;
807 } else
808 /* there is another entry, we can simply delete this
809 * one and can still use the other one.
810 */
811 tt_global_del_orig_entry(bat_priv, tt_global_entry,
812 orig_node, message);
813}
814
815
816
Sven Eckelmannde7aae62012-02-05 18:55:22 +0100817static void tt_global_del(struct bat_priv *bat_priv,
818 struct orig_node *orig_node,
819 const unsigned char *addr,
820 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200822 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100823 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824
Antonio Quartullia73105b2011-04-27 14:27:44 +0200825 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200826 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200827 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200828
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200829 if (!roaming) {
830 tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node,
831 message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800832
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200833 if (hlist_empty(&tt_global_entry->orig_list))
834 tt_global_del_struct(bat_priv, tt_global_entry,
835 message);
836
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800837 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200838 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800839
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200840 /* if we are deleting a global entry due to a roam
841 * event, there are two possibilities:
842 * 1) the client roamed from node A to node B => if there
843 * is only one originator left for this client, we mark
844 * it with TT_CLIENT_ROAM, we start a timer and we
845 * wait for node B to claim it. In case of timeout
846 * the entry is purged.
847 *
848 * If there are other originators left, we directly delete
849 * the originator.
850 * 2) the client roamed to us => we can directly delete
851 * the global entry, since it is useless now. */
852
853 tt_local_entry = tt_local_hash_find(bat_priv,
854 tt_global_entry->common.addr);
855 if (tt_local_entry) {
856 /* local entry exists, case 2: client roamed to us. */
857 tt_global_del_orig_list(tt_global_entry);
858 tt_global_del_struct(bat_priv, tt_global_entry, message);
859 } else
860 /* no local entry exists, case 1: check for roaming */
861 tt_global_del_roaming(bat_priv, tt_global_entry, orig_node,
862 message);
863
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800864
Antonio Quartullicc47f662011-04-27 14:27:57 +0200865out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200866 if (tt_global_entry)
867 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100868 if (tt_local_entry)
869 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200870}
871
872void tt_global_del_orig(struct bat_priv *bat_priv,
873 struct orig_node *orig_node, const char *message)
874{
875 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100876 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200877 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200878 struct hashtable_t *hash = bat_priv->tt_global_hash;
879 struct hlist_node *node, *safe;
880 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200881 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200882
Simon Wunderlich6e801492011-10-19 10:28:26 +0200883 if (!hash)
884 return;
885
Antonio Quartullia73105b2011-04-27 14:27:44 +0200886 for (i = 0; i < hash->size; i++) {
887 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200888 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200890 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100891 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100892 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100893 tt_global_entry = container_of(tt_common_entry,
894 struct tt_global_entry,
895 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200896
897 tt_global_del_orig_entry(bat_priv, tt_global_entry,
898 orig_node, message);
899
900 if (hlist_empty(&tt_global_entry->orig_list)) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200901 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200902 "Deleting global tt entry %pM: %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100903 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200904 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200905 hlist_del_rcu(node);
906 tt_global_entry_free_ref(tt_global_entry);
907 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200908 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200909 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200911 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100912 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913}
914
Antonio Quartullicc47f662011-04-27 14:27:57 +0200915static void tt_global_roam_purge(struct bat_priv *bat_priv)
916{
917 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100918 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200919 struct tt_global_entry *tt_global_entry;
920 struct hlist_node *node, *node_tmp;
921 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200922 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200923 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200924
Antonio Quartullicc47f662011-04-27 14:27:57 +0200925 for (i = 0; i < hash->size; i++) {
926 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200927 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200928
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200929 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100930 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200931 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100932 tt_global_entry = container_of(tt_common_entry,
933 struct tt_global_entry,
934 common);
935 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200936 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100937 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800938 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200939 continue;
940
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100941 bat_dbg(DBG_TT, bat_priv,
942 "Deleting global tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100943 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200944
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200945 hlist_del_rcu(node);
946 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200947 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200948 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200949 }
950
Antonio Quartullicc47f662011-04-27 14:27:57 +0200951}
952
Antonio Quartullia73105b2011-04-27 14:27:44 +0200953static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000954{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200955 struct hashtable_t *hash;
956 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100957 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200958 struct tt_global_entry *tt_global_entry;
959 struct hlist_node *node, *node_tmp;
960 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200961 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200962
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200963 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000964 return;
965
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200966 hash = bat_priv->tt_global_hash;
967
968 for (i = 0; i < hash->size; i++) {
969 head = &hash->table[i];
970 list_lock = &hash->list_locks[i];
971
972 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100973 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200974 head, hash_entry) {
975 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100976 tt_global_entry = container_of(tt_common_entry,
977 struct tt_global_entry,
978 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200979 tt_global_entry_free_ref(tt_global_entry);
980 }
981 spin_unlock_bh(list_lock);
982 }
983
984 hash_destroy(hash);
985
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200986 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000987}
988
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200989static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
990 struct tt_global_entry *tt_global_entry)
991{
992 bool ret = false;
993
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100994 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
995 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200996 ret = true;
997
998 return ret;
999}
1000
Sven Eckelmann747e4222011-05-14 23:14:50 +02001001struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001002 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001003{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001004 struct tt_local_entry *tt_local_entry = NULL;
1005 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001006 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001007 struct neigh_node *router = NULL;
1008 struct hlist_head *head;
1009 struct hlist_node *node;
1010 struct tt_orig_list_entry *orig_entry;
1011 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001013 if (src && atomic_read(&bat_priv->ap_isolation)) {
1014 tt_local_entry = tt_local_hash_find(bat_priv, src);
1015 if (!tt_local_entry)
1016 goto out;
1017 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001018
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001019 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001020 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001021 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001023 /* check whether the clients should not communicate due to AP
1024 * isolation */
1025 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
1026 goto out;
1027
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001028 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001029
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001030 rcu_read_lock();
1031 head = &tt_global_entry->orig_list;
1032 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1033 router = orig_node_get_router(orig_entry->orig_node);
1034 if (!router)
1035 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001036
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001037 if (router->tq_avg > best_tq) {
1038 orig_node = orig_entry->orig_node;
1039 best_tq = router->tq_avg;
1040 }
1041 neigh_node_free_ref(router);
1042 }
1043 /* found anything? */
1044 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1045 orig_node = NULL;
1046 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001047out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001048 if (tt_global_entry)
1049 tt_global_entry_free_ref(tt_global_entry);
1050 if (tt_local_entry)
1051 tt_local_entry_free_ref(tt_local_entry);
1052
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001053 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001055
1056/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001057static uint16_t tt_global_crc(struct bat_priv *bat_priv,
1058 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001059{
1060 uint16_t total = 0, total_one;
1061 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001062 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001063 struct tt_global_entry *tt_global_entry;
1064 struct hlist_node *node;
1065 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001066 uint32_t i;
1067 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001068
1069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1071
1072 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001073 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001074 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001075 tt_global_entry = container_of(tt_common_entry,
1076 struct tt_global_entry,
1077 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001078 /* Roaming clients are in the global table for
1079 * consistency only. They don't have to be
1080 * taken into account while computing the
1081 * global crc
1082 */
1083 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1084 continue;
1085
1086 /* find out if this global entry is announced by this
1087 * originator
1088 */
1089 if (!tt_global_entry_has_orig(tt_global_entry,
1090 orig_node))
1091 continue;
1092
1093 total_one = 0;
1094 for (j = 0; j < ETH_ALEN; j++)
1095 total_one = crc16_byte(total_one,
1096 tt_global_entry->common.addr[j]);
1097 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001098 }
1099 rcu_read_unlock();
1100 }
1101
1102 return total;
1103}
1104
1105/* Calculates the checksum of the local table */
1106uint16_t tt_local_crc(struct bat_priv *bat_priv)
1107{
1108 uint16_t total = 0, total_one;
1109 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001110 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001111 struct hlist_node *node;
1112 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001113 uint32_t i;
1114 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001115
1116 for (i = 0; i < hash->size; i++) {
1117 head = &hash->table[i];
1118
1119 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001120 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001121 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001122 /* not yet committed clients have not to be taken into
1123 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001124 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001125 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001126 total_one = 0;
1127 for (j = 0; j < ETH_ALEN; j++)
1128 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001129 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001130 total ^= total_one;
1131 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001132 rcu_read_unlock();
1133 }
1134
1135 return total;
1136}
1137
1138static void tt_req_list_free(struct bat_priv *bat_priv)
1139{
1140 struct tt_req_node *node, *safe;
1141
1142 spin_lock_bh(&bat_priv->tt_req_list_lock);
1143
1144 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1145 list_del(&node->list);
1146 kfree(node);
1147 }
1148
1149 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1150}
1151
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001152static void tt_save_orig_buffer(struct bat_priv *bat_priv,
1153 struct orig_node *orig_node,
1154 const unsigned char *tt_buff,
1155 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001156{
1157 uint16_t tt_buff_len = tt_len(tt_num_changes);
1158
1159 /* Replace the old buffer only if I received something in the
1160 * last OGM (the OGM could carry no changes) */
1161 spin_lock_bh(&orig_node->tt_buff_lock);
1162 if (tt_buff_len > 0) {
1163 kfree(orig_node->tt_buff);
1164 orig_node->tt_buff_len = 0;
1165 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1166 if (orig_node->tt_buff) {
1167 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1168 orig_node->tt_buff_len = tt_buff_len;
1169 }
1170 }
1171 spin_unlock_bh(&orig_node->tt_buff_lock);
1172}
1173
1174static void tt_req_purge(struct bat_priv *bat_priv)
1175{
1176 struct tt_req_node *node, *safe;
1177
1178 spin_lock_bh(&bat_priv->tt_req_list_lock);
1179 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001180 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001181 list_del(&node->list);
1182 kfree(node);
1183 }
1184 }
1185 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1186}
1187
1188/* returns the pointer to the new tt_req_node struct if no request
1189 * has already been issued for this orig_node, NULL otherwise */
1190static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
1191 struct orig_node *orig_node)
1192{
1193 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1194
1195 spin_lock_bh(&bat_priv->tt_req_list_lock);
1196 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1197 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +01001198 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +08001199 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200 goto unlock;
1201 }
1202
1203 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1204 if (!tt_req_node)
1205 goto unlock;
1206
1207 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1208 tt_req_node->issued_at = jiffies;
1209
1210 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1211unlock:
1212 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1213 return tt_req_node;
1214}
1215
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001216/* data_ptr is useless here, but has to be kept to respect the prototype */
1217static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1218{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001219 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001220
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001221 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001222 return 0;
1223 return 1;
1224}
1225
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1227{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001228 const struct tt_common_entry *tt_common_entry = entry_ptr;
1229 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001230 const struct orig_node *orig_node = data_ptr;
1231
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001232 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001233 return 0;
1234
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001235 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1236 common);
1237
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001238 return tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001239}
1240
1241static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1242 struct hashtable_t *hash,
1243 struct hard_iface *primary_if,
1244 int (*valid_cb)(const void *,
1245 const void *),
1246 void *cb_data)
1247{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001248 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001249 struct tt_query_packet *tt_response;
1250 struct tt_change *tt_change;
1251 struct hlist_node *node;
1252 struct hlist_head *head;
1253 struct sk_buff *skb = NULL;
1254 uint16_t tt_tot, tt_count;
1255 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001256 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001257
1258 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1259 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1260 tt_len -= tt_len % sizeof(struct tt_change);
1261 }
1262 tt_tot = tt_len / sizeof(struct tt_change);
1263
1264 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1265 if (!skb)
1266 goto out;
1267
1268 skb_reserve(skb, ETH_HLEN);
1269 tt_response = (struct tt_query_packet *)skb_put(skb,
1270 tt_query_size + tt_len);
1271 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001272
1273 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1274 tt_count = 0;
1275
1276 rcu_read_lock();
1277 for (i = 0; i < hash->size; i++) {
1278 head = &hash->table[i];
1279
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001280 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001281 head, hash_entry) {
1282 if (tt_count == tt_tot)
1283 break;
1284
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001285 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001286 continue;
1287
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001288 memcpy(tt_change->addr, tt_common_entry->addr,
1289 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001290 tt_change->flags = NO_FLAGS;
1291
1292 tt_count++;
1293 tt_change++;
1294 }
1295 }
1296 rcu_read_unlock();
1297
Antonio Quartulli9d852392011-10-17 14:25:13 +02001298 /* store in the message the number of entries we have successfully
1299 * copied */
1300 tt_response->tt_data = htons(tt_count);
1301
Antonio Quartullia73105b2011-04-27 14:27:44 +02001302out:
1303 return skb;
1304}
1305
Marek Lindnera943cac2011-07-30 13:10:18 +02001306static int send_tt_request(struct bat_priv *bat_priv,
1307 struct orig_node *dst_orig_node,
1308 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001309{
1310 struct sk_buff *skb = NULL;
1311 struct tt_query_packet *tt_request;
1312 struct neigh_node *neigh_node = NULL;
1313 struct hard_iface *primary_if;
1314 struct tt_req_node *tt_req_node = NULL;
1315 int ret = 1;
1316
1317 primary_if = primary_if_get_selected(bat_priv);
1318 if (!primary_if)
1319 goto out;
1320
1321 /* The new tt_req will be issued only if I'm not waiting for a
1322 * reply from the same orig_node yet */
1323 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1324 if (!tt_req_node)
1325 goto out;
1326
1327 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1328 if (!skb)
1329 goto out;
1330
1331 skb_reserve(skb, ETH_HLEN);
1332
1333 tt_request = (struct tt_query_packet *)skb_put(skb,
1334 sizeof(struct tt_query_packet));
1335
Sven Eckelmann76543d12011-11-20 15:47:38 +01001336 tt_request->header.packet_type = BAT_TT_QUERY;
1337 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001338 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1339 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001340 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001342 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001343 tt_request->flags = TT_REQUEST;
1344
1345 if (full_table)
1346 tt_request->flags |= TT_FULL_TABLE;
1347
1348 neigh_node = orig_node_get_router(dst_orig_node);
1349 if (!neigh_node)
1350 goto out;
1351
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001352 bat_dbg(DBG_TT, bat_priv,
1353 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1354 dst_orig_node->orig, neigh_node->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001355 (full_table ? 'F' : '.'));
1356
1357 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1358 ret = 0;
1359
1360out:
1361 if (neigh_node)
1362 neigh_node_free_ref(neigh_node);
1363 if (primary_if)
1364 hardif_free_ref(primary_if);
1365 if (ret)
1366 kfree_skb(skb);
1367 if (ret && tt_req_node) {
1368 spin_lock_bh(&bat_priv->tt_req_list_lock);
1369 list_del(&tt_req_node->list);
1370 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1371 kfree(tt_req_node);
1372 }
1373 return ret;
1374}
1375
1376static bool send_other_tt_response(struct bat_priv *bat_priv,
1377 struct tt_query_packet *tt_request)
1378{
1379 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1380 struct neigh_node *neigh_node = NULL;
1381 struct hard_iface *primary_if = NULL;
1382 uint8_t orig_ttvn, req_ttvn, ttvn;
1383 int ret = false;
1384 unsigned char *tt_buff;
1385 bool full_table;
1386 uint16_t tt_len, tt_tot;
1387 struct sk_buff *skb = NULL;
1388 struct tt_query_packet *tt_response;
1389
1390 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001391 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1392 tt_request->src, tt_request->ttvn, tt_request->dst,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001393 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1394
1395 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001396 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397 if (!req_dst_orig_node)
1398 goto out;
1399
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001400 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401 if (!res_dst_orig_node)
1402 goto out;
1403
1404 neigh_node = orig_node_get_router(res_dst_orig_node);
1405 if (!neigh_node)
1406 goto out;
1407
1408 primary_if = primary_if_get_selected(bat_priv);
1409 if (!primary_if)
1410 goto out;
1411
1412 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1413 req_ttvn = tt_request->ttvn;
1414
Antonio Quartulli015758d2011-07-09 17:52:13 +02001415 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416 if (orig_ttvn != req_ttvn ||
1417 tt_request->tt_data != req_dst_orig_node->tt_crc)
1418 goto out;
1419
Antonio Quartulli015758d2011-07-09 17:52:13 +02001420 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001421 if (tt_request->flags & TT_FULL_TABLE ||
1422 !req_dst_orig_node->tt_buff)
1423 full_table = true;
1424 else
1425 full_table = false;
1426
1427 /* In this version, fragmentation is not implemented, then
1428 * I'll send only one packet with as much TT entries as I can */
1429 if (!full_table) {
1430 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1431 tt_len = req_dst_orig_node->tt_buff_len;
1432 tt_tot = tt_len / sizeof(struct tt_change);
1433
1434 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1435 tt_len + ETH_HLEN);
1436 if (!skb)
1437 goto unlock;
1438
1439 skb_reserve(skb, ETH_HLEN);
1440 tt_response = (struct tt_query_packet *)skb_put(skb,
1441 sizeof(struct tt_query_packet) + tt_len);
1442 tt_response->ttvn = req_ttvn;
1443 tt_response->tt_data = htons(tt_tot);
1444
1445 tt_buff = skb->data + sizeof(struct tt_query_packet);
1446 /* Copy the last orig_node's OGM buffer */
1447 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1448 req_dst_orig_node->tt_buff_len);
1449
1450 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1451 } else {
1452 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1453 sizeof(struct tt_change);
1454 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1455
1456 skb = tt_response_fill_table(tt_len, ttvn,
1457 bat_priv->tt_global_hash,
1458 primary_if, tt_global_valid_entry,
1459 req_dst_orig_node);
1460 if (!skb)
1461 goto out;
1462
1463 tt_response = (struct tt_query_packet *)skb->data;
1464 }
1465
Sven Eckelmann76543d12011-11-20 15:47:38 +01001466 tt_response->header.packet_type = BAT_TT_QUERY;
1467 tt_response->header.version = COMPAT_VERSION;
1468 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001469 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1470 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1471 tt_response->flags = TT_RESPONSE;
1472
1473 if (full_table)
1474 tt_response->flags |= TT_FULL_TABLE;
1475
1476 bat_dbg(DBG_TT, bat_priv,
1477 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1478 res_dst_orig_node->orig, neigh_node->addr,
1479 req_dst_orig_node->orig, req_ttvn);
1480
1481 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1482 ret = true;
1483 goto out;
1484
1485unlock:
1486 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1487
1488out:
1489 if (res_dst_orig_node)
1490 orig_node_free_ref(res_dst_orig_node);
1491 if (req_dst_orig_node)
1492 orig_node_free_ref(req_dst_orig_node);
1493 if (neigh_node)
1494 neigh_node_free_ref(neigh_node);
1495 if (primary_if)
1496 hardif_free_ref(primary_if);
1497 if (!ret)
1498 kfree_skb(skb);
1499 return ret;
1500
1501}
1502static bool send_my_tt_response(struct bat_priv *bat_priv,
1503 struct tt_query_packet *tt_request)
1504{
1505 struct orig_node *orig_node = NULL;
1506 struct neigh_node *neigh_node = NULL;
1507 struct hard_iface *primary_if = NULL;
1508 uint8_t my_ttvn, req_ttvn, ttvn;
1509 int ret = false;
1510 unsigned char *tt_buff;
1511 bool full_table;
1512 uint16_t tt_len, tt_tot;
1513 struct sk_buff *skb = NULL;
1514 struct tt_query_packet *tt_response;
1515
1516 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001517 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1518 tt_request->src, tt_request->ttvn,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1520
1521
1522 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1523 req_ttvn = tt_request->ttvn;
1524
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001525 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001526 if (!orig_node)
1527 goto out;
1528
1529 neigh_node = orig_node_get_router(orig_node);
1530 if (!neigh_node)
1531 goto out;
1532
1533 primary_if = primary_if_get_selected(bat_priv);
1534 if (!primary_if)
1535 goto out;
1536
1537 /* If the full table has been explicitly requested or the gap
1538 * is too big send the whole local translation table */
1539 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1540 !bat_priv->tt_buff)
1541 full_table = true;
1542 else
1543 full_table = false;
1544
1545 /* In this version, fragmentation is not implemented, then
1546 * I'll send only one packet with as much TT entries as I can */
1547 if (!full_table) {
1548 spin_lock_bh(&bat_priv->tt_buff_lock);
1549 tt_len = bat_priv->tt_buff_len;
1550 tt_tot = tt_len / sizeof(struct tt_change);
1551
1552 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1553 tt_len + ETH_HLEN);
1554 if (!skb)
1555 goto unlock;
1556
1557 skb_reserve(skb, ETH_HLEN);
1558 tt_response = (struct tt_query_packet *)skb_put(skb,
1559 sizeof(struct tt_query_packet) + tt_len);
1560 tt_response->ttvn = req_ttvn;
1561 tt_response->tt_data = htons(tt_tot);
1562
1563 tt_buff = skb->data + sizeof(struct tt_query_packet);
1564 memcpy(tt_buff, bat_priv->tt_buff,
1565 bat_priv->tt_buff_len);
1566 spin_unlock_bh(&bat_priv->tt_buff_lock);
1567 } else {
1568 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1569 sizeof(struct tt_change);
1570 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1571
1572 skb = tt_response_fill_table(tt_len, ttvn,
1573 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001574 primary_if, tt_local_valid_entry,
1575 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576 if (!skb)
1577 goto out;
1578
1579 tt_response = (struct tt_query_packet *)skb->data;
1580 }
1581
Sven Eckelmann76543d12011-11-20 15:47:38 +01001582 tt_response->header.packet_type = BAT_TT_QUERY;
1583 tt_response->header.version = COMPAT_VERSION;
1584 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001585 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1586 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1587 tt_response->flags = TT_RESPONSE;
1588
1589 if (full_table)
1590 tt_response->flags |= TT_FULL_TABLE;
1591
1592 bat_dbg(DBG_TT, bat_priv,
1593 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1594 orig_node->orig, neigh_node->addr,
1595 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1596
1597 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1598 ret = true;
1599 goto out;
1600
1601unlock:
1602 spin_unlock_bh(&bat_priv->tt_buff_lock);
1603out:
1604 if (orig_node)
1605 orig_node_free_ref(orig_node);
1606 if (neigh_node)
1607 neigh_node_free_ref(neigh_node);
1608 if (primary_if)
1609 hardif_free_ref(primary_if);
1610 if (!ret)
1611 kfree_skb(skb);
1612 /* This packet was for me, so it doesn't need to be re-routed */
1613 return true;
1614}
1615
1616bool send_tt_response(struct bat_priv *bat_priv,
1617 struct tt_query_packet *tt_request)
1618{
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001619 if (is_my_mac(tt_request->dst)) {
1620 /* don't answer backbone gws! */
1621 if (bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1622 return true;
1623
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 return send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001625 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001626 return send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001627 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628}
1629
1630static void _tt_update_changes(struct bat_priv *bat_priv,
1631 struct orig_node *orig_node,
1632 struct tt_change *tt_change,
1633 uint16_t tt_num_changes, uint8_t ttvn)
1634{
1635 int i;
1636
1637 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001638 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 tt_global_del(bat_priv, orig_node,
1640 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001641 "tt removed by changes",
1642 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643 else
1644 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001645 (tt_change + i)->addr, ttvn, false,
1646 (tt_change + i)->flags &
1647 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648 /* In case of problem while storing a
1649 * global_entry, we stop the updating
1650 * procedure without committing the
1651 * ttvn change. This will avoid to send
1652 * corrupted data on tt_request
1653 */
1654 return;
1655 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001656 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001657}
1658
1659static void tt_fill_gtable(struct bat_priv *bat_priv,
1660 struct tt_query_packet *tt_response)
1661{
1662 struct orig_node *orig_node = NULL;
1663
1664 orig_node = orig_hash_find(bat_priv, tt_response->src);
1665 if (!orig_node)
1666 goto out;
1667
1668 /* Purge the old table first.. */
1669 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1670
1671 _tt_update_changes(bat_priv, orig_node,
1672 (struct tt_change *)(tt_response + 1),
1673 tt_response->tt_data, tt_response->ttvn);
1674
1675 spin_lock_bh(&orig_node->tt_buff_lock);
1676 kfree(orig_node->tt_buff);
1677 orig_node->tt_buff_len = 0;
1678 orig_node->tt_buff = NULL;
1679 spin_unlock_bh(&orig_node->tt_buff_lock);
1680
1681 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1682
1683out:
1684 if (orig_node)
1685 orig_node_free_ref(orig_node);
1686}
1687
Marek Lindnera943cac2011-07-30 13:10:18 +02001688static void tt_update_changes(struct bat_priv *bat_priv,
1689 struct orig_node *orig_node,
1690 uint16_t tt_num_changes, uint8_t ttvn,
1691 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692{
1693 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1694 ttvn);
1695
1696 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1697 tt_num_changes);
1698 atomic_set(&orig_node->last_ttvn, ttvn);
1699}
1700
1701bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1702{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001703 struct tt_local_entry *tt_local_entry = NULL;
1704 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001707 if (!tt_local_entry)
1708 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001709 /* Check if the client has been logically deleted (but is kept for
1710 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001711 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001712 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001713 ret = true;
1714out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001716 tt_local_entry_free_ref(tt_local_entry);
1717 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718}
1719
1720void handle_tt_response(struct bat_priv *bat_priv,
1721 struct tt_query_packet *tt_response)
1722{
1723 struct tt_req_node *node, *safe;
1724 struct orig_node *orig_node = NULL;
1725
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001726 bat_dbg(DBG_TT, bat_priv,
1727 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1728 tt_response->src, tt_response->ttvn, tt_response->tt_data,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001729 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1730
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001731 /* we should have never asked a backbone gw */
1732 if (bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1733 goto out;
1734
Antonio Quartullia73105b2011-04-27 14:27:44 +02001735 orig_node = orig_hash_find(bat_priv, tt_response->src);
1736 if (!orig_node)
1737 goto out;
1738
1739 if (tt_response->flags & TT_FULL_TABLE)
1740 tt_fill_gtable(bat_priv, tt_response);
1741 else
1742 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1743 tt_response->ttvn,
1744 (struct tt_change *)(tt_response + 1));
1745
1746 /* Delete the tt_req_node from pending tt_requests list */
1747 spin_lock_bh(&bat_priv->tt_req_list_lock);
1748 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1749 if (!compare_eth(node->addr, tt_response->src))
1750 continue;
1751 list_del(&node->list);
1752 kfree(node);
1753 }
1754 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1755
1756 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001758 /* Roaming phase is over: tables are in sync again. I can
1759 * unset the flag */
1760 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761out:
1762 if (orig_node)
1763 orig_node_free_ref(orig_node);
1764}
1765
1766int tt_init(struct bat_priv *bat_priv)
1767{
1768 if (!tt_local_init(bat_priv))
1769 return 0;
1770
1771 if (!tt_global_init(bat_priv))
1772 return 0;
1773
1774 tt_start_timer(bat_priv);
1775
1776 return 1;
1777}
1778
Antonio Quartullicc47f662011-04-27 14:27:57 +02001779static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001781 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782
Antonio Quartullicc47f662011-04-27 14:27:57 +02001783 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784
Antonio Quartullicc47f662011-04-27 14:27:57 +02001785 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1786 list_del(&node->list);
1787 kfree(node);
1788 }
1789
1790 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1791}
1792
1793static void tt_roam_purge(struct bat_priv *bat_priv)
1794{
1795 struct tt_roam_node *node, *safe;
1796
1797 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1798 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001799 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001800 continue;
1801
1802 list_del(&node->list);
1803 kfree(node);
1804 }
1805 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1806}
1807
1808/* This function checks whether the client already reached the
1809 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1810 * will not be sent.
1811 *
1812 * returns true if the ROAMING_ADV can be sent, false otherwise */
1813static bool tt_check_roam_count(struct bat_priv *bat_priv,
1814 uint8_t *client)
1815{
1816 struct tt_roam_node *tt_roam_node;
1817 bool ret = false;
1818
1819 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1820 /* The new tt_req will be issued only if I'm not waiting for a
1821 * reply from the same orig_node yet */
1822 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1823 if (!compare_eth(tt_roam_node->addr, client))
1824 continue;
1825
Marek Lindner032b7962011-12-20 19:30:40 +08001826 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001827 continue;
1828
1829 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1830 /* Sorry, you roamed too many times! */
1831 goto unlock;
1832 ret = true;
1833 break;
1834 }
1835
1836 if (!ret) {
1837 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1838 if (!tt_roam_node)
1839 goto unlock;
1840
1841 tt_roam_node->first_time = jiffies;
1842 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1843 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1844
1845 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1846 ret = true;
1847 }
1848
1849unlock:
1850 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1851 return ret;
1852}
1853
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001854static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1855 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001856{
1857 struct neigh_node *neigh_node = NULL;
1858 struct sk_buff *skb = NULL;
1859 struct roam_adv_packet *roam_adv_packet;
1860 int ret = 1;
1861 struct hard_iface *primary_if;
1862
1863 /* before going on we have to check whether the client has
1864 * already roamed to us too many times */
1865 if (!tt_check_roam_count(bat_priv, client))
1866 goto out;
1867
1868 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1869 if (!skb)
1870 goto out;
1871
1872 skb_reserve(skb, ETH_HLEN);
1873
1874 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1875 sizeof(struct roam_adv_packet));
1876
Sven Eckelmann76543d12011-11-20 15:47:38 +01001877 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1878 roam_adv_packet->header.version = COMPAT_VERSION;
1879 roam_adv_packet->header.ttl = TTL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001880 primary_if = primary_if_get_selected(bat_priv);
1881 if (!primary_if)
1882 goto out;
1883 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1884 hardif_free_ref(primary_if);
1885 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1886 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1887
1888 neigh_node = orig_node_get_router(orig_node);
1889 if (!neigh_node)
1890 goto out;
1891
1892 bat_dbg(DBG_TT, bat_priv,
1893 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1894 orig_node->orig, client, neigh_node->addr);
1895
1896 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1897 ret = 0;
1898
1899out:
1900 if (neigh_node)
1901 neigh_node_free_ref(neigh_node);
1902 if (ret)
1903 kfree_skb(skb);
1904 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905}
1906
1907static void tt_purge(struct work_struct *work)
1908{
1909 struct delayed_work *delayed_work =
1910 container_of(work, struct delayed_work, work);
1911 struct bat_priv *bat_priv =
1912 container_of(delayed_work, struct bat_priv, tt_work);
1913
1914 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001915 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001916 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001917 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918
1919 tt_start_timer(bat_priv);
1920}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001921
1922void tt_free(struct bat_priv *bat_priv)
1923{
1924 cancel_delayed_work_sync(&bat_priv->tt_work);
1925
1926 tt_local_table_free(bat_priv);
1927 tt_global_table_free(bat_priv);
1928 tt_req_list_free(bat_priv);
1929 tt_changes_list_free(bat_priv);
1930 tt_roam_list_free(bat_priv);
1931
1932 kfree(bat_priv->tt_buff);
1933}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001934
Antonio Quartulli697f2532011-11-07 16:47:01 +01001935/* This function will enable or disable the specified flags for all the entries
1936 * in the given hash table and returns the number of modified entries */
1937static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1938 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001939{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001940 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001941 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001942 struct hlist_head *head;
1943 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001944 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001945
1946 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001947 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001948
1949 for (i = 0; i < hash->size; i++) {
1950 head = &hash->table[i];
1951
1952 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001953 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001954 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001955 if (enable) {
1956 if ((tt_common_entry->flags & flags) == flags)
1957 continue;
1958 tt_common_entry->flags |= flags;
1959 } else {
1960 if (!(tt_common_entry->flags & flags))
1961 continue;
1962 tt_common_entry->flags &= ~flags;
1963 }
1964 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001965 }
1966 rcu_read_unlock();
1967 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001968out:
1969 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001970}
1971
1972/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1973static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1974{
1975 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001976 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001977 struct tt_local_entry *tt_local_entry;
1978 struct hlist_node *node, *node_tmp;
1979 struct hlist_head *head;
1980 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001981 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001982
1983 if (!hash)
1984 return;
1985
1986 for (i = 0; i < hash->size; i++) {
1987 head = &hash->table[i];
1988 list_lock = &hash->list_locks[i];
1989
1990 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001991 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001992 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001993 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001994 continue;
1995
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001996 bat_dbg(DBG_TT, bat_priv,
1997 "Deleting local tt entry (%pM): pending\n",
1998 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001999
2000 atomic_dec(&bat_priv->num_local_tt);
2001 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002002 tt_local_entry = container_of(tt_common_entry,
2003 struct tt_local_entry,
2004 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002005 tt_local_entry_free_ref(tt_local_entry);
2006 }
2007 spin_unlock_bh(list_lock);
2008 }
2009
2010}
2011
2012void tt_commit_changes(struct bat_priv *bat_priv)
2013{
Antonio Quartulli697f2532011-11-07 16:47:01 +01002014 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
2015 TT_CLIENT_NEW, false);
2016 /* all the reset entries have now to be effectively counted as local
2017 * entries */
2018 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002019 tt_local_purge_pending_clients(bat_priv);
2020
2021 /* Increment the TTVN only once per OGM interval */
2022 atomic_inc(&bat_priv->ttvn);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002023 bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
2024 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002025 bat_priv->tt_poss_change = false;
2026}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002027
2028bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
2029{
2030 struct tt_local_entry *tt_local_entry = NULL;
2031 struct tt_global_entry *tt_global_entry = NULL;
2032 bool ret = true;
2033
2034 if (!atomic_read(&bat_priv->ap_isolation))
2035 return false;
2036
2037 tt_local_entry = tt_local_hash_find(bat_priv, dst);
2038 if (!tt_local_entry)
2039 goto out;
2040
2041 tt_global_entry = tt_global_hash_find(bat_priv, src);
2042 if (!tt_global_entry)
2043 goto out;
2044
2045 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
2046 goto out;
2047
2048 ret = false;
2049
2050out:
2051 if (tt_global_entry)
2052 tt_global_entry_free_ref(tt_global_entry);
2053 if (tt_local_entry)
2054 tt_local_entry_free_ref(tt_local_entry);
2055 return ret;
2056}
Marek Lindnera943cac2011-07-30 13:10:18 +02002057
2058void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
2059 const unsigned char *tt_buff, uint8_t tt_num_changes,
2060 uint8_t ttvn, uint16_t tt_crc)
2061{
2062 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2063 bool full_table = true;
2064
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002065 /* don't care about a backbone gateways updates. */
2066 if (bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2067 return;
2068
Antonio Quartulli17071572011-11-07 16:36:40 +01002069 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2070 * increased by one -> we can apply the attached changes */
2071 if ((!orig_node->tt_initialised && ttvn == 1) ||
2072 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002073 /* the OGM could not contain the changes due to their size or
2074 * because they have already been sent TT_OGM_APPEND_MAX times.
2075 * In this case send a tt request */
2076 if (!tt_num_changes) {
2077 full_table = false;
2078 goto request_table;
2079 }
2080
2081 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
2082 (struct tt_change *)tt_buff);
2083
2084 /* Even if we received the precomputed crc with the OGM, we
2085 * prefer to recompute it to spot any possible inconsistency
2086 * in the global table */
2087 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
2088
2089 /* The ttvn alone is not enough to guarantee consistency
2090 * because a single value could represent different states
2091 * (due to the wrap around). Thus a node has to check whether
2092 * the resulting table (after applying the changes) is still
2093 * consistent or not. E.g. a node could disconnect while its
2094 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2095 * checking the CRC value is mandatory to detect the
2096 * inconsistency */
2097 if (orig_node->tt_crc != tt_crc)
2098 goto request_table;
2099
2100 /* Roaming phase is over: tables are in sync again. I can
2101 * unset the flag */
2102 orig_node->tt_poss_change = false;
2103 } else {
2104 /* if we missed more than one change or our tables are not
2105 * in sync anymore -> request fresh tt data */
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002106
Antonio Quartulli17071572011-11-07 16:36:40 +01002107 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2108 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002109request_table:
Sven Eckelmann86ceb362012-03-07 09:07:45 +01002110 bat_dbg(DBG_TT, bat_priv,
2111 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2112 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2113 orig_node->tt_crc, tt_num_changes);
Marek Lindnera943cac2011-07-30 13:10:18 +02002114 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
2115 full_table);
2116 return;
2117 }
2118 }
2119}