blob: c632475df375cd273d8fed5977214eb83fc3d077 [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 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +010039static int compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010041 const void *data1 = container_of(node, struct tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020042 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
Antonio Quartullia73105b2011-04-27 14:27:44 +020047static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048{
Antonio Quartullia73105b2011-04-27 14:27:44 +020049 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
50 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
51 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
Antonio Quartulli48100ba2011-10-30 12:17:33 +010054static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
55 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000056{
Marek Lindner7aadf882011-02-18 12:28:09 +000057 struct hlist_head *head;
58 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010059 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020060 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000061
62 if (!hash)
63 return NULL;
64
65 index = choose_orig(data, hash->size);
66 head = &hash->table[index];
67
68 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010069 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
70 if (!compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000071 continue;
72
Antonio Quartulli48100ba2011-10-30 12:17:33 +010073 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020074 continue;
75
Antonio Quartulli48100ba2011-10-30 12:17:33 +010076 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000077 break;
78 }
79 rcu_read_unlock();
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 return tt_common_entry_tmp;
82}
83
84static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
85 const void *data)
86{
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
89
90 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
91 if (tt_common_entry)
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000095}
96
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020098 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000099{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100103 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
104 if (tt_common_entry)
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Marek Lindner7aadf882011-02-18 12:28:09 +0000109}
110
Antonio Quartullia73105b2011-04-27 14:27:44 +0200111static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
112{
113 unsigned long deadline;
114 deadline = starting_time + msecs_to_jiffies(timeout);
115
116 return time_after(jiffies, deadline);
117}
118
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200119static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
120{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200123}
124
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125static void tt_global_entry_free_rcu(struct rcu_head *rcu)
126{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100127 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200128 struct tt_global_entry *tt_global_entry;
129
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100130 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
132 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200133
134 if (tt_global_entry->orig_node)
135 orig_node_free_ref(tt_global_entry->orig_node);
136
137 kfree(tt_global_entry);
138}
139
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200140static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
141{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 if (atomic_dec_and_test(&tt_global_entry->common.refcount))
143 call_rcu(&tt_global_entry->common.rcu,
144 tt_global_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200145}
146
Antonio Quartulliff66c972011-06-30 01:14:00 +0200147static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
148 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200149{
150 struct tt_change_node *tt_change_node;
151
152 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
153
154 if (!tt_change_node)
155 return;
156
Antonio Quartulliff66c972011-06-30 01:14:00 +0200157 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200158 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
159
160 spin_lock_bh(&bat_priv->tt_changes_list_lock);
161 /* track the change in the OGMinterval list */
162 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
163 atomic_inc(&bat_priv->tt_local_changes);
164 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
165
166 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
167}
168
169int tt_len(int changes_num)
170{
171 return changes_num * sizeof(struct tt_change);
172}
173
174static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200176 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177 return 1;
178
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200179 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200181 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182 return 0;
183
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 return 1;
185}
186
Antonio Quartullibc279082011-07-07 15:35:35 +0200187void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
188 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189{
190 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200191 struct tt_local_entry *tt_local_entry = NULL;
192 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100193 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200195 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200197 if (tt_local_entry) {
198 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200199 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 }
201
Sven Eckelmann704509b2011-05-14 23:14:54 +0200202 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200203 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200204 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200205
Antonio Quartullia73105b2011-04-27 14:27:44 +0200206 bat_dbg(DBG_TT, bat_priv,
207 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
208 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100210 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
211 tt_local_entry->common.flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200212 if (is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100213 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
214 atomic_set(&tt_local_entry->common.refcount, 2);
215 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
217 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000218 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100219 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100221 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
222 &tt_local_entry->common,
223 &tt_local_entry->common.hash_entry);
224
225 if (unlikely(hash_added != 0)) {
226 /* remove the reference for the hash */
227 tt_local_entry_free_ref(tt_local_entry);
228 goto out;
229 }
230
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100231 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200232
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) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100236 tt_local_entry->common.flags |= TT_CLIENT_NEW;
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200237
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200239 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240
Antonio Quartullicc47f662011-04-27 14:27:57 +0200241 /* Check whether it is a roaming! */
242 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200243 /* This node is probably going to update its tt table */
244 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100245 /* The global entry has to be marked as ROAMING and has to be
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200246 * kept for consistency purpose */
David S. Miller220b07e2011-12-16 15:07:28 -0500247 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100248 tt_global_entry->roam_at = jiffies;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100249 send_roam_adv(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200250 tt_global_entry->orig_node);
251 }
252out:
253 if (tt_local_entry)
254 tt_local_entry_free_ref(tt_local_entry);
255 if (tt_global_entry)
256 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257}
258
Antonio Quartullia73105b2011-04-27 14:27:44 +0200259int tt_changes_fill_buffer(struct bat_priv *bat_priv,
260 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200262 int count = 0, tot_changes = 0;
263 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264
Antonio Quartullia73105b2011-04-27 14:27:44 +0200265 if (buff_len > 0)
266 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
Antonio Quartullia73105b2011-04-27 14:27:44 +0200268 spin_lock_bh(&bat_priv->tt_changes_list_lock);
269 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
Antonio Quartullia73105b2011-04-27 14:27:44 +0200271 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
272 list) {
273 if (count < tot_changes) {
274 memcpy(buff + tt_len(count),
275 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276 count++;
277 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200278 list_del(&entry->list);
279 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200281 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
Antonio Quartullia73105b2011-04-27 14:27:44 +0200283 /* Keep the buffer for possible tt_request */
284 spin_lock_bh(&bat_priv->tt_buff_lock);
285 kfree(bat_priv->tt_buff);
286 bat_priv->tt_buff_len = 0;
287 bat_priv->tt_buff = NULL;
288 /* We check whether this new OGM has no changes due to size
289 * problems */
290 if (buff_len > 0) {
291 /**
292 * if kmalloc() fails we will reply with the full table
293 * instead of providing the diff
294 */
295 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
296 if (bat_priv->tt_buff) {
297 memcpy(bat_priv->tt_buff, buff, buff_len);
298 bat_priv->tt_buff_len = buff_len;
299 }
300 }
301 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartullia73105b2011-04-27 14:27:44 +0200303 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304}
305
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200306int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307{
308 struct net_device *net_dev = (struct net_device *)seq->private;
309 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200310 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100311 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200312 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000313 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200315 uint32_t i;
316 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Marek Lindner32ae9b22011-04-20 15:40:58 +0200318 primary_if = primary_if_get_selected(bat_priv);
319 if (!primary_if) {
320 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
321 "please specify interfaces to enable it\n",
322 net_dev->name);
323 goto out;
324 }
325
326 if (primary_if->if_status != IF_ACTIVE) {
327 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
328 "primary interface not active\n",
329 net_dev->name);
330 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 }
332
333 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200334 "announced via TT (TTVN: %u):\n",
335 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 for (i = 0; i < hash->size; i++) {
338 head = &hash->table[i];
339
Marek Lindner7aadf882011-02-18 12:28:09 +0000340 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100341 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000342 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200343 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100344 tt_common_entry->addr,
345 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200346 TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100347 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200348 TT_CLIENT_NOPURGE ? 'P' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100349 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200350 TT_CLIENT_NEW ? 'N' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100351 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200352 TT_CLIENT_PENDING ? 'X' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100353 (tt_common_entry->flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200354 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000356 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200358out:
359 if (primary_if)
360 hardif_free_ref(primary_if);
361 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362}
363
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200364static void tt_local_set_pending(struct bat_priv *bat_priv,
365 struct tt_local_entry *tt_local_entry,
366 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100368 tt_local_event(bat_priv, tt_local_entry->common.addr,
369 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Antonio Quartulli015758d2011-07-09 17:52:13 +0200371 /* The local client has to be marked as "pending to be removed" but has
372 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200373 * response issued before the net ttvn increment (consistency check) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100374 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375}
376
Antonio Quartullia73105b2011-04-27 14:27:44 +0200377void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200378 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200380 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200382 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200383 if (!tt_local_entry)
384 goto out;
385
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200386 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
387 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
388
389 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100390 "%s\n", tt_local_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200391out:
392 if (tt_local_entry)
393 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394}
395
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200398 struct hashtable_t *hash = bat_priv->tt_local_hash;
399 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100400 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000401 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200403 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200404 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 for (i = 0; i < hash->size; i++) {
407 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200408 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200410 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100411 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000412 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100413 tt_local_entry = container_of(tt_common_entry,
414 struct tt_local_entry,
415 common);
416 if (tt_local_entry->common.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 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100420 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200421 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 Quartulli48100ba2011-10-30 12:17:33 +0100431 tt_local_entry->common.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 Quartulli48100ba2011-10-30 12:17:33 +0100442 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200444 struct hlist_node *node, *node_tmp;
445 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200446 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200447
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200448 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449 return;
450
Antonio Quartullia73105b2011-04-27 14:27:44 +0200451 hash = bat_priv->tt_local_hash;
452
453 for (i = 0; i < hash->size; i++) {
454 head = &hash->table[i];
455 list_lock = &hash->list_locks[i];
456
457 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100458 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200459 head, hash_entry) {
460 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100461 tt_local_entry = container_of(tt_common_entry,
462 struct tt_local_entry,
463 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200464 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200465 }
466 spin_unlock_bh(list_lock);
467 }
468
469 hash_destroy(hash);
470
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200471 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472}
473
Antonio Quartullia73105b2011-04-27 14:27:44 +0200474static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200476 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477 return 1;
478
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200479 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200481 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 return 0;
483
484 return 1;
485}
486
Antonio Quartullia73105b2011-04-27 14:27:44 +0200487static void tt_changes_list_free(struct bat_priv *bat_priv)
488{
489 struct tt_change_node *entry, *safe;
490
491 spin_lock_bh(&bat_priv->tt_changes_list_lock);
492
493 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
494 list) {
495 list_del(&entry->list);
496 kfree(entry);
497 }
498
499 atomic_set(&bat_priv->tt_local_changes, 0);
500 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
501}
502
503/* caller must hold orig_node refcount */
504int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200505 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
506 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200508 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200509 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200510 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100511 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
Antonio Quartullia73105b2011-04-27 14:27:44 +0200513 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514
Antonio Quartullia73105b2011-04-27 14:27:44 +0200515 if (!tt_global_entry) {
516 tt_global_entry =
517 kmalloc(sizeof(*tt_global_entry),
518 GFP_ATOMIC);
519 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200520 goto out;
521
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100522 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
523 tt_global_entry->common.flags = NO_FLAGS;
524 atomic_set(&tt_global_entry->common.refcount, 2);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200525 /* Assign the new orig_node */
526 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200527 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200528 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200529 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200530
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100531 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
532 choose_orig, &tt_global_entry->common,
533 &tt_global_entry->common.hash_entry);
534
535 if (unlikely(hash_added != 0)) {
536 /* remove the reference for the hash */
537 tt_global_entry_free_ref(tt_global_entry);
538 goto out_remove;
539 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200540 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200541 } else {
542 if (tt_global_entry->orig_node != orig_node) {
543 atomic_dec(&tt_global_entry->orig_node->tt_size);
544 orig_node_tmp = tt_global_entry->orig_node;
545 atomic_inc(&orig_node->refcount);
546 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200547 orig_node_free_ref(orig_node_tmp);
548 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549 }
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100550 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200551 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200552 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200554
Antonio Quartullibc279082011-07-07 15:35:35 +0200555 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100556 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200557
Antonio Quartullia73105b2011-04-27 14:27:44 +0200558 bat_dbg(DBG_TT, bat_priv,
559 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100560 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200561
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100562out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200563 /* remove address from local hash if present */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100564 tt_local_remove(bat_priv, tt_global_entry->common.addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200565 "global tt received", roaming);
566 ret = 1;
567out:
568 if (tt_global_entry)
569 tt_global_entry_free_ref(tt_global_entry);
570 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571}
572
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200573int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574{
575 struct net_device *net_dev = (struct net_device *)seq->private;
576 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200577 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100578 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200579 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200580 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000581 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200583 uint32_t i;
584 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585
Marek Lindner32ae9b22011-04-20 15:40:58 +0200586 primary_if = primary_if_get_selected(bat_priv);
587 if (!primary_if) {
588 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
589 "specify interfaces to enable it\n",
590 net_dev->name);
591 goto out;
592 }
593
594 if (primary_if->if_status != IF_ACTIVE) {
595 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
596 "primary interface not active\n",
597 net_dev->name);
598 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 }
600
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200601 seq_printf(seq,
602 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200604 seq_printf(seq, " %-13s %s %-15s %s %s\n",
605 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607 for (i = 0; i < hash->size; i++) {
608 head = &hash->table[i];
609
Marek Lindner7aadf882011-02-18 12:28:09 +0000610 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100611 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000612 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100613 tt_global_entry = container_of(tt_common_entry,
614 struct tt_global_entry,
615 common);
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200616 seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100617 "[%c%c%c]\n",
618 tt_global_entry->common.addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200619 tt_global_entry->ttvn,
620 tt_global_entry->orig_node->orig,
621 (uint8_t) atomic_read(
622 &tt_global_entry->orig_node->
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200623 last_ttvn),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100624 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200625 TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100626 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200627 TT_CLIENT_PENDING ? 'X' : '.'),
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100628 (tt_global_entry->common.flags &
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200629 TT_CLIENT_WIFI ? 'W' : '.'));
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 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200633out:
634 if (primary_if)
635 hardif_free_ref(primary_if);
636 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637}
638
Antonio Quartullia73105b2011-04-27 14:27:44 +0200639static void _tt_global_del(struct bat_priv *bat_priv,
640 struct tt_global_entry *tt_global_entry,
641 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200643 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200644 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200645
646 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200647 "Deleting global tt entry %pM (via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100648 tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649 message);
650
Antonio Quartullia73105b2011-04-27 14:27:44 +0200651 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200652
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100653 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
654 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200655out:
656 if (tt_global_entry)
657 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658}
659
Antonio Quartullia73105b2011-04-27 14:27:44 +0200660void tt_global_del(struct bat_priv *bat_priv,
661 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200662 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200664 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100665 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666
Antonio Quartullia73105b2011-04-27 14:27:44 +0200667 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200668 if (!tt_global_entry)
669 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200670
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200671 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200672 if (roaming) {
Antonio Quartulli797399b2011-12-04 22:38:27 +0100673 /* if we are deleting a global entry due to a roam
674 * event, there are two possibilities:
675 * 1) the client roamed from node A to node B => we mark
676 * it with TT_CLIENT_ROAM, we start a timer and we
677 * wait for node B to claim it. In case of timeout
678 * the entry is purged.
679 * 2) the client roamed to us => we can directly delete
680 * the global entry, since it is useless now. */
681 tt_local_entry = tt_local_hash_find(bat_priv,
David S. Millerb26e4782011-12-16 02:11:14 -0500682 tt_global_entry->common.addr);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100683 if (!tt_local_entry) {
David S. Millerb26e4782011-12-16 02:11:14 -0500684 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100685 tt_global_entry->roam_at = jiffies;
686 goto out;
687 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200688 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200689 _tt_global_del(bat_priv, tt_global_entry, message);
690 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200691out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200692 if (tt_global_entry)
693 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100694 if (tt_local_entry)
695 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696}
697
698void tt_global_del_orig(struct bat_priv *bat_priv,
699 struct orig_node *orig_node, const char *message)
700{
701 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100702 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200703 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200704 struct hashtable_t *hash = bat_priv->tt_global_hash;
705 struct hlist_node *node, *safe;
706 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200707 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200708
Simon Wunderlich6e801492011-10-19 10:28:26 +0200709 if (!hash)
710 return;
711
Antonio Quartullia73105b2011-04-27 14:27:44 +0200712 for (i = 0; i < hash->size; i++) {
713 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200714 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200716 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100717 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200718 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100719 tt_global_entry = container_of(tt_common_entry,
720 struct tt_global_entry,
721 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200722 if (tt_global_entry->orig_node == orig_node) {
723 bat_dbg(DBG_TT, bat_priv,
724 "Deleting global tt entry %pM "
Antonio Quartulli87944972011-09-19 12:29:19 +0200725 "(via %pM): %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100726 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200727 tt_global_entry->orig_node->orig,
728 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200729 hlist_del_rcu(node);
730 tt_global_entry_free_ref(tt_global_entry);
731 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200732 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200733 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200735 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100736 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737}
738
Antonio Quartullicc47f662011-04-27 14:27:57 +0200739static void tt_global_roam_purge(struct bat_priv *bat_priv)
740{
741 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100742 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200743 struct tt_global_entry *tt_global_entry;
744 struct hlist_node *node, *node_tmp;
745 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200746 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200747 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200748
Antonio Quartullicc47f662011-04-27 14:27:57 +0200749 for (i = 0; i < hash->size; i++) {
750 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200751 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200752
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200753 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100754 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200755 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100756 tt_global_entry = container_of(tt_common_entry,
757 struct tt_global_entry,
758 common);
759 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200760 continue;
761 if (!is_out_of_time(tt_global_entry->roam_at,
762 TT_CLIENT_ROAM_TIMEOUT * 1000))
763 continue;
764
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200765 bat_dbg(DBG_TT, bat_priv, "Deleting global "
766 "tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100767 tt_global_entry->common.addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200768 atomic_dec(&tt_global_entry->orig_node->tt_size);
769 hlist_del_rcu(node);
770 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200771 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200772 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200773 }
774
Antonio Quartullicc47f662011-04-27 14:27:57 +0200775}
776
Antonio Quartullia73105b2011-04-27 14:27:44 +0200777static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200779 struct hashtable_t *hash;
780 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100781 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200782 struct tt_global_entry *tt_global_entry;
783 struct hlist_node *node, *node_tmp;
784 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200785 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200786
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200787 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788 return;
789
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200790 hash = bat_priv->tt_global_hash;
791
792 for (i = 0; i < hash->size; i++) {
793 head = &hash->table[i];
794 list_lock = &hash->list_locks[i];
795
796 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100797 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200798 head, hash_entry) {
799 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100800 tt_global_entry = container_of(tt_common_entry,
801 struct tt_global_entry,
802 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200803 tt_global_entry_free_ref(tt_global_entry);
804 }
805 spin_unlock_bh(list_lock);
806 }
807
808 hash_destroy(hash);
809
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200810 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811}
812
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200813static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
814 struct tt_global_entry *tt_global_entry)
815{
816 bool ret = false;
817
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100818 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
819 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200820 ret = true;
821
822 return ret;
823}
824
Sven Eckelmann747e4222011-05-14 23:14:50 +0200825struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200826 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200828 struct tt_local_entry *tt_local_entry = NULL;
829 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000830 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200832 if (src && atomic_read(&bat_priv->ap_isolation)) {
833 tt_local_entry = tt_local_hash_find(bat_priv, src);
834 if (!tt_local_entry)
835 goto out;
836 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000837
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200838 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200839 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000840 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000841
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200842 /* check whether the clients should not communicate due to AP
843 * isolation */
844 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
845 goto out;
846
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200847 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200848 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000849
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200850 /* A global client marked as PENDING has already moved from that
851 * originator */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100852 if (tt_global_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200853 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200854
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200855 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000856
857out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200858 if (tt_global_entry)
859 tt_global_entry_free_ref(tt_global_entry);
860 if (tt_local_entry)
861 tt_local_entry_free_ref(tt_local_entry);
862
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000863 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200865
866/* Calculates the checksum of the local table of a given orig_node */
867uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
868{
869 uint16_t total = 0, total_one;
870 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100871 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200872 struct tt_global_entry *tt_global_entry;
873 struct hlist_node *node;
874 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200875 uint32_t i;
876 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200877
878 for (i = 0; i < hash->size; i++) {
879 head = &hash->table[i];
880
881 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100882 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200883 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100884 tt_global_entry = container_of(tt_common_entry,
885 struct tt_global_entry,
886 common);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200887 if (compare_eth(tt_global_entry->orig_node,
888 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200889 /* Roaming clients are in the global table for
890 * consistency only. They don't have to be
891 * taken into account while computing the
892 * global crc */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100893 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200894 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200895 total_one = 0;
896 for (j = 0; j < ETH_ALEN; j++)
897 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100898 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200899 total ^= total_one;
900 }
901 }
902 rcu_read_unlock();
903 }
904
905 return total;
906}
907
908/* Calculates the checksum of the local table */
909uint16_t tt_local_crc(struct bat_priv *bat_priv)
910{
911 uint16_t total = 0, total_one;
912 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100913 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200914 struct hlist_node *node;
915 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200916 uint32_t i;
917 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200918
919 for (i = 0; i < hash->size; i++) {
920 head = &hash->table[i];
921
922 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100923 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200924 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200925 /* not yet committed clients have not to be taken into
926 * account while computing the CRC */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100927 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200928 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200929 total_one = 0;
930 for (j = 0; j < ETH_ALEN; j++)
931 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100932 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200933 total ^= total_one;
934 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200935 rcu_read_unlock();
936 }
937
938 return total;
939}
940
941static void tt_req_list_free(struct bat_priv *bat_priv)
942{
943 struct tt_req_node *node, *safe;
944
945 spin_lock_bh(&bat_priv->tt_req_list_lock);
946
947 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
948 list_del(&node->list);
949 kfree(node);
950 }
951
952 spin_unlock_bh(&bat_priv->tt_req_list_lock);
953}
954
955void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
956 const unsigned char *tt_buff, uint8_t tt_num_changes)
957{
958 uint16_t tt_buff_len = tt_len(tt_num_changes);
959
960 /* Replace the old buffer only if I received something in the
961 * last OGM (the OGM could carry no changes) */
962 spin_lock_bh(&orig_node->tt_buff_lock);
963 if (tt_buff_len > 0) {
964 kfree(orig_node->tt_buff);
965 orig_node->tt_buff_len = 0;
966 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
967 if (orig_node->tt_buff) {
968 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
969 orig_node->tt_buff_len = tt_buff_len;
970 }
971 }
972 spin_unlock_bh(&orig_node->tt_buff_lock);
973}
974
975static void tt_req_purge(struct bat_priv *bat_priv)
976{
977 struct tt_req_node *node, *safe;
978
979 spin_lock_bh(&bat_priv->tt_req_list_lock);
980 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
981 if (is_out_of_time(node->issued_at,
982 TT_REQUEST_TIMEOUT * 1000)) {
983 list_del(&node->list);
984 kfree(node);
985 }
986 }
987 spin_unlock_bh(&bat_priv->tt_req_list_lock);
988}
989
990/* returns the pointer to the new tt_req_node struct if no request
991 * has already been issued for this orig_node, NULL otherwise */
992static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
993 struct orig_node *orig_node)
994{
995 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
996
997 spin_lock_bh(&bat_priv->tt_req_list_lock);
998 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
999 if (compare_eth(tt_req_node_tmp, orig_node) &&
1000 !is_out_of_time(tt_req_node_tmp->issued_at,
1001 TT_REQUEST_TIMEOUT * 1000))
1002 goto unlock;
1003 }
1004
1005 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1006 if (!tt_req_node)
1007 goto unlock;
1008
1009 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1010 tt_req_node->issued_at = jiffies;
1011
1012 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1013unlock:
1014 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1015 return tt_req_node;
1016}
1017
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001018/* data_ptr is useless here, but has to be kept to respect the prototype */
1019static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1020{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001021 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001022
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001023 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001024 return 0;
1025 return 1;
1026}
1027
Antonio Quartullia73105b2011-04-27 14:27:44 +02001028static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1029{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001030 const struct tt_common_entry *tt_common_entry = entry_ptr;
1031 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001032 const struct orig_node *orig_node = data_ptr;
1033
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001034 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001035 return 0;
1036
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001037 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1038 common);
1039
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040 return (tt_global_entry->orig_node == orig_node);
1041}
1042
1043static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1044 struct hashtable_t *hash,
1045 struct hard_iface *primary_if,
1046 int (*valid_cb)(const void *,
1047 const void *),
1048 void *cb_data)
1049{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001050 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001051 struct tt_query_packet *tt_response;
1052 struct tt_change *tt_change;
1053 struct hlist_node *node;
1054 struct hlist_head *head;
1055 struct sk_buff *skb = NULL;
1056 uint16_t tt_tot, tt_count;
1057 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001058 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001059
1060 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1061 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1062 tt_len -= tt_len % sizeof(struct tt_change);
1063 }
1064 tt_tot = tt_len / sizeof(struct tt_change);
1065
1066 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1067 if (!skb)
1068 goto out;
1069
1070 skb_reserve(skb, ETH_HLEN);
1071 tt_response = (struct tt_query_packet *)skb_put(skb,
1072 tt_query_size + tt_len);
1073 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001074
1075 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1076 tt_count = 0;
1077
1078 rcu_read_lock();
1079 for (i = 0; i < hash->size; i++) {
1080 head = &hash->table[i];
1081
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001082 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001083 head, hash_entry) {
1084 if (tt_count == tt_tot)
1085 break;
1086
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001087 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001088 continue;
1089
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001090 memcpy(tt_change->addr, tt_common_entry->addr,
1091 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001092 tt_change->flags = NO_FLAGS;
1093
1094 tt_count++;
1095 tt_change++;
1096 }
1097 }
1098 rcu_read_unlock();
1099
Antonio Quartulli9d852392011-10-17 14:25:13 +02001100 /* store in the message the number of entries we have successfully
1101 * copied */
1102 tt_response->tt_data = htons(tt_count);
1103
Antonio Quartullia73105b2011-04-27 14:27:44 +02001104out:
1105 return skb;
1106}
1107
Marek Lindnera943cac2011-07-30 13:10:18 +02001108static int send_tt_request(struct bat_priv *bat_priv,
1109 struct orig_node *dst_orig_node,
1110 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001111{
1112 struct sk_buff *skb = NULL;
1113 struct tt_query_packet *tt_request;
1114 struct neigh_node *neigh_node = NULL;
1115 struct hard_iface *primary_if;
1116 struct tt_req_node *tt_req_node = NULL;
1117 int ret = 1;
1118
1119 primary_if = primary_if_get_selected(bat_priv);
1120 if (!primary_if)
1121 goto out;
1122
1123 /* The new tt_req will be issued only if I'm not waiting for a
1124 * reply from the same orig_node yet */
1125 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1126 if (!tt_req_node)
1127 goto out;
1128
1129 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1130 if (!skb)
1131 goto out;
1132
1133 skb_reserve(skb, ETH_HLEN);
1134
1135 tt_request = (struct tt_query_packet *)skb_put(skb,
1136 sizeof(struct tt_query_packet));
1137
1138 tt_request->packet_type = BAT_TT_QUERY;
1139 tt_request->version = COMPAT_VERSION;
1140 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1141 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1142 tt_request->ttl = TTL;
1143 tt_request->ttvn = ttvn;
1144 tt_request->tt_data = tt_crc;
1145 tt_request->flags = TT_REQUEST;
1146
1147 if (full_table)
1148 tt_request->flags |= TT_FULL_TABLE;
1149
1150 neigh_node = orig_node_get_router(dst_orig_node);
1151 if (!neigh_node)
1152 goto out;
1153
1154 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1155 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1156 (full_table ? 'F' : '.'));
1157
1158 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1159 ret = 0;
1160
1161out:
1162 if (neigh_node)
1163 neigh_node_free_ref(neigh_node);
1164 if (primary_if)
1165 hardif_free_ref(primary_if);
1166 if (ret)
1167 kfree_skb(skb);
1168 if (ret && tt_req_node) {
1169 spin_lock_bh(&bat_priv->tt_req_list_lock);
1170 list_del(&tt_req_node->list);
1171 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1172 kfree(tt_req_node);
1173 }
1174 return ret;
1175}
1176
1177static bool send_other_tt_response(struct bat_priv *bat_priv,
1178 struct tt_query_packet *tt_request)
1179{
1180 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1181 struct neigh_node *neigh_node = NULL;
1182 struct hard_iface *primary_if = NULL;
1183 uint8_t orig_ttvn, req_ttvn, ttvn;
1184 int ret = false;
1185 unsigned char *tt_buff;
1186 bool full_table;
1187 uint16_t tt_len, tt_tot;
1188 struct sk_buff *skb = NULL;
1189 struct tt_query_packet *tt_response;
1190
1191 bat_dbg(DBG_TT, bat_priv,
1192 "Received TT_REQUEST from %pM for "
1193 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1194 tt_request->ttvn, tt_request->dst,
1195 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1196
1197 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001198 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001199 if (!req_dst_orig_node)
1200 goto out;
1201
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001202 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001203 if (!res_dst_orig_node)
1204 goto out;
1205
1206 neigh_node = orig_node_get_router(res_dst_orig_node);
1207 if (!neigh_node)
1208 goto out;
1209
1210 primary_if = primary_if_get_selected(bat_priv);
1211 if (!primary_if)
1212 goto out;
1213
1214 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1215 req_ttvn = tt_request->ttvn;
1216
Antonio Quartulli015758d2011-07-09 17:52:13 +02001217 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001218 if (orig_ttvn != req_ttvn ||
1219 tt_request->tt_data != req_dst_orig_node->tt_crc)
1220 goto out;
1221
Antonio Quartulli015758d2011-07-09 17:52:13 +02001222 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001223 if (tt_request->flags & TT_FULL_TABLE ||
1224 !req_dst_orig_node->tt_buff)
1225 full_table = true;
1226 else
1227 full_table = false;
1228
1229 /* In this version, fragmentation is not implemented, then
1230 * I'll send only one packet with as much TT entries as I can */
1231 if (!full_table) {
1232 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1233 tt_len = req_dst_orig_node->tt_buff_len;
1234 tt_tot = tt_len / sizeof(struct tt_change);
1235
1236 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1237 tt_len + ETH_HLEN);
1238 if (!skb)
1239 goto unlock;
1240
1241 skb_reserve(skb, ETH_HLEN);
1242 tt_response = (struct tt_query_packet *)skb_put(skb,
1243 sizeof(struct tt_query_packet) + tt_len);
1244 tt_response->ttvn = req_ttvn;
1245 tt_response->tt_data = htons(tt_tot);
1246
1247 tt_buff = skb->data + sizeof(struct tt_query_packet);
1248 /* Copy the last orig_node's OGM buffer */
1249 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1250 req_dst_orig_node->tt_buff_len);
1251
1252 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1253 } else {
1254 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1255 sizeof(struct tt_change);
1256 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1257
1258 skb = tt_response_fill_table(tt_len, ttvn,
1259 bat_priv->tt_global_hash,
1260 primary_if, tt_global_valid_entry,
1261 req_dst_orig_node);
1262 if (!skb)
1263 goto out;
1264
1265 tt_response = (struct tt_query_packet *)skb->data;
1266 }
1267
1268 tt_response->packet_type = BAT_TT_QUERY;
1269 tt_response->version = COMPAT_VERSION;
1270 tt_response->ttl = TTL;
1271 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1272 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1273 tt_response->flags = TT_RESPONSE;
1274
1275 if (full_table)
1276 tt_response->flags |= TT_FULL_TABLE;
1277
1278 bat_dbg(DBG_TT, bat_priv,
1279 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1280 res_dst_orig_node->orig, neigh_node->addr,
1281 req_dst_orig_node->orig, req_ttvn);
1282
1283 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1284 ret = true;
1285 goto out;
1286
1287unlock:
1288 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1289
1290out:
1291 if (res_dst_orig_node)
1292 orig_node_free_ref(res_dst_orig_node);
1293 if (req_dst_orig_node)
1294 orig_node_free_ref(req_dst_orig_node);
1295 if (neigh_node)
1296 neigh_node_free_ref(neigh_node);
1297 if (primary_if)
1298 hardif_free_ref(primary_if);
1299 if (!ret)
1300 kfree_skb(skb);
1301 return ret;
1302
1303}
1304static bool send_my_tt_response(struct bat_priv *bat_priv,
1305 struct tt_query_packet *tt_request)
1306{
1307 struct orig_node *orig_node = NULL;
1308 struct neigh_node *neigh_node = NULL;
1309 struct hard_iface *primary_if = NULL;
1310 uint8_t my_ttvn, req_ttvn, ttvn;
1311 int ret = false;
1312 unsigned char *tt_buff;
1313 bool full_table;
1314 uint16_t tt_len, tt_tot;
1315 struct sk_buff *skb = NULL;
1316 struct tt_query_packet *tt_response;
1317
1318 bat_dbg(DBG_TT, bat_priv,
1319 "Received TT_REQUEST from %pM for "
1320 "ttvn: %u (me) [%c]\n", tt_request->src,
1321 tt_request->ttvn,
1322 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1323
1324
1325 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1326 req_ttvn = tt_request->ttvn;
1327
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001328 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001329 if (!orig_node)
1330 goto out;
1331
1332 neigh_node = orig_node_get_router(orig_node);
1333 if (!neigh_node)
1334 goto out;
1335
1336 primary_if = primary_if_get_selected(bat_priv);
1337 if (!primary_if)
1338 goto out;
1339
1340 /* If the full table has been explicitly requested or the gap
1341 * is too big send the whole local translation table */
1342 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1343 !bat_priv->tt_buff)
1344 full_table = true;
1345 else
1346 full_table = false;
1347
1348 /* In this version, fragmentation is not implemented, then
1349 * I'll send only one packet with as much TT entries as I can */
1350 if (!full_table) {
1351 spin_lock_bh(&bat_priv->tt_buff_lock);
1352 tt_len = bat_priv->tt_buff_len;
1353 tt_tot = tt_len / sizeof(struct tt_change);
1354
1355 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1356 tt_len + ETH_HLEN);
1357 if (!skb)
1358 goto unlock;
1359
1360 skb_reserve(skb, ETH_HLEN);
1361 tt_response = (struct tt_query_packet *)skb_put(skb,
1362 sizeof(struct tt_query_packet) + tt_len);
1363 tt_response->ttvn = req_ttvn;
1364 tt_response->tt_data = htons(tt_tot);
1365
1366 tt_buff = skb->data + sizeof(struct tt_query_packet);
1367 memcpy(tt_buff, bat_priv->tt_buff,
1368 bat_priv->tt_buff_len);
1369 spin_unlock_bh(&bat_priv->tt_buff_lock);
1370 } else {
1371 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1372 sizeof(struct tt_change);
1373 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1374
1375 skb = tt_response_fill_table(tt_len, ttvn,
1376 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001377 primary_if, tt_local_valid_entry,
1378 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001379 if (!skb)
1380 goto out;
1381
1382 tt_response = (struct tt_query_packet *)skb->data;
1383 }
1384
1385 tt_response->packet_type = BAT_TT_QUERY;
1386 tt_response->version = COMPAT_VERSION;
1387 tt_response->ttl = TTL;
1388 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1389 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1390 tt_response->flags = TT_RESPONSE;
1391
1392 if (full_table)
1393 tt_response->flags |= TT_FULL_TABLE;
1394
1395 bat_dbg(DBG_TT, bat_priv,
1396 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1397 orig_node->orig, neigh_node->addr,
1398 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1399
1400 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1401 ret = true;
1402 goto out;
1403
1404unlock:
1405 spin_unlock_bh(&bat_priv->tt_buff_lock);
1406out:
1407 if (orig_node)
1408 orig_node_free_ref(orig_node);
1409 if (neigh_node)
1410 neigh_node_free_ref(neigh_node);
1411 if (primary_if)
1412 hardif_free_ref(primary_if);
1413 if (!ret)
1414 kfree_skb(skb);
1415 /* This packet was for me, so it doesn't need to be re-routed */
1416 return true;
1417}
1418
1419bool send_tt_response(struct bat_priv *bat_priv,
1420 struct tt_query_packet *tt_request)
1421{
1422 if (is_my_mac(tt_request->dst))
1423 return send_my_tt_response(bat_priv, tt_request);
1424 else
1425 return send_other_tt_response(bat_priv, tt_request);
1426}
1427
1428static void _tt_update_changes(struct bat_priv *bat_priv,
1429 struct orig_node *orig_node,
1430 struct tt_change *tt_change,
1431 uint16_t tt_num_changes, uint8_t ttvn)
1432{
1433 int i;
1434
1435 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001436 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437 tt_global_del(bat_priv, orig_node,
1438 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001439 "tt removed by changes",
1440 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001441 else
1442 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001443 (tt_change + i)->addr, ttvn, false,
1444 (tt_change + i)->flags &
1445 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446 /* In case of problem while storing a
1447 * global_entry, we stop the updating
1448 * procedure without committing the
1449 * ttvn change. This will avoid to send
1450 * corrupted data on tt_request
1451 */
1452 return;
1453 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001454 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455}
1456
1457static void tt_fill_gtable(struct bat_priv *bat_priv,
1458 struct tt_query_packet *tt_response)
1459{
1460 struct orig_node *orig_node = NULL;
1461
1462 orig_node = orig_hash_find(bat_priv, tt_response->src);
1463 if (!orig_node)
1464 goto out;
1465
1466 /* Purge the old table first.. */
1467 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1468
1469 _tt_update_changes(bat_priv, orig_node,
1470 (struct tt_change *)(tt_response + 1),
1471 tt_response->tt_data, tt_response->ttvn);
1472
1473 spin_lock_bh(&orig_node->tt_buff_lock);
1474 kfree(orig_node->tt_buff);
1475 orig_node->tt_buff_len = 0;
1476 orig_node->tt_buff = NULL;
1477 spin_unlock_bh(&orig_node->tt_buff_lock);
1478
1479 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1480
1481out:
1482 if (orig_node)
1483 orig_node_free_ref(orig_node);
1484}
1485
Marek Lindnera943cac2011-07-30 13:10:18 +02001486static void tt_update_changes(struct bat_priv *bat_priv,
1487 struct orig_node *orig_node,
1488 uint16_t tt_num_changes, uint8_t ttvn,
1489 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490{
1491 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1492 ttvn);
1493
1494 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1495 tt_num_changes);
1496 atomic_set(&orig_node->last_ttvn, ttvn);
1497}
1498
1499bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1500{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001501 struct tt_local_entry *tt_local_entry = NULL;
1502 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001505 if (!tt_local_entry)
1506 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001507 /* Check if the client has been logically deleted (but is kept for
1508 * consistency purpose) */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001509 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001510 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001511 ret = true;
1512out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001513 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001514 tt_local_entry_free_ref(tt_local_entry);
1515 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001516}
1517
1518void handle_tt_response(struct bat_priv *bat_priv,
1519 struct tt_query_packet *tt_response)
1520{
1521 struct tt_req_node *node, *safe;
1522 struct orig_node *orig_node = NULL;
1523
1524 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1525 "ttvn %d t_size: %d [%c]\n",
1526 tt_response->src, tt_response->ttvn,
1527 tt_response->tt_data,
1528 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1529
1530 orig_node = orig_hash_find(bat_priv, tt_response->src);
1531 if (!orig_node)
1532 goto out;
1533
1534 if (tt_response->flags & TT_FULL_TABLE)
1535 tt_fill_gtable(bat_priv, tt_response);
1536 else
1537 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1538 tt_response->ttvn,
1539 (struct tt_change *)(tt_response + 1));
1540
1541 /* Delete the tt_req_node from pending tt_requests list */
1542 spin_lock_bh(&bat_priv->tt_req_list_lock);
1543 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1544 if (!compare_eth(node->addr, tt_response->src))
1545 continue;
1546 list_del(&node->list);
1547 kfree(node);
1548 }
1549 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1550
1551 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001552 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001553 /* Roaming phase is over: tables are in sync again. I can
1554 * unset the flag */
1555 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556out:
1557 if (orig_node)
1558 orig_node_free_ref(orig_node);
1559}
1560
1561int tt_init(struct bat_priv *bat_priv)
1562{
1563 if (!tt_local_init(bat_priv))
1564 return 0;
1565
1566 if (!tt_global_init(bat_priv))
1567 return 0;
1568
1569 tt_start_timer(bat_priv);
1570
1571 return 1;
1572}
1573
Antonio Quartullicc47f662011-04-27 14:27:57 +02001574static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001576 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001577
Antonio Quartullicc47f662011-04-27 14:27:57 +02001578 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001579
Antonio Quartullicc47f662011-04-27 14:27:57 +02001580 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1581 list_del(&node->list);
1582 kfree(node);
1583 }
1584
1585 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1586}
1587
1588static void tt_roam_purge(struct bat_priv *bat_priv)
1589{
1590 struct tt_roam_node *node, *safe;
1591
1592 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1593 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1594 if (!is_out_of_time(node->first_time,
1595 ROAMING_MAX_TIME * 1000))
1596 continue;
1597
1598 list_del(&node->list);
1599 kfree(node);
1600 }
1601 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1602}
1603
1604/* This function checks whether the client already reached the
1605 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1606 * will not be sent.
1607 *
1608 * returns true if the ROAMING_ADV can be sent, false otherwise */
1609static bool tt_check_roam_count(struct bat_priv *bat_priv,
1610 uint8_t *client)
1611{
1612 struct tt_roam_node *tt_roam_node;
1613 bool ret = false;
1614
1615 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1616 /* The new tt_req will be issued only if I'm not waiting for a
1617 * reply from the same orig_node yet */
1618 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1619 if (!compare_eth(tt_roam_node->addr, client))
1620 continue;
1621
1622 if (is_out_of_time(tt_roam_node->first_time,
1623 ROAMING_MAX_TIME * 1000))
1624 continue;
1625
1626 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1627 /* Sorry, you roamed too many times! */
1628 goto unlock;
1629 ret = true;
1630 break;
1631 }
1632
1633 if (!ret) {
1634 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1635 if (!tt_roam_node)
1636 goto unlock;
1637
1638 tt_roam_node->first_time = jiffies;
1639 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1640 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1641
1642 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1643 ret = true;
1644 }
1645
1646unlock:
1647 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1648 return ret;
1649}
1650
1651void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1652 struct orig_node *orig_node)
1653{
1654 struct neigh_node *neigh_node = NULL;
1655 struct sk_buff *skb = NULL;
1656 struct roam_adv_packet *roam_adv_packet;
1657 int ret = 1;
1658 struct hard_iface *primary_if;
1659
1660 /* before going on we have to check whether the client has
1661 * already roamed to us too many times */
1662 if (!tt_check_roam_count(bat_priv, client))
1663 goto out;
1664
1665 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1666 if (!skb)
1667 goto out;
1668
1669 skb_reserve(skb, ETH_HLEN);
1670
1671 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1672 sizeof(struct roam_adv_packet));
1673
1674 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1675 roam_adv_packet->version = COMPAT_VERSION;
1676 roam_adv_packet->ttl = TTL;
1677 primary_if = primary_if_get_selected(bat_priv);
1678 if (!primary_if)
1679 goto out;
1680 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1681 hardif_free_ref(primary_if);
1682 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1683 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1684
1685 neigh_node = orig_node_get_router(orig_node);
1686 if (!neigh_node)
1687 goto out;
1688
1689 bat_dbg(DBG_TT, bat_priv,
1690 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1691 orig_node->orig, client, neigh_node->addr);
1692
1693 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1694 ret = 0;
1695
1696out:
1697 if (neigh_node)
1698 neigh_node_free_ref(neigh_node);
1699 if (ret)
1700 kfree_skb(skb);
1701 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702}
1703
1704static void tt_purge(struct work_struct *work)
1705{
1706 struct delayed_work *delayed_work =
1707 container_of(work, struct delayed_work, work);
1708 struct bat_priv *bat_priv =
1709 container_of(delayed_work, struct bat_priv, tt_work);
1710
1711 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001712 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001713 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001714 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715
1716 tt_start_timer(bat_priv);
1717}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001718
1719void tt_free(struct bat_priv *bat_priv)
1720{
1721 cancel_delayed_work_sync(&bat_priv->tt_work);
1722
1723 tt_local_table_free(bat_priv);
1724 tt_global_table_free(bat_priv);
1725 tt_req_list_free(bat_priv);
1726 tt_changes_list_free(bat_priv);
1727 tt_roam_list_free(bat_priv);
1728
1729 kfree(bat_priv->tt_buff);
1730}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001731
Antonio Quartulli697f2532011-11-07 16:47:01 +01001732/* This function will enable or disable the specified flags for all the entries
1733 * in the given hash table and returns the number of modified entries */
1734static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1735 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001736{
Antonio Quartullic90681b2011-10-05 17:05:25 +02001737 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01001738 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001739 struct hlist_head *head;
1740 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001741 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001742
1743 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01001744 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001745
1746 for (i = 0; i < hash->size; i++) {
1747 head = &hash->table[i];
1748
1749 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001750 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001751 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01001752 if (enable) {
1753 if ((tt_common_entry->flags & flags) == flags)
1754 continue;
1755 tt_common_entry->flags |= flags;
1756 } else {
1757 if (!(tt_common_entry->flags & flags))
1758 continue;
1759 tt_common_entry->flags &= ~flags;
1760 }
1761 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001762 }
1763 rcu_read_unlock();
1764 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01001765out:
1766 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001767}
1768
1769/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1770static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1771{
1772 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001773 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001774 struct tt_local_entry *tt_local_entry;
1775 struct hlist_node *node, *node_tmp;
1776 struct hlist_head *head;
1777 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001778 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001779
1780 if (!hash)
1781 return;
1782
1783 for (i = 0; i < hash->size; i++) {
1784 head = &hash->table[i];
1785 list_lock = &hash->list_locks[i];
1786
1787 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001788 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001789 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001790 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001791 continue;
1792
1793 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001794 "(%pM): pending\n", tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001795
1796 atomic_dec(&bat_priv->num_local_tt);
1797 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001798 tt_local_entry = container_of(tt_common_entry,
1799 struct tt_local_entry,
1800 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001801 tt_local_entry_free_ref(tt_local_entry);
1802 }
1803 spin_unlock_bh(list_lock);
1804 }
1805
1806}
1807
1808void tt_commit_changes(struct bat_priv *bat_priv)
1809{
Antonio Quartulli697f2532011-11-07 16:47:01 +01001810 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
1811 TT_CLIENT_NEW, false);
1812 /* all the reset entries have now to be effectively counted as local
1813 * entries */
1814 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001815 tt_local_purge_pending_clients(bat_priv);
1816
1817 /* Increment the TTVN only once per OGM interval */
1818 atomic_inc(&bat_priv->ttvn);
1819 bat_priv->tt_poss_change = false;
1820}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001821
1822bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1823{
1824 struct tt_local_entry *tt_local_entry = NULL;
1825 struct tt_global_entry *tt_global_entry = NULL;
1826 bool ret = true;
1827
1828 if (!atomic_read(&bat_priv->ap_isolation))
1829 return false;
1830
1831 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1832 if (!tt_local_entry)
1833 goto out;
1834
1835 tt_global_entry = tt_global_hash_find(bat_priv, src);
1836 if (!tt_global_entry)
1837 goto out;
1838
1839 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1840 goto out;
1841
1842 ret = false;
1843
1844out:
1845 if (tt_global_entry)
1846 tt_global_entry_free_ref(tt_global_entry);
1847 if (tt_local_entry)
1848 tt_local_entry_free_ref(tt_local_entry);
1849 return ret;
1850}
Marek Lindnera943cac2011-07-30 13:10:18 +02001851
1852void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1853 const unsigned char *tt_buff, uint8_t tt_num_changes,
1854 uint8_t ttvn, uint16_t tt_crc)
1855{
1856 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1857 bool full_table = true;
1858
Antonio Quartulli17071572011-11-07 16:36:40 +01001859 /* orig table not initialised AND first diff is in the OGM OR the ttvn
1860 * increased by one -> we can apply the attached changes */
1861 if ((!orig_node->tt_initialised && ttvn == 1) ||
1862 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001863 /* the OGM could not contain the changes due to their size or
1864 * because they have already been sent TT_OGM_APPEND_MAX times.
1865 * In this case send a tt request */
1866 if (!tt_num_changes) {
1867 full_table = false;
1868 goto request_table;
1869 }
1870
1871 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1872 (struct tt_change *)tt_buff);
1873
1874 /* Even if we received the precomputed crc with the OGM, we
1875 * prefer to recompute it to spot any possible inconsistency
1876 * in the global table */
1877 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1878
1879 /* The ttvn alone is not enough to guarantee consistency
1880 * because a single value could represent different states
1881 * (due to the wrap around). Thus a node has to check whether
1882 * the resulting table (after applying the changes) is still
1883 * consistent or not. E.g. a node could disconnect while its
1884 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1885 * checking the CRC value is mandatory to detect the
1886 * inconsistency */
1887 if (orig_node->tt_crc != tt_crc)
1888 goto request_table;
1889
1890 /* Roaming phase is over: tables are in sync again. I can
1891 * unset the flag */
1892 orig_node->tt_poss_change = false;
1893 } else {
1894 /* if we missed more than one change or our tables are not
1895 * in sync anymore -> request fresh tt data */
Antonio Quartulli17071572011-11-07 16:36:40 +01001896 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
1897 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02001898request_table:
1899 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1900 "Need to retrieve the correct information "
1901 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1902 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1903 orig_ttvn, tt_crc, orig_node->tt_crc,
1904 tt_num_changes);
1905 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1906 full_table);
1907 return;
1908 }
1909 }
1910}