blob: c7416947a4e05697ea51c9d8c56e32a259a4440a [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 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 Quartulliced72932013-04-24 16:37:51 +020030#include <linux/crc32c.h>
Antonio Quartullia73105b2011-04-27 14:27:44 +020031
Antonio Quartullidec05072012-11-10 11:00:32 +010032/* hash class keys */
33static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34static struct lock_class_key batadv_tt_global_hash_lock_class_key;
35
Sven Eckelmann56303d32012-06-05 22:31:31 +020036static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
37 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020038static void batadv_tt_purge(struct work_struct *work);
39static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020040batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020041static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42 struct batadv_orig_node *orig_node,
43 const unsigned char *addr,
44 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Marek Lindner7aadf882011-02-18 12:28:09 +000046/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020047static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000048{
Sven Eckelmann56303d32012-06-05 22:31:31 +020049 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000051
52 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
53}
54
Sven Eckelmann56303d32012-06-05 22:31:31 +020055static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020056batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000057{
Marek Lindner7aadf882011-02-18 12:28:09 +000058 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +020059 struct batadv_tt_common_entry *tt_common_entry;
60 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020061 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000062
63 if (!hash)
64 return NULL;
65
Sven Eckelmannda641192012-05-12 13:48:56 +020066 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000067 head = &hash->table[index];
68
69 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -080070 hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020071 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000072 continue;
73
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020075 continue;
76
Antonio Quartulli48100ba2011-10-30 12:17:33 +010077 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000078 break;
79 }
80 rcu_read_unlock();
81
Antonio Quartulli48100ba2011-10-30 12:17:33 +010082 return tt_common_entry_tmp;
83}
84
Sven Eckelmann56303d32012-06-05 22:31:31 +020085static struct batadv_tt_local_entry *
86batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010087{
Sven Eckelmann56303d32012-06-05 22:31:31 +020088 struct batadv_tt_common_entry *tt_common_entry;
89 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010090
Sven Eckelmann807736f2012-07-15 22:26:51 +020091 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010092 if (tt_common_entry)
93 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020094 struct batadv_tt_local_entry,
95 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000097}
98
Sven Eckelmann56303d32012-06-05 22:31:31 +020099static struct batadv_tt_global_entry *
100batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000101{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200102 struct batadv_tt_common_entry *tt_common_entry;
103 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000104
Sven Eckelmann807736f2012-07-15 22:26:51 +0200105 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100106 if (tt_common_entry)
107 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200108 struct batadv_tt_global_entry,
109 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000111}
112
Sven Eckelmanna5130882012-05-16 20:23:16 +0200113static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100116 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
117 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200118}
119
Sven Eckelmanna5130882012-05-16 20:23:16 +0200120static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200121{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200122 struct batadv_tt_common_entry *tt_common_entry;
123 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200124
Sven Eckelmann56303d32012-06-05 22:31:31 +0200125 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
126 tt_global_entry = container_of(tt_common_entry,
127 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200128
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129 kfree(tt_global_entry);
130}
131
Sven Eckelmanna5130882012-05-16 20:23:16 +0200132static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200133batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200135 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200136 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100137 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200138 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200139 }
140}
141
Sven Eckelmanna5130882012-05-16 20:23:16 +0200142static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200143{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200144 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200145
Sven Eckelmann56303d32012-06-05 22:31:31 +0200146 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800147
148 /* We are in an rcu callback here, therefore we cannot use
149 * batadv_orig_node_free_ref() and its call_rcu():
150 * An rcu_barrier() wouldn't wait for that to finish
151 */
152 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200166/**
167 * batadv_tt_local_event - store a local TT event (ADD/DEL)
168 * @bat_priv: the bat priv with all the soft interface information
169 * @tt_local_entry: the TT entry involved in the event
170 * @event_flags: flags to store in the event structure
171 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200172static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200173 struct batadv_tt_local_entry *tt_local_entry,
174 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200176 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200177 struct batadv_tt_common_entry *common = &tt_local_entry->common;
178 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200179 bool event_removed = false;
180 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200181
182 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200183 if (!tt_change_node)
184 return;
185
Antonio Quartulliff66c972011-06-30 01:14:00 +0200186 tt_change_node->change.flags = flags;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800187 tt_change_node->change.reserved = 0;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200188 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200189
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200190 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200191
192 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200193 spin_lock_bh(&bat_priv->tt.changes_list_lock);
194 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200195 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200196 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200197 continue;
198
199 /* DEL+ADD in the same orig interval have no effect and can be
200 * removed to avoid silly behaviour on the receiver side. The
201 * other way around (ADD+DEL) can happen in case of roaming of
202 * a client still in the NEW state. Roaming of NEW clients is
203 * now possible due to automatically recognition of "temporary"
204 * clients
205 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200206 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 if (!del_op_requested && del_op_entry)
208 goto del;
209 if (del_op_requested && !del_op_entry)
210 goto del;
211 continue;
212del:
213 list_del(&entry->list);
214 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000215 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200216 event_removed = true;
217 goto unlock;
218 }
219
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200221 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200222
223unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200224 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200225
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200226 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200227 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200228 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200229 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200230}
231
Marek Lindner335fbe02013-04-23 21:40:02 +0800232/**
233 * batadv_tt_len - compute length in bytes of given number of tt changes
234 * @changes_num: number of tt changes
235 *
236 * Returns computed length in bytes.
237 */
238static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200239{
Marek Lindner335fbe02013-04-23 21:40:02 +0800240 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200241}
242
Sven Eckelmann56303d32012-06-05 22:31:31 +0200243static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200245 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200246 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Sven Eckelmann807736f2012-07-15 22:26:51 +0200248 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Sven Eckelmann807736f2012-07-15 22:26:51 +0200250 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200251 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252
Antonio Quartullidec05072012-11-10 11:00:32 +0100253 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
254 &batadv_tt_local_hash_lock_class_key);
255
Sven Eckelmann5346c352012-05-05 13:27:28 +0200256 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257}
258
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200259static void batadv_tt_global_free(struct batadv_priv *bat_priv,
260 struct batadv_tt_global_entry *tt_global,
261 const char *message)
262{
263 batadv_dbg(BATADV_DBG_TT, bat_priv,
264 "Deleting global tt entry %pM: %s\n",
265 tt_global->common.addr, message);
266
267 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
268 batadv_choose_orig, tt_global->common.addr);
269 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200270}
271
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200272void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
273 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200275 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200276 struct batadv_tt_local_entry *tt_local;
277 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200278 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200279 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100280 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200281 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
Antonio Quartulli47c94652012-09-23 22:38:35 +0200283 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200284 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Antonio Quartulli47c94652012-09-23 22:38:35 +0200286 if (tt_local) {
287 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200288 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
289 batadv_dbg(BATADV_DBG_TT, bat_priv,
290 "Re-adding pending client %pM\n", addr);
291 /* whatever the reason why the PENDING flag was set,
292 * this is a client which was enqueued to be removed in
293 * this orig_interval. Since it popped up again, the
294 * flag can be reset like it was never enqueued
295 */
296 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
297 goto add_event;
298 }
299
300 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
301 batadv_dbg(BATADV_DBG_TT, bat_priv,
302 "Roaming client %pM came back to its original location\n",
303 addr);
304 /* the ROAM flag is set because this client roamed away
305 * and the node got a roaming_advertisement message. Now
306 * that the client popped up again at its original
307 * location such flag can be unset
308 */
309 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
310 roamed_back = true;
311 }
312 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 }
314
Antonio Quartulli47c94652012-09-23 22:38:35 +0200315 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
316 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200317 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200318
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200319 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200320 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200321 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322
Antonio Quartulli47c94652012-09-23 22:38:35 +0200323 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100324 /* The local entry has to be marked as NEW to avoid to send it in
325 * a full table response going out before the next ttvn increment
326 * (consistency check)
327 */
328 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Sven Eckelmann95638772012-05-12 02:09:31 +0200329 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200330 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
331 atomic_set(&tt_local->common.refcount, 2);
332 tt_local->last_seen = jiffies;
333 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334
335 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200336 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200337 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338
Sven Eckelmann807736f2012-07-15 22:26:51 +0200339 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200340 batadv_choose_orig, &tt_local->common,
341 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100342
343 if (unlikely(hash_added != 0)) {
344 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200345 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100346 goto out;
347 }
348
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200349add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200350 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200351
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200352check_roaming:
353 /* Check whether it is a roaming, but don't do anything if the roaming
354 * process has already been handled
355 */
356 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200357 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200358 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200359 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800360 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200361 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200362 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200363 }
364 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200365 if (roamed_back) {
366 batadv_tt_global_free(bat_priv, tt_global,
367 "Roaming canceled");
368 tt_global = NULL;
369 } else {
370 /* The global entry has to be marked as ROAMING and
371 * has to be kept for consistency purpose
372 */
373 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
374 tt_global->roam_at = jiffies;
375 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200376 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200377
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200378out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200379 if (tt_local)
380 batadv_tt_local_entry_free_ref(tt_local);
381 if (tt_global)
382 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383}
384
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800385/**
386 * batadv_tt_tvlv_container_update - update the translation table tvlv container
387 * after local tt changes have been committed
388 * @bat_priv: the bat priv with all the soft interface information
389 */
390static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800392 struct batadv_tt_change_node *entry, *safe;
393 struct batadv_tvlv_tt_data *tt_data;
394 struct batadv_tvlv_tt_change *tt_change;
395 int tt_diff_len = 0, tt_change_len = 0;
396 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800398 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800399
400 /* if we have too many changes for one packet don't send any
401 * and wait for the tt table request which will be fragmented
402 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800403 if (tt_diff_len > bat_priv->soft_iface->mtu)
404 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800405
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800406 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
407 if (!tt_data)
408 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800409
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800410 tt_data->flags = BATADV_TT_OGM_DIFF;
411 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
Antonio Quartulliced72932013-04-24 16:37:51 +0200412 tt_data->crc = htonl(bat_priv->tt.local_crc);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800413
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800414 if (tt_diff_len == 0)
415 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800416
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800417 tt_diff_entries_num = tt_diff_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Sven Eckelmann807736f2012-07-15 22:26:51 +0200419 spin_lock_bh(&bat_priv->tt.changes_list_lock);
420 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800422 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
423
Sven Eckelmann807736f2012-07-15 22:26:51 +0200424 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100425 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800426 if (tt_diff_entries_count < tt_diff_entries_num) {
427 memcpy(tt_change + tt_diff_entries_count,
428 &entry->change,
429 sizeof(struct batadv_tvlv_tt_change));
430 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200432 list_del(&entry->list);
433 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200435 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200438 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
439 kfree(bat_priv->tt.last_changeset);
440 bat_priv->tt.last_changeset_len = 0;
441 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800442 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800443 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800444 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800445 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200446 * instead of providing the diff
447 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800448 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200449 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800450 memcpy(bat_priv->tt.last_changeset,
451 tt_change, tt_change_len);
452 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200453 }
454 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200455 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800457container_register:
458 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
459 sizeof(*tt_data) + tt_change_len);
460 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461}
462
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200463int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464{
465 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200466 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200467 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200468 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100469 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200472 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100473 int last_seen_secs;
474 int last_seen_msecs;
475 unsigned long last_seen_jiffies;
476 bool no_purge;
477 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Marek Lindner30da63a2012-08-03 17:15:46 +0200479 primary_if = batadv_seq_print_text_primary_if_get(seq);
480 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200481 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100483 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200484 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100485 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
486 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100487 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
488 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 for (i = 0; i < hash->size; i++) {
491 head = &hash->table[i];
492
Marek Lindner7aadf882011-02-18 12:28:09 +0000493 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800494 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000495 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100496 tt_local = container_of(tt_common_entry,
497 struct batadv_tt_local_entry,
498 common);
499 last_seen_jiffies = jiffies - tt_local->last_seen;
500 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
501 last_seen_secs = last_seen_msecs / 1000;
502 last_seen_msecs = last_seen_msecs % 1000;
503
504 no_purge = tt_common_entry->flags & np_flag;
505
506 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100507 tt_common_entry->addr,
508 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200509 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100510 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100511 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100513 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200514 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100515 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100516 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100517 no_purge ? 0 : last_seen_secs,
518 no_purge ? 0 : last_seen_msecs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000520 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200522out:
523 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200524 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200525 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526}
527
Sven Eckelmann56303d32012-06-05 22:31:31 +0200528static void
529batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
530 struct batadv_tt_local_entry *tt_local_entry,
531 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200533 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Antonio Quartulli015758d2011-07-09 17:52:13 +0200535 /* The local client has to be marked as "pending to be removed" but has
536 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200537 * response issued before the net ttvn increment (consistency check)
538 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200539 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100540
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200541 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200542 "Local tt entry (%pM) pending to be removed: %s\n",
543 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544}
545
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200546/**
547 * batadv_tt_local_remove - logically remove an entry from the local table
548 * @bat_priv: the bat priv with all the soft interface information
549 * @addr: the MAC address of the client to remove
550 * @message: message to append to the log on deletion
551 * @roaming: true if the deletion is due to a roaming event
552 *
553 * Returns the flags assigned to the local entry before being deleted
554 */
555uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
556 const uint8_t *addr, const char *message,
557 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200559 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200560 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmanna5130882012-05-16 20:23:16 +0200562 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200563 if (!tt_local_entry)
564 goto out;
565
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200566 curr_flags = tt_local_entry->common.flags;
567
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200568 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200569 /* if this global entry addition is due to a roaming, the node has to
570 * mark the local entry as "roamed" in order to correctly reroute
571 * packets later
572 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200573 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200574 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200575 /* mark the local client as ROAMed */
576 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
577 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200578
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200579 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
580 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
581 message);
582 goto out;
583 }
584 /* if this client has been added right now, it is possible to
585 * immediately purge it
586 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200587 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200588 hlist_del_rcu(&tt_local_entry->common.hash_entry);
589 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200590
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200591out:
592 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200593 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200594
595 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596}
597
Sven Eckelmann56303d32012-06-05 22:31:31 +0200598static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200599 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601 struct batadv_tt_local_entry *tt_local_entry;
602 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800603 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200604
Sasha Levinb67bfe02013-02-27 17:06:00 -0800605 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200606 hash_entry) {
607 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200608 struct batadv_tt_local_entry,
609 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200610 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
611 continue;
612
613 /* entry already marked for deletion */
614 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
615 continue;
616
617 if (!batadv_has_timed_out(tt_local_entry->last_seen,
618 BATADV_TT_LOCAL_TIMEOUT))
619 continue;
620
621 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
622 BATADV_TT_CLIENT_DEL, "timed out");
623 }
624}
625
Sven Eckelmann56303d32012-06-05 22:31:31 +0200626static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200627{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200628 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200630 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200631 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 for (i = 0; i < hash->size; i++) {
634 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200635 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200637 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200638 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641}
642
Sven Eckelmann56303d32012-06-05 22:31:31 +0200643static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200645 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200646 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200647 struct batadv_tt_common_entry *tt_common_entry;
648 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800649 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200650 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200651 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200652
Sven Eckelmann807736f2012-07-15 22:26:51 +0200653 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 return;
655
Sven Eckelmann807736f2012-07-15 22:26:51 +0200656 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200657
658 for (i = 0; i < hash->size; i++) {
659 head = &hash->table[i];
660 list_lock = &hash->list_locks[i];
661
662 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800663 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200664 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800665 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200666 tt_local = container_of(tt_common_entry,
667 struct batadv_tt_local_entry,
668 common);
669 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200670 }
671 spin_unlock_bh(list_lock);
672 }
673
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200674 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200675
Sven Eckelmann807736f2012-07-15 22:26:51 +0200676 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677}
678
Sven Eckelmann56303d32012-06-05 22:31:31 +0200679static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200681 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200682 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
Sven Eckelmann807736f2012-07-15 22:26:51 +0200684 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Sven Eckelmann807736f2012-07-15 22:26:51 +0200686 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200687 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688
Antonio Quartullidec05072012-11-10 11:00:32 +0100689 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
690 &batadv_tt_global_hash_lock_class_key);
691
Sven Eckelmann5346c352012-05-05 13:27:28 +0200692 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693}
694
Sven Eckelmann56303d32012-06-05 22:31:31 +0200695static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200697 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200698
Sven Eckelmann807736f2012-07-15 22:26:51 +0200699 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200700
Sven Eckelmann807736f2012-07-15 22:26:51 +0200701 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200702 list) {
703 list_del(&entry->list);
704 kfree(entry);
705 }
706
Sven Eckelmann807736f2012-07-15 22:26:51 +0200707 atomic_set(&bat_priv->tt.local_changes, 0);
708 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200709}
710
Antonio Quartullid657e622012-07-01 14:09:12 +0200711/* retrieves the orig_tt_list_entry belonging to orig_node from the
712 * batadv_tt_global_entry list
713 *
714 * returns it with an increased refcounter, NULL if not found
715 */
716static struct batadv_tt_orig_list_entry *
717batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
718 const struct batadv_orig_node *orig_node)
719{
720 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
721 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +0200722
723 rcu_read_lock();
724 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800725 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +0200726 if (tmp_orig_entry->orig_node != orig_node)
727 continue;
728 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
729 continue;
730
731 orig_entry = tmp_orig_entry;
732 break;
733 }
734 rcu_read_unlock();
735
736 return orig_entry;
737}
738
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200739/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200740 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200742static bool
743batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
744 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200745{
Antonio Quartullid657e622012-07-01 14:09:12 +0200746 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747 bool found = false;
748
Antonio Quartullid657e622012-07-01 14:09:12 +0200749 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
750 if (orig_entry) {
751 found = true;
752 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200754
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200755 return found;
756}
757
Sven Eckelmanna5130882012-05-16 20:23:16 +0200758static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200759batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200760 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200761{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200762 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200763
Antonio Quartullid657e622012-07-01 14:09:12 +0200764 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200765 if (orig_entry) {
766 /* refresh the ttvn: the current value could be a bogus one that
767 * was added during a "temporary client detection"
768 */
769 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200770 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200771 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200772
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200773 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
774 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200775 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200776
777 INIT_HLIST_NODE(&orig_entry->list);
778 atomic_inc(&orig_node->refcount);
779 atomic_inc(&orig_node->tt_size);
780 orig_entry->orig_node = orig_node;
781 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200782 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200783
Antonio Quartullid657e622012-07-01 14:09:12 +0200784 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200785 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200786 &tt_global->orig_list);
787 spin_unlock_bh(&tt_global->list_lock);
788out:
789 if (orig_entry)
790 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200791}
792
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200793/**
794 * batadv_tt_global_add - add a new TT global entry or update an existing one
795 * @bat_priv: the bat priv with all the soft interface information
796 * @orig_node: the originator announcing the client
797 * @tt_addr: the mac address of the non-mesh client
798 * @flags: TT flags that have to be set for this non-mesh client
799 * @ttvn: the tt version number ever announcing this non-mesh client
800 *
801 * Add a new TT global entry for the given originator. If the entry already
802 * exists add a new reference to the given originator (a global entry can have
803 * references to multiple originators) and adjust the flags attribute to reflect
804 * the function argument.
805 * If a TT local entry exists for this non-mesh client remove it.
806 *
807 * The caller must hold orig_node refcount.
808 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200809int batadv_tt_global_add(struct batadv_priv *bat_priv,
810 struct batadv_orig_node *orig_node,
Antonio Quartullid4ff40f2013-04-18 15:13:01 +0200811 const unsigned char *tt_addr, uint16_t flags,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200812 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200814 struct batadv_tt_global_entry *tt_global_entry;
815 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200816 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100817 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200818 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200819 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820
Sven Eckelmanna5130882012-05-16 20:23:16 +0200821 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200822 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
823
824 /* if the node already has a local client for this entry, it has to wait
825 * for a roaming advertisement instead of manually messing up the global
826 * table
827 */
828 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
829 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
830 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831
Antonio Quartullia73105b2011-04-27 14:27:44 +0200832 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200833 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200834 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200835 goto out;
836
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200837 common = &tt_global_entry->common;
838 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839
Antonio Quartullid4f44692012-05-25 00:00:54 +0200840 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200841 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200842 /* node must store current time in case of roaming. This is
843 * needed to purge this entry out on timeout (if nobody claims
844 * it)
845 */
846 if (flags & BATADV_TT_CLIENT_ROAM)
847 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200848 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200849 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200850
851 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
852 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200853
Sven Eckelmann807736f2012-07-15 22:26:51 +0200854 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200855 batadv_compare_tt,
856 batadv_choose_orig, common,
857 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100858
859 if (unlikely(hash_added != 0)) {
860 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200861 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100862 goto out_remove;
863 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200864 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200865 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200866 /* If there is already a global entry, we can use this one for
867 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200868 * But if we are trying to add a temporary client then here are
869 * two options at this point:
870 * 1) the global client is not a temporary client: the global
871 * client has to be left as it is, temporary information
872 * should never override any already known client state
873 * 2) the global client is a temporary client: purge the
874 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200875 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200876 if (flags & BATADV_TT_CLIENT_TEMP) {
877 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
878 goto out;
879 if (batadv_tt_global_entry_has_orig(tt_global_entry,
880 orig_node))
881 goto out_remove;
882 batadv_tt_global_del_orig_list(tt_global_entry);
883 goto add_orig_entry;
884 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200885
886 /* if the client was temporary added before receiving the first
887 * OGM announcing it, we have to clear the TEMP flag
888 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200889 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200890
Antonio Quartullie9c001362012-11-07 15:05:33 +0100891 /* the change can carry possible "attribute" flags like the
892 * TT_CLIENT_WIFI, therefore they have to be copied in the
893 * client entry
894 */
895 tt_global_entry->common.flags |= flags;
896
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200897 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
898 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200899 * delete + roaming change for this originator.
900 *
901 * We should first delete the old originator before adding the
902 * new one.
903 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200904 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200905 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200906 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200907 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908 }
909 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200910add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200911 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200912 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200913
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200914 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200915 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200916 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200917 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200918
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100919out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200920
Antonio Quartullia73105b2011-04-27 14:27:44 +0200921 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200922 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
923 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +1000924 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200925 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
926
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200927 if (!(flags & BATADV_TT_CLIENT_ROAM))
928 /* this is a normal global add. Therefore the client is not in a
929 * roaming state anymore.
930 */
931 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
932
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200933out:
934 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200935 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200936 if (tt_local_entry)
937 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200938 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939}
940
Sven Eckelmann981d8902012-10-07 13:34:15 +0200941/* batadv_transtable_best_orig - Get best originator list entry from tt entry
942 * @tt_global_entry: global translation table entry to be analyzed
943 *
944 * This functon assumes the caller holds rcu_read_lock().
945 * Returns best originator list entry or NULL on errors.
946 */
947static struct batadv_tt_orig_list_entry *
948batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
949{
950 struct batadv_neigh_node *router = NULL;
951 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200952 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
953 int best_tq = 0;
954
955 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800956 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200957 router = batadv_orig_node_get_router(orig_entry->orig_node);
958 if (!router)
959 continue;
960
961 if (router->tq_avg > best_tq) {
962 best_entry = orig_entry;
963 best_tq = router->tq_avg;
964 }
965
966 batadv_neigh_node_free_ref(router);
967 }
968
969 return best_entry;
970}
971
972/* batadv_tt_global_print_entry - print all orig nodes who announce the address
973 * for this global entry
974 * @tt_global_entry: global translation table entry to be printed
975 * @seq: debugfs table seq_file struct
976 *
977 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200978 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200979static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200980batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200981 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200982{
983 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200984 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200985 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200986 uint16_t flags;
987 uint8_t last_ttvn;
988
989 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200990 flags = tt_common_entry->flags;
991
992 best_entry = batadv_transtable_best_orig(tt_global_entry);
993 if (best_entry) {
994 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100995 seq_printf(seq,
Antonio Quartulliced72932013-04-24 16:37:51 +0200996 " %c %pM (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200997 '*', tt_global_entry->common.addr,
998 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100999 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001000 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1001 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1002 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1003 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001004
1005 head = &tt_global_entry->orig_list;
1006
Sasha Levinb67bfe02013-02-27 17:06:00 -08001007 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001008 if (best_entry == orig_entry)
1009 continue;
1010
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001011 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001012 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1013 '+', tt_global_entry->common.addr,
1014 orig_entry->ttvn, orig_entry->orig_node->orig,
1015 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001016 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001017 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1018 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001019 }
1020}
1021
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001022int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023{
1024 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001025 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001026 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001027 struct batadv_tt_common_entry *tt_common_entry;
1028 struct batadv_tt_global_entry *tt_global;
1029 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001031 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032
Marek Lindner30da63a2012-08-03 17:15:46 +02001033 primary_if = batadv_seq_print_text_primary_if_get(seq);
1034 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001035 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001037 seq_printf(seq,
1038 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039 net_dev->name);
Antonio Quartulliced72932013-04-24 16:37:51 +02001040 seq_printf(seq, " %-13s %s %-15s %s (%-10s) %s\n",
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001041 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1042 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044 for (i = 0; i < hash->size; i++) {
1045 head = &hash->table[i];
1046
Marek Lindner7aadf882011-02-18 12:28:09 +00001047 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001048 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001049 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001050 tt_global = container_of(tt_common_entry,
1051 struct batadv_tt_global_entry,
1052 common);
1053 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001055 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001056 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001057out:
1058 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001059 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001060 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061}
1062
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001063/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001064static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001065batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001066{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001067 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001068 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001070
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001071 spin_lock_bh(&tt_global_entry->list_lock);
1072 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1074 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001075 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001076 }
1077 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001078}
1079
Sven Eckelmanna5130882012-05-16 20:23:16 +02001080static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001081batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1082 struct batadv_tt_global_entry *tt_global_entry,
1083 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001084 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001085{
1086 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001088 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001089
1090 spin_lock_bh(&tt_global_entry->list_lock);
1091 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001093 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001094 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001095 "Deleting %pM from global tt entry %pM: %s\n",
1096 orig_node->orig,
1097 tt_global_entry->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001098 hlist_del_rcu(&orig_entry->list);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001099 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001100 }
1101 }
1102 spin_unlock_bh(&tt_global_entry->list_lock);
1103}
1104
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001105/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001106 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1107 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001108 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001109static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001110batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1111 struct batadv_tt_global_entry *tt_global_entry,
1112 struct batadv_orig_node *orig_node,
1113 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001114{
1115 bool last_entry = true;
1116 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001117 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001118
1119 /* no local entry exists, case 1:
1120 * Check if this is the last one or if other entries exist.
1121 */
1122
1123 rcu_read_lock();
1124 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001125 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001126 if (orig_entry->orig_node != orig_node) {
1127 last_entry = false;
1128 break;
1129 }
1130 }
1131 rcu_read_unlock();
1132
1133 if (last_entry) {
1134 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001135 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001136 tt_global_entry->roam_at = jiffies;
1137 } else
1138 /* there is another entry, we can simply delete this
1139 * one and can still use the other one.
1140 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001141 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1142 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001143}
1144
1145
1146
Sven Eckelmann56303d32012-06-05 22:31:31 +02001147static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1148 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001149 const unsigned char *addr,
1150 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001152 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154
Sven Eckelmanna5130882012-05-16 20:23:16 +02001155 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001156 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001157 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001158
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001159 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001160 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1161 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001162
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001163 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001164 batadv_tt_global_free(bat_priv, tt_global_entry,
1165 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001166
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001167 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001168 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001169
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 /* if we are deleting a global entry due to a roam
1171 * event, there are two possibilities:
1172 * 1) the client roamed from node A to node B => if there
1173 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001174 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001175 * wait for node B to claim it. In case of timeout
1176 * the entry is purged.
1177 *
1178 * If there are other originators left, we directly delete
1179 * the originator.
1180 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001181 * the global entry, since it is useless now.
1182 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001183 local_entry = batadv_tt_local_hash_find(bat_priv,
1184 tt_global_entry->common.addr);
1185 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001186 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001187 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001188 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001189 } else
1190 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001191 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1192 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001193
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001194
Antonio Quartullicc47f662011-04-27 14:27:57 +02001195out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001196 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001197 batadv_tt_global_entry_free_ref(tt_global_entry);
1198 if (local_entry)
1199 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200}
1201
Sven Eckelmann56303d32012-06-05 22:31:31 +02001202void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1203 struct batadv_orig_node *orig_node,
1204 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001206 struct batadv_tt_global_entry *tt_global;
1207 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001208 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001209 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001210 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001211 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001212 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001213
Simon Wunderlich6e801492011-10-19 10:28:26 +02001214 if (!hash)
1215 return;
1216
Antonio Quartullia73105b2011-04-27 14:27:44 +02001217 for (i = 0; i < hash->size; i++) {
1218 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001219 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001220
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001221 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001222 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001223 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001224 tt_global = container_of(tt_common_entry,
1225 struct batadv_tt_global_entry,
1226 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001227
Sven Eckelmann56303d32012-06-05 22:31:31 +02001228 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001229 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001230
Sven Eckelmann56303d32012-06-05 22:31:31 +02001231 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001232 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001233 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001234 tt_global->common.addr, message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001235 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001236 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001237 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001238 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001239 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001240 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001241 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242}
1243
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001244static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1245 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001246{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001247 bool purge = false;
1248 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1249 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001250
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001251 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1252 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1253 purge = true;
1254 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001255 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001256
1257 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1258 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1259 purge = true;
1260 *msg = "Temporary client timeout\n";
1261 }
1262
1263 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001264}
1265
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001266static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001267{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001268 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001269 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001270 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001271 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001272 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001273 char *msg = NULL;
1274 struct batadv_tt_common_entry *tt_common;
1275 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001276
Antonio Quartullicc47f662011-04-27 14:27:57 +02001277 for (i = 0; i < hash->size; i++) {
1278 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001279 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001280
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001281 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001282 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001283 hash_entry) {
1284 tt_global = container_of(tt_common,
1285 struct batadv_tt_global_entry,
1286 common);
1287
1288 if (!batadv_tt_global_to_purge(tt_global, &msg))
1289 continue;
1290
1291 batadv_dbg(BATADV_DBG_TT, bat_priv,
1292 "Deleting global tt entry (%pM): %s\n",
1293 tt_global->common.addr, msg);
1294
Sasha Levinb67bfe02013-02-27 17:06:00 -08001295 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001296
1297 batadv_tt_global_entry_free_ref(tt_global);
1298 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001299 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001300 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001301}
1302
Sven Eckelmann56303d32012-06-05 22:31:31 +02001303static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001304{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001305 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001306 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001307 struct batadv_tt_common_entry *tt_common_entry;
1308 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001309 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001310 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001311 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001312
Sven Eckelmann807736f2012-07-15 22:26:51 +02001313 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001314 return;
1315
Sven Eckelmann807736f2012-07-15 22:26:51 +02001316 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001317
1318 for (i = 0; i < hash->size; i++) {
1319 head = &hash->table[i];
1320 list_lock = &hash->list_locks[i];
1321
1322 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001323 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001324 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001325 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001326 tt_global = container_of(tt_common_entry,
1327 struct batadv_tt_global_entry,
1328 common);
1329 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001330 }
1331 spin_unlock_bh(list_lock);
1332 }
1333
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001334 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001335
Sven Eckelmann807736f2012-07-15 22:26:51 +02001336 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001337}
1338
Sven Eckelmann56303d32012-06-05 22:31:31 +02001339static bool
1340_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1341 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001342{
1343 bool ret = false;
1344
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001345 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1346 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001347 ret = true;
1348
1349 return ret;
1350}
1351
Sven Eckelmann56303d32012-06-05 22:31:31 +02001352struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1353 const uint8_t *src,
1354 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001356 struct batadv_tt_local_entry *tt_local_entry = NULL;
1357 struct batadv_tt_global_entry *tt_global_entry = NULL;
1358 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001359 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001360
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001361 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001362 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001363 if (!tt_local_entry ||
1364 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001365 goto out;
1366 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001367
Sven Eckelmanna5130882012-05-16 20:23:16 +02001368 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001369 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001370 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001371
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001372 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001373 * isolation
1374 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001375 if (tt_local_entry &&
1376 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001377 goto out;
1378
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001379 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001380 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001381 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001382 if (best_entry)
1383 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001384 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1385 orig_node = NULL;
1386 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001387
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001388out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001389 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001390 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001391 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001392 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001393
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001394 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001395}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396
Antonio Quartulliced72932013-04-24 16:37:51 +02001397/**
1398 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1399 * to the given orig_node
1400 * @bat_priv: the bat priv with all the soft interface information
1401 */
1402static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001403 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001405 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001406 struct batadv_tt_common_entry *tt_common;
1407 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001408 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001409 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001410
1411 for (i = 0; i < hash->size; i++) {
1412 head = &hash->table[i];
1413
1414 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001415 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001416 tt_global = container_of(tt_common,
1417 struct batadv_tt_global_entry,
1418 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 /* Roaming clients are in the global table for
1420 * consistency only. They don't have to be
1421 * taken into account while computing the
1422 * global crc
1423 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001424 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001425 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001426 /* Temporary clients have not been announced yet, so
1427 * they have to be skipped while computing the global
1428 * crc
1429 */
1430 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1431 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001432
1433 /* find out if this global entry is announced by this
1434 * originator
1435 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001436 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001437 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001438 continue;
1439
Antonio Quartulliced72932013-04-24 16:37:51 +02001440 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001441 }
1442 rcu_read_unlock();
1443 }
1444
Antonio Quartulliced72932013-04-24 16:37:51 +02001445 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446}
1447
Antonio Quartulliced72932013-04-24 16:37:51 +02001448/**
1449 * batadv_tt_local_crc - calculates the checksum of the local table
1450 * @bat_priv: the bat priv with all the soft interface information
1451 */
1452static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001453{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001454 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001455 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456 struct hlist_head *head;
Antonio Quartulliced72932013-04-24 16:37:51 +02001457 uint32_t i, crc = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001458
1459 for (i = 0; i < hash->size; i++) {
1460 head = &hash->table[i];
1461
1462 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001463 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001464 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001465 * account while computing the CRC
1466 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001467 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001468 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02001469
1470 crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472 rcu_read_unlock();
1473 }
1474
Antonio Quartulliced72932013-04-24 16:37:51 +02001475 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476}
1477
Sven Eckelmann56303d32012-06-05 22:31:31 +02001478static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001479{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001480 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481
Sven Eckelmann807736f2012-07-15 22:26:51 +02001482 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483
Sven Eckelmann807736f2012-07-15 22:26:51 +02001484 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001485 list_del(&node->list);
1486 kfree(node);
1487 }
1488
Sven Eckelmann807736f2012-07-15 22:26:51 +02001489 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490}
1491
Sven Eckelmann56303d32012-06-05 22:31:31 +02001492static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1493 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001494 const unsigned char *tt_buff,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001495 uint16_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001497 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001498
1499 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001500 * last OGM (the OGM could carry no changes)
1501 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001502 spin_lock_bh(&orig_node->tt_buff_lock);
1503 if (tt_buff_len > 0) {
1504 kfree(orig_node->tt_buff);
1505 orig_node->tt_buff_len = 0;
1506 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1507 if (orig_node->tt_buff) {
1508 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1509 orig_node->tt_buff_len = tt_buff_len;
1510 }
1511 }
1512 spin_unlock_bh(&orig_node->tt_buff_lock);
1513}
1514
Sven Eckelmann56303d32012-06-05 22:31:31 +02001515static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001516{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001517 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518
Sven Eckelmann807736f2012-07-15 22:26:51 +02001519 spin_lock_bh(&bat_priv->tt.req_list_lock);
1520 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001521 if (batadv_has_timed_out(node->issued_at,
1522 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001523 list_del(&node->list);
1524 kfree(node);
1525 }
1526 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001527 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528}
1529
1530/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001531 * has already been issued for this orig_node, NULL otherwise
1532 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001533static struct batadv_tt_req_node *
1534batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1535 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001536{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001537 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001538
Sven Eckelmann807736f2012-07-15 22:26:51 +02001539 spin_lock_bh(&bat_priv->tt.req_list_lock);
1540 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001541 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1542 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001543 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001544 goto unlock;
1545 }
1546
1547 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1548 if (!tt_req_node)
1549 goto unlock;
1550
1551 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1552 tt_req_node->issued_at = jiffies;
1553
Sven Eckelmann807736f2012-07-15 22:26:51 +02001554 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001556 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001557 return tt_req_node;
1558}
1559
Marek Lindner335fbe02013-04-23 21:40:02 +08001560/**
1561 * batadv_tt_local_valid - verify that given tt entry is a valid one
1562 * @entry_ptr: to be checked local tt entry
1563 * @data_ptr: not used but definition required to satisfy the callback prototype
1564 *
1565 * Returns 1 if the entry is a valid, 0 otherwise.
1566 */
1567static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001568{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001569 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001570
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001571 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001572 return 0;
1573 return 1;
1574}
1575
Sven Eckelmanna5130882012-05-16 20:23:16 +02001576static int batadv_tt_global_valid(const void *entry_ptr,
1577 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001579 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1580 const struct batadv_tt_global_entry *tt_global_entry;
1581 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001582
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001583 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1584 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001585 return 0;
1586
Sven Eckelmann56303d32012-06-05 22:31:31 +02001587 tt_global_entry = container_of(tt_common_entry,
1588 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001589 common);
1590
Sven Eckelmanna5130882012-05-16 20:23:16 +02001591 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001592}
1593
Marek Lindner335fbe02013-04-23 21:40:02 +08001594/**
1595 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1596 * tt entries from the specified tt hash
1597 * @bat_priv: the bat priv with all the soft interface information
1598 * @hash: hash table containing the tt entries
1599 * @tt_len: expected tvlv tt data buffer length in number of bytes
1600 * @valid_cb: function to filter tt change entries
1601 * @cb_data: data passed to the filter function as argument
1602 *
1603 * Returns pointer to allocated tvlv tt data buffer if operation was
1604 * successful or NULL otherwise.
1605 */
1606static struct batadv_tvlv_tt_data *
1607batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1608 struct batadv_hashtable *hash, uint16_t tt_len,
1609 int (*valid_cb)(const void *, const void *),
1610 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001612 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08001613 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1614 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08001616 uint16_t tt_tot, tt_num_entries = 0;
1617 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001618 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619
Marek Lindner335fbe02013-04-23 21:40:02 +08001620 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1621 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1622 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624
Marek Lindner335fbe02013-04-23 21:40:02 +08001625 tt_tot = tt_len / sizeof(struct batadv_tvlv_tt_change);
1626
1627 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1628 GFP_ATOMIC);
1629 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630 goto out;
1631
Marek Lindner335fbe02013-04-23 21:40:02 +08001632 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001633
1634 rcu_read_lock();
1635 for (i = 0; i < hash->size; i++) {
1636 head = &hash->table[i];
1637
Sasha Levinb67bfe02013-02-27 17:06:00 -08001638 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08001640 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641 break;
1642
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001643 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644 continue;
1645
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001646 memcpy(tt_change->addr, tt_common_entry->addr,
1647 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001648 tt_change->flags = tt_common_entry->flags;
Marek Lindner335fbe02013-04-23 21:40:02 +08001649 tt_change->reserved = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001650
Marek Lindner335fbe02013-04-23 21:40:02 +08001651 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001652 tt_change++;
1653 }
1654 }
1655 rcu_read_unlock();
1656
1657out:
Marek Lindner335fbe02013-04-23 21:40:02 +08001658 return tvlv_tt_data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659}
1660
Antonio Quartulliced72932013-04-24 16:37:51 +02001661/**
1662 * batadv_send_tt_request - send a TT Request message to a given node
1663 * @bat_priv: the bat priv with all the soft interface information
1664 * @dst_orig_node: the destination of the message
1665 * @ttvn: the version number that the source of the message is looking for
1666 * @tt_crc: the CRC associated with the version number
1667 * @full_table: ask for the entire translation table if true, while only for the
1668 * last TT diff otherwise
1669 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001670static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1671 struct batadv_orig_node *dst_orig_node,
Antonio Quartulliced72932013-04-24 16:37:51 +02001672 uint8_t ttvn, uint32_t tt_crc,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001673 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001674{
Marek Lindner335fbe02013-04-23 21:40:02 +08001675 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001676 struct batadv_hard_iface *primary_if;
1677 struct batadv_tt_req_node *tt_req_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001678 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001680 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001681 if (!primary_if)
1682 goto out;
1683
1684 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001685 * reply from the same orig_node yet
1686 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001687 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688 if (!tt_req_node)
1689 goto out;
1690
Marek Lindner335fbe02013-04-23 21:40:02 +08001691 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1692 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001693 goto out;
1694
Marek Lindner335fbe02013-04-23 21:40:02 +08001695 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1696 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulliced72932013-04-24 16:37:51 +02001697 tvlv_tt_data->crc = htonl(tt_crc);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698
1699 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001700 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001702 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001703 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001705 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08001706 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1707 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1708 tvlv_tt_data, sizeof(*tvlv_tt_data));
1709 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001710
1711out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001713 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001715 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 kfree(tt_req_node);
1719 }
Marek Lindner335fbe02013-04-23 21:40:02 +08001720 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721 return ret;
1722}
1723
Marek Lindner335fbe02013-04-23 21:40:02 +08001724/**
1725 * batadv_send_other_tt_response - send reply to tt request concerning another
1726 * node's translation table
1727 * @bat_priv: the bat priv with all the soft interface information
1728 * @tt_data: tt data containing the tt request information
1729 * @req_src: mac address of tt request sender
1730 * @req_dst: mac address of tt request recipient
1731 *
1732 * Returns true if tt request reply was sent, false otherwise.
1733 */
1734static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1735 struct batadv_tvlv_tt_data *tt_data,
1736 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001737{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001738 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001739 struct batadv_orig_node *res_dst_orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001740 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1741 uint8_t orig_ttvn, req_ttvn;
1742 uint16_t tt_len;
1743 bool ret = false, full_table;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001745 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001746 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001747 req_src, tt_data->ttvn, req_dst,
1748 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001749
1750 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08001751 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001752 if (!req_dst_orig_node)
1753 goto out;
1754
Marek Lindner335fbe02013-04-23 21:40:02 +08001755 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001756 if (!res_dst_orig_node)
1757 goto out;
1758
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001760 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761
Marek Lindner335fbe02013-04-23 21:40:02 +08001762 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 if (orig_ttvn != req_ttvn ||
Antonio Quartulliced72932013-04-24 16:37:51 +02001764 tt_data->crc != htonl(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 goto out;
1766
Antonio Quartulli015758d2011-07-09 17:52:13 +02001767 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08001768 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001769 !req_dst_orig_node->tt_buff)
1770 full_table = true;
1771 else
1772 full_table = false;
1773
Marek Lindner335fbe02013-04-23 21:40:02 +08001774 /* TT fragmentation hasn't been implemented yet, so send as many
1775 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001776 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001777 if (!full_table) {
1778 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1779 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Marek Lindner335fbe02013-04-23 21:40:02 +08001781 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1782 GFP_ATOMIC);
1783 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 goto unlock;
1785
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786 /* Copy the last orig_node's OGM buffer */
Marek Lindner335fbe02013-04-23 21:40:02 +08001787 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1790 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001791 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
Marek Lindner335fbe02013-04-23 21:40:02 +08001792 tt_len = batadv_tt_len(tt_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793
Marek Lindner335fbe02013-04-23 21:40:02 +08001794 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1795 bat_priv->tt.global_hash,
1796 tt_len,
1797 batadv_tt_global_valid,
1798 req_dst_orig_node);
1799 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001800 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801 }
1802
Marek Lindner335fbe02013-04-23 21:40:02 +08001803 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1804 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805
1806 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001807 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001809 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001810 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1811 res_dst_orig_node->orig, req_dst_orig_node->orig,
1812 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001813
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001814 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001815
Marek Lindner335fbe02013-04-23 21:40:02 +08001816 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1817 req_src, BATADV_TVLV_TT, 1,
1818 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001819
Marek Lindner335fbe02013-04-23 21:40:02 +08001820 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001821 goto out;
1822
1823unlock:
1824 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1825
1826out:
1827 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001828 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001830 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08001831 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001833}
Sven Eckelmann96412692012-06-05 22:31:30 +02001834
Marek Lindner335fbe02013-04-23 21:40:02 +08001835/**
1836 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1837 * translation table
1838 * @bat_priv: the bat priv with all the soft interface information
1839 * @tt_data: tt data containing the tt request information
1840 * @req_src: mac address of tt request sender
1841 *
1842 * Returns true if tt request reply was sent, false otherwise.
1843 */
1844static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1845 struct batadv_tvlv_tt_data *tt_data,
1846 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001847{
Marek Lindner335fbe02013-04-23 21:40:02 +08001848 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann170173b2012-10-07 12:02:22 +02001849 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001850 struct batadv_hard_iface *primary_if = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08001851 uint8_t my_ttvn, req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852 bool full_table;
Marek Lindner335fbe02013-04-23 21:40:02 +08001853 uint16_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001854
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001855 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001856 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08001857 req_src, tt_data->ttvn,
1858 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859
1860
Sven Eckelmann807736f2012-07-15 22:26:51 +02001861 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08001862 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863
Marek Lindner335fbe02013-04-23 21:40:02 +08001864 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865 if (!orig_node)
1866 goto out;
1867
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001868 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869 if (!primary_if)
1870 goto out;
1871
1872 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001873 * is too big send the whole local translation table
1874 */
Marek Lindner335fbe02013-04-23 21:40:02 +08001875 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001876 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001877 full_table = true;
1878 else
1879 full_table = false;
1880
Marek Lindner335fbe02013-04-23 21:40:02 +08001881 /* TT fragmentation hasn't been implemented yet, so send as many
1882 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001883 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001884 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001885 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1886 tt_len = bat_priv->tt.last_changeset_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001887
Marek Lindner335fbe02013-04-23 21:40:02 +08001888 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1889 GFP_ATOMIC);
1890 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891 goto unlock;
1892
Marek Lindner335fbe02013-04-23 21:40:02 +08001893 /* Copy the last orig_node's OGM buffer */
1894 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001895 bat_priv->tt.last_changeset_len);
1896 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001897 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001898 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Marek Lindner335fbe02013-04-23 21:40:02 +08001899 tt_len = batadv_tt_len(tt_len);
1900 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001901
Marek Lindner335fbe02013-04-23 21:40:02 +08001902 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1903 bat_priv->tt.local_hash,
1904 tt_len,
1905 batadv_tt_local_valid,
1906 NULL);
1907 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909 }
1910
Marek Lindner335fbe02013-04-23 21:40:02 +08001911 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1912 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001913
1914 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08001915 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001916
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001917 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001918 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1919 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001921 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001922
Marek Lindner335fbe02013-04-23 21:40:02 +08001923 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1924 req_src, BATADV_TVLV_TT, 1,
1925 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1926
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927 goto out;
1928
1929unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001930 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001931out:
1932 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001933 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001935 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08001936 kfree(tvlv_tt_data);
1937 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001938 return true;
1939}
1940
Marek Lindner335fbe02013-04-23 21:40:02 +08001941/**
1942 * batadv_send_tt_response - send reply to tt request
1943 * @bat_priv: the bat priv with all the soft interface information
1944 * @tt_data: tt data containing the tt request information
1945 * @req_src: mac address of tt request sender
1946 * @req_dst: mac address of tt request recipient
1947 *
1948 * Returns true if tt request reply was sent, false otherwise.
1949 */
1950static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1951 struct batadv_tvlv_tt_data *tt_data,
1952 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953{
Marek Lindner335fbe02013-04-23 21:40:02 +08001954 if (batadv_is_my_mac(bat_priv, req_dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001955 /* don't answer backbone gws! */
Marek Lindner335fbe02013-04-23 21:40:02 +08001956 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001957 return true;
1958
Marek Lindner335fbe02013-04-23 21:40:02 +08001959 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001960 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08001961 return batadv_send_other_tt_response(bat_priv, tt_data,
1962 req_src, req_dst);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001963 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964}
1965
Sven Eckelmann56303d32012-06-05 22:31:31 +02001966static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1967 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08001968 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001969 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970{
1971 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001972 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001973
1974 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001975 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1976 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001977 batadv_tt_global_del(bat_priv, orig_node,
1978 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001979 "tt removed by changes",
1980 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001981 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001982 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001983 (tt_change + i)->addr,
1984 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001985 /* In case of problem while storing a
1986 * global_entry, we stop the updating
1987 * procedure without committing the
1988 * ttvn change. This will avoid to send
1989 * corrupted data on tt_request
1990 */
1991 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001992 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001993 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001994 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001995}
1996
Sven Eckelmann56303d32012-06-05 22:31:31 +02001997static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08001998 struct batadv_tvlv_tt_data *tt_data,
1999 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002000{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002001 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002002
Marek Lindner335fbe02013-04-23 21:40:02 +08002003 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002004 if (!orig_node)
2005 goto out;
2006
2007 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002008 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009
Sven Eckelmanna5130882012-05-16 20:23:16 +02002010 _batadv_tt_update_changes(bat_priv, orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002011 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2012 num_entries, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013
2014 spin_lock_bh(&orig_node->tt_buff_lock);
2015 kfree(orig_node->tt_buff);
2016 orig_node->tt_buff_len = 0;
2017 orig_node->tt_buff = NULL;
2018 spin_unlock_bh(&orig_node->tt_buff_lock);
2019
Marek Lindner335fbe02013-04-23 21:40:02 +08002020 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021
2022out:
2023 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002024 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025}
2026
Sven Eckelmann56303d32012-06-05 22:31:31 +02002027static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2028 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002029 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002030 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002032 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2033 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002034
Sven Eckelmanna5130882012-05-16 20:23:16 +02002035 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2036 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002037 atomic_set(&orig_node->last_ttvn, ttvn);
2038}
2039
Sven Eckelmann56303d32012-06-05 22:31:31 +02002040bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002041{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002042 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002043 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002044
Sven Eckelmanna5130882012-05-16 20:23:16 +02002045 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002046 if (!tt_local_entry)
2047 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002048 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002049 * consistency purpose)
2050 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002051 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2052 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002053 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002054 ret = true;
2055out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002056 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002057 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002058 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059}
2060
Marek Lindner335fbe02013-04-23 21:40:02 +08002061/**
2062 * batadv_handle_tt_response - process incoming tt reply
2063 * @bat_priv: the bat priv with all the soft interface information
2064 * @tt_data: tt data containing the tt request information
2065 * @resp_src: mac address of tt reply sender
2066 * @num_entries: number of tt change entries appended to the tt data
2067 */
2068static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2069 struct batadv_tvlv_tt_data *tt_data,
2070 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002072 struct batadv_tt_req_node *node, *safe;
2073 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002074 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002075
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002076 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002077 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002078 resp_src, tt_data->ttvn, num_entries,
2079 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002081 /* we should have never asked a backbone gw */
Marek Lindner335fbe02013-04-23 21:40:02 +08002082 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002083 goto out;
2084
Marek Lindner335fbe02013-04-23 21:40:02 +08002085 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002086 if (!orig_node)
2087 goto out;
2088
Marek Lindner335fbe02013-04-23 21:40:02 +08002089 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2090 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002091 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002092 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2093 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2094 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002095 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096
2097 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002098 spin_lock_bh(&bat_priv->tt.req_list_lock);
2099 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002100 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101 continue;
2102 list_del(&node->list);
2103 kfree(node);
2104 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002105 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106
2107 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002108 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002109out:
2110 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002111 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112}
2113
Sven Eckelmann56303d32012-06-05 22:31:31 +02002114static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002115{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002116 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002117
Sven Eckelmann807736f2012-07-15 22:26:51 +02002118 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002119
Sven Eckelmann807736f2012-07-15 22:26:51 +02002120 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002121 list_del(&node->list);
2122 kfree(node);
2123 }
2124
Sven Eckelmann807736f2012-07-15 22:26:51 +02002125 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002126}
2127
Sven Eckelmann56303d32012-06-05 22:31:31 +02002128static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002129{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002130 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002131
Sven Eckelmann807736f2012-07-15 22:26:51 +02002132 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2133 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002134 if (!batadv_has_timed_out(node->first_time,
2135 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002136 continue;
2137
2138 list_del(&node->list);
2139 kfree(node);
2140 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002141 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002142}
2143
2144/* This function checks whether the client already reached the
2145 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2146 * will not be sent.
2147 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002148 * returns true if the ROAMING_ADV can be sent, false otherwise
2149 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002150static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002151 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002152{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002153 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002154 bool ret = false;
2155
Sven Eckelmann807736f2012-07-15 22:26:51 +02002156 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002157 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002158 * reply from the same orig_node yet
2159 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002160 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002161 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002162 continue;
2163
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002164 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002165 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002166 continue;
2167
Sven Eckelmann3e348192012-05-16 20:23:22 +02002168 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002169 /* Sorry, you roamed too many times! */
2170 goto unlock;
2171 ret = true;
2172 break;
2173 }
2174
2175 if (!ret) {
2176 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2177 if (!tt_roam_node)
2178 goto unlock;
2179
2180 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002181 atomic_set(&tt_roam_node->counter,
2182 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002183 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2184
Sven Eckelmann807736f2012-07-15 22:26:51 +02002185 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002186 ret = true;
2187 }
2188
2189unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002190 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002191 return ret;
2192}
2193
Sven Eckelmann56303d32012-06-05 22:31:31 +02002194static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2195 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002196{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002197 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002198 struct batadv_tvlv_roam_adv tvlv_roam;
2199
2200 primary_if = batadv_primary_if_get_selected(bat_priv);
2201 if (!primary_if)
2202 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002203
2204 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002205 * already roamed to us too many times
2206 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002207 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208 goto out;
2209
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002210 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002211 "Sending ROAMING_ADV to %pM (client %pM)\n",
2212 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002213
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002214 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002215
Marek Lindner122edaa2013-04-23 21:40:03 +08002216 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2217 tvlv_roam.reserved = 0;
2218
2219 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2220 orig_node->orig, BATADV_TVLV_ROAM, 1,
2221 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002222
2223out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002224 if (primary_if)
2225 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002226}
2227
Sven Eckelmanna5130882012-05-16 20:23:16 +02002228static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002229{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002230 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002231 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002232 struct batadv_priv *bat_priv;
2233
2234 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002235 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2236 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002237
Sven Eckelmanna5130882012-05-16 20:23:16 +02002238 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002239 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002240 batadv_tt_req_purge(bat_priv);
2241 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002242
Antonio Quartulli72414442012-12-25 13:14:37 +01002243 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2244 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002245}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002246
Sven Eckelmann56303d32012-06-05 22:31:31 +02002247void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002248{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002249 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2250 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2251
Sven Eckelmann807736f2012-07-15 22:26:51 +02002252 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002253
Sven Eckelmanna5130882012-05-16 20:23:16 +02002254 batadv_tt_local_table_free(bat_priv);
2255 batadv_tt_global_table_free(bat_priv);
2256 batadv_tt_req_list_free(bat_priv);
2257 batadv_tt_changes_list_free(bat_priv);
2258 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002259
Sven Eckelmann807736f2012-07-15 22:26:51 +02002260 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002261}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002262
Antonio Quartulli697f2532011-11-07 16:47:01 +01002263/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002264 * in the given hash table and returns the number of modified entries
2265 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002266static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2267 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002268{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002269 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002270 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002271 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002272 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002273
2274 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002275 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002276
2277 for (i = 0; i < hash->size; i++) {
2278 head = &hash->table[i];
2279
2280 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002281 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002282 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002283 if (enable) {
2284 if ((tt_common_entry->flags & flags) == flags)
2285 continue;
2286 tt_common_entry->flags |= flags;
2287 } else {
2288 if (!(tt_common_entry->flags & flags))
2289 continue;
2290 tt_common_entry->flags &= ~flags;
2291 }
2292 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002293 }
2294 rcu_read_unlock();
2295 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002296out:
2297 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002298}
2299
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002300/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002301static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002302{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002303 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002304 struct batadv_tt_common_entry *tt_common;
2305 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002306 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002307 struct hlist_head *head;
2308 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002309 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002310
2311 if (!hash)
2312 return;
2313
2314 for (i = 0; i < hash->size; i++) {
2315 head = &hash->table[i];
2316 list_lock = &hash->list_locks[i];
2317
2318 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002319 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002320 hash_entry) {
2321 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002322 continue;
2323
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002324 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002325 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002326 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002327
Sven Eckelmann807736f2012-07-15 22:26:51 +02002328 atomic_dec(&bat_priv->tt.local_entry_num);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002329 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002330 tt_local = container_of(tt_common,
2331 struct batadv_tt_local_entry,
2332 common);
2333 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002334 }
2335 spin_unlock_bh(list_lock);
2336 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002337}
2338
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002339/**
2340 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2341 * have been queued in the time since the last commit
2342 * @bat_priv: the bat priv with all the soft interface information
2343 */
2344void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002345{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002346 uint16_t changed_num = 0;
2347
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002348 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2349 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2350 batadv_tt_tvlv_container_update(bat_priv);
2351 return;
2352 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002353
Sven Eckelmann807736f2012-07-15 22:26:51 +02002354 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002355 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002356
2357 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002358 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002359 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002360 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002361
2362 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002363 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002364 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002365 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002366 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002367
2368 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002369 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002370 batadv_tt_tvlv_container_update(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002371}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002372
Sven Eckelmann56303d32012-06-05 22:31:31 +02002373bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002374 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002375{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002376 struct batadv_tt_local_entry *tt_local_entry = NULL;
2377 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002378 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002379
2380 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002381 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002382
Sven Eckelmanna5130882012-05-16 20:23:16 +02002383 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002384 if (!tt_local_entry)
2385 goto out;
2386
Sven Eckelmanna5130882012-05-16 20:23:16 +02002387 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002388 if (!tt_global_entry)
2389 goto out;
2390
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002391 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002392 goto out;
2393
Marek Lindner5870adc2012-06-20 17:16:05 +02002394 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002395
2396out:
2397 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002398 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002399 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002400 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002401 return ret;
2402}
Marek Lindnera943cac2011-07-30 13:10:18 +02002403
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002404/**
2405 * batadv_tt_update_orig - update global translation table with new tt
2406 * information received via ogms
2407 * @bat_priv: the bat priv with all the soft interface information
2408 * @orig: the orig_node of the ogm
2409 * @tt_buff: buffer holding the tt information
2410 * @tt_num_changes: number of tt changes inside the tt buffer
2411 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02002412 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002413 */
2414static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2415 struct batadv_orig_node *orig_node,
2416 const unsigned char *tt_buff,
2417 uint16_t tt_num_changes, uint8_t ttvn,
Antonio Quartulliced72932013-04-24 16:37:51 +02002418 uint32_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002419{
2420 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2421 bool full_table = true;
Marek Lindner335fbe02013-04-23 21:40:02 +08002422 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002423
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002424 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002425 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002426 return;
2427
Antonio Quartulli17071572011-11-07 16:36:40 +01002428 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002429 * increased by one -> we can apply the attached changes
2430 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002431 if ((!orig_node->tt_initialised && ttvn == 1) ||
2432 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002433 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002434 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2435 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002436 * In this case send a tt request
2437 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002438 if (!tt_num_changes) {
2439 full_table = false;
2440 goto request_table;
2441 }
2442
Marek Lindner335fbe02013-04-23 21:40:02 +08002443 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002444 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002445 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002446
2447 /* Even if we received the precomputed crc with the OGM, we
2448 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002449 * in the global table
2450 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002451 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002452
2453 /* The ttvn alone is not enough to guarantee consistency
2454 * because a single value could represent different states
2455 * (due to the wrap around). Thus a node has to check whether
2456 * the resulting table (after applying the changes) is still
2457 * consistent or not. E.g. a node could disconnect while its
2458 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2459 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002460 * inconsistency
2461 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002462 if (orig_node->tt_crc != tt_crc)
2463 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002464 } else {
2465 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002466 * in sync anymore -> request fresh tt data
2467 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002468 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2469 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002470request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002471 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulliced72932013-04-24 16:37:51 +02002472 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002473 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2474 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002475 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2476 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002477 return;
2478 }
2479 }
2480}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002481
2482/* returns true whether we know that the client has moved from its old
2483 * originator to another one. This entry is kept is still kept for consistency
2484 * purposes
2485 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002486bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002487 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002488{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002489 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002490 bool ret = false;
2491
Sven Eckelmanna5130882012-05-16 20:23:16 +02002492 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002493 if (!tt_global_entry)
2494 goto out;
2495
Antonio Quartullic1d07432013-01-15 22:17:19 +10002496 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002497 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002498out:
2499 return ret;
2500}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002501
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002502/**
2503 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2504 * @bat_priv: the bat priv with all the soft interface information
2505 * @addr: the MAC address of the local client to query
2506 *
2507 * Returns true if the local client is known to be roaming (it is not served by
2508 * this node anymore) or not. If yes, the client is still present in the table
2509 * to keep the latter consistent with the node TTVN
2510 */
2511bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2512 uint8_t *addr)
2513{
2514 struct batadv_tt_local_entry *tt_local_entry;
2515 bool ret = false;
2516
2517 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2518 if (!tt_local_entry)
2519 goto out;
2520
2521 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2522 batadv_tt_local_entry_free_ref(tt_local_entry);
2523out:
2524 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002525}
2526
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002527bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2528 struct batadv_orig_node *orig_node,
2529 const unsigned char *addr)
2530{
2531 bool ret = false;
2532
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002533 /* if the originator is a backbone node (meaning it belongs to the same
2534 * LAN of this node) the temporary client must not be added because to
2535 * reach such destination the node must use the LAN instead of the mesh
2536 */
2537 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2538 goto out;
2539
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002540 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2541 BATADV_TT_CLIENT_TEMP,
2542 atomic_read(&orig_node->last_ttvn)))
2543 goto out;
2544
2545 batadv_dbg(BATADV_DBG_TT, bat_priv,
2546 "Added temporary global client (addr: %pM orig: %pM)\n",
2547 addr, orig_node->orig);
2548 ret = true;
2549out:
2550 return ret;
2551}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002552
2553/**
2554 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2555 * @bat_priv: the bat priv with all the soft interface information
2556 * @orig: the orig_node of the ogm
2557 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2558 * @tvlv_value: tvlv buffer containing the gateway data
2559 * @tvlv_value_len: tvlv buffer length
2560 */
2561static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2562 struct batadv_orig_node *orig,
2563 uint8_t flags,
2564 void *tvlv_value,
2565 uint16_t tvlv_value_len)
2566{
2567 struct batadv_tvlv_tt_data *tt_data;
2568 uint16_t num_entries;
2569
2570 if (tvlv_value_len < sizeof(*tt_data))
2571 return;
2572
2573 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2574 tvlv_value_len -= sizeof(*tt_data);
2575
2576 num_entries = tvlv_value_len / batadv_tt_len(1);
2577
2578 batadv_tt_update_orig(bat_priv, orig,
2579 (unsigned char *)(tt_data + 1),
Antonio Quartulliced72932013-04-24 16:37:51 +02002580 num_entries, tt_data->ttvn, ntohl(tt_data->crc));
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002581}
2582
2583/**
Marek Lindner335fbe02013-04-23 21:40:02 +08002584 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2585 * container
2586 * @bat_priv: the bat priv with all the soft interface information
2587 * @src: mac address of tt tvlv sender
2588 * @dst: mac address of tt tvlv recipient
2589 * @tvlv_value: tvlv buffer containing the tt data
2590 * @tvlv_value_len: tvlv buffer length
2591 *
2592 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2593 * otherwise.
2594 */
2595static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2596 uint8_t *src, uint8_t *dst,
2597 void *tvlv_value,
2598 uint16_t tvlv_value_len)
2599{
2600 struct batadv_tvlv_tt_data *tt_data;
2601 uint16_t num_entries;
2602 char tt_flag;
2603 bool ret;
2604
2605 if (tvlv_value_len < sizeof(*tt_data))
2606 return NET_RX_SUCCESS;
2607
2608 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2609 tvlv_value_len -= sizeof(*tt_data);
2610
2611 num_entries = tvlv_value_len / batadv_tt_len(1);
2612
2613 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2614 case BATADV_TT_REQUEST:
2615 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2616
2617 /* If this node cannot provide a TT response the tt_request is
2618 * forwarded
2619 */
2620 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2621 if (!ret) {
2622 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2623 tt_flag = 'F';
2624 else
2625 tt_flag = '.';
2626
2627 batadv_dbg(BATADV_DBG_TT, bat_priv,
2628 "Routing TT_REQUEST to %pM [%c]\n",
2629 dst, tt_flag);
2630 /* tvlv API will re-route the packet */
2631 return NET_RX_DROP;
2632 }
2633 break;
2634 case BATADV_TT_RESPONSE:
2635 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2636
2637 if (batadv_is_my_mac(bat_priv, dst)) {
2638 batadv_handle_tt_response(bat_priv, tt_data,
2639 src, num_entries);
2640 return NET_RX_SUCCESS;
2641 }
2642
2643 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2644 tt_flag = 'F';
2645 else
2646 tt_flag = '.';
2647
2648 batadv_dbg(BATADV_DBG_TT, bat_priv,
2649 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2650
2651 /* tvlv API will re-route the packet */
2652 return NET_RX_DROP;
2653 }
2654
2655 return NET_RX_SUCCESS;
2656}
2657
2658/**
Marek Lindner122edaa2013-04-23 21:40:03 +08002659 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
2660 * @bat_priv: the bat priv with all the soft interface information
2661 * @src: mac address of tt tvlv sender
2662 * @dst: mac address of tt tvlv recipient
2663 * @tvlv_value: tvlv buffer containing the tt data
2664 * @tvlv_value_len: tvlv buffer length
2665 *
2666 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
2667 * otherwise.
2668 */
2669static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2670 uint8_t *src, uint8_t *dst,
2671 void *tvlv_value,
2672 uint16_t tvlv_value_len)
2673{
2674 struct batadv_tvlv_roam_adv *roaming_adv;
2675 struct batadv_orig_node *orig_node = NULL;
2676
2677 /* If this node is not the intended recipient of the
2678 * roaming advertisement the packet is forwarded
2679 * (the tvlv API will re-route the packet).
2680 */
2681 if (!batadv_is_my_mac(bat_priv, dst))
2682 return NET_RX_DROP;
2683
2684 /* check if it is a backbone gateway. we don't accept
2685 * roaming advertisement from it, as it has the same
2686 * entries as we have.
2687 */
2688 if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
2689 goto out;
2690
2691 if (tvlv_value_len < sizeof(*roaming_adv))
2692 goto out;
2693
2694 orig_node = batadv_orig_hash_find(bat_priv, src);
2695 if (!orig_node)
2696 goto out;
2697
2698 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
2699 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
2700
2701 batadv_dbg(BATADV_DBG_TT, bat_priv,
2702 "Received ROAMING_ADV from %pM (client %pM)\n",
2703 src, roaming_adv->client);
2704
2705 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
2706 BATADV_TT_CLIENT_ROAM,
2707 atomic_read(&orig_node->last_ttvn) + 1);
2708
2709out:
2710 if (orig_node)
2711 batadv_orig_node_free_ref(orig_node);
2712 return NET_RX_SUCCESS;
2713}
2714
2715/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002716 * batadv_tt_init - initialise the translation table internals
2717 * @bat_priv: the bat priv with all the soft interface information
2718 *
2719 * Return 0 on success or negative error number in case of failure.
2720 */
2721int batadv_tt_init(struct batadv_priv *bat_priv)
2722{
2723 int ret;
2724
2725 ret = batadv_tt_local_init(bat_priv);
2726 if (ret < 0)
2727 return ret;
2728
2729 ret = batadv_tt_global_init(bat_priv);
2730 if (ret < 0)
2731 return ret;
2732
2733 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08002734 batadv_tt_tvlv_unicast_handler_v1,
2735 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002736
Marek Lindner122edaa2013-04-23 21:40:03 +08002737 batadv_tvlv_handler_register(bat_priv, NULL,
2738 batadv_roam_tvlv_unicast_handler_v1,
2739 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
2740
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002741 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2742 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2743 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2744
2745 return 1;
2746}