blob: 1f128e1656a7d26e79b389d40019d8fbec4c5c44 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020025#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020026#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020029#include "routing.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullia73105b2011-04-27 14:27:44 +020031#include <linux/crc16.h>
32
33static void _tt_global_del(struct bat_priv *bat_priv,
34 struct tt_global_entry *tt_global_entry,
35 const char *message);
36static void tt_purge(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Marek Lindner7aadf882011-02-18 12:28:09 +000038/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020039static int compare_ltt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Sven Eckelmann747e4222011-05-14 23:14:50 +020041 const void *data1 = container_of(node, struct tt_local_entry,
42 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
47/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020048static int compare_gtt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000049{
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 const void *data1 = container_of(node, struct tt_global_entry,
51 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000052
53 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
54}
55
Antonio Quartullia73105b2011-04-27 14:27:44 +020056static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Antonio Quartullia73105b2011-04-27 14:27:44 +020058 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
59 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
60 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061}
62
Antonio Quartulli2dafb492011-05-05 08:42:45 +020063static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020064 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000065{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020066 struct hashtable_t *hash = bat_priv->tt_local_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000067 struct hlist_head *head;
68 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020069 struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000070 int index;
71
72 if (!hash)
73 return NULL;
74
75 index = choose_orig(data, hash->size);
76 head = &hash->table[index];
77
78 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +020079 hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
80 if (!compare_eth(tt_local_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000081 continue;
82
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020083 if (!atomic_inc_not_zero(&tt_local_entry->refcount))
84 continue;
85
Antonio Quartulli2dafb492011-05-05 08:42:45 +020086 tt_local_entry_tmp = tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000087 break;
88 }
89 rcu_read_unlock();
90
Antonio Quartulli2dafb492011-05-05 08:42:45 +020091 return tt_local_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +000092}
93
Antonio Quartulli2dafb492011-05-05 08:42:45 +020094static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020095 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000096{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097 struct hashtable_t *hash = bat_priv->tt_global_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000098 struct hlist_head *head;
99 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200100 struct tt_global_entry *tt_global_entry;
101 struct tt_global_entry *tt_global_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102 int index;
103
104 if (!hash)
105 return NULL;
106
107 index = choose_orig(data, hash->size);
108 head = &hash->table[index];
109
110 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200111 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
112 if (!compare_eth(tt_global_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000113 continue;
114
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115 if (!atomic_inc_not_zero(&tt_global_entry->refcount))
116 continue;
117
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200118 tt_global_entry_tmp = tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000119 break;
120 }
121 rcu_read_unlock();
122
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200123 return tt_global_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +0000124}
125
Antonio Quartullia73105b2011-04-27 14:27:44 +0200126static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
127{
128 unsigned long deadline;
129 deadline = starting_time + msecs_to_jiffies(timeout);
130
131 return time_after(jiffies, deadline);
132}
133
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
135{
136 if (atomic_dec_and_test(&tt_local_entry->refcount))
137 kfree_rcu(tt_local_entry, rcu);
138}
139
140static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
141{
142 if (atomic_dec_and_test(&tt_global_entry->refcount))
143 kfree_rcu(tt_global_entry, rcu);
144}
145
Antonio Quartulliff66c972011-06-30 01:14:00 +0200146static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
147 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200148{
149 struct tt_change_node *tt_change_node;
150
151 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
152
153 if (!tt_change_node)
154 return;
155
Antonio Quartulliff66c972011-06-30 01:14:00 +0200156 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200157 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
158
159 spin_lock_bh(&bat_priv->tt_changes_list_lock);
160 /* track the change in the OGMinterval list */
161 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
162 atomic_inc(&bat_priv->tt_local_changes);
163 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
164
165 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
166}
167
168int tt_len(int changes_num)
169{
170 return changes_num * sizeof(struct tt_change);
171}
172
173static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200175 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 return 1;
177
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200180 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 return 0;
182
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 return 1;
184}
185
Antonio Quartullibc279082011-07-07 15:35:35 +0200186void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
187 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188{
189 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200190 struct tt_local_entry *tt_local_entry = NULL;
191 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200193 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200195 if (tt_local_entry) {
196 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200197 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 }
199
Sven Eckelmann704509b2011-05-14 23:14:54 +0200200 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200201 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200202 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200203
Antonio Quartullia73105b2011-04-27 14:27:44 +0200204 bat_dbg(DBG_TT, bat_priv,
205 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
206 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200208 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
209 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200210 tt_local_entry->flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200211 if (is_wifi_iface(ifindex))
212 tt_local_entry->flags |= TT_CLIENT_WIFI;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200213 atomic_set(&tt_local_entry->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214
215 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000216 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200217 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218
Antonio Quartulliff66c972011-06-30 01:14:00 +0200219 tt_local_event(bat_priv, addr, tt_local_entry->flags);
220
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200221 /* The local entry has to be marked as NEW to avoid to send it in
222 * a full table response going out before the next ttvn increment
223 * (consistency check) */
224 tt_local_entry->flags |= TT_CLIENT_NEW;
225
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200226 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
227 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200228
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200230 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231
Antonio Quartullicc47f662011-04-27 14:27:57 +0200232 /* Check whether it is a roaming! */
233 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200234 /* This node is probably going to update its tt table */
235 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200236 /* The global entry has to be marked as PENDING and has to be
237 * kept for consistency purpose */
238 tt_global_entry->flags |= TT_CLIENT_PENDING;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200239 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200240 tt_global_entry->orig_node);
241 }
242out:
243 if (tt_local_entry)
244 tt_local_entry_free_ref(tt_local_entry);
245 if (tt_global_entry)
246 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247}
248
Antonio Quartullia73105b2011-04-27 14:27:44 +0200249int tt_changes_fill_buffer(struct bat_priv *bat_priv,
250 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200252 int count = 0, tot_changes = 0;
253 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Antonio Quartullia73105b2011-04-27 14:27:44 +0200255 if (buff_len > 0)
256 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
Antonio Quartullia73105b2011-04-27 14:27:44 +0200258 spin_lock_bh(&bat_priv->tt_changes_list_lock);
259 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260
Antonio Quartullia73105b2011-04-27 14:27:44 +0200261 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
262 list) {
263 if (count < tot_changes) {
264 memcpy(buff + tt_len(count),
265 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 count++;
267 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200268 list_del(&entry->list);
269 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200271 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
Antonio Quartullia73105b2011-04-27 14:27:44 +0200273 /* Keep the buffer for possible tt_request */
274 spin_lock_bh(&bat_priv->tt_buff_lock);
275 kfree(bat_priv->tt_buff);
276 bat_priv->tt_buff_len = 0;
277 bat_priv->tt_buff = NULL;
278 /* We check whether this new OGM has no changes due to size
279 * problems */
280 if (buff_len > 0) {
281 /**
282 * if kmalloc() fails we will reply with the full table
283 * instead of providing the diff
284 */
285 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
286 if (bat_priv->tt_buff) {
287 memcpy(bat_priv->tt_buff, buff, buff_len);
288 bat_priv->tt_buff_len = buff_len;
289 }
290 }
291 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292
Antonio Quartullia73105b2011-04-27 14:27:44 +0200293 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294}
295
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200296int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297{
298 struct net_device *net_dev = (struct net_device *)seq->private;
299 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200300 struct hashtable_t *hash = bat_priv->tt_local_hash;
301 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200302 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000303 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 size_t buf_size, pos;
306 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200307 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
Marek Lindner32ae9b22011-04-20 15:40:58 +0200309 primary_if = primary_if_get_selected(bat_priv);
310 if (!primary_if) {
311 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
312 "please specify interfaces to enable it\n",
313 net_dev->name);
314 goto out;
315 }
316
317 if (primary_if->if_status != IF_ACTIVE) {
318 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
319 "primary interface not active\n",
320 net_dev->name);
321 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322 }
323
324 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200325 "announced via TT (TTVN: %u):\n",
326 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328 buf_size = 1;
329 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
330 for (i = 0; i < hash->size; i++) {
331 head = &hash->table[i];
332
Marek Lindner7aadf882011-02-18 12:28:09 +0000333 rcu_read_lock();
334 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000336 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 }
338
339 buff = kmalloc(buf_size, GFP_ATOMIC);
340 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200341 ret = -ENOMEM;
342 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000344
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 buff[0] = '\0';
346 pos = 0;
347
348 for (i = 0; i < hash->size; i++) {
349 head = &hash->table[i];
350
Marek Lindner7aadf882011-02-18 12:28:09 +0000351 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200352 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000353 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200355 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000357 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 }
359
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360 seq_printf(seq, "%s", buff);
361 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200362out:
363 if (primary_if)
364 hardif_free_ref(primary_if);
365 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366}
367
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200368static void tt_local_set_pending(struct bat_priv *bat_priv,
369 struct tt_local_entry *tt_local_entry,
370 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371{
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200372 tt_local_event(bat_priv, tt_local_entry->addr,
373 tt_local_entry->flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374
Antonio Quartulli015758d2011-07-09 17:52:13 +0200375 /* The local client has to be marked as "pending to be removed" but has
376 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200377 * response issued before the net ttvn increment (consistency check) */
378 tt_local_entry->flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379}
380
Antonio Quartullia73105b2011-04-27 14:27:44 +0200381void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200382 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200384 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200386 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200387 if (!tt_local_entry)
388 goto out;
389
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200390 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
391 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
392
393 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
394 "%s\n", tt_local_entry->addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200395out:
396 if (tt_local_entry)
397 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398}
399
Antonio Quartullia73105b2011-04-27 14:27:44 +0200400static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200402 struct hashtable_t *hash = bat_priv->tt_local_hash;
403 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000404 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200406 spinlock_t *list_lock; /* protects write access to the hash lists */
Marek Lindner7aadf882011-02-18 12:28:09 +0000407 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409 for (i = 0; i < hash->size; i++) {
410 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200411 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200413 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200414 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000415 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200416 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000417 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200419 /* entry already marked for deletion */
420 if (tt_local_entry->flags & TT_CLIENT_PENDING)
421 continue;
422
Antonio Quartullia73105b2011-04-27 14:27:44 +0200423 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200424 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000425 continue;
426
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200427 tt_local_set_pending(bat_priv, tt_local_entry,
428 TT_CLIENT_DEL);
429 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
430 "pending to be removed: timed out\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200431 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200433 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 }
435
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436}
437
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200441 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200442 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200443 struct hlist_node *node, *node_tmp;
444 struct hlist_head *head;
445 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200446
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200447 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 return;
449
Antonio Quartullia73105b2011-04-27 14:27:44 +0200450 hash = bat_priv->tt_local_hash;
451
452 for (i = 0; i < hash->size; i++) {
453 head = &hash->table[i];
454 list_lock = &hash->list_locks[i];
455
456 spin_lock_bh(list_lock);
457 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
458 head, hash_entry) {
459 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200460 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200461 }
462 spin_unlock_bh(list_lock);
463 }
464
465 hash_destroy(hash);
466
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200467 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468}
469
Antonio Quartullia73105b2011-04-27 14:27:44 +0200470static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200472 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 return 1;
474
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200475 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200477 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 return 0;
479
480 return 1;
481}
482
Antonio Quartullia73105b2011-04-27 14:27:44 +0200483static void tt_changes_list_free(struct bat_priv *bat_priv)
484{
485 struct tt_change_node *entry, *safe;
486
487 spin_lock_bh(&bat_priv->tt_changes_list_lock);
488
489 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
490 list) {
491 list_del(&entry->list);
492 kfree(entry);
493 }
494
495 atomic_set(&bat_priv->tt_local_changes, 0);
496 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
497}
498
499/* caller must hold orig_node refcount */
500int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200501 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
502 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200504 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200505 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200506 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Antonio Quartullia73105b2011-04-27 14:27:44 +0200508 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
Antonio Quartullia73105b2011-04-27 14:27:44 +0200510 if (!tt_global_entry) {
511 tt_global_entry =
512 kmalloc(sizeof(*tt_global_entry),
513 GFP_ATOMIC);
514 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200515 goto out;
516
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
518 /* Assign the new orig_node */
519 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200520 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200521 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200522 tt_global_entry->flags = NO_FLAGS;
523 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200524 atomic_set(&tt_global_entry->refcount, 2);
525
Antonio Quartullia73105b2011-04-27 14:27:44 +0200526 hash_add(bat_priv->tt_global_hash, compare_gtt,
527 choose_orig, tt_global_entry,
528 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200529 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200530 } else {
531 if (tt_global_entry->orig_node != orig_node) {
532 atomic_dec(&tt_global_entry->orig_node->tt_size);
533 orig_node_tmp = tt_global_entry->orig_node;
534 atomic_inc(&orig_node->refcount);
535 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200536 orig_node_free_ref(orig_node_tmp);
537 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200539 tt_global_entry->ttvn = ttvn;
540 tt_global_entry->flags = NO_FLAGS;
541 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200543
Antonio Quartullibc279082011-07-07 15:35:35 +0200544 if (wifi)
545 tt_global_entry->flags |= TT_CLIENT_WIFI;
546
Antonio Quartullia73105b2011-04-27 14:27:44 +0200547 bat_dbg(DBG_TT, bat_priv,
548 "Creating new global tt entry: %pM (via %pM)\n",
549 tt_global_entry->addr, orig_node->orig);
550
551 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200552 tt_local_remove(bat_priv, tt_global_entry->addr,
553 "global tt received", roaming);
554 ret = 1;
555out:
556 if (tt_global_entry)
557 tt_global_entry_free_ref(tt_global_entry);
558 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559}
560
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200561int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562{
563 struct net_device *net_dev = (struct net_device *)seq->private;
564 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200565 struct hashtable_t *hash = bat_priv->tt_global_hash;
566 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200567 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000568 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570 size_t buf_size, pos;
571 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200572 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573
Marek Lindner32ae9b22011-04-20 15:40:58 +0200574 primary_if = primary_if_get_selected(bat_priv);
575 if (!primary_if) {
576 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
577 "specify interfaces to enable it\n",
578 net_dev->name);
579 goto out;
580 }
581
582 if (primary_if->if_status != IF_ACTIVE) {
583 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
584 "primary interface not active\n",
585 net_dev->name);
586 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587 }
588
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200589 seq_printf(seq,
590 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200592 seq_printf(seq, " %-13s %s %-15s %s\n",
593 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200596 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
597 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 for (i = 0; i < hash->size; i++) {
599 head = &hash->table[i];
600
Marek Lindner7aadf882011-02-18 12:28:09 +0000601 rcu_read_lock();
602 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200603 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000604 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 }
606
607 buff = kmalloc(buf_size, GFP_ATOMIC);
608 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200609 ret = -ENOMEM;
610 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200612
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 buff[0] = '\0';
614 pos = 0;
615
616 for (i = 0; i < hash->size; i++) {
617 head = &hash->table[i];
618
Marek Lindner7aadf882011-02-18 12:28:09 +0000619 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200620 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000621 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622 pos += snprintf(buff + pos, 61,
623 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200624 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200625 tt_global_entry->ttvn,
626 tt_global_entry->orig_node->orig,
627 (uint8_t) atomic_read(
628 &tt_global_entry->orig_node->
629 last_ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000631 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632 }
633
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 seq_printf(seq, "%s", buff);
635 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200636out:
637 if (primary_if)
638 hardif_free_ref(primary_if);
639 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640}
641
Antonio Quartullia73105b2011-04-27 14:27:44 +0200642static void _tt_global_del(struct bat_priv *bat_priv,
643 struct tt_global_entry *tt_global_entry,
644 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200646 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200648
649 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200650 "Deleting global tt entry %pM (via %pM): %s\n",
651 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652 message);
653
Antonio Quartullia73105b2011-04-27 14:27:44 +0200654 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200655
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200656 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
657 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200658out:
659 if (tt_global_entry)
660 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661}
662
Antonio Quartullia73105b2011-04-27 14:27:44 +0200663void tt_global_del(struct bat_priv *bat_priv,
664 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200665 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200667 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668
Antonio Quartullia73105b2011-04-27 14:27:44 +0200669 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200670 if (!tt_global_entry)
671 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200672
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200673 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200674 if (roaming) {
675 tt_global_entry->flags |= TT_CLIENT_ROAM;
676 tt_global_entry->roam_at = jiffies;
677 goto out;
678 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200679 _tt_global_del(bat_priv, tt_global_entry, message);
680 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200681out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200682 if (tt_global_entry)
683 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200684}
685
686void tt_global_del_orig(struct bat_priv *bat_priv,
687 struct orig_node *orig_node, const char *message)
688{
689 struct tt_global_entry *tt_global_entry;
690 int i;
691 struct hashtable_t *hash = bat_priv->tt_global_hash;
692 struct hlist_node *node, *safe;
693 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200694 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200695
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696 for (i = 0; i < hash->size; i++) {
697 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200698 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200700 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701 hlist_for_each_entry_safe(tt_global_entry, node, safe,
702 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200703 if (tt_global_entry->orig_node == orig_node) {
704 bat_dbg(DBG_TT, bat_priv,
705 "Deleting global tt entry %pM "
706 "(via %pM): originator time out\n",
707 tt_global_entry->addr,
708 tt_global_entry->orig_node->orig);
709 hlist_del_rcu(node);
710 tt_global_entry_free_ref(tt_global_entry);
711 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200712 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200713 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200715 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716}
717
Antonio Quartullicc47f662011-04-27 14:27:57 +0200718static void tt_global_roam_purge(struct bat_priv *bat_priv)
719{
720 struct hashtable_t *hash = bat_priv->tt_global_hash;
721 struct tt_global_entry *tt_global_entry;
722 struct hlist_node *node, *node_tmp;
723 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200724 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200725 int i;
726
Antonio Quartullicc47f662011-04-27 14:27:57 +0200727 for (i = 0; i < hash->size; i++) {
728 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200729 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200730
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200731 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200732 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
733 head, hash_entry) {
734 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
735 continue;
736 if (!is_out_of_time(tt_global_entry->roam_at,
737 TT_CLIENT_ROAM_TIMEOUT * 1000))
738 continue;
739
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200740 bat_dbg(DBG_TT, bat_priv, "Deleting global "
741 "tt entry (%pM): Roaming timeout\n",
742 tt_global_entry->addr);
743 atomic_dec(&tt_global_entry->orig_node->tt_size);
744 hlist_del_rcu(node);
745 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200746 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200747 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200748 }
749
Antonio Quartullicc47f662011-04-27 14:27:57 +0200750}
751
Antonio Quartullia73105b2011-04-27 14:27:44 +0200752static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200754 struct hashtable_t *hash;
755 spinlock_t *list_lock; /* protects write access to the hash lists */
756 struct tt_global_entry *tt_global_entry;
757 struct hlist_node *node, *node_tmp;
758 struct hlist_head *head;
759 int i;
760
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200761 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 return;
763
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200764 hash = bat_priv->tt_global_hash;
765
766 for (i = 0; i < hash->size; i++) {
767 head = &hash->table[i];
768 list_lock = &hash->list_locks[i];
769
770 spin_lock_bh(list_lock);
771 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
772 head, hash_entry) {
773 hlist_del_rcu(node);
774 tt_global_entry_free_ref(tt_global_entry);
775 }
776 spin_unlock_bh(list_lock);
777 }
778
779 hash_destroy(hash);
780
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200781 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782}
783
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200784static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
785 struct tt_global_entry *tt_global_entry)
786{
787 bool ret = false;
788
789 if (tt_local_entry->flags & TT_CLIENT_WIFI &&
790 tt_global_entry->flags & TT_CLIENT_WIFI)
791 ret = true;
792
793 return ret;
794}
795
Sven Eckelmann747e4222011-05-14 23:14:50 +0200796struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200797 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200799 struct tt_local_entry *tt_local_entry = NULL;
800 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000801 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200803 if (src && atomic_read(&bat_priv->ap_isolation)) {
804 tt_local_entry = tt_local_hash_find(bat_priv, src);
805 if (!tt_local_entry)
806 goto out;
807 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000808
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200809 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200810 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000811 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200813 /* check whether the clients should not communicate due to AP
814 * isolation */
815 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
816 goto out;
817
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200818 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200819 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000820
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200821 /* A global client marked as PENDING has already moved from that
822 * originator */
823 if (tt_global_entry->flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200824 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200825
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200826 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000827
828out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200829 if (tt_global_entry)
830 tt_global_entry_free_ref(tt_global_entry);
831 if (tt_local_entry)
832 tt_local_entry_free_ref(tt_local_entry);
833
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000834 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200836
837/* Calculates the checksum of the local table of a given orig_node */
838uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
839{
840 uint16_t total = 0, total_one;
841 struct hashtable_t *hash = bat_priv->tt_global_hash;
842 struct tt_global_entry *tt_global_entry;
843 struct hlist_node *node;
844 struct hlist_head *head;
845 int i, j;
846
847 for (i = 0; i < hash->size; i++) {
848 head = &hash->table[i];
849
850 rcu_read_lock();
851 hlist_for_each_entry_rcu(tt_global_entry, node,
852 head, hash_entry) {
853 if (compare_eth(tt_global_entry->orig_node,
854 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200855 /* Roaming clients are in the global table for
856 * consistency only. They don't have to be
857 * taken into account while computing the
858 * global crc */
859 if (tt_global_entry->flags & TT_CLIENT_ROAM)
860 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200861 total_one = 0;
862 for (j = 0; j < ETH_ALEN; j++)
863 total_one = crc16_byte(total_one,
864 tt_global_entry->addr[j]);
865 total ^= total_one;
866 }
867 }
868 rcu_read_unlock();
869 }
870
871 return total;
872}
873
874/* Calculates the checksum of the local table */
875uint16_t tt_local_crc(struct bat_priv *bat_priv)
876{
877 uint16_t total = 0, total_one;
878 struct hashtable_t *hash = bat_priv->tt_local_hash;
879 struct tt_local_entry *tt_local_entry;
880 struct hlist_node *node;
881 struct hlist_head *head;
882 int i, j;
883
884 for (i = 0; i < hash->size; i++) {
885 head = &hash->table[i];
886
887 rcu_read_lock();
888 hlist_for_each_entry_rcu(tt_local_entry, node,
889 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200890 /* not yet committed clients have not to be taken into
891 * account while computing the CRC */
892 if (tt_local_entry->flags & TT_CLIENT_NEW)
893 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894 total_one = 0;
895 for (j = 0; j < ETH_ALEN; j++)
896 total_one = crc16_byte(total_one,
897 tt_local_entry->addr[j]);
898 total ^= total_one;
899 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200900 rcu_read_unlock();
901 }
902
903 return total;
904}
905
906static void tt_req_list_free(struct bat_priv *bat_priv)
907{
908 struct tt_req_node *node, *safe;
909
910 spin_lock_bh(&bat_priv->tt_req_list_lock);
911
912 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
913 list_del(&node->list);
914 kfree(node);
915 }
916
917 spin_unlock_bh(&bat_priv->tt_req_list_lock);
918}
919
920void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
921 const unsigned char *tt_buff, uint8_t tt_num_changes)
922{
923 uint16_t tt_buff_len = tt_len(tt_num_changes);
924
925 /* Replace the old buffer only if I received something in the
926 * last OGM (the OGM could carry no changes) */
927 spin_lock_bh(&orig_node->tt_buff_lock);
928 if (tt_buff_len > 0) {
929 kfree(orig_node->tt_buff);
930 orig_node->tt_buff_len = 0;
931 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
932 if (orig_node->tt_buff) {
933 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
934 orig_node->tt_buff_len = tt_buff_len;
935 }
936 }
937 spin_unlock_bh(&orig_node->tt_buff_lock);
938}
939
940static void tt_req_purge(struct bat_priv *bat_priv)
941{
942 struct tt_req_node *node, *safe;
943
944 spin_lock_bh(&bat_priv->tt_req_list_lock);
945 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
946 if (is_out_of_time(node->issued_at,
947 TT_REQUEST_TIMEOUT * 1000)) {
948 list_del(&node->list);
949 kfree(node);
950 }
951 }
952 spin_unlock_bh(&bat_priv->tt_req_list_lock);
953}
954
955/* returns the pointer to the new tt_req_node struct if no request
956 * has already been issued for this orig_node, NULL otherwise */
957static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
958 struct orig_node *orig_node)
959{
960 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
961
962 spin_lock_bh(&bat_priv->tt_req_list_lock);
963 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
964 if (compare_eth(tt_req_node_tmp, orig_node) &&
965 !is_out_of_time(tt_req_node_tmp->issued_at,
966 TT_REQUEST_TIMEOUT * 1000))
967 goto unlock;
968 }
969
970 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
971 if (!tt_req_node)
972 goto unlock;
973
974 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
975 tt_req_node->issued_at = jiffies;
976
977 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
978unlock:
979 spin_unlock_bh(&bat_priv->tt_req_list_lock);
980 return tt_req_node;
981}
982
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200983/* data_ptr is useless here, but has to be kept to respect the prototype */
984static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
985{
986 const struct tt_local_entry *tt_local_entry = entry_ptr;
987
988 if (tt_local_entry->flags & TT_CLIENT_NEW)
989 return 0;
990 return 1;
991}
992
Antonio Quartullia73105b2011-04-27 14:27:44 +0200993static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
994{
995 const struct tt_global_entry *tt_global_entry = entry_ptr;
996 const struct orig_node *orig_node = data_ptr;
997
Antonio Quartullicc47f662011-04-27 14:27:57 +0200998 if (tt_global_entry->flags & TT_CLIENT_ROAM)
999 return 0;
1000
Antonio Quartullia73105b2011-04-27 14:27:44 +02001001 return (tt_global_entry->orig_node == orig_node);
1002}
1003
1004static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1005 struct hashtable_t *hash,
1006 struct hard_iface *primary_if,
1007 int (*valid_cb)(const void *,
1008 const void *),
1009 void *cb_data)
1010{
1011 struct tt_local_entry *tt_local_entry;
1012 struct tt_query_packet *tt_response;
1013 struct tt_change *tt_change;
1014 struct hlist_node *node;
1015 struct hlist_head *head;
1016 struct sk_buff *skb = NULL;
1017 uint16_t tt_tot, tt_count;
1018 ssize_t tt_query_size = sizeof(struct tt_query_packet);
1019 int i;
1020
1021 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1022 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1023 tt_len -= tt_len % sizeof(struct tt_change);
1024 }
1025 tt_tot = tt_len / sizeof(struct tt_change);
1026
1027 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1028 if (!skb)
1029 goto out;
1030
1031 skb_reserve(skb, ETH_HLEN);
1032 tt_response = (struct tt_query_packet *)skb_put(skb,
1033 tt_query_size + tt_len);
1034 tt_response->ttvn = ttvn;
1035 tt_response->tt_data = htons(tt_tot);
1036
1037 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1038 tt_count = 0;
1039
1040 rcu_read_lock();
1041 for (i = 0; i < hash->size; i++) {
1042 head = &hash->table[i];
1043
1044 hlist_for_each_entry_rcu(tt_local_entry, node,
1045 head, hash_entry) {
1046 if (tt_count == tt_tot)
1047 break;
1048
1049 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
1050 continue;
1051
1052 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
1053 tt_change->flags = NO_FLAGS;
1054
1055 tt_count++;
1056 tt_change++;
1057 }
1058 }
1059 rcu_read_unlock();
1060
1061out:
1062 return skb;
1063}
1064
1065int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1066 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1067{
1068 struct sk_buff *skb = NULL;
1069 struct tt_query_packet *tt_request;
1070 struct neigh_node *neigh_node = NULL;
1071 struct hard_iface *primary_if;
1072 struct tt_req_node *tt_req_node = NULL;
1073 int ret = 1;
1074
1075 primary_if = primary_if_get_selected(bat_priv);
1076 if (!primary_if)
1077 goto out;
1078
1079 /* The new tt_req will be issued only if I'm not waiting for a
1080 * reply from the same orig_node yet */
1081 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1082 if (!tt_req_node)
1083 goto out;
1084
1085 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1086 if (!skb)
1087 goto out;
1088
1089 skb_reserve(skb, ETH_HLEN);
1090
1091 tt_request = (struct tt_query_packet *)skb_put(skb,
1092 sizeof(struct tt_query_packet));
1093
1094 tt_request->packet_type = BAT_TT_QUERY;
1095 tt_request->version = COMPAT_VERSION;
1096 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1097 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1098 tt_request->ttl = TTL;
1099 tt_request->ttvn = ttvn;
1100 tt_request->tt_data = tt_crc;
1101 tt_request->flags = TT_REQUEST;
1102
1103 if (full_table)
1104 tt_request->flags |= TT_FULL_TABLE;
1105
1106 neigh_node = orig_node_get_router(dst_orig_node);
1107 if (!neigh_node)
1108 goto out;
1109
1110 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1111 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1112 (full_table ? 'F' : '.'));
1113
1114 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1115 ret = 0;
1116
1117out:
1118 if (neigh_node)
1119 neigh_node_free_ref(neigh_node);
1120 if (primary_if)
1121 hardif_free_ref(primary_if);
1122 if (ret)
1123 kfree_skb(skb);
1124 if (ret && tt_req_node) {
1125 spin_lock_bh(&bat_priv->tt_req_list_lock);
1126 list_del(&tt_req_node->list);
1127 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1128 kfree(tt_req_node);
1129 }
1130 return ret;
1131}
1132
1133static bool send_other_tt_response(struct bat_priv *bat_priv,
1134 struct tt_query_packet *tt_request)
1135{
1136 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1137 struct neigh_node *neigh_node = NULL;
1138 struct hard_iface *primary_if = NULL;
1139 uint8_t orig_ttvn, req_ttvn, ttvn;
1140 int ret = false;
1141 unsigned char *tt_buff;
1142 bool full_table;
1143 uint16_t tt_len, tt_tot;
1144 struct sk_buff *skb = NULL;
1145 struct tt_query_packet *tt_response;
1146
1147 bat_dbg(DBG_TT, bat_priv,
1148 "Received TT_REQUEST from %pM for "
1149 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1150 tt_request->ttvn, tt_request->dst,
1151 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1152
1153 /* Let's get the orig node of the REAL destination */
1154 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1155 if (!req_dst_orig_node)
1156 goto out;
1157
1158 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1159 if (!res_dst_orig_node)
1160 goto out;
1161
1162 neigh_node = orig_node_get_router(res_dst_orig_node);
1163 if (!neigh_node)
1164 goto out;
1165
1166 primary_if = primary_if_get_selected(bat_priv);
1167 if (!primary_if)
1168 goto out;
1169
1170 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1171 req_ttvn = tt_request->ttvn;
1172
Antonio Quartulli015758d2011-07-09 17:52:13 +02001173 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001174 if (orig_ttvn != req_ttvn ||
1175 tt_request->tt_data != req_dst_orig_node->tt_crc)
1176 goto out;
1177
Antonio Quartulli015758d2011-07-09 17:52:13 +02001178 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001179 if (tt_request->flags & TT_FULL_TABLE ||
1180 !req_dst_orig_node->tt_buff)
1181 full_table = true;
1182 else
1183 full_table = false;
1184
1185 /* In this version, fragmentation is not implemented, then
1186 * I'll send only one packet with as much TT entries as I can */
1187 if (!full_table) {
1188 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1189 tt_len = req_dst_orig_node->tt_buff_len;
1190 tt_tot = tt_len / sizeof(struct tt_change);
1191
1192 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1193 tt_len + ETH_HLEN);
1194 if (!skb)
1195 goto unlock;
1196
1197 skb_reserve(skb, ETH_HLEN);
1198 tt_response = (struct tt_query_packet *)skb_put(skb,
1199 sizeof(struct tt_query_packet) + tt_len);
1200 tt_response->ttvn = req_ttvn;
1201 tt_response->tt_data = htons(tt_tot);
1202
1203 tt_buff = skb->data + sizeof(struct tt_query_packet);
1204 /* Copy the last orig_node's OGM buffer */
1205 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1206 req_dst_orig_node->tt_buff_len);
1207
1208 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1209 } else {
1210 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1211 sizeof(struct tt_change);
1212 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1213
1214 skb = tt_response_fill_table(tt_len, ttvn,
1215 bat_priv->tt_global_hash,
1216 primary_if, tt_global_valid_entry,
1217 req_dst_orig_node);
1218 if (!skb)
1219 goto out;
1220
1221 tt_response = (struct tt_query_packet *)skb->data;
1222 }
1223
1224 tt_response->packet_type = BAT_TT_QUERY;
1225 tt_response->version = COMPAT_VERSION;
1226 tt_response->ttl = TTL;
1227 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1228 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1229 tt_response->flags = TT_RESPONSE;
1230
1231 if (full_table)
1232 tt_response->flags |= TT_FULL_TABLE;
1233
1234 bat_dbg(DBG_TT, bat_priv,
1235 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1236 res_dst_orig_node->orig, neigh_node->addr,
1237 req_dst_orig_node->orig, req_ttvn);
1238
1239 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1240 ret = true;
1241 goto out;
1242
1243unlock:
1244 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1245
1246out:
1247 if (res_dst_orig_node)
1248 orig_node_free_ref(res_dst_orig_node);
1249 if (req_dst_orig_node)
1250 orig_node_free_ref(req_dst_orig_node);
1251 if (neigh_node)
1252 neigh_node_free_ref(neigh_node);
1253 if (primary_if)
1254 hardif_free_ref(primary_if);
1255 if (!ret)
1256 kfree_skb(skb);
1257 return ret;
1258
1259}
1260static bool send_my_tt_response(struct bat_priv *bat_priv,
1261 struct tt_query_packet *tt_request)
1262{
1263 struct orig_node *orig_node = NULL;
1264 struct neigh_node *neigh_node = NULL;
1265 struct hard_iface *primary_if = NULL;
1266 uint8_t my_ttvn, req_ttvn, ttvn;
1267 int ret = false;
1268 unsigned char *tt_buff;
1269 bool full_table;
1270 uint16_t tt_len, tt_tot;
1271 struct sk_buff *skb = NULL;
1272 struct tt_query_packet *tt_response;
1273
1274 bat_dbg(DBG_TT, bat_priv,
1275 "Received TT_REQUEST from %pM for "
1276 "ttvn: %u (me) [%c]\n", tt_request->src,
1277 tt_request->ttvn,
1278 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1279
1280
1281 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1282 req_ttvn = tt_request->ttvn;
1283
1284 orig_node = get_orig_node(bat_priv, tt_request->src);
1285 if (!orig_node)
1286 goto out;
1287
1288 neigh_node = orig_node_get_router(orig_node);
1289 if (!neigh_node)
1290 goto out;
1291
1292 primary_if = primary_if_get_selected(bat_priv);
1293 if (!primary_if)
1294 goto out;
1295
1296 /* If the full table has been explicitly requested or the gap
1297 * is too big send the whole local translation table */
1298 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1299 !bat_priv->tt_buff)
1300 full_table = true;
1301 else
1302 full_table = false;
1303
1304 /* In this version, fragmentation is not implemented, then
1305 * I'll send only one packet with as much TT entries as I can */
1306 if (!full_table) {
1307 spin_lock_bh(&bat_priv->tt_buff_lock);
1308 tt_len = bat_priv->tt_buff_len;
1309 tt_tot = tt_len / sizeof(struct tt_change);
1310
1311 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1312 tt_len + ETH_HLEN);
1313 if (!skb)
1314 goto unlock;
1315
1316 skb_reserve(skb, ETH_HLEN);
1317 tt_response = (struct tt_query_packet *)skb_put(skb,
1318 sizeof(struct tt_query_packet) + tt_len);
1319 tt_response->ttvn = req_ttvn;
1320 tt_response->tt_data = htons(tt_tot);
1321
1322 tt_buff = skb->data + sizeof(struct tt_query_packet);
1323 memcpy(tt_buff, bat_priv->tt_buff,
1324 bat_priv->tt_buff_len);
1325 spin_unlock_bh(&bat_priv->tt_buff_lock);
1326 } else {
1327 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1328 sizeof(struct tt_change);
1329 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1330
1331 skb = tt_response_fill_table(tt_len, ttvn,
1332 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001333 primary_if, tt_local_valid_entry,
1334 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001335 if (!skb)
1336 goto out;
1337
1338 tt_response = (struct tt_query_packet *)skb->data;
1339 }
1340
1341 tt_response->packet_type = BAT_TT_QUERY;
1342 tt_response->version = COMPAT_VERSION;
1343 tt_response->ttl = TTL;
1344 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1345 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1346 tt_response->flags = TT_RESPONSE;
1347
1348 if (full_table)
1349 tt_response->flags |= TT_FULL_TABLE;
1350
1351 bat_dbg(DBG_TT, bat_priv,
1352 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1353 orig_node->orig, neigh_node->addr,
1354 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1355
1356 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1357 ret = true;
1358 goto out;
1359
1360unlock:
1361 spin_unlock_bh(&bat_priv->tt_buff_lock);
1362out:
1363 if (orig_node)
1364 orig_node_free_ref(orig_node);
1365 if (neigh_node)
1366 neigh_node_free_ref(neigh_node);
1367 if (primary_if)
1368 hardif_free_ref(primary_if);
1369 if (!ret)
1370 kfree_skb(skb);
1371 /* This packet was for me, so it doesn't need to be re-routed */
1372 return true;
1373}
1374
1375bool send_tt_response(struct bat_priv *bat_priv,
1376 struct tt_query_packet *tt_request)
1377{
1378 if (is_my_mac(tt_request->dst))
1379 return send_my_tt_response(bat_priv, tt_request);
1380 else
1381 return send_other_tt_response(bat_priv, tt_request);
1382}
1383
1384static void _tt_update_changes(struct bat_priv *bat_priv,
1385 struct orig_node *orig_node,
1386 struct tt_change *tt_change,
1387 uint16_t tt_num_changes, uint8_t ttvn)
1388{
1389 int i;
1390
1391 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001392 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001393 tt_global_del(bat_priv, orig_node,
1394 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001395 "tt removed by changes",
1396 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397 else
1398 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001399 (tt_change + i)->addr, ttvn, false,
1400 (tt_change + i)->flags &
1401 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001402 /* In case of problem while storing a
1403 * global_entry, we stop the updating
1404 * procedure without committing the
1405 * ttvn change. This will avoid to send
1406 * corrupted data on tt_request
1407 */
1408 return;
1409 }
1410}
1411
1412static void tt_fill_gtable(struct bat_priv *bat_priv,
1413 struct tt_query_packet *tt_response)
1414{
1415 struct orig_node *orig_node = NULL;
1416
1417 orig_node = orig_hash_find(bat_priv, tt_response->src);
1418 if (!orig_node)
1419 goto out;
1420
1421 /* Purge the old table first.. */
1422 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1423
1424 _tt_update_changes(bat_priv, orig_node,
1425 (struct tt_change *)(tt_response + 1),
1426 tt_response->tt_data, tt_response->ttvn);
1427
1428 spin_lock_bh(&orig_node->tt_buff_lock);
1429 kfree(orig_node->tt_buff);
1430 orig_node->tt_buff_len = 0;
1431 orig_node->tt_buff = NULL;
1432 spin_unlock_bh(&orig_node->tt_buff_lock);
1433
1434 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1435
1436out:
1437 if (orig_node)
1438 orig_node_free_ref(orig_node);
1439}
1440
1441void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1442 uint16_t tt_num_changes, uint8_t ttvn,
1443 struct tt_change *tt_change)
1444{
1445 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1446 ttvn);
1447
1448 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1449 tt_num_changes);
1450 atomic_set(&orig_node->last_ttvn, ttvn);
1451}
1452
1453bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1454{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001455 struct tt_local_entry *tt_local_entry = NULL;
1456 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457
Antonio Quartullia73105b2011-04-27 14:27:44 +02001458 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001459 if (!tt_local_entry)
1460 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001461 /* Check if the client has been logically deleted (but is kept for
1462 * consistency purpose) */
1463 if (tt_local_entry->flags & TT_CLIENT_PENDING)
1464 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001465 ret = true;
1466out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001468 tt_local_entry_free_ref(tt_local_entry);
1469 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470}
1471
1472void handle_tt_response(struct bat_priv *bat_priv,
1473 struct tt_query_packet *tt_response)
1474{
1475 struct tt_req_node *node, *safe;
1476 struct orig_node *orig_node = NULL;
1477
1478 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1479 "ttvn %d t_size: %d [%c]\n",
1480 tt_response->src, tt_response->ttvn,
1481 tt_response->tt_data,
1482 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1483
1484 orig_node = orig_hash_find(bat_priv, tt_response->src);
1485 if (!orig_node)
1486 goto out;
1487
1488 if (tt_response->flags & TT_FULL_TABLE)
1489 tt_fill_gtable(bat_priv, tt_response);
1490 else
1491 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1492 tt_response->ttvn,
1493 (struct tt_change *)(tt_response + 1));
1494
1495 /* Delete the tt_req_node from pending tt_requests list */
1496 spin_lock_bh(&bat_priv->tt_req_list_lock);
1497 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1498 if (!compare_eth(node->addr, tt_response->src))
1499 continue;
1500 list_del(&node->list);
1501 kfree(node);
1502 }
1503 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1504
1505 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001507 /* Roaming phase is over: tables are in sync again. I can
1508 * unset the flag */
1509 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510out:
1511 if (orig_node)
1512 orig_node_free_ref(orig_node);
1513}
1514
1515int tt_init(struct bat_priv *bat_priv)
1516{
1517 if (!tt_local_init(bat_priv))
1518 return 0;
1519
1520 if (!tt_global_init(bat_priv))
1521 return 0;
1522
1523 tt_start_timer(bat_priv);
1524
1525 return 1;
1526}
1527
Antonio Quartullicc47f662011-04-27 14:27:57 +02001528static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001530 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001531
Antonio Quartullicc47f662011-04-27 14:27:57 +02001532 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001533
Antonio Quartullicc47f662011-04-27 14:27:57 +02001534 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1535 list_del(&node->list);
1536 kfree(node);
1537 }
1538
1539 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1540}
1541
1542static void tt_roam_purge(struct bat_priv *bat_priv)
1543{
1544 struct tt_roam_node *node, *safe;
1545
1546 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1547 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1548 if (!is_out_of_time(node->first_time,
1549 ROAMING_MAX_TIME * 1000))
1550 continue;
1551
1552 list_del(&node->list);
1553 kfree(node);
1554 }
1555 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1556}
1557
1558/* This function checks whether the client already reached the
1559 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1560 * will not be sent.
1561 *
1562 * returns true if the ROAMING_ADV can be sent, false otherwise */
1563static bool tt_check_roam_count(struct bat_priv *bat_priv,
1564 uint8_t *client)
1565{
1566 struct tt_roam_node *tt_roam_node;
1567 bool ret = false;
1568
1569 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1570 /* The new tt_req will be issued only if I'm not waiting for a
1571 * reply from the same orig_node yet */
1572 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1573 if (!compare_eth(tt_roam_node->addr, client))
1574 continue;
1575
1576 if (is_out_of_time(tt_roam_node->first_time,
1577 ROAMING_MAX_TIME * 1000))
1578 continue;
1579
1580 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1581 /* Sorry, you roamed too many times! */
1582 goto unlock;
1583 ret = true;
1584 break;
1585 }
1586
1587 if (!ret) {
1588 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1589 if (!tt_roam_node)
1590 goto unlock;
1591
1592 tt_roam_node->first_time = jiffies;
1593 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1594 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1595
1596 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1597 ret = true;
1598 }
1599
1600unlock:
1601 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1602 return ret;
1603}
1604
1605void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1606 struct orig_node *orig_node)
1607{
1608 struct neigh_node *neigh_node = NULL;
1609 struct sk_buff *skb = NULL;
1610 struct roam_adv_packet *roam_adv_packet;
1611 int ret = 1;
1612 struct hard_iface *primary_if;
1613
1614 /* before going on we have to check whether the client has
1615 * already roamed to us too many times */
1616 if (!tt_check_roam_count(bat_priv, client))
1617 goto out;
1618
1619 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1620 if (!skb)
1621 goto out;
1622
1623 skb_reserve(skb, ETH_HLEN);
1624
1625 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1626 sizeof(struct roam_adv_packet));
1627
1628 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1629 roam_adv_packet->version = COMPAT_VERSION;
1630 roam_adv_packet->ttl = TTL;
1631 primary_if = primary_if_get_selected(bat_priv);
1632 if (!primary_if)
1633 goto out;
1634 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1635 hardif_free_ref(primary_if);
1636 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1637 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1638
1639 neigh_node = orig_node_get_router(orig_node);
1640 if (!neigh_node)
1641 goto out;
1642
1643 bat_dbg(DBG_TT, bat_priv,
1644 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1645 orig_node->orig, client, neigh_node->addr);
1646
1647 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1648 ret = 0;
1649
1650out:
1651 if (neigh_node)
1652 neigh_node_free_ref(neigh_node);
1653 if (ret)
1654 kfree_skb(skb);
1655 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656}
1657
1658static void tt_purge(struct work_struct *work)
1659{
1660 struct delayed_work *delayed_work =
1661 container_of(work, struct delayed_work, work);
1662 struct bat_priv *bat_priv =
1663 container_of(delayed_work, struct bat_priv, tt_work);
1664
1665 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001666 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001667 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001668 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669
1670 tt_start_timer(bat_priv);
1671}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001672
1673void tt_free(struct bat_priv *bat_priv)
1674{
1675 cancel_delayed_work_sync(&bat_priv->tt_work);
1676
1677 tt_local_table_free(bat_priv);
1678 tt_global_table_free(bat_priv);
1679 tt_req_list_free(bat_priv);
1680 tt_changes_list_free(bat_priv);
1681 tt_roam_list_free(bat_priv);
1682
1683 kfree(bat_priv->tt_buff);
1684}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001685
1686/* This function will reset the specified flags from all the entries in
1687 * the given hash table and will increment num_local_tt for each involved
1688 * entry */
1689static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
1690{
1691 int i;
1692 struct hashtable_t *hash = bat_priv->tt_local_hash;
1693 struct hlist_head *head;
1694 struct hlist_node *node;
1695 struct tt_local_entry *tt_local_entry;
1696
1697 if (!hash)
1698 return;
1699
1700 for (i = 0; i < hash->size; i++) {
1701 head = &hash->table[i];
1702
1703 rcu_read_lock();
1704 hlist_for_each_entry_rcu(tt_local_entry, node,
1705 head, hash_entry) {
1706 tt_local_entry->flags &= ~flags;
1707 atomic_inc(&bat_priv->num_local_tt);
1708 }
1709 rcu_read_unlock();
1710 }
1711
1712}
1713
1714/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1715static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1716{
1717 struct hashtable_t *hash = bat_priv->tt_local_hash;
1718 struct tt_local_entry *tt_local_entry;
1719 struct hlist_node *node, *node_tmp;
1720 struct hlist_head *head;
1721 spinlock_t *list_lock; /* protects write access to the hash lists */
1722 int i;
1723
1724 if (!hash)
1725 return;
1726
1727 for (i = 0; i < hash->size; i++) {
1728 head = &hash->table[i];
1729 list_lock = &hash->list_locks[i];
1730
1731 spin_lock_bh(list_lock);
1732 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
1733 head, hash_entry) {
1734 if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
1735 continue;
1736
1737 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
1738 "(%pM): pending\n", tt_local_entry->addr);
1739
1740 atomic_dec(&bat_priv->num_local_tt);
1741 hlist_del_rcu(node);
1742 tt_local_entry_free_ref(tt_local_entry);
1743 }
1744 spin_unlock_bh(list_lock);
1745 }
1746
1747}
1748
1749void tt_commit_changes(struct bat_priv *bat_priv)
1750{
1751 tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
1752 tt_local_purge_pending_clients(bat_priv);
1753
1754 /* Increment the TTVN only once per OGM interval */
1755 atomic_inc(&bat_priv->ttvn);
1756 bat_priv->tt_poss_change = false;
1757}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001758
1759bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1760{
1761 struct tt_local_entry *tt_local_entry = NULL;
1762 struct tt_global_entry *tt_global_entry = NULL;
1763 bool ret = true;
1764
1765 if (!atomic_read(&bat_priv->ap_isolation))
1766 return false;
1767
1768 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1769 if (!tt_local_entry)
1770 goto out;
1771
1772 tt_global_entry = tt_global_hash_find(bat_priv, src);
1773 if (!tt_global_entry)
1774 goto out;
1775
1776 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1777 goto out;
1778
1779 ret = false;
1780
1781out:
1782 if (tt_global_entry)
1783 tt_global_entry_free_ref(tt_global_entry);
1784 if (tt_local_entry)
1785 tt_local_entry_free_ref(tt_local_entry);
1786 return ret;
1787}