blob: eb8490e504e257f7f28fd56be88f3eabfb00fd68 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
Sven Eckelmann56303d32012-06-05 22:31:31 +020032static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020034static void batadv_tt_purge(struct work_struct *work);
35static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020036batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Marek Lindner7aadf882011-02-18 12:28:09 +000038/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020039static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Sven Eckelmann56303d32012-06-05 22:31:31 +020041 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020042 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
Sven Eckelmann56303d32012-06-05 22:31:31 +020047static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048{
Sven Eckelmanna5130882012-05-16 20:23:16 +020049 INIT_DELAYED_WORK(&bat_priv->tt_work, batadv_tt_purge);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020050 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt_work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020051 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
Sven Eckelmann56303d32012-06-05 22:31:31 +020054static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020055batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000056{
Marek Lindner7aadf882011-02-18 12:28:09 +000057 struct hlist_head *head;
58 struct hlist_node *node;
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();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010070 hlist_for_each_entry_rcu(tt_common_entry, node, 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 Eckelmanna5130882012-05-16 20:23:16 +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 Eckelmanna5130882012-05-16 20:23:16 +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
Marek Lindner7aadf882011-02-18 12:28:09 +0000112}
113
Sven Eckelmanna5130882012-05-16 20:23:16 +0200114static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200116{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100117 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
118 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200119}
120
Sven Eckelmanna5130882012-05-16 20:23:16 +0200121static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200122{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200123 struct batadv_tt_common_entry *tt_common_entry;
124 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200125
Sven Eckelmann56303d32012-06-05 22:31:31 +0200126 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
127 tt_global_entry = container_of(tt_common_entry,
128 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Simon Wunderlich531027f2011-10-19 11:02:25 +0200130 kfree(tt_global_entry);
131}
132
Sven Eckelmanna5130882012-05-16 20:23:16 +0200133static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200134batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200135{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200136 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100138 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200139 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 }
141}
142
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200145 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200146
Sven Eckelmann56303d32012-06-05 22:31:31 +0200147 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200148 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200149 kfree(orig_entry);
150}
151
Sven Eckelmanna5130882012-05-16 20:23:16 +0200152static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200153batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200154{
Antonio Quartullid657e622012-07-01 14:09:12 +0200155 if (!atomic_dec_and_test(&orig_entry->refcount))
156 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000157 /* to avoid race conditions, immediately decrease the tt counter */
158 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200159 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200160}
161
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200164{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200166 bool event_removed = false;
167 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168
169 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
170
171 if (!tt_change_node)
172 return;
173
Antonio Quartulliff66c972011-06-30 01:14:00 +0200174 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200175 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
176
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200177 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200178
179 /* check for ADD+DEL or DEL+ADD events */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200180 spin_lock_bh(&bat_priv->tt_changes_list_lock);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200181 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
182 list) {
183 if (!batadv_compare_eth(entry->change.addr, addr))
184 continue;
185
186 /* DEL+ADD in the same orig interval have no effect and can be
187 * removed to avoid silly behaviour on the receiver side. The
188 * other way around (ADD+DEL) can happen in case of roaming of
189 * a client still in the NEW state. Roaming of NEW clients is
190 * now possible due to automatically recognition of "temporary"
191 * clients
192 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200193 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200194 if (!del_op_requested && del_op_entry)
195 goto del;
196 if (del_op_requested && !del_op_entry)
197 goto del;
198 continue;
199del:
200 list_del(&entry->list);
201 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000202 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200203 event_removed = true;
204 goto unlock;
205 }
206
Antonio Quartullia73105b2011-04-27 14:27:44 +0200207 /* track the change in the OGMinterval list */
208 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200209
210unlock:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
212
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213 if (event_removed)
214 atomic_dec(&bat_priv->tt_local_changes);
215 else
216 atomic_inc(&bat_priv->tt_local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200217}
218
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200219int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200220{
Sven Eckelmann96412692012-06-05 22:31:30 +0200221 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200222}
223
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200226 if (bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200227 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200229 bat_priv->tt_local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200231 if (!bat_priv->tt_local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200232 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
Sven Eckelmann5346c352012-05-05 13:27:28 +0200234 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235}
236
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200237void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
238 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200240 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
241 struct batadv_tt_local_entry *tt_local_entry = NULL;
242 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200243 struct hlist_head *head;
244 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200245 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100246 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Sven Eckelmanna5130882012-05-16 20:23:16 +0200248 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200250 if (tt_local_entry) {
251 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200252 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
253 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200254 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 }
256
Sven Eckelmann704509b2011-05-14 23:14:54 +0200257 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200258 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200259 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200260
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200261 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200262 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
263 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100265 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200266 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200267 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200268 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100269 atomic_set(&tt_local_entry->common.refcount, 2);
270 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271
272 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200273 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200274 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100276 /* The local entry has to be marked as NEW to avoid to send it in
277 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200278 * (consistency check)
279 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200280 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100281
Sven Eckelmanna5130882012-05-16 20:23:16 +0200282 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200283 batadv_choose_orig,
284 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200285 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100286
287 if (unlikely(hash_added != 0)) {
288 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200289 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100290 goto out;
291 }
292
Sven Eckelmanna5130882012-05-16 20:23:16 +0200293 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200294
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200296 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297
Antonio Quartullicc47f662011-04-27 14:27:57 +0200298 /* Check whether it is a roaming! */
299 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200300 /* These node are probably going to update their tt table */
301 head = &tt_global_entry->orig_list;
302 rcu_read_lock();
303 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
304 orig_entry->orig_node->tt_poss_change = true;
305
Sven Eckelmanna5130882012-05-16 20:23:16 +0200306 batadv_send_roam_adv(bat_priv,
307 tt_global_entry->common.addr,
308 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200309 }
310 rcu_read_unlock();
311 /* The global entry has to be marked as ROAMING and
312 * has to be kept for consistency purpose
313 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200314 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100315 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200316 }
317out:
318 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200319 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200320 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200321 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322}
323
Sven Eckelmanna5130882012-05-16 20:23:16 +0200324static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
325 int *packet_buff_len,
326 int min_packet_len,
327 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800329 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800331 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
332
333 /* keep old buffer if kmalloc should fail */
334 if (new_buff) {
335 memcpy(new_buff, *packet_buff, min_packet_len);
336 kfree(*packet_buff);
337 *packet_buff = new_buff;
338 *packet_buff_len = new_packet_len;
339 }
340}
341
Sven Eckelmann56303d32012-06-05 22:31:31 +0200342static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200343 unsigned char **packet_buff,
344 int *packet_buff_len,
345 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800346{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800348 int req_len;
349
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200350 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800351
352 req_len = min_packet_len;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200353 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800354
355 /* if we have too many changes for one packet don't send any
356 * and wait for the tt table request which will be fragmented
357 */
358 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
359 req_len = min_packet_len;
360
Sven Eckelmanna5130882012-05-16 20:23:16 +0200361 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
362 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800363
364 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200365 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800366}
367
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200369 unsigned char **packet_buff,
370 int *packet_buff_len,
371 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800372{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200373 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800374 int count = 0, tot_changes = 0, new_len;
375 unsigned char *tt_buff;
376
Sven Eckelmanna5130882012-05-16 20:23:16 +0200377 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
378 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800379
380 new_len = *packet_buff_len - min_packet_len;
381 tt_buff = *packet_buff + min_packet_len;
382
383 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200384 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Antonio Quartullia73105b2011-04-27 14:27:44 +0200386 spin_lock_bh(&bat_priv->tt_changes_list_lock);
387 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100390 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200391 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200392 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200393 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 count++;
395 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396 list_del(&entry->list);
397 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200399 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
Antonio Quartullia73105b2011-04-27 14:27:44 +0200401 /* Keep the buffer for possible tt_request */
402 spin_lock_bh(&bat_priv->tt_buff_lock);
403 kfree(bat_priv->tt_buff);
404 bat_priv->tt_buff_len = 0;
405 bat_priv->tt_buff = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800406 /* check whether this new OGM has no changes due to size problems */
407 if (new_len > 0) {
408 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200409 * instead of providing the diff
410 */
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800411 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200412 if (bat_priv->tt_buff) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800413 memcpy(bat_priv->tt_buff, tt_buff, new_len);
414 bat_priv->tt_buff_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200415 }
416 }
417 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Marek Lindner08ad76e2012-04-23 16:32:55 +0800419 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420}
421
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200422int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423{
424 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200426 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200427 struct batadv_tt_common_entry *tt_common_entry;
428 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000429 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200431 uint32_t i;
432 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200434 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200435 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100436 ret = seq_printf(seq,
437 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200438 net_dev->name);
439 goto out;
440 }
441
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200442 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100443 ret = seq_printf(seq,
444 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200445 net_dev->name);
446 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 }
448
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100449 seq_printf(seq,
450 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Antonio Quartullia73105b2011-04-27 14:27:44 +0200451 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453 for (i = 0; i < hash->size; i++) {
454 head = &hash->table[i];
455
Marek Lindner7aadf882011-02-18 12:28:09 +0000456 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100457 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000458 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200459 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100460 tt_common_entry->addr,
461 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200462 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100463 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200464 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100465 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200466 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100467 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200468 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100469 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200470 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000472 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200474out:
475 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200476 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200477 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478}
479
Sven Eckelmann56303d32012-06-05 22:31:31 +0200480static void
481batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
482 struct batadv_tt_local_entry *tt_local_entry,
483 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200485 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
486 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487
Antonio Quartulli015758d2011-07-09 17:52:13 +0200488 /* The local client has to be marked as "pending to be removed" but has
489 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200490 * response issued before the net ttvn increment (consistency check)
491 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200492 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100493
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200494 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200495 "Local tt entry (%pM) pending to be removed: %s\n",
496 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497}
498
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200500 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200502 struct batadv_tt_local_entry *tt_local_entry = NULL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200503 uint16_t flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Sven Eckelmanna5130882012-05-16 20:23:16 +0200505 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200506 if (!tt_local_entry)
507 goto out;
508
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200509 flags = BATADV_TT_CLIENT_DEL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200510 if (roaming)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200511 flags |= BATADV_TT_CLIENT_ROAM;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200512
513 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200514out:
515 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200516 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517}
518
Sven Eckelmann56303d32012-06-05 22:31:31 +0200519static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200520 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200522 struct batadv_tt_local_entry *tt_local_entry;
523 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000524 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200525
526 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
527 hash_entry) {
528 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200529 struct batadv_tt_local_entry,
530 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200531 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
532 continue;
533
534 /* entry already marked for deletion */
535 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
536 continue;
537
538 if (!batadv_has_timed_out(tt_local_entry->last_seen,
539 BATADV_TT_LOCAL_TIMEOUT))
540 continue;
541
542 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
543 BATADV_TT_CLIENT_DEL, "timed out");
544 }
545}
546
Sven Eckelmann56303d32012-06-05 22:31:31 +0200547static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200548{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200549 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200551 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200552 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554 for (i = 0; i < hash->size; i++) {
555 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200556 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200558 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200559 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200560 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 }
562
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563}
564
Sven Eckelmann56303d32012-06-05 22:31:31 +0200565static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200567 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200568 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200569 struct batadv_tt_common_entry *tt_common_entry;
570 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200571 struct hlist_node *node, *node_tmp;
572 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200573 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200574
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200575 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576 return;
577
Antonio Quartullia73105b2011-04-27 14:27:44 +0200578 hash = bat_priv->tt_local_hash;
579
580 for (i = 0; i < hash->size; i++) {
581 head = &hash->table[i];
582 list_lock = &hash->list_locks[i];
583
584 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100585 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200586 head, hash_entry) {
587 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200588 tt_local = container_of(tt_common_entry,
589 struct batadv_tt_local_entry,
590 common);
591 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200592 }
593 spin_unlock_bh(list_lock);
594 }
595
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200596 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200597
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200598 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599}
600
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200603 if (bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200604 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200606 bat_priv->tt_global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200608 if (!bat_priv->tt_global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200609 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610
Sven Eckelmann5346c352012-05-05 13:27:28 +0200611 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612}
613
Sven Eckelmann56303d32012-06-05 22:31:31 +0200614static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200615{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200616 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200617
618 spin_lock_bh(&bat_priv->tt_changes_list_lock);
619
620 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
621 list) {
622 list_del(&entry->list);
623 kfree(entry);
624 }
625
626 atomic_set(&bat_priv->tt_local_changes, 0);
627 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
628}
629
Antonio Quartullid657e622012-07-01 14:09:12 +0200630/* retrieves the orig_tt_list_entry belonging to orig_node from the
631 * batadv_tt_global_entry list
632 *
633 * returns it with an increased refcounter, NULL if not found
634 */
635static struct batadv_tt_orig_list_entry *
636batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
637 const struct batadv_orig_node *orig_node)
638{
639 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
640 const struct hlist_head *head;
641 struct hlist_node *node;
642
643 rcu_read_lock();
644 head = &entry->orig_list;
645 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
646 if (tmp_orig_entry->orig_node != orig_node)
647 continue;
648 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
649 continue;
650
651 orig_entry = tmp_orig_entry;
652 break;
653 }
654 rcu_read_unlock();
655
656 return orig_entry;
657}
658
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200659/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200660 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200661 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200662static bool
663batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
664 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200665{
Antonio Quartullid657e622012-07-01 14:09:12 +0200666 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200667 bool found = false;
668
Antonio Quartullid657e622012-07-01 14:09:12 +0200669 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
670 if (orig_entry) {
671 found = true;
672 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200673 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200674
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200675 return found;
676}
677
Sven Eckelmanna5130882012-05-16 20:23:16 +0200678static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200679batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200681{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200683
Antonio Quartullid657e622012-07-01 14:09:12 +0200684 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
685 if (orig_entry)
686 goto out;
687
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200688 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
689 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200690 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200691
692 INIT_HLIST_NODE(&orig_entry->list);
693 atomic_inc(&orig_node->refcount);
694 atomic_inc(&orig_node->tt_size);
695 orig_entry->orig_node = orig_node;
696 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200697 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200698
Antonio Quartullid657e622012-07-01 14:09:12 +0200699 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200700 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200701 &tt_global->orig_list);
702 spin_unlock_bh(&tt_global->list_lock);
703out:
704 if (orig_entry)
705 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200706}
707
Antonio Quartullia73105b2011-04-27 14:27:44 +0200708/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200709int batadv_tt_global_add(struct batadv_priv *bat_priv,
710 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200711 const unsigned char *tt_addr, uint8_t flags,
712 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200714 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200715 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100716 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200717 struct batadv_tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718
Sven Eckelmanna5130882012-05-16 20:23:16 +0200719 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720
Antonio Quartullia73105b2011-04-27 14:27:44 +0200721 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200722 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200723 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200724 goto out;
725
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200726 common = &tt_global_entry->common;
727 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200728
Antonio Quartullid4f44692012-05-25 00:00:54 +0200729 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200730 tt_global_entry->roam_at = 0;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200731 atomic_set(&common->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200732
733 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
734 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200735
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200736 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200737 batadv_compare_tt,
738 batadv_choose_orig, common,
739 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100740
741 if (unlikely(hash_added != 0)) {
742 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200743 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100744 goto out_remove;
745 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200746 } else {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747 /* there is already a global entry, use this one. */
748
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200749 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
750 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200751 * delete + roaming change for this originator.
752 *
753 * We should first delete the old originator before adding the
754 * new one.
755 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200756 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200757 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200758 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200759 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200761
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200763 /* add the new orig_entry (if needed) */
764 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200765
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200766 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200767 "Creating new global tt entry: %pM (via %pM)\n",
768 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200769
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100770out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200771 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200772 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200773 "global tt received",
774 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200775 ret = 1;
776out:
777 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200778 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200779 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780}
781
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200782/* print all orig nodes who announce the address for this global entry.
783 * it is assumed that the caller holds rcu_read_lock();
784 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200785static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200786batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200787 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200788{
789 struct hlist_head *head;
790 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200791 struct batadv_tt_orig_list_entry *orig_entry;
792 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200793 uint16_t flags;
794 uint8_t last_ttvn;
795
796 tt_common_entry = &tt_global_entry->common;
797
798 head = &tt_global_entry->orig_list;
799
800 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
801 flags = tt_common_entry->flags;
802 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
803 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
804 tt_global_entry->common.addr, orig_entry->ttvn,
805 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200806 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
807 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200808 }
809}
810
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200811int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812{
813 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200814 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200815 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200816 struct batadv_tt_common_entry *tt_common_entry;
817 struct batadv_tt_global_entry *tt_global;
818 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000819 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200821 uint32_t i;
822 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200824 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200825 if (!primary_if) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100826 ret = seq_printf(seq,
827 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200828 net_dev->name);
829 goto out;
830 }
831
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200832 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100833 ret = seq_printf(seq,
834 "BATMAN mesh %s disabled - primary interface not active\n",
Marek Lindner32ae9b22011-04-20 15:40:58 +0200835 net_dev->name);
836 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 }
838
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200839 seq_printf(seq,
840 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000841 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200842 seq_printf(seq, " %-13s %s %-15s %s %s\n",
843 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845 for (i = 0; i < hash->size; i++) {
846 head = &hash->table[i];
847
Marek Lindner7aadf882011-02-18 12:28:09 +0000848 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100849 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000850 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200851 tt_global = container_of(tt_common_entry,
852 struct batadv_tt_global_entry,
853 common);
854 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000856 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200858out:
859 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200860 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200861 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862}
863
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200864/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200865static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200866batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200868 struct hlist_head *head;
869 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200870 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200871
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200872 spin_lock_bh(&tt_global_entry->list_lock);
873 head = &tt_global_entry->orig_list;
874 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
875 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200876 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200877 }
878 spin_unlock_bh(&tt_global_entry->list_lock);
879
880}
881
Sven Eckelmanna5130882012-05-16 20:23:16 +0200882static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200883batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
884 struct batadv_tt_global_entry *tt_global_entry,
885 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200886 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200887{
888 struct hlist_head *head;
889 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200890 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200891
892 spin_lock_bh(&tt_global_entry->list_lock);
893 head = &tt_global_entry->orig_list;
894 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
895 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200896 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200897 "Deleting %pM from global tt entry %pM: %s\n",
898 orig_node->orig,
899 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200900 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200901 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200902 }
903 }
904 spin_unlock_bh(&tt_global_entry->list_lock);
905}
906
Sven Eckelmann56303d32012-06-05 22:31:31 +0200907static void
908batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
909 struct batadv_tt_global_entry *tt_global_entry,
910 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200911{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200912 batadv_dbg(BATADV_DBG_TT, bat_priv,
913 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200914 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200915
Sven Eckelmanna5130882012-05-16 20:23:16 +0200916 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200917 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200918 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200919
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920}
921
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200922/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200923 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
924 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200925 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200926static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200927batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
928 struct batadv_tt_global_entry *tt_global_entry,
929 struct batadv_orig_node *orig_node,
930 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200931{
932 bool last_entry = true;
933 struct hlist_head *head;
934 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200935 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200936
937 /* no local entry exists, case 1:
938 * Check if this is the last one or if other entries exist.
939 */
940
941 rcu_read_lock();
942 head = &tt_global_entry->orig_list;
943 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
944 if (orig_entry->orig_node != orig_node) {
945 last_entry = false;
946 break;
947 }
948 }
949 rcu_read_unlock();
950
951 if (last_entry) {
952 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200953 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200954 tt_global_entry->roam_at = jiffies;
955 } else
956 /* there is another entry, we can simply delete this
957 * one and can still use the other one.
958 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200959 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
960 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200961}
962
963
964
Sven Eckelmann56303d32012-06-05 22:31:31 +0200965static void batadv_tt_global_del(struct batadv_priv *bat_priv,
966 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200967 const unsigned char *addr,
968 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000969{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200970 struct batadv_tt_global_entry *tt_global_entry = NULL;
971 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200974 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200975 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200976
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200977 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200978 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
979 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800980
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200981 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200982 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
983 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200984
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800985 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200986 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800987
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200988 /* if we are deleting a global entry due to a roam
989 * event, there are two possibilities:
990 * 1) the client roamed from node A to node B => if there
991 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200992 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200993 * wait for node B to claim it. In case of timeout
994 * the entry is purged.
995 *
996 * If there are other originators left, we directly delete
997 * the originator.
998 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200999 * the global entry, since it is useless now.
1000 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001001 local_entry = batadv_tt_local_hash_find(bat_priv,
1002 tt_global_entry->common.addr);
1003 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001004 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001005 batadv_tt_global_del_orig_list(tt_global_entry);
1006 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001007 } else
1008 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001009 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1010 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001011
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001012
Antonio Quartullicc47f662011-04-27 14:27:57 +02001013out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001014 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001015 batadv_tt_global_entry_free_ref(tt_global_entry);
1016 if (local_entry)
1017 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001018}
1019
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1021 struct batadv_orig_node *orig_node,
1022 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001023{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 struct batadv_tt_global_entry *tt_global;
1025 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001026 uint32_t i;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001027 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001028 struct hlist_node *node, *safe;
1029 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001030 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031
Simon Wunderlich6e801492011-10-19 10:28:26 +02001032 if (!hash)
1033 return;
1034
Antonio Quartullia73105b2011-04-27 14:27:44 +02001035 for (i = 0; i < hash->size; i++) {
1036 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001037 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001038
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001039 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001040 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001041 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042 tt_global = container_of(tt_common_entry,
1043 struct batadv_tt_global_entry,
1044 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001045
Sven Eckelmann56303d32012-06-05 22:31:31 +02001046 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001047 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001048
Sven Eckelmann56303d32012-06-05 22:31:31 +02001049 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001050 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001051 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001052 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001053 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001054 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001055 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001056 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001057 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001059 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060}
1061
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062static void batadv_tt_global_roam_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001063 struct hlist_head *head)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001064{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001065 struct batadv_tt_common_entry *tt_common_entry;
1066 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001067 struct hlist_node *node, *node_tmp;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001068
1069 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
1070 hash_entry) {
1071 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001072 struct batadv_tt_global_entry,
1073 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001074 if (!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001075 continue;
1076 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1077 BATADV_TT_CLIENT_ROAM_TIMEOUT))
1078 continue;
1079
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001080 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001081 "Deleting global tt entry (%pM): Roaming timeout\n",
1082 tt_global_entry->common.addr);
1083
1084 hlist_del_rcu(node);
1085 batadv_tt_global_entry_free_ref(tt_global_entry);
1086 }
1087}
1088
Sven Eckelmann56303d32012-06-05 22:31:31 +02001089static void batadv_tt_global_roam_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001090{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001091 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001092 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001093 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001094 uint32_t i;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001095
Antonio Quartullicc47f662011-04-27 14:27:57 +02001096 for (i = 0; i < hash->size; i++) {
1097 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001098 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001099
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001100 spin_lock_bh(list_lock);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001101 batadv_tt_global_roam_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001102 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001103 }
1104
Antonio Quartullicc47f662011-04-27 14:27:57 +02001105}
1106
Sven Eckelmann56303d32012-06-05 22:31:31 +02001107static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001109 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001110 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111 struct batadv_tt_common_entry *tt_common_entry;
1112 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001113 struct hlist_node *node, *node_tmp;
1114 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001115 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001116
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001117 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118 return;
1119
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001120 hash = bat_priv->tt_global_hash;
1121
1122 for (i = 0; i < hash->size; i++) {
1123 head = &hash->table[i];
1124 list_lock = &hash->list_locks[i];
1125
1126 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001127 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001128 head, hash_entry) {
1129 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001130 tt_global = container_of(tt_common_entry,
1131 struct batadv_tt_global_entry,
1132 common);
1133 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001134 }
1135 spin_unlock_bh(list_lock);
1136 }
1137
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001138 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001139
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001140 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001141}
1142
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143static bool
1144_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1145 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001146{
1147 bool ret = false;
1148
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001149 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1150 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001151 ret = true;
1152
1153 return ret;
1154}
1155
Sven Eckelmann56303d32012-06-05 22:31:31 +02001156struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1157 const uint8_t *src,
1158 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001160 struct batadv_tt_local_entry *tt_local_entry = NULL;
1161 struct batadv_tt_global_entry *tt_global_entry = NULL;
1162 struct batadv_orig_node *orig_node = NULL;
1163 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164 struct hlist_head *head;
1165 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001166 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001167 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001169 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001170 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001171 if (!tt_local_entry)
1172 goto out;
1173 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001174
Sven Eckelmanna5130882012-05-16 20:23:16 +02001175 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001176 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001177 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001179 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001180 * isolation
1181 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001182 if (tt_local_entry &&
1183 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001184 goto out;
1185
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001186 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001187
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001188 rcu_read_lock();
1189 head = &tt_global_entry->orig_list;
1190 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001191 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001192 if (!router)
1193 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001194
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001195 if (router->tq_avg > best_tq) {
1196 orig_node = orig_entry->orig_node;
1197 best_tq = router->tq_avg;
1198 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001199 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001200 }
1201 /* found anything? */
1202 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1203 orig_node = NULL;
1204 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001205out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001206 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001207 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001208 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001209 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001210
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001211 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001212}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001213
1214/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1216 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001217{
1218 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001219 struct batadv_hashtable *hash = bat_priv->tt_global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001220 struct batadv_tt_common_entry *tt_common;
1221 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001222 struct hlist_node *node;
1223 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001224 uint32_t i;
1225 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001226
1227 for (i = 0; i < hash->size; i++) {
1228 head = &hash->table[i];
1229
1230 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001231 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001232 tt_global = container_of(tt_common,
1233 struct batadv_tt_global_entry,
1234 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001235 /* Roaming clients are in the global table for
1236 * consistency only. They don't have to be
1237 * taken into account while computing the
1238 * global crc
1239 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001240 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001241 continue;
1242
1243 /* find out if this global entry is announced by this
1244 * originator
1245 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001246 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001247 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001248 continue;
1249
1250 total_one = 0;
1251 for (j = 0; j < ETH_ALEN; j++)
1252 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001253 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001254 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001255 }
1256 rcu_read_unlock();
1257 }
1258
1259 return total;
1260}
1261
1262/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001263static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001264{
1265 uint16_t total = 0, total_one;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001266 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001267 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001268 struct hlist_node *node;
1269 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001270 uint32_t i;
1271 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001272
1273 for (i = 0; i < hash->size; i++) {
1274 head = &hash->table[i];
1275
1276 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001277 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001278 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001279 * account while computing the CRC
1280 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001281 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001282 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001283 total_one = 0;
1284 for (j = 0; j < ETH_ALEN; j++)
1285 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001286 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001287 total ^= total_one;
1288 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001289 rcu_read_unlock();
1290 }
1291
1292 return total;
1293}
1294
Sven Eckelmann56303d32012-06-05 22:31:31 +02001295static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001296{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001297 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001298
1299 spin_lock_bh(&bat_priv->tt_req_list_lock);
1300
1301 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1302 list_del(&node->list);
1303 kfree(node);
1304 }
1305
1306 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1307}
1308
Sven Eckelmann56303d32012-06-05 22:31:31 +02001309static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1310 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001311 const unsigned char *tt_buff,
1312 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001313{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001314 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001315
1316 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001317 * last OGM (the OGM could carry no changes)
1318 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001319 spin_lock_bh(&orig_node->tt_buff_lock);
1320 if (tt_buff_len > 0) {
1321 kfree(orig_node->tt_buff);
1322 orig_node->tt_buff_len = 0;
1323 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1324 if (orig_node->tt_buff) {
1325 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1326 orig_node->tt_buff_len = tt_buff_len;
1327 }
1328 }
1329 spin_unlock_bh(&orig_node->tt_buff_lock);
1330}
1331
Sven Eckelmann56303d32012-06-05 22:31:31 +02001332static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001333{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001334 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001335
1336 spin_lock_bh(&bat_priv->tt_req_list_lock);
1337 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001338 if (batadv_has_timed_out(node->issued_at,
1339 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340 list_del(&node->list);
1341 kfree(node);
1342 }
1343 }
1344 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1345}
1346
1347/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001348 * has already been issued for this orig_node, NULL otherwise
1349 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001350static struct batadv_tt_req_node *
1351batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1352 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001353{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001354 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001355
1356 spin_lock_bh(&bat_priv->tt_req_list_lock);
1357 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001358 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1359 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001360 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001361 goto unlock;
1362 }
1363
1364 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1365 if (!tt_req_node)
1366 goto unlock;
1367
1368 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1369 tt_req_node->issued_at = jiffies;
1370
1371 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1372unlock:
1373 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1374 return tt_req_node;
1375}
1376
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001377/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001378static int batadv_tt_local_valid_entry(const void *entry_ptr,
1379 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001380{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001381 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001382
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001383 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001384 return 0;
1385 return 1;
1386}
1387
Sven Eckelmanna5130882012-05-16 20:23:16 +02001388static int batadv_tt_global_valid(const void *entry_ptr,
1389 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001391 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1392 const struct batadv_tt_global_entry *tt_global_entry;
1393 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001395 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001396 return 0;
1397
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398 tt_global_entry = container_of(tt_common_entry,
1399 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001400 common);
1401
Sven Eckelmanna5130882012-05-16 20:23:16 +02001402 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001403}
1404
Sven Eckelmanna5130882012-05-16 20:23:16 +02001405static struct sk_buff *
1406batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001407 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001408 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001409 int (*valid_cb)(const void *, const void *),
1410 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001411{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001412 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001413 struct batadv_tt_query_packet *tt_response;
1414 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415 struct hlist_node *node;
1416 struct hlist_head *head;
1417 struct sk_buff *skb = NULL;
1418 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001419 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001420 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001421 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001422
1423 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1424 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001425 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001426 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001427 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001428
Sven Eckelmann96412692012-06-05 22:31:30 +02001429 len = tt_query_size + tt_len;
1430 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001431 if (!skb)
1432 goto out;
1433
1434 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001435 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001436 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437
Sven Eckelmann96412692012-06-05 22:31:30 +02001438 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439 tt_count = 0;
1440
1441 rcu_read_lock();
1442 for (i = 0; i < hash->size; i++) {
1443 head = &hash->table[i];
1444
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001445 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446 head, hash_entry) {
1447 if (tt_count == tt_tot)
1448 break;
1449
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001450 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001451 continue;
1452
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001453 memcpy(tt_change->addr, tt_common_entry->addr,
1454 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001455 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456
1457 tt_count++;
1458 tt_change++;
1459 }
1460 }
1461 rcu_read_unlock();
1462
Antonio Quartulli9d852392011-10-17 14:25:13 +02001463 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001464 * copied
1465 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001466 tt_response->tt_data = htons(tt_count);
1467
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468out:
1469 return skb;
1470}
1471
Sven Eckelmann56303d32012-06-05 22:31:31 +02001472static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1473 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001474 uint8_t ttvn, uint16_t tt_crc,
1475 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476{
1477 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001478 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001479 struct batadv_neigh_node *neigh_node = NULL;
1480 struct batadv_hard_iface *primary_if;
1481 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001483 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001485 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486 if (!primary_if)
1487 goto out;
1488
1489 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001490 * reply from the same orig_node yet
1491 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001492 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001493 if (!tt_req_node)
1494 goto out;
1495
Sven Eckelmann96412692012-06-05 22:31:30 +02001496 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497 if (!skb)
1498 goto out;
1499
1500 skb_reserve(skb, ETH_HLEN);
1501
Sven Eckelmann96412692012-06-05 22:31:30 +02001502 tt_req_len = sizeof(*tt_request);
1503 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001505 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001506 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1508 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001509 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001511 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001512 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001513
1514 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001515 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001516
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001517 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518 if (!neigh_node)
1519 goto out;
1520
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001521 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001522 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1523 dst_orig_node->orig, neigh_node->addr,
1524 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001525
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001526 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001527
Sven Eckelmann9455e342012-05-12 02:09:37 +02001528 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529 ret = 0;
1530
1531out:
1532 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001533 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001534 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001535 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001536 if (ret)
1537 kfree_skb(skb);
1538 if (ret && tt_req_node) {
1539 spin_lock_bh(&bat_priv->tt_req_list_lock);
1540 list_del(&tt_req_node->list);
1541 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1542 kfree(tt_req_node);
1543 }
1544 return ret;
1545}
1546
Sven Eckelmann96412692012-06-05 22:31:30 +02001547static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001548batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001549 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001550{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001551 struct batadv_orig_node *req_dst_orig_node = NULL;
1552 struct batadv_orig_node *res_dst_orig_node = NULL;
1553 struct batadv_neigh_node *neigh_node = NULL;
1554 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555 uint8_t orig_ttvn, req_ttvn, ttvn;
1556 int ret = false;
1557 unsigned char *tt_buff;
1558 bool full_table;
1559 uint16_t tt_len, tt_tot;
1560 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001561 struct batadv_tt_query_packet *tt_response;
1562 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001563
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001564 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001565 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1566 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001567 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001568
1569 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001570 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001571 if (!req_dst_orig_node)
1572 goto out;
1573
Sven Eckelmannda641192012-05-12 13:48:56 +02001574 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575 if (!res_dst_orig_node)
1576 goto out;
1577
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001578 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001579 if (!neigh_node)
1580 goto out;
1581
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001582 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001583 if (!primary_if)
1584 goto out;
1585
1586 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1587 req_ttvn = tt_request->ttvn;
1588
Antonio Quartulli015758d2011-07-09 17:52:13 +02001589 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001590 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001591 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001592 goto out;
1593
Antonio Quartulli015758d2011-07-09 17:52:13 +02001594 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001595 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596 !req_dst_orig_node->tt_buff)
1597 full_table = true;
1598 else
1599 full_table = false;
1600
1601 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001602 * I'll send only one packet with as much TT entries as I can
1603 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001604 if (!full_table) {
1605 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1606 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001607 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608
Sven Eckelmann96412692012-06-05 22:31:30 +02001609 len = sizeof(*tt_response) + tt_len;
1610 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 if (!skb)
1612 goto unlock;
1613
1614 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001615 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1616 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 tt_response->ttvn = req_ttvn;
1618 tt_response->tt_data = htons(tt_tot);
1619
Sven Eckelmann96412692012-06-05 22:31:30 +02001620 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621 /* Copy the last orig_node's OGM buffer */
1622 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1623 req_dst_orig_node->tt_buff_len);
1624
1625 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1626 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001627 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1628 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001629 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1630
Sven Eckelmanna5130882012-05-16 20:23:16 +02001631 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1632 bat_priv->tt_global_hash,
1633 primary_if,
1634 batadv_tt_global_valid,
1635 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636 if (!skb)
1637 goto out;
1638
Sven Eckelmann96412692012-06-05 22:31:30 +02001639 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001640 }
1641
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001642 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001643 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001644 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001645 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1646 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001647 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648
1649 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001650 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001652 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001653 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1654 res_dst_orig_node->orig, neigh_node->addr,
1655 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001657 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001658
Sven Eckelmann9455e342012-05-12 02:09:37 +02001659 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001660 ret = true;
1661 goto out;
1662
1663unlock:
1664 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1665
1666out:
1667 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001668 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001670 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001672 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001674 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001675 if (!ret)
1676 kfree_skb(skb);
1677 return ret;
1678
1679}
Sven Eckelmann96412692012-06-05 22:31:30 +02001680
1681static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001682batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001683 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001685 struct batadv_orig_node *orig_node = NULL;
1686 struct batadv_neigh_node *neigh_node = NULL;
1687 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688 uint8_t my_ttvn, req_ttvn, ttvn;
1689 int ret = false;
1690 unsigned char *tt_buff;
1691 bool full_table;
1692 uint16_t tt_len, tt_tot;
1693 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001694 struct batadv_tt_query_packet *tt_response;
1695 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001697 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001698 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1699 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001700 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701
1702
1703 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1704 req_ttvn = tt_request->ttvn;
1705
Sven Eckelmannda641192012-05-12 13:48:56 +02001706 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 if (!orig_node)
1708 goto out;
1709
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001710 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001711 if (!neigh_node)
1712 goto out;
1713
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001714 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 if (!primary_if)
1716 goto out;
1717
1718 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001719 * is too big send the whole local translation table
1720 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001721 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001722 !bat_priv->tt_buff)
1723 full_table = true;
1724 else
1725 full_table = false;
1726
1727 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001728 * I'll send only one packet with as much TT entries as I can
1729 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730 if (!full_table) {
1731 spin_lock_bh(&bat_priv->tt_buff_lock);
1732 tt_len = bat_priv->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001733 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001734
Sven Eckelmann96412692012-06-05 22:31:30 +02001735 len = sizeof(*tt_response) + tt_len;
1736 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001737 if (!skb)
1738 goto unlock;
1739
1740 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001741 tt_response = (struct batadv_tt_query_packet *)skb_put(skb,
1742 len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743 tt_response->ttvn = req_ttvn;
1744 tt_response->tt_data = htons(tt_tot);
1745
Sven Eckelmann96412692012-06-05 22:31:30 +02001746 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 memcpy(tt_buff, bat_priv->tt_buff,
1748 bat_priv->tt_buff_len);
1749 spin_unlock_bh(&bat_priv->tt_buff_lock);
1750 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001751 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt);
1752 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1754
Sven Eckelmanna5130882012-05-16 20:23:16 +02001755 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1756 bat_priv->tt_local_hash,
1757 primary_if,
1758 batadv_tt_local_valid_entry,
1759 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001760 if (!skb)
1761 goto out;
1762
Sven Eckelmann96412692012-06-05 22:31:30 +02001763 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001764 }
1765
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001766 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001767 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001768 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001769 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1770 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001771 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001772
1773 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001774 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001775
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001776 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001777 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1778 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001779 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001781 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001782
Sven Eckelmann9455e342012-05-12 02:09:37 +02001783 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 ret = true;
1785 goto out;
1786
1787unlock:
1788 spin_unlock_bh(&bat_priv->tt_buff_lock);
1789out:
1790 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001791 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001792 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001793 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001795 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796 if (!ret)
1797 kfree_skb(skb);
1798 /* This packet was for me, so it doesn't need to be re-routed */
1799 return true;
1800}
1801
Sven Eckelmann56303d32012-06-05 22:31:31 +02001802bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001803 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001804{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001805 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001806 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001807 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001808 return true;
1809
Sven Eckelmanna5130882012-05-16 20:23:16 +02001810 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001811 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001812 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001813 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814}
1815
Sven Eckelmann56303d32012-06-05 22:31:31 +02001816static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1817 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001818 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001819 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820{
1821 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001822 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
1824 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001825 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1826 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001827 batadv_tt_global_del(bat_priv, orig_node,
1828 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001829 "tt removed by changes",
1830 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001831 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001832 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001833 (tt_change + i)->addr,
1834 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001835 /* In case of problem while storing a
1836 * global_entry, we stop the updating
1837 * procedure without committing the
1838 * ttvn change. This will avoid to send
1839 * corrupted data on tt_request
1840 */
1841 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001842 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001844 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845}
1846
Sven Eckelmann56303d32012-06-05 22:31:31 +02001847static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001848 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001849{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001850 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851
Sven Eckelmannda641192012-05-12 13:48:56 +02001852 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853 if (!orig_node)
1854 goto out;
1855
1856 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001857 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001858
Sven Eckelmanna5130882012-05-16 20:23:16 +02001859 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001860 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001861 ntohs(tt_response->tt_data),
1862 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863
1864 spin_lock_bh(&orig_node->tt_buff_lock);
1865 kfree(orig_node->tt_buff);
1866 orig_node->tt_buff_len = 0;
1867 orig_node->tt_buff = NULL;
1868 spin_unlock_bh(&orig_node->tt_buff_lock);
1869
1870 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1871
1872out:
1873 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001874 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875}
1876
Sven Eckelmann56303d32012-06-05 22:31:31 +02001877static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1878 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001879 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001880 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001882 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1883 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001884
Sven Eckelmanna5130882012-05-16 20:23:16 +02001885 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1886 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001887 atomic_set(&orig_node->last_ttvn, ttvn);
1888}
1889
Sven Eckelmann56303d32012-06-05 22:31:31 +02001890bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001892 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001893 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001894
Sven Eckelmanna5130882012-05-16 20:23:16 +02001895 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001896 if (!tt_local_entry)
1897 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001898 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001899 * consistency purpose)
1900 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001901 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001902 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001903 ret = true;
1904out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001906 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001907 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908}
1909
Sven Eckelmann56303d32012-06-05 22:31:31 +02001910void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001911 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001912{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001913 struct batadv_tt_req_node *node, *safe;
1914 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001915 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001916
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001917 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001918 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1919 tt_response->src, tt_response->ttvn,
1920 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001921 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001923 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001924 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001925 goto out;
1926
Sven Eckelmannda641192012-05-12 13:48:56 +02001927 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928 if (!orig_node)
1929 goto out;
1930
Sven Eckelmann96412692012-06-05 22:31:30 +02001931 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001932 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001933 } else {
1934 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001935 batadv_tt_update_changes(bat_priv, orig_node,
1936 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001937 tt_response->ttvn, tt_change);
1938 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939
1940 /* Delete the tt_req_node from pending tt_requests list */
1941 spin_lock_bh(&bat_priv->tt_req_list_lock);
1942 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001943 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001944 continue;
1945 list_del(&node->list);
1946 kfree(node);
1947 }
1948 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1949
1950 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001951 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001952 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001953 * unset the flag
1954 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001955 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956out:
1957 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001958 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959}
1960
Sven Eckelmann56303d32012-06-05 22:31:31 +02001961int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001963 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964
Sven Eckelmanna5130882012-05-16 20:23:16 +02001965 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001966 if (ret < 0)
1967 return ret;
1968
Sven Eckelmanna5130882012-05-16 20:23:16 +02001969 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001970 if (ret < 0)
1971 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972
Sven Eckelmanna5130882012-05-16 20:23:16 +02001973 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001974
1975 return 1;
1976}
1977
Sven Eckelmann56303d32012-06-05 22:31:31 +02001978static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001979{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001980 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001981
Antonio Quartullicc47f662011-04-27 14:27:57 +02001982 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983
Antonio Quartullicc47f662011-04-27 14:27:57 +02001984 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1985 list_del(&node->list);
1986 kfree(node);
1987 }
1988
1989 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1990}
1991
Sven Eckelmann56303d32012-06-05 22:31:31 +02001992static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001993{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001994 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001995
1996 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1997 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001998 if (!batadv_has_timed_out(node->first_time,
1999 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002000 continue;
2001
2002 list_del(&node->list);
2003 kfree(node);
2004 }
2005 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
2006}
2007
2008/* This function checks whether the client already reached the
2009 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2010 * will not be sent.
2011 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002012 * returns true if the ROAMING_ADV can be sent, false otherwise
2013 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002014static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002015 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002016{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002017 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002018 bool ret = false;
2019
2020 spin_lock_bh(&bat_priv->tt_roam_list_lock);
2021 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002022 * reply from the same orig_node yet
2023 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02002024 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002025 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002026 continue;
2027
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002028 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002029 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002030 continue;
2031
Sven Eckelmann3e348192012-05-16 20:23:22 +02002032 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002033 /* Sorry, you roamed too many times! */
2034 goto unlock;
2035 ret = true;
2036 break;
2037 }
2038
2039 if (!ret) {
2040 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2041 if (!tt_roam_node)
2042 goto unlock;
2043
2044 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002045 atomic_set(&tt_roam_node->counter,
2046 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002047 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2048
2049 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
2050 ret = true;
2051 }
2052
2053unlock:
2054 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
2055 return ret;
2056}
2057
Sven Eckelmann56303d32012-06-05 22:31:31 +02002058static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2059 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002060{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002061 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002062 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002063 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002064 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002065 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002066 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002067
2068 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002069 * already roamed to us too many times
2070 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002071 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002072 goto out;
2073
Sven Eckelmann96412692012-06-05 22:31:30 +02002074 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002075 if (!skb)
2076 goto out;
2077
2078 skb_reserve(skb, ETH_HLEN);
2079
Sven Eckelmann96412692012-06-05 22:31:30 +02002080 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002081
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002082 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002083 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002084 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002085 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002086 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002087 if (!primary_if)
2088 goto out;
2089 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002090 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002091 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2092 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2093
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002094 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002095 if (!neigh_node)
2096 goto out;
2097
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002098 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002099 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2100 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002101
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002102 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002103
Sven Eckelmann9455e342012-05-12 02:09:37 +02002104 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002105 ret = 0;
2106
2107out:
2108 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002109 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002110 if (ret)
2111 kfree_skb(skb);
2112 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002113}
2114
Sven Eckelmanna5130882012-05-16 20:23:16 +02002115static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002117 struct delayed_work *delayed_work;
2118 struct batadv_priv *bat_priv;
2119
2120 delayed_work = container_of(work, struct delayed_work, work);
2121 bat_priv = container_of(delayed_work, struct batadv_priv, tt_work);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122
Sven Eckelmanna5130882012-05-16 20:23:16 +02002123 batadv_tt_local_purge(bat_priv);
2124 batadv_tt_global_roam_purge(bat_priv);
2125 batadv_tt_req_purge(bat_priv);
2126 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002127
Sven Eckelmanna5130882012-05-16 20:23:16 +02002128 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002129}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002130
Sven Eckelmann56303d32012-06-05 22:31:31 +02002131void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002132{
2133 cancel_delayed_work_sync(&bat_priv->tt_work);
2134
Sven Eckelmanna5130882012-05-16 20:23:16 +02002135 batadv_tt_local_table_free(bat_priv);
2136 batadv_tt_global_table_free(bat_priv);
2137 batadv_tt_req_list_free(bat_priv);
2138 batadv_tt_changes_list_free(bat_priv);
2139 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002140
2141 kfree(bat_priv->tt_buff);
2142}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002143
Antonio Quartulli697f2532011-11-07 16:47:01 +01002144/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002145 * in the given hash table and returns the number of modified entries
2146 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002147static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2148 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002149{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002150 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002151 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002152 struct hlist_head *head;
2153 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002155
2156 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002157 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002158
2159 for (i = 0; i < hash->size; i++) {
2160 head = &hash->table[i];
2161
2162 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002163 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002164 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002165 if (enable) {
2166 if ((tt_common_entry->flags & flags) == flags)
2167 continue;
2168 tt_common_entry->flags |= flags;
2169 } else {
2170 if (!(tt_common_entry->flags & flags))
2171 continue;
2172 tt_common_entry->flags &= ~flags;
2173 }
2174 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002175 }
2176 rcu_read_unlock();
2177 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002178out:
2179 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002180}
2181
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002182/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002183static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002184{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002185 struct batadv_hashtable *hash = bat_priv->tt_local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002186 struct batadv_tt_common_entry *tt_common;
2187 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002188 struct hlist_node *node, *node_tmp;
2189 struct hlist_head *head;
2190 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002191 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002192
2193 if (!hash)
2194 return;
2195
2196 for (i = 0; i < hash->size; i++) {
2197 head = &hash->table[i];
2198 list_lock = &hash->list_locks[i];
2199
2200 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002201 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2202 hash_entry) {
2203 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002204 continue;
2205
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002206 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002207 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002208 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002209
2210 atomic_dec(&bat_priv->num_local_tt);
2211 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002212 tt_local = container_of(tt_common,
2213 struct batadv_tt_local_entry,
2214 common);
2215 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002216 }
2217 spin_unlock_bh(list_lock);
2218 }
2219
2220}
2221
Sven Eckelmann56303d32012-06-05 22:31:31 +02002222static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002223 unsigned char **packet_buff,
2224 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002225{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002226 uint16_t changed_num = 0;
2227
2228 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2229 return -ENOENT;
2230
Sven Eckelmanna5130882012-05-16 20:23:16 +02002231 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002232 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002233
2234 /* all reset entries have to be counted as local entries */
Antonio Quartulli697f2532011-11-07 16:47:01 +01002235 atomic_add(changed_num, &bat_priv->num_local_tt);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002236 batadv_tt_local_purge_pending_clients(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002237 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002238
2239 /* Increment the TTVN only once per OGM interval */
2240 atomic_inc(&bat_priv->ttvn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002241 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002242 "Local changes committed, updating to ttvn %u\n",
2243 (uint8_t)atomic_read(&bat_priv->ttvn));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002244 bat_priv->tt_poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002245
2246 /* reset the sending counter */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002247 atomic_set(&bat_priv->tt_ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002248
Sven Eckelmanna5130882012-05-16 20:23:16 +02002249 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2250 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002251}
2252
2253/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002254int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002255 unsigned char **packet_buff, int *packet_buff_len,
2256 int packet_min_len)
2257{
2258 int tt_num_changes;
2259
2260 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002261 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2262 packet_buff_len,
2263 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002264
2265 /* if the changes have been sent often enough */
2266 if ((tt_num_changes < 0) &&
Sven Eckelmann3e348192012-05-16 20:23:22 +02002267 (!batadv_atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2269 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002270 tt_num_changes = 0;
2271 }
2272
2273 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002274}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002275
Sven Eckelmann56303d32012-06-05 22:31:31 +02002276bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002277 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002278{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002279 struct batadv_tt_local_entry *tt_local_entry = NULL;
2280 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002281 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002282
2283 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002284 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002285
Sven Eckelmanna5130882012-05-16 20:23:16 +02002286 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002287 if (!tt_local_entry)
2288 goto out;
2289
Sven Eckelmanna5130882012-05-16 20:23:16 +02002290 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002291 if (!tt_global_entry)
2292 goto out;
2293
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002294 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002295 goto out;
2296
Marek Lindner5870adc2012-06-20 17:16:05 +02002297 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002298
2299out:
2300 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002301 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002302 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002303 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002304 return ret;
2305}
Marek Lindnera943cac2011-07-30 13:10:18 +02002306
Sven Eckelmann56303d32012-06-05 22:31:31 +02002307void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2308 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002309 const unsigned char *tt_buff, uint8_t tt_num_changes,
2310 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002311{
2312 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2313 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002314 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002315
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002316 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002317 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002318 return;
2319
Antonio Quartulli17071572011-11-07 16:36:40 +01002320 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002321 * increased by one -> we can apply the attached changes
2322 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002323 if ((!orig_node->tt_initialised && ttvn == 1) ||
2324 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002325 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002326 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2327 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002328 * In this case send a tt request
2329 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002330 if (!tt_num_changes) {
2331 full_table = false;
2332 goto request_table;
2333 }
2334
Sven Eckelmann96412692012-06-05 22:31:30 +02002335 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002336 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002337 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002338
2339 /* Even if we received the precomputed crc with the OGM, we
2340 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002341 * in the global table
2342 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002343 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002344
2345 /* The ttvn alone is not enough to guarantee consistency
2346 * because a single value could represent different states
2347 * (due to the wrap around). Thus a node has to check whether
2348 * the resulting table (after applying the changes) is still
2349 * consistent or not. E.g. a node could disconnect while its
2350 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2351 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002352 * inconsistency
2353 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002354 if (orig_node->tt_crc != tt_crc)
2355 goto request_table;
2356
2357 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002358 * unset the flag
2359 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002360 orig_node->tt_poss_change = false;
2361 } else {
2362 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002363 * in sync anymore -> request fresh tt data
2364 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002365 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2366 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002367request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002368 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002369 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2370 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2371 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002372 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2373 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002374 return;
2375 }
2376 }
2377}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002378
2379/* returns true whether we know that the client has moved from its old
2380 * originator to another one. This entry is kept is still kept for consistency
2381 * purposes
2382 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002383bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002384 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002385{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002386 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002387 bool ret = false;
2388
Sven Eckelmanna5130882012-05-16 20:23:16 +02002389 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002390 if (!tt_global_entry)
2391 goto out;
2392
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002393 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002394 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002395out:
2396 return ret;
2397}