blob: bc06af4781b3bb5b8f145c4710a7ddcd36a6c323 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
Sven Eckelmannde7aae62012-02-05 18:55:22 +010032static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
33 struct orig_node *orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +020034static void tt_purge(struct work_struct *work);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +020035static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
Marek Lindner7aadf882011-02-18 12:28:09 +000037/* returns 1 if they are the same mac addr */
Antonio Quartulli48100ba2011-10-30 12:17:33 +010038static int compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000039{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010040 const void *data1 = container_of(node, struct tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020041 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000042
43 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
44}
45
Antonio Quartullia73105b2011-04-27 14:27:44 +020046static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047{
Antonio Quartullia73105b2011-04-27 14:27:44 +020048 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020049 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt_work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020050 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051}
52
Antonio Quartulli48100ba2011-10-30 12:17:33 +010053static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
54 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000055{
Marek Lindner7aadf882011-02-18 12:28:09 +000056 struct hlist_head *head;
57 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010058 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020059 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000060
61 if (!hash)
62 return NULL;
63
64 index = choose_orig(data, hash->size);
65 head = &hash->table[index];
66
67 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010068 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
69 if (!compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000070 continue;
71
Antonio Quartulli48100ba2011-10-30 12:17:33 +010072 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020073 continue;
74
Antonio Quartulli48100ba2011-10-30 12:17:33 +010075 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000076 break;
77 }
78 rcu_read_unlock();
79
Antonio Quartulli48100ba2011-10-30 12:17:33 +010080 return tt_common_entry_tmp;
81}
82
83static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
84 const void *data)
85{
86 struct tt_common_entry *tt_common_entry;
87 struct tt_local_entry *tt_local_entry = NULL;
88
89 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
90 if (tt_common_entry)
91 tt_local_entry = container_of(tt_common_entry,
92 struct tt_local_entry, common);
93 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000094}
95
Antonio Quartulli2dafb492011-05-05 08:42:45 +020096static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020097 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000098{
Antonio Quartulli48100ba2011-10-30 12:17:33 +010099 struct tt_common_entry *tt_common_entry;
100 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100102 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
103 if (tt_common_entry)
104 tt_global_entry = container_of(tt_common_entry,
105 struct tt_global_entry, common);
106 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000107
Marek Lindner7aadf882011-02-18 12:28:09 +0000108}
109
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200110static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
111{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100112 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
113 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200114}
115
Simon Wunderlich531027f2011-10-19 11:02:25 +0200116static void tt_global_entry_free_rcu(struct rcu_head *rcu)
117{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100118 struct tt_common_entry *tt_common_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200119 struct tt_global_entry *tt_global_entry;
120
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
122 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
123 common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200124
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125 kfree(tt_global_entry);
126}
127
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200128static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
129{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200130 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
131 tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100132 call_rcu(&tt_global_entry->common.rcu,
133 tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200134 }
135}
136
137static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
138{
139 struct tt_orig_list_entry *orig_entry;
140
141 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
142 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200143 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 kfree(orig_entry);
145}
146
147static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
148{
149 call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200150}
151
Antonio Quartulliff66c972011-06-30 01:14:00 +0200152static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
153 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200154{
155 struct tt_change_node *tt_change_node;
156
157 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
158
159 if (!tt_change_node)
160 return;
161
Antonio Quartulliff66c972011-06-30 01:14:00 +0200162 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200163 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
164
165 spin_lock_bh(&bat_priv->tt_changes_list_lock);
166 /* track the change in the OGMinterval list */
167 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
168 atomic_inc(&bat_priv->tt_local_changes);
169 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
170
171 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
172}
173
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200174int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175{
176 return changes_num * sizeof(struct tt_change);
177}
178
179static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200181 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200182 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200184 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200186 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200187 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Sven Eckelmann5346c352012-05-05 13:27:28 +0200189 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190}
191
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200192void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
193 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194{
195 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200196 struct tt_local_entry *tt_local_entry = NULL;
197 struct tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200198 struct hlist_head *head;
199 struct hlist_node *node;
200 struct tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100201 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200203 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200205 if (tt_local_entry) {
206 tt_local_entry->last_seen = jiffies;
Antonio Quartulli521251f2012-01-16 00:36:58 +0100207 /* possibly unset the TT_CLIENT_PENDING flag */
208 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200209 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 }
211
Sven Eckelmann704509b2011-05-14 23:14:54 +0200212 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200213 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200214 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200215
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216 bat_dbg(DBG_TT, bat_priv,
217 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
218 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100220 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
221 tt_local_entry->common.flags = NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200222 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100223 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
224 atomic_set(&tt_local_entry->common.refcount, 2);
225 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
227 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000228 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100229 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100231 /* The local entry has to be marked as NEW to avoid to send it in
232 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200233 * (consistency check)
234 */
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100235 tt_local_entry->common.flags |= TT_CLIENT_NEW;
236
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100237 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
238 &tt_local_entry->common,
239 &tt_local_entry->common.hash_entry);
240
241 if (unlikely(hash_added != 0)) {
242 /* remove the reference for the hash */
243 tt_local_entry_free_ref(tt_local_entry);
244 goto out;
245 }
246
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100247 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200248
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200250 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Antonio Quartullicc47f662011-04-27 14:27:57 +0200252 /* Check whether it is a roaming! */
253 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200254 /* These node are probably going to update their tt table */
255 head = &tt_global_entry->orig_list;
256 rcu_read_lock();
257 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
258 orig_entry->orig_node->tt_poss_change = true;
259
260 send_roam_adv(bat_priv, tt_global_entry->common.addr,
261 orig_entry->orig_node);
262 }
263 rcu_read_unlock();
264 /* The global entry has to be marked as ROAMING and
265 * has to be kept for consistency purpose
266 */
David S. Miller220b07e2011-12-16 15:07:28 -0500267 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100268 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200269 }
270out:
271 if (tt_local_entry)
272 tt_local_entry_free_ref(tt_local_entry);
273 if (tt_global_entry)
274 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275}
276
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800277static void tt_realloc_packet_buff(unsigned char **packet_buff,
278 int *packet_buff_len, int min_packet_len,
279 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800281 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800283 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
284
285 /* keep old buffer if kmalloc should fail */
286 if (new_buff) {
287 memcpy(new_buff, *packet_buff, min_packet_len);
288 kfree(*packet_buff);
289 *packet_buff = new_buff;
290 *packet_buff_len = new_packet_len;
291 }
292}
293
294static void tt_prepare_packet_buff(struct bat_priv *bat_priv,
295 unsigned char **packet_buff,
296 int *packet_buff_len, int min_packet_len)
297{
298 struct hard_iface *primary_if;
299 int req_len;
300
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200301 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800302
303 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200304 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800305
306 /* if we have too many changes for one packet don't send any
307 * and wait for the tt table request which will be fragmented
308 */
309 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
310 req_len = min_packet_len;
311
312 tt_realloc_packet_buff(packet_buff, packet_buff_len,
313 min_packet_len, req_len);
314
315 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200316 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800317}
318
319static int tt_changes_fill_buff(struct bat_priv *bat_priv,
320 unsigned char **packet_buff,
321 int *packet_buff_len, int min_packet_len)
322{
323 struct tt_change_node *entry, *safe;
324 int count = 0, tot_changes = 0, new_len;
325 unsigned char *tt_buff;
326
327 tt_prepare_packet_buff(bat_priv, packet_buff,
328 packet_buff_len, min_packet_len);
329
330 new_len = *packet_buff_len - min_packet_len;
331 tt_buff = *packet_buff + min_packet_len;
332
333 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200334 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Antonio Quartullia73105b2011-04-27 14:27:44 +0200336 spin_lock_bh(&bat_priv->tt_changes_list_lock);
337 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338
Antonio Quartullia73105b2011-04-27 14:27:44 +0200339 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100340 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200341 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200342 memcpy(tt_buff + batadv_tt_len(count),
Antonio Quartullia73105b2011-04-27 14:27:44 +0200343 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344 count++;
345 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200346 list_del(&entry->list);
347 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200349 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350
Antonio Quartullia73105b2011-04-27 14:27:44 +0200351 /* Keep the buffer for possible tt_request */
352 spin_lock_bh(&bat_priv->tt_buff_lock);
353 kfree(bat_priv->tt_buff);
354 bat_priv->tt_buff_len = 0;
355 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800356 /* check whether this new OGM has no changes due to size problems */
357 if (new_len > 0) {
358 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200359 * instead of providing the diff
360 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800361 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200362 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800363 memcpy(bat_priv->tt_buff, tt_buff, new_len);
364 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200365 }
366 }
367 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368
Marek Lindner08ad76e2012-04-23 16:32:55 +0800369 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370}
371
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200372int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373{
374 struct net_device *net_dev = (struct net_device *)seq->private;
375 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200376 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100377 struct tt_common_entry *tt_common_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200378 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000379 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200381 uint32_t i;
382 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200384 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200385 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100386 ret = seq_printf(seq,
387 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200388 net_dev->name);
389 goto out;
390 }
391
392 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100393 ret = seq_printf(seq,
394 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200395 net_dev->name);
396 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 }
398
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100399 seq_printf(seq,
400 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200401 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 for (i = 0; i < hash->size; i++) {
404 head = &hash->table[i];
405
Marek Lindner7aadf882011-02-18 12:28:09 +0000406 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100407 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000408 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200409 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100410 tt_common_entry->addr,
411 (tt_common_entry->flags &
412 TT_CLIENT_ROAM ? 'R' : '.'),
413 (tt_common_entry->flags &
414 TT_CLIENT_NOPURGE ? 'P' : '.'),
415 (tt_common_entry->flags &
416 TT_CLIENT_NEW ? 'N' : '.'),
417 (tt_common_entry->flags &
418 TT_CLIENT_PENDING ? 'X' : '.'),
419 (tt_common_entry->flags &
420 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000422 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200424out:
425 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200426 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200427 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428}
429
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200430static void tt_local_set_pending(struct bat_priv *bat_priv,
431 struct tt_local_entry *tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100432 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100434 tt_local_event(bat_priv, tt_local_entry->common.addr,
435 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436
Antonio Quartulli015758d2011-07-09 17:52:13 +0200437 /* The local client has to be marked as "pending to be removed" but has
438 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200439 * response issued before the net ttvn increment (consistency check)
440 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100441 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100442
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100443 bat_dbg(DBG_TT, bat_priv,
444 "Local tt entry (%pM) pending to be removed: %s\n",
445 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446}
447
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200448void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
449 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200451 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200453 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200454 if (!tt_local_entry)
455 goto out;
456
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200457 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100458 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200459out:
460 if (tt_local_entry)
461 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462}
463
Antonio Quartullia73105b2011-04-27 14:27:44 +0200464static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200466 struct hashtable_t *hash = bat_priv->tt_local_hash;
467 struct tt_local_entry *tt_local_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100468 struct tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000469 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200471 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200472 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 for (i = 0; i < hash->size; i++) {
475 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200476 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200478 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100479 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000480 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100481 tt_local_entry = container_of(tt_common_entry,
482 struct tt_local_entry,
483 common);
484 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000485 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200487 /* entry already marked for deletion */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100488 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200489 continue;
490
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100491 if (!has_timed_out(tt_local_entry->last_seen,
Marek Lindner032b7962011-12-20 19:30:40 +0800492 TT_LOCAL_TIMEOUT))
Marek Lindner7aadf882011-02-18 12:28:09 +0000493 continue;
494
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200495 tt_local_set_pending(bat_priv, tt_local_entry,
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100496 TT_CLIENT_DEL, "timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200498 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499 }
500
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501}
502
Antonio Quartullia73105b2011-04-27 14:27:44 +0200503static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200505 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200506 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100507 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200508 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200509 struct hlist_node *node, *node_tmp;
510 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200511 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200512
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200513 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514 return;
515
Antonio Quartullia73105b2011-04-27 14:27:44 +0200516 hash = bat_priv->tt_local_hash;
517
518 for (i = 0; i < hash->size; i++) {
519 head = &hash->table[i];
520 list_lock = &hash->list_locks[i];
521
522 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100523 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200524 head, hash_entry) {
525 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100526 tt_local_entry = container_of(tt_common_entry,
527 struct tt_local_entry,
528 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200529 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200530 }
531 spin_unlock_bh(list_lock);
532 }
533
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200534 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200535
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200536 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537}
538
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200541 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200542 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200544 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200546 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200547 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548
Sven Eckelmann5346c352012-05-05 13:27:28 +0200549 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550}
551
Antonio Quartullia73105b2011-04-27 14:27:44 +0200552static void tt_changes_list_free(struct bat_priv *bat_priv)
553{
554 struct tt_change_node *entry, *safe;
555
556 spin_lock_bh(&bat_priv->tt_changes_list_lock);
557
558 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
559 list) {
560 list_del(&entry->list);
561 kfree(entry);
562 }
563
564 atomic_set(&bat_priv->tt_local_changes, 0);
565 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
566}
567
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200568/* find out if an orig_node is already in the list of a tt_global_entry.
569 * returns 1 if found, 0 otherwise
570 */
571static bool tt_global_entry_has_orig(const struct tt_global_entry *entry,
572 const struct orig_node *orig_node)
573{
574 struct tt_orig_list_entry *tmp_orig_entry;
575 const struct hlist_head *head;
576 struct hlist_node *node;
577 bool found = false;
578
579 rcu_read_lock();
580 head = &entry->orig_list;
581 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
582 if (tmp_orig_entry->orig_node == orig_node) {
583 found = true;
584 break;
585 }
586 }
587 rcu_read_unlock();
588 return found;
589}
590
591static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
592 struct orig_node *orig_node,
593 int ttvn)
594{
595 struct tt_orig_list_entry *orig_entry;
596
597 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
598 if (!orig_entry)
599 return;
600
601 INIT_HLIST_NODE(&orig_entry->list);
602 atomic_inc(&orig_node->refcount);
603 atomic_inc(&orig_node->tt_size);
604 orig_entry->orig_node = orig_node;
605 orig_entry->ttvn = ttvn;
606
607 spin_lock_bh(&tt_global_entry->list_lock);
608 hlist_add_head_rcu(&orig_entry->list,
609 &tt_global_entry->orig_list);
610 spin_unlock_bh(&tt_global_entry->list_lock);
611}
612
Antonio Quartullia73105b2011-04-27 14:27:44 +0200613/* caller must hold orig_node refcount */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200614int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
615 const unsigned char *tt_addr, uint8_t ttvn,
616 bool roaming, bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200618 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200619 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100620 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624 if (!tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200625 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
626 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200627 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200628 goto out;
629
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100630 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200631
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100632 tt_global_entry->common.flags = NO_FLAGS;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200633 tt_global_entry->roam_at = 0;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200634 atomic_set(&tt_global_entry->common.refcount, 2);
635
636 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
637 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100639 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
640 choose_orig, &tt_global_entry->common,
641 &tt_global_entry->common.hash_entry);
642
643 if (unlikely(hash_added != 0)) {
644 /* remove the reference for the hash */
645 tt_global_entry_free_ref(tt_global_entry);
646 goto out_remove;
647 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200648
649 tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200650 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200651 /* there is already a global entry, use this one. */
652
653 /* If there is the TT_CLIENT_ROAM flag set, there is only one
654 * originator left in the list and we previously received a
655 * delete + roaming change for this originator.
656 *
657 * We should first delete the old originator before adding the
658 * new one.
659 */
660 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
661 tt_global_del_orig_list(tt_global_entry);
662 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
663 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200665
666 if (!tt_global_entry_has_orig(tt_global_entry, orig_node))
667 tt_global_add_orig_entry(tt_global_entry, orig_node,
668 ttvn);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200670
Antonio Quartullibc279082011-07-07 15:35:35 +0200671 if (wifi)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100672 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
Antonio Quartullibc279082011-07-07 15:35:35 +0200673
Antonio Quartullia73105b2011-04-27 14:27:44 +0200674 bat_dbg(DBG_TT, bat_priv,
675 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100676 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100678out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200679 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200680 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
681 "global tt received", roaming);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200682 ret = 1;
683out:
684 if (tt_global_entry)
685 tt_global_entry_free_ref(tt_global_entry);
686 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687}
688
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200689/* print all orig nodes who announce the address for this global entry.
690 * it is assumed that the caller holds rcu_read_lock();
691 */
692static void tt_global_print_entry(struct tt_global_entry *tt_global_entry,
693 struct seq_file *seq)
694{
695 struct hlist_head *head;
696 struct hlist_node *node;
697 struct tt_orig_list_entry *orig_entry;
698 struct tt_common_entry *tt_common_entry;
699 uint16_t flags;
700 uint8_t last_ttvn;
701
702 tt_common_entry = &tt_global_entry->common;
703
704 head = &tt_global_entry->orig_list;
705
706 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
707 flags = tt_common_entry->flags;
708 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
709 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
710 tt_global_entry->common.addr, orig_entry->ttvn,
711 orig_entry->orig_node->orig, last_ttvn,
712 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
713 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
714 }
715}
716
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200717int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718{
719 struct net_device *net_dev = (struct net_device *)seq->private;
720 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200721 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100722 struct tt_common_entry *tt_common_entry;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200723 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200724 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000725 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200727 uint32_t i;
728 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200730 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200731 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100732 ret = seq_printf(seq,
733 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200734 net_dev->name);
735 goto out;
736 }
737
738 if (primary_if->if_status != IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100739 ret = seq_printf(seq,
740 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200741 net_dev->name);
742 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743 }
744
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200745 seq_printf(seq,
746 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200748 seq_printf(seq, " %-13s %s %-15s %s %s\n",
749 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751 for (i = 0; i < hash->size; i++) {
752 head = &hash->table[i];
753
Marek Lindner7aadf882011-02-18 12:28:09 +0000754 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100755 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000756 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100757 tt_global_entry = container_of(tt_common_entry,
758 struct tt_global_entry,
759 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200760 tt_global_print_entry(tt_global_entry, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000762 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200764out:
765 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200766 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200767 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768}
769
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200770/* deletes the orig list of a tt_global_entry */
771static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200773 struct hlist_head *head;
774 struct hlist_node *node, *safe;
775 struct tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200776
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200777 spin_lock_bh(&tt_global_entry->list_lock);
778 head = &tt_global_entry->orig_list;
779 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
780 hlist_del_rcu(node);
781 tt_orig_list_entry_free_ref(orig_entry);
782 }
783 spin_unlock_bh(&tt_global_entry->list_lock);
784
785}
786
787static void tt_global_del_orig_entry(struct bat_priv *bat_priv,
788 struct tt_global_entry *tt_global_entry,
789 struct orig_node *orig_node,
790 const char *message)
791{
792 struct hlist_head *head;
793 struct hlist_node *node, *safe;
794 struct tt_orig_list_entry *orig_entry;
795
796 spin_lock_bh(&tt_global_entry->list_lock);
797 head = &tt_global_entry->orig_list;
798 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
799 if (orig_entry->orig_node == orig_node) {
800 bat_dbg(DBG_TT, bat_priv,
801 "Deleting %pM from global tt entry %pM: %s\n",
802 orig_node->orig, tt_global_entry->common.addr,
803 message);
804 hlist_del_rcu(node);
805 tt_orig_list_entry_free_ref(orig_entry);
806 }
807 }
808 spin_unlock_bh(&tt_global_entry->list_lock);
809}
810
811static void tt_global_del_struct(struct bat_priv *bat_priv,
812 struct tt_global_entry *tt_global_entry,
813 const char *message)
814{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200815 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200816 "Deleting global tt entry %pM: %s\n",
817 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200818
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100819 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
820 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200821 tt_global_entry_free_ref(tt_global_entry);
822
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823}
824
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825/* If the client is to be deleted, we check if it is the last origantor entry
826 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
827 * otherwise we simply remove the originator scheduled for deletion.
828 */
829static void tt_global_del_roaming(struct bat_priv *bat_priv,
830 struct tt_global_entry *tt_global_entry,
831 struct orig_node *orig_node,
832 const char *message)
833{
834 bool last_entry = true;
835 struct hlist_head *head;
836 struct hlist_node *node;
837 struct tt_orig_list_entry *orig_entry;
838
839 /* no local entry exists, case 1:
840 * Check if this is the last one or if other entries exist.
841 */
842
843 rcu_read_lock();
844 head = &tt_global_entry->orig_list;
845 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
846 if (orig_entry->orig_node != orig_node) {
847 last_entry = false;
848 break;
849 }
850 }
851 rcu_read_unlock();
852
853 if (last_entry) {
854 /* its the last one, mark for roaming. */
855 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
856 tt_global_entry->roam_at = jiffies;
857 } else
858 /* there is another entry, we can simply delete this
859 * one and can still use the other one.
860 */
861 tt_global_del_orig_entry(bat_priv, tt_global_entry,
862 orig_node, message);
863}
864
865
866
Sven Eckelmannde7aae62012-02-05 18:55:22 +0100867static void tt_global_del(struct bat_priv *bat_priv,
868 struct orig_node *orig_node,
869 const unsigned char *addr,
870 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200872 struct tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli797399b2011-12-04 22:38:27 +0100873 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
Antonio Quartullia73105b2011-04-27 14:27:44 +0200875 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200876 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200877 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200878
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200879 if (!roaming) {
880 tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node,
881 message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800882
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200883 if (hlist_empty(&tt_global_entry->orig_list))
884 tt_global_del_struct(bat_priv, tt_global_entry,
885 message);
886
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800887 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200888 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800889
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200890 /* if we are deleting a global entry due to a roam
891 * event, there are two possibilities:
892 * 1) the client roamed from node A to node B => if there
893 * is only one originator left for this client, we mark
894 * it with TT_CLIENT_ROAM, we start a timer and we
895 * wait for node B to claim it. In case of timeout
896 * the entry is purged.
897 *
898 * If there are other originators left, we directly delete
899 * the originator.
900 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200901 * the global entry, since it is useless now.
902 */
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200903 tt_local_entry = tt_local_hash_find(bat_priv,
904 tt_global_entry->common.addr);
905 if (tt_local_entry) {
906 /* local entry exists, case 2: client roamed to us. */
907 tt_global_del_orig_list(tt_global_entry);
908 tt_global_del_struct(bat_priv, tt_global_entry, message);
909 } else
910 /* no local entry exists, case 1: check for roaming */
911 tt_global_del_roaming(bat_priv, tt_global_entry, orig_node,
912 message);
913
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800914
Antonio Quartullicc47f662011-04-27 14:27:57 +0200915out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200916 if (tt_global_entry)
917 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli797399b2011-12-04 22:38:27 +0100918 if (tt_local_entry)
919 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200920}
921
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200922void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
923 struct orig_node *orig_node, const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200924{
925 struct tt_global_entry *tt_global_entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100926 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200927 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200928 struct hashtable_t *hash = bat_priv->tt_global_hash;
929 struct hlist_node *node, *safe;
930 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200931 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200932
Simon Wunderlich6e801492011-10-19 10:28:26 +0200933 if (!hash)
934 return;
935
Antonio Quartullia73105b2011-04-27 14:27:44 +0200936 for (i = 0; i < hash->size; i++) {
937 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200938 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200940 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100941 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100942 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100943 tt_global_entry = container_of(tt_common_entry,
944 struct tt_global_entry,
945 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200946
947 tt_global_del_orig_entry(bat_priv, tt_global_entry,
948 orig_node, message);
949
950 if (hlist_empty(&tt_global_entry->orig_list)) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200951 bat_dbg(DBG_TT, bat_priv,
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200952 "Deleting global tt entry %pM: %s\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100953 tt_global_entry->common.addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200954 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200955 hlist_del_rcu(node);
956 tt_global_entry_free_ref(tt_global_entry);
957 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200958 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200959 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200961 atomic_set(&orig_node->tt_size, 0);
Antonio Quartulli17071572011-11-07 16:36:40 +0100962 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963}
964
Antonio Quartullicc47f662011-04-27 14:27:57 +0200965static void tt_global_roam_purge(struct bat_priv *bat_priv)
966{
967 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100968 struct tt_common_entry *tt_common_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200969 struct tt_global_entry *tt_global_entry;
970 struct hlist_node *node, *node_tmp;
971 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200972 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200973 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200974
Antonio Quartullicc47f662011-04-27 14:27:57 +0200975 for (i = 0; i < hash->size; i++) {
976 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200977 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200978
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200979 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100980 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200981 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100982 tt_global_entry = container_of(tt_common_entry,
983 struct tt_global_entry,
984 common);
985 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200986 continue;
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100987 if (!has_timed_out(tt_global_entry->roam_at,
Marek Lindner032b7962011-12-20 19:30:40 +0800988 TT_CLIENT_ROAM_TIMEOUT))
Antonio Quartullicc47f662011-04-27 14:27:57 +0200989 continue;
990
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100991 bat_dbg(DBG_TT, bat_priv,
992 "Deleting global tt entry (%pM): Roaming timeout\n",
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100993 tt_global_entry->common.addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200994
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200995 hlist_del_rcu(node);
996 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200997 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200998 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200999 }
1000
Antonio Quartullicc47f662011-04-27 14:27:57 +02001001}
1002
Antonio Quartullia73105b2011-04-27 14:27:44 +02001003static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001004{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001005 struct hashtable_t *hash;
1006 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001007 struct tt_common_entry *tt_common_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001008 struct tt_global_entry *tt_global_entry;
1009 struct hlist_node *node, *node_tmp;
1010 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001011 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001012
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001013 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014 return;
1015
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001016 hash = bat_priv->tt_global_hash;
1017
1018 for (i = 0; i < hash->size; i++) {
1019 head = &hash->table[i];
1020 list_lock = &hash->list_locks[i];
1021
1022 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001023 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001024 head, hash_entry) {
1025 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001026 tt_global_entry = container_of(tt_common_entry,
1027 struct tt_global_entry,
1028 common);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001029 tt_global_entry_free_ref(tt_global_entry);
1030 }
1031 spin_unlock_bh(list_lock);
1032 }
1033
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001034 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001035
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001036 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037}
1038
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001039static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
1040 struct tt_global_entry *tt_global_entry)
1041{
1042 bool ret = false;
1043
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001044 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1045 tt_global_entry->common.flags & TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001046 ret = true;
1047
1048 return ret;
1049}
1050
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001051struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1052 const uint8_t *src,
1053 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054{
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001055 struct tt_local_entry *tt_local_entry = NULL;
1056 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001057 struct orig_node *orig_node = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001058 struct neigh_node *router = NULL;
1059 struct hlist_head *head;
1060 struct hlist_node *node;
1061 struct tt_orig_list_entry *orig_entry;
1062 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001064 if (src && atomic_read(&bat_priv->ap_isolation)) {
1065 tt_local_entry = tt_local_hash_find(bat_priv, src);
1066 if (!tt_local_entry)
1067 goto out;
1068 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001069
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001070 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001071 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001072 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001074 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001075 * isolation
1076 */
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001077 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
1078 goto out;
1079
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001080 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001081
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001082 rcu_read_lock();
1083 head = &tt_global_entry->orig_list;
1084 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001085 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086 if (!router)
1087 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001088
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001089 if (router->tq_avg > best_tq) {
1090 orig_node = orig_entry->orig_node;
1091 best_tq = router->tq_avg;
1092 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001093 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094 }
1095 /* found anything? */
1096 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1097 orig_node = NULL;
1098 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001099out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001100 if (tt_global_entry)
1101 tt_global_entry_free_ref(tt_global_entry);
1102 if (tt_local_entry)
1103 tt_local_entry_free_ref(tt_local_entry);
1104
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001105 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001106}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001107
1108/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001109static uint16_t tt_global_crc(struct bat_priv *bat_priv,
1110 struct orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001111{
1112 uint16_t total = 0, total_one;
1113 struct hashtable_t *hash = bat_priv->tt_global_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001114 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001115 struct tt_global_entry *tt_global_entry;
1116 struct hlist_node *node;
1117 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001118 uint32_t i;
1119 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001120
1121 for (i = 0; i < hash->size; i++) {
1122 head = &hash->table[i];
1123
1124 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001125 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001126 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001127 tt_global_entry = container_of(tt_common_entry,
1128 struct tt_global_entry,
1129 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001130 /* Roaming clients are in the global table for
1131 * consistency only. They don't have to be
1132 * taken into account while computing the
1133 * global crc
1134 */
1135 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1136 continue;
1137
1138 /* find out if this global entry is announced by this
1139 * originator
1140 */
1141 if (!tt_global_entry_has_orig(tt_global_entry,
1142 orig_node))
1143 continue;
1144
1145 total_one = 0;
1146 for (j = 0; j < ETH_ALEN; j++)
1147 total_one = crc16_byte(total_one,
1148 tt_global_entry->common.addr[j]);
1149 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001150 }
1151 rcu_read_unlock();
1152 }
1153
1154 return total;
1155}
1156
1157/* Calculates the checksum of the local table */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001158static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001159{
1160 uint16_t total = 0, total_one;
1161 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001162 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163 struct hlist_node *node;
1164 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001165 uint32_t i;
1166 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167
1168 for (i = 0; i < hash->size; i++) {
1169 head = &hash->table[i];
1170
1171 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001172 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001173 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001174 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001175 * account while computing the CRC
1176 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001177 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001178 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001179 total_one = 0;
1180 for (j = 0; j < ETH_ALEN; j++)
1181 total_one = crc16_byte(total_one,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001182 tt_common_entry->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001183 total ^= total_one;
1184 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001185 rcu_read_unlock();
1186 }
1187
1188 return total;
1189}
1190
1191static void tt_req_list_free(struct bat_priv *bat_priv)
1192{
1193 struct tt_req_node *node, *safe;
1194
1195 spin_lock_bh(&bat_priv->tt_req_list_lock);
1196
1197 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1198 list_del(&node->list);
1199 kfree(node);
1200 }
1201
1202 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1203}
1204
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001205static void tt_save_orig_buffer(struct bat_priv *bat_priv,
1206 struct orig_node *orig_node,
1207 const unsigned char *tt_buff,
1208 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001209{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001210 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001211
1212 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001213 * last OGM (the OGM could carry no changes)
1214 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001215 spin_lock_bh(&orig_node->tt_buff_lock);
1216 if (tt_buff_len > 0) {
1217 kfree(orig_node->tt_buff);
1218 orig_node->tt_buff_len = 0;
1219 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1220 if (orig_node->tt_buff) {
1221 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1222 orig_node->tt_buff_len = tt_buff_len;
1223 }
1224 }
1225 spin_unlock_bh(&orig_node->tt_buff_lock);
1226}
1227
1228static void tt_req_purge(struct bat_priv *bat_priv)
1229{
1230 struct tt_req_node *node, *safe;
1231
1232 spin_lock_bh(&bat_priv->tt_req_list_lock);
1233 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001234 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001235 list_del(&node->list);
1236 kfree(node);
1237 }
1238 }
1239 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1240}
1241
1242/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001243 * has already been issued for this orig_node, NULL otherwise
1244 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001245static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
1246 struct orig_node *orig_node)
1247{
1248 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1249
1250 spin_lock_bh(&bat_priv->tt_req_list_lock);
1251 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1252 if (compare_eth(tt_req_node_tmp, orig_node) &&
Martin Hundebølla04ccd52011-12-08 13:32:41 +01001253 !has_timed_out(tt_req_node_tmp->issued_at,
Marek Lindner032b7962011-12-20 19:30:40 +08001254 TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001255 goto unlock;
1256 }
1257
1258 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1259 if (!tt_req_node)
1260 goto unlock;
1261
1262 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1263 tt_req_node->issued_at = jiffies;
1264
1265 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1266unlock:
1267 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1268 return tt_req_node;
1269}
1270
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001271/* data_ptr is useless here, but has to be kept to respect the prototype */
1272static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1273{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001274 const struct tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001275
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001276 if (tt_common_entry->flags & TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001277 return 0;
1278 return 1;
1279}
1280
Antonio Quartullia73105b2011-04-27 14:27:44 +02001281static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1282{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001283 const struct tt_common_entry *tt_common_entry = entry_ptr;
1284 const struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001285 const struct orig_node *orig_node = data_ptr;
1286
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001287 if (tt_common_entry->flags & TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001288 return 0;
1289
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001290 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1291 common);
1292
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001293 return tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001294}
1295
1296static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1297 struct hashtable_t *hash,
1298 struct hard_iface *primary_if,
1299 int (*valid_cb)(const void *,
1300 const void *),
1301 void *cb_data)
1302{
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001303 struct tt_common_entry *tt_common_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001304 struct tt_query_packet *tt_response;
1305 struct tt_change *tt_change;
1306 struct hlist_node *node;
1307 struct hlist_head *head;
1308 struct sk_buff *skb = NULL;
1309 uint16_t tt_tot, tt_count;
1310 ssize_t tt_query_size = sizeof(struct tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001311 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001312
1313 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1314 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1315 tt_len -= tt_len % sizeof(struct tt_change);
1316 }
1317 tt_tot = tt_len / sizeof(struct tt_change);
1318
1319 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1320 if (!skb)
1321 goto out;
1322
1323 skb_reserve(skb, ETH_HLEN);
1324 tt_response = (struct tt_query_packet *)skb_put(skb,
1325 tt_query_size + tt_len);
1326 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001327
1328 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1329 tt_count = 0;
1330
1331 rcu_read_lock();
1332 for (i = 0; i < hash->size; i++) {
1333 head = &hash->table[i];
1334
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001335 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001336 head, hash_entry) {
1337 if (tt_count == tt_tot)
1338 break;
1339
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001340 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341 continue;
1342
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001343 memcpy(tt_change->addr, tt_common_entry->addr,
1344 ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001345 tt_change->flags = NO_FLAGS;
1346
1347 tt_count++;
1348 tt_change++;
1349 }
1350 }
1351 rcu_read_unlock();
1352
Antonio Quartulli9d852392011-10-17 14:25:13 +02001353 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001354 * copied
1355 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001356 tt_response->tt_data = htons(tt_count);
1357
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358out:
1359 return skb;
1360}
1361
Marek Lindnera943cac2011-07-30 13:10:18 +02001362static int send_tt_request(struct bat_priv *bat_priv,
1363 struct orig_node *dst_orig_node,
1364 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001365{
1366 struct sk_buff *skb = NULL;
1367 struct tt_query_packet *tt_request;
1368 struct neigh_node *neigh_node = NULL;
1369 struct hard_iface *primary_if;
1370 struct tt_req_node *tt_req_node = NULL;
1371 int ret = 1;
1372
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001373 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001374 if (!primary_if)
1375 goto out;
1376
1377 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001378 * reply from the same orig_node yet
1379 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001380 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1381 if (!tt_req_node)
1382 goto out;
1383
1384 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1385 if (!skb)
1386 goto out;
1387
1388 skb_reserve(skb, ETH_HLEN);
1389
1390 tt_request = (struct tt_query_packet *)skb_put(skb,
1391 sizeof(struct tt_query_packet));
1392
Sven Eckelmann76543d12011-11-20 15:47:38 +01001393 tt_request->header.packet_type = BAT_TT_QUERY;
1394 tt_request->header.version = COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1396 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +01001397 tt_request->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001399 tt_request->tt_data = htons(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 tt_request->flags = TT_REQUEST;
1401
1402 if (full_table)
1403 tt_request->flags |= TT_FULL_TABLE;
1404
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001405 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001406 if (!neigh_node)
1407 goto out;
1408
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001409 bat_dbg(DBG_TT, bat_priv,
1410 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1411 dst_orig_node->orig, neigh_node->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001412 (full_table ? 'F' : '.'));
1413
Martin Hundebøllf8214862012-04-20 17:02:45 +02001414 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1415
Sven Eckelmann9455e342012-05-12 02:09:37 +02001416 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001417 ret = 0;
1418
1419out:
1420 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001421 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001422 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001423 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001424 if (ret)
1425 kfree_skb(skb);
1426 if (ret && tt_req_node) {
1427 spin_lock_bh(&bat_priv->tt_req_list_lock);
1428 list_del(&tt_req_node->list);
1429 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1430 kfree(tt_req_node);
1431 }
1432 return ret;
1433}
1434
1435static bool send_other_tt_response(struct bat_priv *bat_priv,
1436 struct tt_query_packet *tt_request)
1437{
1438 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1439 struct neigh_node *neigh_node = NULL;
1440 struct hard_iface *primary_if = NULL;
1441 uint8_t orig_ttvn, req_ttvn, ttvn;
1442 int ret = false;
1443 unsigned char *tt_buff;
1444 bool full_table;
1445 uint16_t tt_len, tt_tot;
1446 struct sk_buff *skb = NULL;
1447 struct tt_query_packet *tt_response;
1448
1449 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001450 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1451 tt_request->src, tt_request->ttvn, tt_request->dst,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1453
1454 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001455 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 if (!req_dst_orig_node)
1457 goto out;
1458
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001459 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001460 if (!res_dst_orig_node)
1461 goto out;
1462
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001463 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001464 if (!neigh_node)
1465 goto out;
1466
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001467 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468 if (!primary_if)
1469 goto out;
1470
1471 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1472 req_ttvn = tt_request->ttvn;
1473
Antonio Quartulli015758d2011-07-09 17:52:13 +02001474 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001476 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477 goto out;
1478
Antonio Quartulli015758d2011-07-09 17:52:13 +02001479 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480 if (tt_request->flags & TT_FULL_TABLE ||
1481 !req_dst_orig_node->tt_buff)
1482 full_table = true;
1483 else
1484 full_table = false;
1485
1486 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001487 * I'll send only one packet with as much TT entries as I can
1488 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489 if (!full_table) {
1490 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1491 tt_len = req_dst_orig_node->tt_buff_len;
1492 tt_tot = tt_len / sizeof(struct tt_change);
1493
1494 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1495 tt_len + ETH_HLEN);
1496 if (!skb)
1497 goto unlock;
1498
1499 skb_reserve(skb, ETH_HLEN);
1500 tt_response = (struct tt_query_packet *)skb_put(skb,
1501 sizeof(struct tt_query_packet) + tt_len);
1502 tt_response->ttvn = req_ttvn;
1503 tt_response->tt_data = htons(tt_tot);
1504
1505 tt_buff = skb->data + sizeof(struct tt_query_packet);
1506 /* Copy the last orig_node's OGM buffer */
1507 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1508 req_dst_orig_node->tt_buff_len);
1509
1510 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1511 } else {
1512 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1513 sizeof(struct tt_change);
1514 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1515
1516 skb = tt_response_fill_table(tt_len, ttvn,
1517 bat_priv->tt_global_hash,
1518 primary_if, tt_global_valid_entry,
1519 req_dst_orig_node);
1520 if (!skb)
1521 goto out;
1522
1523 tt_response = (struct tt_query_packet *)skb->data;
1524 }
1525
Sven Eckelmann76543d12011-11-20 15:47:38 +01001526 tt_response->header.packet_type = BAT_TT_QUERY;
1527 tt_response->header.version = COMPAT_VERSION;
1528 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1530 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1531 tt_response->flags = TT_RESPONSE;
1532
1533 if (full_table)
1534 tt_response->flags |= TT_FULL_TABLE;
1535
1536 bat_dbg(DBG_TT, bat_priv,
1537 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1538 res_dst_orig_node->orig, neigh_node->addr,
1539 req_dst_orig_node->orig, req_ttvn);
1540
Martin Hundebøllf8214862012-04-20 17:02:45 +02001541 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1542
Sven Eckelmann9455e342012-05-12 02:09:37 +02001543 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001544 ret = true;
1545 goto out;
1546
1547unlock:
1548 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1549
1550out:
1551 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001552 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001554 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001556 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001557 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001558 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559 if (!ret)
1560 kfree_skb(skb);
1561 return ret;
1562
1563}
1564static bool send_my_tt_response(struct bat_priv *bat_priv,
1565 struct tt_query_packet *tt_request)
1566{
1567 struct orig_node *orig_node = NULL;
1568 struct neigh_node *neigh_node = NULL;
1569 struct hard_iface *primary_if = NULL;
1570 uint8_t my_ttvn, req_ttvn, ttvn;
1571 int ret = false;
1572 unsigned char *tt_buff;
1573 bool full_table;
1574 uint16_t tt_len, tt_tot;
1575 struct sk_buff *skb = NULL;
1576 struct tt_query_packet *tt_response;
1577
1578 bat_dbg(DBG_TT, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001579 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1580 tt_request->src, tt_request->ttvn,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1582
1583
1584 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1585 req_ttvn = tt_request->ttvn;
1586
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001587 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588 if (!orig_node)
1589 goto out;
1590
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001591 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001592 if (!neigh_node)
1593 goto out;
1594
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001595 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596 if (!primary_if)
1597 goto out;
1598
1599 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001600 * is too big send the whole local translation table
1601 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001602 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1603 !bat_priv->tt_buff)
1604 full_table = true;
1605 else
1606 full_table = false;
1607
1608 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001609 * I'll send only one packet with as much TT entries as I can
1610 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 if (!full_table) {
1612 spin_lock_bh(&bat_priv->tt_buff_lock);
1613 tt_len = bat_priv->tt_buff_len;
1614 tt_tot = tt_len / sizeof(struct tt_change);
1615
1616 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1617 tt_len + ETH_HLEN);
1618 if (!skb)
1619 goto unlock;
1620
1621 skb_reserve(skb, ETH_HLEN);
1622 tt_response = (struct tt_query_packet *)skb_put(skb,
1623 sizeof(struct tt_query_packet) + tt_len);
1624 tt_response->ttvn = req_ttvn;
1625 tt_response->tt_data = htons(tt_tot);
1626
1627 tt_buff = skb->data + sizeof(struct tt_query_packet);
1628 memcpy(tt_buff, bat_priv->tt_buff,
1629 bat_priv->tt_buff_len);
1630 spin_unlock_bh(&bat_priv->tt_buff_lock);
1631 } else {
1632 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1633 sizeof(struct tt_change);
1634 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1635
1636 skb = tt_response_fill_table(tt_len, ttvn,
1637 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001638 primary_if, tt_local_valid_entry,
1639 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001640 if (!skb)
1641 goto out;
1642
1643 tt_response = (struct tt_query_packet *)skb->data;
1644 }
1645
Sven Eckelmann76543d12011-11-20 15:47:38 +01001646 tt_response->header.packet_type = BAT_TT_QUERY;
1647 tt_response->header.version = COMPAT_VERSION;
1648 tt_response->header.ttl = TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1650 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1651 tt_response->flags = TT_RESPONSE;
1652
1653 if (full_table)
1654 tt_response->flags |= TT_FULL_TABLE;
1655
1656 bat_dbg(DBG_TT, bat_priv,
1657 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1658 orig_node->orig, neigh_node->addr,
1659 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1660
Martin Hundebøllf8214862012-04-20 17:02:45 +02001661 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1662
Sven Eckelmann9455e342012-05-12 02:09:37 +02001663 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001664 ret = true;
1665 goto out;
1666
1667unlock:
1668 spin_unlock_bh(&bat_priv->tt_buff_lock);
1669out:
1670 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001671 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001672 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001673 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001674 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001675 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676 if (!ret)
1677 kfree_skb(skb);
1678 /* This packet was for me, so it doesn't need to be re-routed */
1679 return true;
1680}
1681
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001682bool batadv_send_tt_response(struct bat_priv *bat_priv,
1683 struct tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001685 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001686 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001687 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001688 return true;
1689
Antonio Quartullia73105b2011-04-27 14:27:44 +02001690 return send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001691 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692 return send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001693 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694}
1695
1696static void _tt_update_changes(struct bat_priv *bat_priv,
1697 struct orig_node *orig_node,
1698 struct tt_change *tt_change,
1699 uint16_t tt_num_changes, uint8_t ttvn)
1700{
1701 int i;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001702 int is_wifi;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
1704 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001705 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 tt_global_del(bat_priv, orig_node,
1707 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001708 "tt removed by changes",
1709 (tt_change + i)->flags & TT_CLIENT_ROAM);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001710 } else {
1711 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1712 if (!batadv_tt_global_add(bat_priv, orig_node,
1713 (tt_change + i)->addr, ttvn,
1714 false, is_wifi))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 /* In case of problem while storing a
1716 * global_entry, we stop the updating
1717 * procedure without committing the
1718 * ttvn change. This will avoid to send
1719 * corrupted data on tt_request
1720 */
1721 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001722 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001723 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001724 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001725}
1726
1727static void tt_fill_gtable(struct bat_priv *bat_priv,
1728 struct tt_query_packet *tt_response)
1729{
1730 struct orig_node *orig_node = NULL;
1731
1732 orig_node = orig_hash_find(bat_priv, tt_response->src);
1733 if (!orig_node)
1734 goto out;
1735
1736 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001737 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001738
1739 _tt_update_changes(bat_priv, orig_node,
1740 (struct tt_change *)(tt_response + 1),
Al Virof25bd582012-04-22 07:44:27 +01001741 ntohs(tt_response->tt_data), tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742
1743 spin_lock_bh(&orig_node->tt_buff_lock);
1744 kfree(orig_node->tt_buff);
1745 orig_node->tt_buff_len = 0;
1746 orig_node->tt_buff = NULL;
1747 spin_unlock_bh(&orig_node->tt_buff_lock);
1748
1749 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1750
1751out:
1752 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001753 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754}
1755
Marek Lindnera943cac2011-07-30 13:10:18 +02001756static void tt_update_changes(struct bat_priv *bat_priv,
1757 struct orig_node *orig_node,
1758 uint16_t tt_num_changes, uint8_t ttvn,
1759 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001760{
1761 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1762 ttvn);
1763
1764 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1765 tt_num_changes);
1766 atomic_set(&orig_node->last_ttvn, ttvn);
1767}
1768
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001769bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001770{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001771 struct tt_local_entry *tt_local_entry = NULL;
1772 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001773
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001775 if (!tt_local_entry)
1776 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001777 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001778 * consistency purpose)
1779 */
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001780 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001781 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001782 ret = true;
1783out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001785 tt_local_entry_free_ref(tt_local_entry);
1786 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001787}
1788
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001789void batadv_handle_tt_response(struct bat_priv *bat_priv,
1790 struct tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791{
1792 struct tt_req_node *node, *safe;
1793 struct orig_node *orig_node = NULL;
1794
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001795 bat_dbg(DBG_TT, bat_priv,
1796 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Al Virof25bd582012-04-22 07:44:27 +01001797 tt_response->src, tt_response->ttvn,
1798 ntohs(tt_response->tt_data),
Antonio Quartullia73105b2011-04-27 14:27:44 +02001799 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1800
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001801 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001802 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001803 goto out;
1804
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805 orig_node = orig_hash_find(bat_priv, tt_response->src);
1806 if (!orig_node)
1807 goto out;
1808
1809 if (tt_response->flags & TT_FULL_TABLE)
1810 tt_fill_gtable(bat_priv, tt_response);
1811 else
Al Virof25bd582012-04-22 07:44:27 +01001812 tt_update_changes(bat_priv, orig_node,
1813 ntohs(tt_response->tt_data),
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814 tt_response->ttvn,
1815 (struct tt_change *)(tt_response + 1));
1816
1817 /* Delete the tt_req_node from pending tt_requests list */
1818 spin_lock_bh(&bat_priv->tt_req_list_lock);
1819 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1820 if (!compare_eth(node->addr, tt_response->src))
1821 continue;
1822 list_del(&node->list);
1823 kfree(node);
1824 }
1825 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1826
1827 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001829 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001830 * unset the flag
1831 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001832 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001833out:
1834 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001835 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836}
1837
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001838int batadv_tt_init(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001840 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841
Sven Eckelmann5346c352012-05-05 13:27:28 +02001842 ret = tt_local_init(bat_priv);
1843 if (ret < 0)
1844 return ret;
1845
1846 ret = tt_global_init(bat_priv);
1847 if (ret < 0)
1848 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001849
1850 tt_start_timer(bat_priv);
1851
1852 return 1;
1853}
1854
Antonio Quartullicc47f662011-04-27 14:27:57 +02001855static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001857 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001858
Antonio Quartullicc47f662011-04-27 14:27:57 +02001859 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001860
Antonio Quartullicc47f662011-04-27 14:27:57 +02001861 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1862 list_del(&node->list);
1863 kfree(node);
1864 }
1865
1866 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1867}
1868
1869static void tt_roam_purge(struct bat_priv *bat_priv)
1870{
1871 struct tt_roam_node *node, *safe;
1872
1873 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1874 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Marek Lindner032b7962011-12-20 19:30:40 +08001875 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001876 continue;
1877
1878 list_del(&node->list);
1879 kfree(node);
1880 }
1881 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1882}
1883
1884/* This function checks whether the client already reached the
1885 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1886 * will not be sent.
1887 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001888 * returns true if the ROAMING_ADV can be sent, false otherwise
1889 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001890static bool tt_check_roam_count(struct bat_priv *bat_priv,
1891 uint8_t *client)
1892{
1893 struct tt_roam_node *tt_roam_node;
1894 bool ret = false;
1895
1896 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1897 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001898 * reply from the same orig_node yet
1899 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001900 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1901 if (!compare_eth(tt_roam_node->addr, client))
1902 continue;
1903
Marek Lindner032b7962011-12-20 19:30:40 +08001904 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02001905 continue;
1906
1907 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1908 /* Sorry, you roamed too many times! */
1909 goto unlock;
1910 ret = true;
1911 break;
1912 }
1913
1914 if (!ret) {
1915 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1916 if (!tt_roam_node)
1917 goto unlock;
1918
1919 tt_roam_node->first_time = jiffies;
1920 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1921 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1922
1923 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1924 ret = true;
1925 }
1926
1927unlock:
1928 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1929 return ret;
1930}
1931
Sven Eckelmannde7aae62012-02-05 18:55:22 +01001932static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1933 struct orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001934{
1935 struct neigh_node *neigh_node = NULL;
1936 struct sk_buff *skb = NULL;
1937 struct roam_adv_packet *roam_adv_packet;
1938 int ret = 1;
1939 struct hard_iface *primary_if;
1940
1941 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001942 * already roamed to us too many times
1943 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001944 if (!tt_check_roam_count(bat_priv, client))
1945 goto out;
1946
1947 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1948 if (!skb)
1949 goto out;
1950
1951 skb_reserve(skb, ETH_HLEN);
1952
1953 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1954 sizeof(struct roam_adv_packet));
1955
Sven Eckelmann76543d12011-11-20 15:47:38 +01001956 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1957 roam_adv_packet->header.version = COMPAT_VERSION;
1958 roam_adv_packet->header.ttl = TTL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001959 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001960 if (!primary_if)
1961 goto out;
1962 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001963 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001964 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1965 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1966
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001967 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001968 if (!neigh_node)
1969 goto out;
1970
1971 bat_dbg(DBG_TT, bat_priv,
1972 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1973 orig_node->orig, client, neigh_node->addr);
1974
Martin Hundebøllf8214862012-04-20 17:02:45 +02001975 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
1976
Sven Eckelmann9455e342012-05-12 02:09:37 +02001977 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001978 ret = 0;
1979
1980out:
1981 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001982 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001983 if (ret)
1984 kfree_skb(skb);
1985 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986}
1987
1988static void tt_purge(struct work_struct *work)
1989{
1990 struct delayed_work *delayed_work =
1991 container_of(work, struct delayed_work, work);
1992 struct bat_priv *bat_priv =
1993 container_of(delayed_work, struct bat_priv, tt_work);
1994
1995 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001996 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001997 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001998 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001999
2000 tt_start_timer(bat_priv);
2001}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002002
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002003void batadv_tt_free(struct bat_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002004{
2005 cancel_delayed_work_sync(&bat_priv->tt_work);
2006
2007 tt_local_table_free(bat_priv);
2008 tt_global_table_free(bat_priv);
2009 tt_req_list_free(bat_priv);
2010 tt_changes_list_free(bat_priv);
2011 tt_roam_list_free(bat_priv);
2012
2013 kfree(bat_priv->tt_buff);
2014}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002015
Antonio Quartulli697f2532011-11-07 16:47:01 +01002016/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002017 * in the given hash table and returns the number of modified entries
2018 */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002019static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2020 bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002021{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002022 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002023 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002024 struct hlist_head *head;
2025 struct hlist_node *node;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002026 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002027
2028 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002029 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002030
2031 for (i = 0; i < hash->size; i++) {
2032 head = &hash->table[i];
2033
2034 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002035 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002036 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002037 if (enable) {
2038 if ((tt_common_entry->flags & flags) == flags)
2039 continue;
2040 tt_common_entry->flags |= flags;
2041 } else {
2042 if (!(tt_common_entry->flags & flags))
2043 continue;
2044 tt_common_entry->flags &= ~flags;
2045 }
2046 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002047 }
2048 rcu_read_unlock();
2049 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002050out:
2051 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002052}
2053
2054/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
2055static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
2056{
2057 struct hashtable_t *hash = bat_priv->tt_local_hash;
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002058 struct tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002059 struct tt_local_entry *tt_local_entry;
2060 struct hlist_node *node, *node_tmp;
2061 struct hlist_head *head;
2062 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002063 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002064
2065 if (!hash)
2066 return;
2067
2068 for (i = 0; i < hash->size; i++) {
2069 head = &hash->table[i];
2070 list_lock = &hash->list_locks[i];
2071
2072 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002073 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002074 head, hash_entry) {
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002075 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002076 continue;
2077
Sven Eckelmann86ceb362012-03-07 09:07:45 +01002078 bat_dbg(DBG_TT, bat_priv,
2079 "Deleting local tt entry (%pM): pending\n",
2080 tt_common_entry->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002081
2082 atomic_dec(&bat_priv->num_local_tt);
2083 hlist_del_rcu(node);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002084 tt_local_entry = container_of(tt_common_entry,
2085 struct tt_local_entry,
2086 common);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002087 tt_local_entry_free_ref(tt_local_entry);
2088 }
2089 spin_unlock_bh(list_lock);
2090 }
2091
2092}
2093
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002094static int tt_commit_changes(struct bat_priv *bat_priv,
2095 unsigned char **packet_buff, int *packet_buff_len,
2096 int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002097{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002098 uint16_t changed_num = 0;
2099
2100 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2101 return -ENOENT;
2102
2103 changed_num = tt_set_flags(bat_priv->tt_local_hash,
2104 TT_CLIENT_NEW, false);
2105
2106 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002107 atomic_add(changed_num, &bat_priv->num_local_tt);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002108 tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002109 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002110
2111 /* Increment the TTVN only once per OGM interval */
2112 atomic_inc(&bat_priv->ttvn);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002113 bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
2114 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002115 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002116
2117 /* reset the sending counter */
2118 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2119
2120 return tt_changes_fill_buff(bat_priv, packet_buff,
2121 packet_buff_len, packet_min_len);
2122}
2123
2124/* when calling this function (hard_iface == primary_if) has to be true */
2125int batadv_tt_append_diff(struct bat_priv *bat_priv,
2126 unsigned char **packet_buff, int *packet_buff_len,
2127 int packet_min_len)
2128{
2129 int tt_num_changes;
2130
2131 /* if at least one change happened */
2132 tt_num_changes = tt_commit_changes(bat_priv, packet_buff,
2133 packet_buff_len, packet_min_len);
2134
2135 /* if the changes have been sent often enough */
2136 if ((tt_num_changes < 0) &&
2137 (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
2138 tt_realloc_packet_buff(packet_buff, packet_buff_len,
2139 packet_min_len, packet_min_len);
2140 tt_num_changes = 0;
2141 }
2142
2143 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002144}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002145
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002146bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2147 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002148{
2149 struct tt_local_entry *tt_local_entry = NULL;
2150 struct tt_global_entry *tt_global_entry = NULL;
2151 bool ret = true;
2152
2153 if (!atomic_read(&bat_priv->ap_isolation))
2154 return false;
2155
2156 tt_local_entry = tt_local_hash_find(bat_priv, dst);
2157 if (!tt_local_entry)
2158 goto out;
2159
2160 tt_global_entry = tt_global_hash_find(bat_priv, src);
2161 if (!tt_global_entry)
2162 goto out;
2163
2164 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
2165 goto out;
2166
2167 ret = false;
2168
2169out:
2170 if (tt_global_entry)
2171 tt_global_entry_free_ref(tt_global_entry);
2172 if (tt_local_entry)
2173 tt_local_entry_free_ref(tt_local_entry);
2174 return ret;
2175}
Marek Lindnera943cac2011-07-30 13:10:18 +02002176
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002177void batadv_tt_update_orig(struct bat_priv *bat_priv,
2178 struct orig_node *orig_node,
2179 const unsigned char *tt_buff, uint8_t tt_num_changes,
2180 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002181{
2182 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2183 bool full_table = true;
2184
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002185 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002186 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002187 return;
2188
Antonio Quartulli17071572011-11-07 16:36:40 +01002189 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002190 * increased by one -> we can apply the attached changes
2191 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002192 if ((!orig_node->tt_initialised && ttvn == 1) ||
2193 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002194 /* the OGM could not contain the changes due to their size or
2195 * because they have already been sent TT_OGM_APPEND_MAX times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002196 * In this case send a tt request
2197 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002198 if (!tt_num_changes) {
2199 full_table = false;
2200 goto request_table;
2201 }
2202
2203 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
2204 (struct tt_change *)tt_buff);
2205
2206 /* Even if we received the precomputed crc with the OGM, we
2207 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002208 * in the global table
2209 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002210 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
2211
2212 /* The ttvn alone is not enough to guarantee consistency
2213 * because a single value could represent different states
2214 * (due to the wrap around). Thus a node has to check whether
2215 * the resulting table (after applying the changes) is still
2216 * consistent or not. E.g. a node could disconnect while its
2217 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2218 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002219 * inconsistency
2220 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002221 if (orig_node->tt_crc != tt_crc)
2222 goto request_table;
2223
2224 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002225 * unset the flag
2226 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002227 orig_node->tt_poss_change = false;
2228 } else {
2229 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002230 * in sync anymore -> request fresh tt data
2231 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002232 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2233 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002234request_table:
Sven Eckelmann86ceb362012-03-07 09:07:45 +01002235 bat_dbg(DBG_TT, bat_priv,
2236 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2237 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2238 orig_node->tt_crc, tt_num_changes);
Marek Lindnera943cac2011-07-30 13:10:18 +02002239 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
2240 full_table);
2241 return;
2242 }
2243 }
2244}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002245
2246/* returns true whether we know that the client has moved from its old
2247 * originator to another one. This entry is kept is still kept for consistency
2248 * purposes
2249 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002250bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2251 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002252{
2253 struct tt_global_entry *tt_global_entry;
2254 bool ret = false;
2255
2256 tt_global_entry = tt_global_hash_find(bat_priv, addr);
2257 if (!tt_global_entry)
2258 goto out;
2259
2260 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
2261 tt_global_entry_free_ref(tt_global_entry);
2262out:
2263 return ret;
2264}