blob: 78b9528bfc2a5143dcc35b391b9bdfcd38f71ea3 [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;
Antonio Quartullic90681b2011-10-05 17:05:25 +020070 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000071
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;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200102 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000103
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
Simon Wunderlich531027f2011-10-19 11:02:25 +0200140static void tt_global_entry_free_rcu(struct rcu_head *rcu)
141{
142 struct tt_global_entry *tt_global_entry;
143
144 tt_global_entry = container_of(rcu, struct tt_global_entry, rcu);
145
146 if (tt_global_entry->orig_node)
147 orig_node_free_ref(tt_global_entry->orig_node);
148
149 kfree(tt_global_entry);
150}
151
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200152static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
153{
154 if (atomic_dec_and_test(&tt_global_entry->refcount))
Simon Wunderlich531027f2011-10-19 11:02:25 +0200155 call_rcu(&tt_global_entry->rcu, tt_global_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200156}
157
Antonio Quartulliff66c972011-06-30 01:14:00 +0200158static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
159 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200160{
161 struct tt_change_node *tt_change_node;
162
163 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
164
165 if (!tt_change_node)
166 return;
167
Antonio Quartulliff66c972011-06-30 01:14:00 +0200168 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200169 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
170
171 spin_lock_bh(&bat_priv->tt_changes_list_lock);
172 /* track the change in the OGMinterval list */
173 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
174 atomic_inc(&bat_priv->tt_local_changes);
175 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
176
177 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
178}
179
180int tt_len(int changes_num)
181{
182 return changes_num * sizeof(struct tt_change);
183}
184
185static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 return 1;
189
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200190 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200192 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 return 0;
194
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 return 1;
196}
197
Antonio Quartullibc279082011-07-07 15:35:35 +0200198void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
199 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200{
201 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200202 struct tt_local_entry *tt_local_entry = NULL;
203 struct tt_global_entry *tt_global_entry = NULL;
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 Quartulli2dafb492011-05-05 08:42:45 +0200220 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
221 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200222 tt_local_entry->flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200223 if (is_wifi_iface(ifindex))
224 tt_local_entry->flags |= TT_CLIENT_WIFI;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200225 atomic_set(&tt_local_entry->refcount, 2);
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 Quartulli5fbc1592011-06-17 16:11:27 +0200229 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Antonio Quartulliff66c972011-06-30 01:14:00 +0200231 tt_local_event(bat_priv, addr, tt_local_entry->flags);
232
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200233 /* The local entry has to be marked as NEW to avoid to send it in
234 * a full table response going out before the next ttvn increment
235 * (consistency check) */
236 tt_local_entry->flags |= TT_CLIENT_NEW;
237
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200238 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
239 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200240
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200242 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
Antonio Quartullicc47f662011-04-27 14:27:57 +0200244 /* Check whether it is a roaming! */
245 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200246 /* This node is probably going to update its tt table */
247 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200248 /* The global entry has to be marked as PENDING and has to be
249 * kept for consistency purpose */
250 tt_global_entry->flags |= TT_CLIENT_PENDING;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200251 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200252 tt_global_entry->orig_node);
253 }
254out:
255 if (tt_local_entry)
256 tt_local_entry_free_ref(tt_local_entry);
257 if (tt_global_entry)
258 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259}
260
Antonio Quartullia73105b2011-04-27 14:27:44 +0200261int tt_changes_fill_buffer(struct bat_priv *bat_priv,
262 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264 int count = 0, tot_changes = 0;
265 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Antonio Quartullia73105b2011-04-27 14:27:44 +0200267 if (buff_len > 0)
268 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartullia73105b2011-04-27 14:27:44 +0200270 spin_lock_bh(&bat_priv->tt_changes_list_lock);
271 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
Antonio Quartullia73105b2011-04-27 14:27:44 +0200273 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
274 list) {
275 if (count < tot_changes) {
276 memcpy(buff + tt_len(count),
277 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 count++;
279 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200280 list_del(&entry->list);
281 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200283 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
Antonio Quartullia73105b2011-04-27 14:27:44 +0200285 /* Keep the buffer for possible tt_request */
286 spin_lock_bh(&bat_priv->tt_buff_lock);
287 kfree(bat_priv->tt_buff);
288 bat_priv->tt_buff_len = 0;
289 bat_priv->tt_buff = NULL;
290 /* We check whether this new OGM has no changes due to size
291 * problems */
292 if (buff_len > 0) {
293 /**
294 * if kmalloc() fails we will reply with the full table
295 * instead of providing the diff
296 */
297 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
298 if (bat_priv->tt_buff) {
299 memcpy(bat_priv->tt_buff, buff, buff_len);
300 bat_priv->tt_buff_len = buff_len;
301 }
302 }
303 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Antonio Quartullia73105b2011-04-27 14:27:44 +0200305 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306}
307
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200308int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309{
310 struct net_device *net_dev = (struct net_device *)seq->private;
311 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200312 struct hashtable_t *hash = bat_priv->tt_local_hash;
313 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200314 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000315 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200317 uint32_t i;
318 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Marek Lindner32ae9b22011-04-20 15:40:58 +0200320 primary_if = primary_if_get_selected(bat_priv);
321 if (!primary_if) {
322 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
323 "please specify interfaces to enable it\n",
324 net_dev->name);
325 goto out;
326 }
327
328 if (primary_if->if_status != IF_ACTIVE) {
329 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
330 "primary interface not active\n",
331 net_dev->name);
332 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 }
334
335 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200336 "announced via TT (TTVN: %u):\n",
337 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 for (i = 0; i < hash->size; i++) {
340 head = &hash->table[i];
341
Marek Lindner7aadf882011-02-18 12:28:09 +0000342 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200343 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000344 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200345 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200346 tt_local_entry->addr,
347 (tt_local_entry->flags &
348 TT_CLIENT_ROAM ? 'R' : '.'),
349 (tt_local_entry->flags &
350 TT_CLIENT_NOPURGE ? 'P' : '.'),
351 (tt_local_entry->flags &
352 TT_CLIENT_NEW ? 'N' : '.'),
353 (tt_local_entry->flags &
354 TT_CLIENT_PENDING ? 'X' : '.'),
355 (tt_local_entry->flags &
356 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000358 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200360out:
361 if (primary_if)
362 hardif_free_ref(primary_if);
363 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364}
365
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200366static void tt_local_set_pending(struct bat_priv *bat_priv,
367 struct tt_local_entry *tt_local_entry,
368 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369{
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200370 tt_local_event(bat_priv, tt_local_entry->addr,
371 tt_local_entry->flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Antonio Quartulli015758d2011-07-09 17:52:13 +0200373 /* The local client has to be marked as "pending to be removed" but has
374 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200375 * response issued before the net ttvn increment (consistency check) */
376 tt_local_entry->flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377}
378
Antonio Quartullia73105b2011-04-27 14:27:44 +0200379void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200380 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200382 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200384 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200385 if (!tt_local_entry)
386 goto out;
387
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200388 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
389 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
390
391 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
392 "%s\n", tt_local_entry->addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200393out:
394 if (tt_local_entry)
395 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396}
397
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200400 struct hashtable_t *hash = bat_priv->tt_local_hash;
401 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000402 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200404 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200405 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407 for (i = 0; i < hash->size; i++) {
408 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200409 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200411 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200412 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000413 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200414 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000415 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200417 /* entry already marked for deletion */
418 if (tt_local_entry->flags & TT_CLIENT_PENDING)
419 continue;
420
Antonio Quartullia73105b2011-04-27 14:27:44 +0200421 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200422 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000423 continue;
424
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200425 tt_local_set_pending(bat_priv, tt_local_entry,
426 TT_CLIENT_DEL);
427 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
428 "pending to be removed: timed out\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200429 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200431 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 }
433
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434}
435
Antonio Quartullia73105b2011-04-27 14:27:44 +0200436static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200439 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200440 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200441 struct hlist_node *node, *node_tmp;
442 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200443 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200444
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200445 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 return;
447
Antonio Quartullia73105b2011-04-27 14:27:44 +0200448 hash = bat_priv->tt_local_hash;
449
450 for (i = 0; i < hash->size; i++) {
451 head = &hash->table[i];
452 list_lock = &hash->list_locks[i];
453
454 spin_lock_bh(list_lock);
455 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
456 head, hash_entry) {
457 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200458 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200459 }
460 spin_unlock_bh(list_lock);
461 }
462
463 hash_destroy(hash);
464
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200465 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466}
467
Antonio Quartullia73105b2011-04-27 14:27:44 +0200468static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200470 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 return 1;
472
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200473 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200475 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476 return 0;
477
478 return 1;
479}
480
Antonio Quartullia73105b2011-04-27 14:27:44 +0200481static void tt_changes_list_free(struct bat_priv *bat_priv)
482{
483 struct tt_change_node *entry, *safe;
484
485 spin_lock_bh(&bat_priv->tt_changes_list_lock);
486
487 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
488 list) {
489 list_del(&entry->list);
490 kfree(entry);
491 }
492
493 atomic_set(&bat_priv->tt_local_changes, 0);
494 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
495}
496
497/* caller must hold orig_node refcount */
498int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200499 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
500 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200502 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200503 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200504 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505
Antonio Quartullia73105b2011-04-27 14:27:44 +0200506 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Antonio Quartullia73105b2011-04-27 14:27:44 +0200508 if (!tt_global_entry) {
509 tt_global_entry =
510 kmalloc(sizeof(*tt_global_entry),
511 GFP_ATOMIC);
512 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200513 goto out;
514
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
516 /* Assign the new orig_node */
517 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200518 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200519 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200520 tt_global_entry->flags = NO_FLAGS;
521 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200522 atomic_set(&tt_global_entry->refcount, 2);
523
Antonio Quartullia73105b2011-04-27 14:27:44 +0200524 hash_add(bat_priv->tt_global_hash, compare_gtt,
525 choose_orig, tt_global_entry,
526 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200527 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200528 } else {
529 if (tt_global_entry->orig_node != orig_node) {
530 atomic_dec(&tt_global_entry->orig_node->tt_size);
531 orig_node_tmp = tt_global_entry->orig_node;
532 atomic_inc(&orig_node->refcount);
533 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200534 orig_node_free_ref(orig_node_tmp);
535 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200537 tt_global_entry->ttvn = ttvn;
538 tt_global_entry->flags = NO_FLAGS;
539 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200541
Antonio Quartullibc279082011-07-07 15:35:35 +0200542 if (wifi)
543 tt_global_entry->flags |= TT_CLIENT_WIFI;
544
Antonio Quartullia73105b2011-04-27 14:27:44 +0200545 bat_dbg(DBG_TT, bat_priv,
546 "Creating new global tt entry: %pM (via %pM)\n",
547 tt_global_entry->addr, orig_node->orig);
548
549 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200550 tt_local_remove(bat_priv, tt_global_entry->addr,
551 "global tt received", roaming);
552 ret = 1;
553out:
554 if (tt_global_entry)
555 tt_global_entry_free_ref(tt_global_entry);
556 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557}
558
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200559int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560{
561 struct net_device *net_dev = (struct net_device *)seq->private;
562 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200563 struct hashtable_t *hash = bat_priv->tt_global_hash;
564 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200565 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000566 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200568 uint32_t i;
569 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
Marek Lindner32ae9b22011-04-20 15:40:58 +0200571 primary_if = primary_if_get_selected(bat_priv);
572 if (!primary_if) {
573 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
574 "specify interfaces to enable it\n",
575 net_dev->name);
576 goto out;
577 }
578
579 if (primary_if->if_status != IF_ACTIVE) {
580 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
581 "primary interface not active\n",
582 net_dev->name);
583 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 }
585
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200586 seq_printf(seq,
587 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200589 seq_printf(seq, " %-13s %s %-15s %s %s\n",
590 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 for (i = 0; i < hash->size; i++) {
593 head = &hash->table[i];
594
Marek Lindner7aadf882011-02-18 12:28:09 +0000595 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200596 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000597 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200598 seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200599 "[%c%c%c]\n", tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200600 tt_global_entry->ttvn,
601 tt_global_entry->orig_node->orig,
602 (uint8_t) atomic_read(
603 &tt_global_entry->orig_node->
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200604 last_ttvn),
605 (tt_global_entry->flags &
606 TT_CLIENT_ROAM ? 'R' : '.'),
607 (tt_global_entry->flags &
608 TT_CLIENT_PENDING ? 'X' : '.'),
609 (tt_global_entry->flags &
610 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000612 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200614out:
615 if (primary_if)
616 hardif_free_ref(primary_if);
617 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618}
619
Antonio Quartullia73105b2011-04-27 14:27:44 +0200620static void _tt_global_del(struct bat_priv *bat_priv,
621 struct tt_global_entry *tt_global_entry,
622 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200625 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200626
627 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200628 "Deleting global tt entry %pM (via %pM): %s\n",
629 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 message);
631
Antonio Quartullia73105b2011-04-27 14:27:44 +0200632 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200633
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200634 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
635 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200636out:
637 if (tt_global_entry)
638 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639}
640
Antonio Quartullia73105b2011-04-27 14:27:44 +0200641void tt_global_del(struct bat_priv *bat_priv,
642 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200643 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200645 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646
Antonio Quartullia73105b2011-04-27 14:27:44 +0200647 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648 if (!tt_global_entry)
649 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200650
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200651 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200652 if (roaming) {
653 tt_global_entry->flags |= TT_CLIENT_ROAM;
654 tt_global_entry->roam_at = jiffies;
655 goto out;
656 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200657 _tt_global_del(bat_priv, tt_global_entry, message);
658 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200659out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200660 if (tt_global_entry)
661 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662}
663
664void tt_global_del_orig(struct bat_priv *bat_priv,
665 struct orig_node *orig_node, const char *message)
666{
667 struct tt_global_entry *tt_global_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200668 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200669 struct hashtable_t *hash = bat_priv->tt_global_hash;
670 struct hlist_node *node, *safe;
671 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200672 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200673
Simon Wunderlich6e801492011-10-19 10:28:26 +0200674 if (!hash)
675 return;
676
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677 for (i = 0; i < hash->size; i++) {
678 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200679 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200681 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200682 hlist_for_each_entry_safe(tt_global_entry, node, safe,
683 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200684 if (tt_global_entry->orig_node == orig_node) {
685 bat_dbg(DBG_TT, bat_priv,
686 "Deleting global tt entry %pM "
Antonio Quartulli87944972011-09-19 12:29:19 +0200687 "(via %pM): %s\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200688 tt_global_entry->addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200689 tt_global_entry->orig_node->orig,
690 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200691 hlist_del_rcu(node);
692 tt_global_entry_free_ref(tt_global_entry);
693 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200695 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200697 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698}
699
Antonio Quartullicc47f662011-04-27 14:27:57 +0200700static void tt_global_roam_purge(struct bat_priv *bat_priv)
701{
702 struct hashtable_t *hash = bat_priv->tt_global_hash;
703 struct tt_global_entry *tt_global_entry;
704 struct hlist_node *node, *node_tmp;
705 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200706 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200707 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200708
Antonio Quartullicc47f662011-04-27 14:27:57 +0200709 for (i = 0; i < hash->size; i++) {
710 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200711 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200712
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200713 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200714 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
715 head, hash_entry) {
716 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
717 continue;
718 if (!is_out_of_time(tt_global_entry->roam_at,
719 TT_CLIENT_ROAM_TIMEOUT * 1000))
720 continue;
721
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200722 bat_dbg(DBG_TT, bat_priv, "Deleting global "
723 "tt entry (%pM): Roaming timeout\n",
724 tt_global_entry->addr);
725 atomic_dec(&tt_global_entry->orig_node->tt_size);
726 hlist_del_rcu(node);
727 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200728 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200729 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200730 }
731
Antonio Quartullicc47f662011-04-27 14:27:57 +0200732}
733
Antonio Quartullia73105b2011-04-27 14:27:44 +0200734static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200736 struct hashtable_t *hash;
737 spinlock_t *list_lock; /* protects write access to the hash lists */
738 struct tt_global_entry *tt_global_entry;
739 struct hlist_node *node, *node_tmp;
740 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200741 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200742
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200743 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 return;
745
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200746 hash = bat_priv->tt_global_hash;
747
748 for (i = 0; i < hash->size; i++) {
749 head = &hash->table[i];
750 list_lock = &hash->list_locks[i];
751
752 spin_lock_bh(list_lock);
753 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
754 head, hash_entry) {
755 hlist_del_rcu(node);
756 tt_global_entry_free_ref(tt_global_entry);
757 }
758 spin_unlock_bh(list_lock);
759 }
760
761 hash_destroy(hash);
762
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200763 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764}
765
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200766static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
767 struct tt_global_entry *tt_global_entry)
768{
769 bool ret = false;
770
771 if (tt_local_entry->flags & TT_CLIENT_WIFI &&
772 tt_global_entry->flags & TT_CLIENT_WIFI)
773 ret = true;
774
775 return ret;
776}
777
Sven Eckelmann747e4222011-05-14 23:14:50 +0200778struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200779 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200781 struct tt_local_entry *tt_local_entry = NULL;
782 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000783 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200785 if (src && atomic_read(&bat_priv->ap_isolation)) {
786 tt_local_entry = tt_local_hash_find(bat_priv, src);
787 if (!tt_local_entry)
788 goto out;
789 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000790
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200791 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200792 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000793 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200795 /* check whether the clients should not communicate due to AP
796 * isolation */
797 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
798 goto out;
799
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200800 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200801 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000802
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200803 /* A global client marked as PENDING has already moved from that
804 * originator */
805 if (tt_global_entry->flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200806 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200807
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200808 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000809
810out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200811 if (tt_global_entry)
812 tt_global_entry_free_ref(tt_global_entry);
813 if (tt_local_entry)
814 tt_local_entry_free_ref(tt_local_entry);
815
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000816 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200818
819/* Calculates the checksum of the local table of a given orig_node */
820uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
821{
822 uint16_t total = 0, total_one;
823 struct hashtable_t *hash = bat_priv->tt_global_hash;
824 struct tt_global_entry *tt_global_entry;
825 struct hlist_node *node;
826 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200827 uint32_t i;
828 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200829
830 for (i = 0; i < hash->size; i++) {
831 head = &hash->table[i];
832
833 rcu_read_lock();
834 hlist_for_each_entry_rcu(tt_global_entry, node,
835 head, hash_entry) {
836 if (compare_eth(tt_global_entry->orig_node,
837 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200838 /* Roaming clients are in the global table for
839 * consistency only. They don't have to be
840 * taken into account while computing the
841 * global crc */
842 if (tt_global_entry->flags & TT_CLIENT_ROAM)
843 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200844 total_one = 0;
845 for (j = 0; j < ETH_ALEN; j++)
846 total_one = crc16_byte(total_one,
847 tt_global_entry->addr[j]);
848 total ^= total_one;
849 }
850 }
851 rcu_read_unlock();
852 }
853
854 return total;
855}
856
857/* Calculates the checksum of the local table */
858uint16_t tt_local_crc(struct bat_priv *bat_priv)
859{
860 uint16_t total = 0, total_one;
861 struct hashtable_t *hash = bat_priv->tt_local_hash;
862 struct tt_local_entry *tt_local_entry;
863 struct hlist_node *node;
864 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200865 uint32_t i;
866 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200867
868 for (i = 0; i < hash->size; i++) {
869 head = &hash->table[i];
870
871 rcu_read_lock();
872 hlist_for_each_entry_rcu(tt_local_entry, node,
873 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200874 /* not yet committed clients have not to be taken into
875 * account while computing the CRC */
876 if (tt_local_entry->flags & TT_CLIENT_NEW)
877 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200878 total_one = 0;
879 for (j = 0; j < ETH_ALEN; j++)
880 total_one = crc16_byte(total_one,
881 tt_local_entry->addr[j]);
882 total ^= total_one;
883 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200884 rcu_read_unlock();
885 }
886
887 return total;
888}
889
890static void tt_req_list_free(struct bat_priv *bat_priv)
891{
892 struct tt_req_node *node, *safe;
893
894 spin_lock_bh(&bat_priv->tt_req_list_lock);
895
896 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
897 list_del(&node->list);
898 kfree(node);
899 }
900
901 spin_unlock_bh(&bat_priv->tt_req_list_lock);
902}
903
904void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
905 const unsigned char *tt_buff, uint8_t tt_num_changes)
906{
907 uint16_t tt_buff_len = tt_len(tt_num_changes);
908
909 /* Replace the old buffer only if I received something in the
910 * last OGM (the OGM could carry no changes) */
911 spin_lock_bh(&orig_node->tt_buff_lock);
912 if (tt_buff_len > 0) {
913 kfree(orig_node->tt_buff);
914 orig_node->tt_buff_len = 0;
915 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
916 if (orig_node->tt_buff) {
917 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
918 orig_node->tt_buff_len = tt_buff_len;
919 }
920 }
921 spin_unlock_bh(&orig_node->tt_buff_lock);
922}
923
924static void tt_req_purge(struct bat_priv *bat_priv)
925{
926 struct tt_req_node *node, *safe;
927
928 spin_lock_bh(&bat_priv->tt_req_list_lock);
929 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
930 if (is_out_of_time(node->issued_at,
931 TT_REQUEST_TIMEOUT * 1000)) {
932 list_del(&node->list);
933 kfree(node);
934 }
935 }
936 spin_unlock_bh(&bat_priv->tt_req_list_lock);
937}
938
939/* returns the pointer to the new tt_req_node struct if no request
940 * has already been issued for this orig_node, NULL otherwise */
941static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
942 struct orig_node *orig_node)
943{
944 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
945
946 spin_lock_bh(&bat_priv->tt_req_list_lock);
947 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
948 if (compare_eth(tt_req_node_tmp, orig_node) &&
949 !is_out_of_time(tt_req_node_tmp->issued_at,
950 TT_REQUEST_TIMEOUT * 1000))
951 goto unlock;
952 }
953
954 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
955 if (!tt_req_node)
956 goto unlock;
957
958 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
959 tt_req_node->issued_at = jiffies;
960
961 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
962unlock:
963 spin_unlock_bh(&bat_priv->tt_req_list_lock);
964 return tt_req_node;
965}
966
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200967/* data_ptr is useless here, but has to be kept to respect the prototype */
968static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
969{
970 const struct tt_local_entry *tt_local_entry = entry_ptr;
971
972 if (tt_local_entry->flags & TT_CLIENT_NEW)
973 return 0;
974 return 1;
975}
976
Antonio Quartullia73105b2011-04-27 14:27:44 +0200977static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
978{
979 const struct tt_global_entry *tt_global_entry = entry_ptr;
980 const struct orig_node *orig_node = data_ptr;
981
Antonio Quartullicc47f662011-04-27 14:27:57 +0200982 if (tt_global_entry->flags & TT_CLIENT_ROAM)
983 return 0;
984
Antonio Quartullia73105b2011-04-27 14:27:44 +0200985 return (tt_global_entry->orig_node == orig_node);
986}
987
988static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
989 struct hashtable_t *hash,
990 struct hard_iface *primary_if,
991 int (*valid_cb)(const void *,
992 const void *),
993 void *cb_data)
994{
995 struct tt_local_entry *tt_local_entry;
996 struct tt_query_packet *tt_response;
997 struct tt_change *tt_change;
998 struct hlist_node *node;
999 struct hlist_head *head;
1000 struct sk_buff *skb = NULL;
1001 uint16_t tt_tot, tt_count;
1002 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001003 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001004
1005 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1006 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1007 tt_len -= tt_len % sizeof(struct tt_change);
1008 }
1009 tt_tot = tt_len / sizeof(struct tt_change);
1010
1011 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1012 if (!skb)
1013 goto out;
1014
1015 skb_reserve(skb, ETH_HLEN);
1016 tt_response = (struct tt_query_packet *)skb_put(skb,
1017 tt_query_size + tt_len);
1018 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001019
1020 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1021 tt_count = 0;
1022
1023 rcu_read_lock();
1024 for (i = 0; i < hash->size; i++) {
1025 head = &hash->table[i];
1026
1027 hlist_for_each_entry_rcu(tt_local_entry, node,
1028 head, hash_entry) {
1029 if (tt_count == tt_tot)
1030 break;
1031
1032 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
1033 continue;
1034
1035 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
1036 tt_change->flags = NO_FLAGS;
1037
1038 tt_count++;
1039 tt_change++;
1040 }
1041 }
1042 rcu_read_unlock();
1043
Antonio Quartulli9d852392011-10-17 14:25:13 +02001044 /* store in the message the number of entries we have successfully
1045 * copied */
1046 tt_response->tt_data = htons(tt_count);
1047
Antonio Quartullia73105b2011-04-27 14:27:44 +02001048out:
1049 return skb;
1050}
1051
Marek Lindnera943cac2011-07-30 13:10:18 +02001052static int send_tt_request(struct bat_priv *bat_priv,
1053 struct orig_node *dst_orig_node,
1054 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001055{
1056 struct sk_buff *skb = NULL;
1057 struct tt_query_packet *tt_request;
1058 struct neigh_node *neigh_node = NULL;
1059 struct hard_iface *primary_if;
1060 struct tt_req_node *tt_req_node = NULL;
1061 int ret = 1;
1062
1063 primary_if = primary_if_get_selected(bat_priv);
1064 if (!primary_if)
1065 goto out;
1066
1067 /* The new tt_req will be issued only if I'm not waiting for a
1068 * reply from the same orig_node yet */
1069 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1070 if (!tt_req_node)
1071 goto out;
1072
1073 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1074 if (!skb)
1075 goto out;
1076
1077 skb_reserve(skb, ETH_HLEN);
1078
1079 tt_request = (struct tt_query_packet *)skb_put(skb,
1080 sizeof(struct tt_query_packet));
1081
1082 tt_request->packet_type = BAT_TT_QUERY;
1083 tt_request->version = COMPAT_VERSION;
1084 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1085 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1086 tt_request->ttl = TTL;
1087 tt_request->ttvn = ttvn;
1088 tt_request->tt_data = tt_crc;
1089 tt_request->flags = TT_REQUEST;
1090
1091 if (full_table)
1092 tt_request->flags |= TT_FULL_TABLE;
1093
1094 neigh_node = orig_node_get_router(dst_orig_node);
1095 if (!neigh_node)
1096 goto out;
1097
1098 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1099 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1100 (full_table ? 'F' : '.'));
1101
1102 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1103 ret = 0;
1104
1105out:
1106 if (neigh_node)
1107 neigh_node_free_ref(neigh_node);
1108 if (primary_if)
1109 hardif_free_ref(primary_if);
1110 if (ret)
1111 kfree_skb(skb);
1112 if (ret && tt_req_node) {
1113 spin_lock_bh(&bat_priv->tt_req_list_lock);
1114 list_del(&tt_req_node->list);
1115 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1116 kfree(tt_req_node);
1117 }
1118 return ret;
1119}
1120
1121static bool send_other_tt_response(struct bat_priv *bat_priv,
1122 struct tt_query_packet *tt_request)
1123{
1124 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1125 struct neigh_node *neigh_node = NULL;
1126 struct hard_iface *primary_if = NULL;
1127 uint8_t orig_ttvn, req_ttvn, ttvn;
1128 int ret = false;
1129 unsigned char *tt_buff;
1130 bool full_table;
1131 uint16_t tt_len, tt_tot;
1132 struct sk_buff *skb = NULL;
1133 struct tt_query_packet *tt_response;
1134
1135 bat_dbg(DBG_TT, bat_priv,
1136 "Received TT_REQUEST from %pM for "
1137 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1138 tt_request->ttvn, tt_request->dst,
1139 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1140
1141 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001142 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001143 if (!req_dst_orig_node)
1144 goto out;
1145
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001146 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001147 if (!res_dst_orig_node)
1148 goto out;
1149
1150 neigh_node = orig_node_get_router(res_dst_orig_node);
1151 if (!neigh_node)
1152 goto out;
1153
1154 primary_if = primary_if_get_selected(bat_priv);
1155 if (!primary_if)
1156 goto out;
1157
1158 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1159 req_ttvn = tt_request->ttvn;
1160
Antonio Quartulli015758d2011-07-09 17:52:13 +02001161 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001162 if (orig_ttvn != req_ttvn ||
1163 tt_request->tt_data != req_dst_orig_node->tt_crc)
1164 goto out;
1165
Antonio Quartulli015758d2011-07-09 17:52:13 +02001166 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167 if (tt_request->flags & TT_FULL_TABLE ||
1168 !req_dst_orig_node->tt_buff)
1169 full_table = true;
1170 else
1171 full_table = false;
1172
1173 /* In this version, fragmentation is not implemented, then
1174 * I'll send only one packet with as much TT entries as I can */
1175 if (!full_table) {
1176 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1177 tt_len = req_dst_orig_node->tt_buff_len;
1178 tt_tot = tt_len / sizeof(struct tt_change);
1179
1180 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1181 tt_len + ETH_HLEN);
1182 if (!skb)
1183 goto unlock;
1184
1185 skb_reserve(skb, ETH_HLEN);
1186 tt_response = (struct tt_query_packet *)skb_put(skb,
1187 sizeof(struct tt_query_packet) + tt_len);
1188 tt_response->ttvn = req_ttvn;
1189 tt_response->tt_data = htons(tt_tot);
1190
1191 tt_buff = skb->data + sizeof(struct tt_query_packet);
1192 /* Copy the last orig_node's OGM buffer */
1193 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1194 req_dst_orig_node->tt_buff_len);
1195
1196 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1197 } else {
1198 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1199 sizeof(struct tt_change);
1200 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1201
1202 skb = tt_response_fill_table(tt_len, ttvn,
1203 bat_priv->tt_global_hash,
1204 primary_if, tt_global_valid_entry,
1205 req_dst_orig_node);
1206 if (!skb)
1207 goto out;
1208
1209 tt_response = (struct tt_query_packet *)skb->data;
1210 }
1211
1212 tt_response->packet_type = BAT_TT_QUERY;
1213 tt_response->version = COMPAT_VERSION;
1214 tt_response->ttl = TTL;
1215 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1216 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1217 tt_response->flags = TT_RESPONSE;
1218
1219 if (full_table)
1220 tt_response->flags |= TT_FULL_TABLE;
1221
1222 bat_dbg(DBG_TT, bat_priv,
1223 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1224 res_dst_orig_node->orig, neigh_node->addr,
1225 req_dst_orig_node->orig, req_ttvn);
1226
1227 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1228 ret = true;
1229 goto out;
1230
1231unlock:
1232 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1233
1234out:
1235 if (res_dst_orig_node)
1236 orig_node_free_ref(res_dst_orig_node);
1237 if (req_dst_orig_node)
1238 orig_node_free_ref(req_dst_orig_node);
1239 if (neigh_node)
1240 neigh_node_free_ref(neigh_node);
1241 if (primary_if)
1242 hardif_free_ref(primary_if);
1243 if (!ret)
1244 kfree_skb(skb);
1245 return ret;
1246
1247}
1248static bool send_my_tt_response(struct bat_priv *bat_priv,
1249 struct tt_query_packet *tt_request)
1250{
1251 struct orig_node *orig_node = NULL;
1252 struct neigh_node *neigh_node = NULL;
1253 struct hard_iface *primary_if = NULL;
1254 uint8_t my_ttvn, req_ttvn, ttvn;
1255 int ret = false;
1256 unsigned char *tt_buff;
1257 bool full_table;
1258 uint16_t tt_len, tt_tot;
1259 struct sk_buff *skb = NULL;
1260 struct tt_query_packet *tt_response;
1261
1262 bat_dbg(DBG_TT, bat_priv,
1263 "Received TT_REQUEST from %pM for "
1264 "ttvn: %u (me) [%c]\n", tt_request->src,
1265 tt_request->ttvn,
1266 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1267
1268
1269 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1270 req_ttvn = tt_request->ttvn;
1271
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001272 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001273 if (!orig_node)
1274 goto out;
1275
1276 neigh_node = orig_node_get_router(orig_node);
1277 if (!neigh_node)
1278 goto out;
1279
1280 primary_if = primary_if_get_selected(bat_priv);
1281 if (!primary_if)
1282 goto out;
1283
1284 /* If the full table has been explicitly requested or the gap
1285 * is too big send the whole local translation table */
1286 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1287 !bat_priv->tt_buff)
1288 full_table = true;
1289 else
1290 full_table = false;
1291
1292 /* In this version, fragmentation is not implemented, then
1293 * I'll send only one packet with as much TT entries as I can */
1294 if (!full_table) {
1295 spin_lock_bh(&bat_priv->tt_buff_lock);
1296 tt_len = bat_priv->tt_buff_len;
1297 tt_tot = tt_len / sizeof(struct tt_change);
1298
1299 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1300 tt_len + ETH_HLEN);
1301 if (!skb)
1302 goto unlock;
1303
1304 skb_reserve(skb, ETH_HLEN);
1305 tt_response = (struct tt_query_packet *)skb_put(skb,
1306 sizeof(struct tt_query_packet) + tt_len);
1307 tt_response->ttvn = req_ttvn;
1308 tt_response->tt_data = htons(tt_tot);
1309
1310 tt_buff = skb->data + sizeof(struct tt_query_packet);
1311 memcpy(tt_buff, bat_priv->tt_buff,
1312 bat_priv->tt_buff_len);
1313 spin_unlock_bh(&bat_priv->tt_buff_lock);
1314 } else {
1315 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1316 sizeof(struct tt_change);
1317 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1318
1319 skb = tt_response_fill_table(tt_len, ttvn,
1320 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001321 primary_if, tt_local_valid_entry,
1322 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001323 if (!skb)
1324 goto out;
1325
1326 tt_response = (struct tt_query_packet *)skb->data;
1327 }
1328
1329 tt_response->packet_type = BAT_TT_QUERY;
1330 tt_response->version = COMPAT_VERSION;
1331 tt_response->ttl = TTL;
1332 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1333 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1334 tt_response->flags = TT_RESPONSE;
1335
1336 if (full_table)
1337 tt_response->flags |= TT_FULL_TABLE;
1338
1339 bat_dbg(DBG_TT, bat_priv,
1340 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1341 orig_node->orig, neigh_node->addr,
1342 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1343
1344 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1345 ret = true;
1346 goto out;
1347
1348unlock:
1349 spin_unlock_bh(&bat_priv->tt_buff_lock);
1350out:
1351 if (orig_node)
1352 orig_node_free_ref(orig_node);
1353 if (neigh_node)
1354 neigh_node_free_ref(neigh_node);
1355 if (primary_if)
1356 hardif_free_ref(primary_if);
1357 if (!ret)
1358 kfree_skb(skb);
1359 /* This packet was for me, so it doesn't need to be re-routed */
1360 return true;
1361}
1362
1363bool send_tt_response(struct bat_priv *bat_priv,
1364 struct tt_query_packet *tt_request)
1365{
1366 if (is_my_mac(tt_request->dst))
1367 return send_my_tt_response(bat_priv, tt_request);
1368 else
1369 return send_other_tt_response(bat_priv, tt_request);
1370}
1371
1372static void _tt_update_changes(struct bat_priv *bat_priv,
1373 struct orig_node *orig_node,
1374 struct tt_change *tt_change,
1375 uint16_t tt_num_changes, uint8_t ttvn)
1376{
1377 int i;
1378
1379 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001380 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001381 tt_global_del(bat_priv, orig_node,
1382 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001383 "tt removed by changes",
1384 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385 else
1386 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001387 (tt_change + i)->addr, ttvn, false,
1388 (tt_change + i)->flags &
1389 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390 /* In case of problem while storing a
1391 * global_entry, we stop the updating
1392 * procedure without committing the
1393 * ttvn change. This will avoid to send
1394 * corrupted data on tt_request
1395 */
1396 return;
1397 }
1398}
1399
1400static void tt_fill_gtable(struct bat_priv *bat_priv,
1401 struct tt_query_packet *tt_response)
1402{
1403 struct orig_node *orig_node = NULL;
1404
1405 orig_node = orig_hash_find(bat_priv, tt_response->src);
1406 if (!orig_node)
1407 goto out;
1408
1409 /* Purge the old table first.. */
1410 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1411
1412 _tt_update_changes(bat_priv, orig_node,
1413 (struct tt_change *)(tt_response + 1),
1414 tt_response->tt_data, tt_response->ttvn);
1415
1416 spin_lock_bh(&orig_node->tt_buff_lock);
1417 kfree(orig_node->tt_buff);
1418 orig_node->tt_buff_len = 0;
1419 orig_node->tt_buff = NULL;
1420 spin_unlock_bh(&orig_node->tt_buff_lock);
1421
1422 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1423
1424out:
1425 if (orig_node)
1426 orig_node_free_ref(orig_node);
1427}
1428
Marek Lindnera943cac2011-07-30 13:10:18 +02001429static void tt_update_changes(struct bat_priv *bat_priv,
1430 struct orig_node *orig_node,
1431 uint16_t tt_num_changes, uint8_t ttvn,
1432 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433{
1434 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1435 ttvn);
1436
1437 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1438 tt_num_changes);
1439 atomic_set(&orig_node->last_ttvn, ttvn);
1440}
1441
1442bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1443{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001444 struct tt_local_entry *tt_local_entry = NULL;
1445 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446
Antonio Quartullia73105b2011-04-27 14:27:44 +02001447 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001448 if (!tt_local_entry)
1449 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001450 /* Check if the client has been logically deleted (but is kept for
1451 * consistency purpose) */
1452 if (tt_local_entry->flags & TT_CLIENT_PENDING)
1453 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001454 ret = true;
1455out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001457 tt_local_entry_free_ref(tt_local_entry);
1458 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001459}
1460
1461void handle_tt_response(struct bat_priv *bat_priv,
1462 struct tt_query_packet *tt_response)
1463{
1464 struct tt_req_node *node, *safe;
1465 struct orig_node *orig_node = NULL;
1466
1467 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1468 "ttvn %d t_size: %d [%c]\n",
1469 tt_response->src, tt_response->ttvn,
1470 tt_response->tt_data,
1471 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1472
1473 orig_node = orig_hash_find(bat_priv, tt_response->src);
1474 if (!orig_node)
1475 goto out;
1476
1477 if (tt_response->flags & TT_FULL_TABLE)
1478 tt_fill_gtable(bat_priv, tt_response);
1479 else
1480 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1481 tt_response->ttvn,
1482 (struct tt_change *)(tt_response + 1));
1483
1484 /* Delete the tt_req_node from pending tt_requests list */
1485 spin_lock_bh(&bat_priv->tt_req_list_lock);
1486 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1487 if (!compare_eth(node->addr, tt_response->src))
1488 continue;
1489 list_del(&node->list);
1490 kfree(node);
1491 }
1492 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1493
1494 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001496 /* Roaming phase is over: tables are in sync again. I can
1497 * unset the flag */
1498 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499out:
1500 if (orig_node)
1501 orig_node_free_ref(orig_node);
1502}
1503
1504int tt_init(struct bat_priv *bat_priv)
1505{
1506 if (!tt_local_init(bat_priv))
1507 return 0;
1508
1509 if (!tt_global_init(bat_priv))
1510 return 0;
1511
1512 tt_start_timer(bat_priv);
1513
1514 return 1;
1515}
1516
Antonio Quartullicc47f662011-04-27 14:27:57 +02001517static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001519 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520
Antonio Quartullicc47f662011-04-27 14:27:57 +02001521 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001522
Antonio Quartullicc47f662011-04-27 14:27:57 +02001523 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1524 list_del(&node->list);
1525 kfree(node);
1526 }
1527
1528 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1529}
1530
1531static void tt_roam_purge(struct bat_priv *bat_priv)
1532{
1533 struct tt_roam_node *node, *safe;
1534
1535 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1536 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1537 if (!is_out_of_time(node->first_time,
1538 ROAMING_MAX_TIME * 1000))
1539 continue;
1540
1541 list_del(&node->list);
1542 kfree(node);
1543 }
1544 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1545}
1546
1547/* This function checks whether the client already reached the
1548 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1549 * will not be sent.
1550 *
1551 * returns true if the ROAMING_ADV can be sent, false otherwise */
1552static bool tt_check_roam_count(struct bat_priv *bat_priv,
1553 uint8_t *client)
1554{
1555 struct tt_roam_node *tt_roam_node;
1556 bool ret = false;
1557
1558 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1559 /* The new tt_req will be issued only if I'm not waiting for a
1560 * reply from the same orig_node yet */
1561 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1562 if (!compare_eth(tt_roam_node->addr, client))
1563 continue;
1564
1565 if (is_out_of_time(tt_roam_node->first_time,
1566 ROAMING_MAX_TIME * 1000))
1567 continue;
1568
1569 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1570 /* Sorry, you roamed too many times! */
1571 goto unlock;
1572 ret = true;
1573 break;
1574 }
1575
1576 if (!ret) {
1577 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1578 if (!tt_roam_node)
1579 goto unlock;
1580
1581 tt_roam_node->first_time = jiffies;
1582 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1583 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1584
1585 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1586 ret = true;
1587 }
1588
1589unlock:
1590 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1591 return ret;
1592}
1593
1594void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1595 struct orig_node *orig_node)
1596{
1597 struct neigh_node *neigh_node = NULL;
1598 struct sk_buff *skb = NULL;
1599 struct roam_adv_packet *roam_adv_packet;
1600 int ret = 1;
1601 struct hard_iface *primary_if;
1602
1603 /* before going on we have to check whether the client has
1604 * already roamed to us too many times */
1605 if (!tt_check_roam_count(bat_priv, client))
1606 goto out;
1607
1608 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1609 if (!skb)
1610 goto out;
1611
1612 skb_reserve(skb, ETH_HLEN);
1613
1614 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1615 sizeof(struct roam_adv_packet));
1616
1617 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1618 roam_adv_packet->version = COMPAT_VERSION;
1619 roam_adv_packet->ttl = TTL;
1620 primary_if = primary_if_get_selected(bat_priv);
1621 if (!primary_if)
1622 goto out;
1623 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1624 hardif_free_ref(primary_if);
1625 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1626 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1627
1628 neigh_node = orig_node_get_router(orig_node);
1629 if (!neigh_node)
1630 goto out;
1631
1632 bat_dbg(DBG_TT, bat_priv,
1633 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1634 orig_node->orig, client, neigh_node->addr);
1635
1636 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1637 ret = 0;
1638
1639out:
1640 if (neigh_node)
1641 neigh_node_free_ref(neigh_node);
1642 if (ret)
1643 kfree_skb(skb);
1644 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001645}
1646
1647static void tt_purge(struct work_struct *work)
1648{
1649 struct delayed_work *delayed_work =
1650 container_of(work, struct delayed_work, work);
1651 struct bat_priv *bat_priv =
1652 container_of(delayed_work, struct bat_priv, tt_work);
1653
1654 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001655 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001657 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001658
1659 tt_start_timer(bat_priv);
1660}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001661
1662void tt_free(struct bat_priv *bat_priv)
1663{
1664 cancel_delayed_work_sync(&bat_priv->tt_work);
1665
1666 tt_local_table_free(bat_priv);
1667 tt_global_table_free(bat_priv);
1668 tt_req_list_free(bat_priv);
1669 tt_changes_list_free(bat_priv);
1670 tt_roam_list_free(bat_priv);
1671
1672 kfree(bat_priv->tt_buff);
1673}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001674
1675/* This function will reset the specified flags from all the entries in
1676 * the given hash table and will increment num_local_tt for each involved
1677 * entry */
1678static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
1679{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001680 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001681 struct hashtable_t *hash = bat_priv->tt_local_hash;
1682 struct hlist_head *head;
1683 struct hlist_node *node;
1684 struct tt_local_entry *tt_local_entry;
1685
1686 if (!hash)
1687 return;
1688
1689 for (i = 0; i < hash->size; i++) {
1690 head = &hash->table[i];
1691
1692 rcu_read_lock();
1693 hlist_for_each_entry_rcu(tt_local_entry, node,
1694 head, hash_entry) {
Antonio Quartulli31901262011-10-16 18:53:37 +02001695 if (!(tt_local_entry->flags & flags))
1696 continue;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001697 tt_local_entry->flags &= ~flags;
1698 atomic_inc(&bat_priv->num_local_tt);
1699 }
1700 rcu_read_unlock();
1701 }
1702
1703}
1704
1705/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1706static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1707{
1708 struct hashtable_t *hash = bat_priv->tt_local_hash;
1709 struct tt_local_entry *tt_local_entry;
1710 struct hlist_node *node, *node_tmp;
1711 struct hlist_head *head;
1712 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001713 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001714
1715 if (!hash)
1716 return;
1717
1718 for (i = 0; i < hash->size; i++) {
1719 head = &hash->table[i];
1720 list_lock = &hash->list_locks[i];
1721
1722 spin_lock_bh(list_lock);
1723 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
1724 head, hash_entry) {
1725 if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
1726 continue;
1727
1728 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
1729 "(%pM): pending\n", tt_local_entry->addr);
1730
1731 atomic_dec(&bat_priv->num_local_tt);
1732 hlist_del_rcu(node);
1733 tt_local_entry_free_ref(tt_local_entry);
1734 }
1735 spin_unlock_bh(list_lock);
1736 }
1737
1738}
1739
1740void tt_commit_changes(struct bat_priv *bat_priv)
1741{
1742 tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
1743 tt_local_purge_pending_clients(bat_priv);
1744
1745 /* Increment the TTVN only once per OGM interval */
1746 atomic_inc(&bat_priv->ttvn);
1747 bat_priv->tt_poss_change = false;
1748}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001749
1750bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1751{
1752 struct tt_local_entry *tt_local_entry = NULL;
1753 struct tt_global_entry *tt_global_entry = NULL;
1754 bool ret = true;
1755
1756 if (!atomic_read(&bat_priv->ap_isolation))
1757 return false;
1758
1759 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1760 if (!tt_local_entry)
1761 goto out;
1762
1763 tt_global_entry = tt_global_hash_find(bat_priv, src);
1764 if (!tt_global_entry)
1765 goto out;
1766
1767 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1768 goto out;
1769
1770 ret = false;
1771
1772out:
1773 if (tt_global_entry)
1774 tt_global_entry_free_ref(tt_global_entry);
1775 if (tt_local_entry)
1776 tt_local_entry_free_ref(tt_local_entry);
1777 return ret;
1778}
Marek Lindnera943cac2011-07-30 13:10:18 +02001779
1780void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1781 const unsigned char *tt_buff, uint8_t tt_num_changes,
1782 uint8_t ttvn, uint16_t tt_crc)
1783{
1784 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1785 bool full_table = true;
1786
1787 /* the ttvn increased by one -> we can apply the attached changes */
1788 if (ttvn - orig_ttvn == 1) {
1789 /* the OGM could not contain the changes due to their size or
1790 * because they have already been sent TT_OGM_APPEND_MAX times.
1791 * In this case send a tt request */
1792 if (!tt_num_changes) {
1793 full_table = false;
1794 goto request_table;
1795 }
1796
1797 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1798 (struct tt_change *)tt_buff);
1799
1800 /* Even if we received the precomputed crc with the OGM, we
1801 * prefer to recompute it to spot any possible inconsistency
1802 * in the global table */
1803 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1804
1805 /* The ttvn alone is not enough to guarantee consistency
1806 * because a single value could represent different states
1807 * (due to the wrap around). Thus a node has to check whether
1808 * the resulting table (after applying the changes) is still
1809 * consistent or not. E.g. a node could disconnect while its
1810 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1811 * checking the CRC value is mandatory to detect the
1812 * inconsistency */
1813 if (orig_node->tt_crc != tt_crc)
1814 goto request_table;
1815
1816 /* Roaming phase is over: tables are in sync again. I can
1817 * unset the flag */
1818 orig_node->tt_poss_change = false;
1819 } else {
1820 /* if we missed more than one change or our tables are not
1821 * in sync anymore -> request fresh tt data */
1822 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
1823request_table:
1824 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1825 "Need to retrieve the correct information "
1826 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1827 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1828 orig_ttvn, tt_crc, orig_node->tt_crc,
1829 tt_num_changes);
1830 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1831 full_table);
1832 return;
1833 }
1834 }
1835}