blob: 408807eb9df34a42362285c432213927309e6e0f [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);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020037static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041
Marek Lindner7aadf882011-02-18 12:28:09 +000042/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020043static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000044{
Sven Eckelmann56303d32012-06-05 22:31:31 +020045 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020046 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000047
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49}
50
Sven Eckelmann56303d32012-06-05 22:31:31 +020051static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052{
Sven Eckelmann807736f2012-07-15 22:26:51 +020053 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020055 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020059batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000060{
Marek Lindner7aadf882011-02-18 12:28:09 +000061 struct hlist_head *head;
62 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020063 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020065 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000066
67 if (!hash)
68 return NULL;
69
Sven Eckelmannda641192012-05-12 13:48:56 +020070 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000071 head = &hash->table[index];
72
73 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020075 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000076 continue;
77
Antonio Quartulli48100ba2011-10-30 12:17:33 +010078 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020079 continue;
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000082 break;
83 }
84 rcu_read_unlock();
85
Antonio Quartulli48100ba2011-10-30 12:17:33 +010086 return tt_common_entry_tmp;
87}
88
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_tt_local_entry *
90batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091{
Sven Eckelmann56303d32012-06-05 22:31:31 +020092 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010094
Sven Eckelmann807736f2012-07-15 22:26:51 +020095 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 struct batadv_tt_local_entry,
99 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101}
102
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103static struct batadv_tt_global_entry *
104batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000105{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Sven Eckelmann807736f2012-07-15 22:26:51 +0200109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_tt_global_entry,
113 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000115
Marek Lindner7aadf882011-02-18 12:28:09 +0000116}
117
Sven Eckelmanna5130882012-05-16 20:23:16 +0200118static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200119batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200120{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200123}
124
Sven Eckelmanna5130882012-05-16 20:23:16 +0200125static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200133
Simon Wunderlich531027f2011-10-19 11:02:25 +0200134 kfree(tt_global_entry);
135}
136
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200139{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200141 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 }
145}
146
Sven Eckelmanna5130882012-05-16 20:23:16 +0200147static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200152 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200167 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182
183 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000206 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 event_removed = true;
208 goto unlock;
209 }
210
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213
214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200217 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200218 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200219 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200223int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224{
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200226}
227
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Sven Eckelmann807736f2012-07-15 22:26:51 +0200233 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann807736f2012-07-15 22:26:51 +0200235 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239}
240
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200241static void batadv_tt_global_free(struct batadv_priv *bat_priv,
242 struct batadv_tt_global_entry *tt_global,
243 const char *message)
244{
245 batadv_dbg(BATADV_DBG_TT, bat_priv,
246 "Deleting global tt entry %pM: %s\n",
247 tt_global->common.addr, message);
248
249 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
250 batadv_choose_orig, tt_global->common.addr);
251 batadv_tt_global_entry_free_ref(tt_global);
252
253}
254
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200255void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
256 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200258 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200259 struct batadv_tt_local_entry *tt_local;
260 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200261 struct hlist_head *head;
262 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200263 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100264 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200265 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Antonio Quartulli47c94652012-09-23 22:38:35 +0200267 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200268 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartulli47c94652012-09-23 22:38:35 +0200270 if (tt_local) {
271 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200272 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
273 batadv_dbg(BATADV_DBG_TT, bat_priv,
274 "Re-adding pending client %pM\n", addr);
275 /* whatever the reason why the PENDING flag was set,
276 * this is a client which was enqueued to be removed in
277 * this orig_interval. Since it popped up again, the
278 * flag can be reset like it was never enqueued
279 */
280 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
281 goto add_event;
282 }
283
284 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
285 batadv_dbg(BATADV_DBG_TT, bat_priv,
286 "Roaming client %pM came back to its original location\n",
287 addr);
288 /* the ROAM flag is set because this client roamed away
289 * and the node got a roaming_advertisement message. Now
290 * that the client popped up again at its original
291 * location such flag can be unset
292 */
293 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
294 roamed_back = true;
295 }
296 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 }
298
Antonio Quartulli47c94652012-09-23 22:38:35 +0200299 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
300 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200301 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200302
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200303 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200304 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200305 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
Antonio Quartulli47c94652012-09-23 22:38:35 +0200307 memcpy(tt_local->common.addr, addr, ETH_ALEN);
308 tt_local->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200309 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200310 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311 atomic_set(&tt_local->common.refcount, 2);
312 tt_local->last_seen = jiffies;
313 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
315 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200316 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200317 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100319 /* The local entry has to be marked as NEW to avoid to send it in
320 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200321 * (consistency check)
322 */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200323 tt_local->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100324
Sven Eckelmann807736f2012-07-15 22:26:51 +0200325 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200326 batadv_choose_orig, &tt_local->common,
327 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100328
329 if (unlikely(hash_added != 0)) {
330 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200331 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100332 goto out;
333 }
334
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200335add_event:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200336 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200337
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200338check_roaming:
339 /* Check whether it is a roaming, but don't do anything if the roaming
340 * process has already been handled
341 */
342 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200343 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200344 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200345 rcu_read_lock();
346 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200347 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200348 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200349 }
350 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200351 if (roamed_back) {
352 batadv_tt_global_free(bat_priv, tt_global,
353 "Roaming canceled");
354 tt_global = NULL;
355 } else {
356 /* The global entry has to be marked as ROAMING and
357 * has to be kept for consistency purpose
358 */
359 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
360 tt_global->roam_at = jiffies;
361 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200362 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200363
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200364out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200365 if (tt_local)
366 batadv_tt_local_entry_free_ref(tt_local);
367 if (tt_global)
368 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369}
370
Sven Eckelmanna5130882012-05-16 20:23:16 +0200371static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
372 int *packet_buff_len,
373 int min_packet_len,
374 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800376 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800378 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
379
380 /* keep old buffer if kmalloc should fail */
381 if (new_buff) {
382 memcpy(new_buff, *packet_buff, min_packet_len);
383 kfree(*packet_buff);
384 *packet_buff = new_buff;
385 *packet_buff_len = new_packet_len;
386 }
387}
388
Sven Eckelmann56303d32012-06-05 22:31:31 +0200389static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200390 unsigned char **packet_buff,
391 int *packet_buff_len,
392 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800393{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200394 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800395 int req_len;
396
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200397 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800398
399 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200400 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800401
402 /* if we have too many changes for one packet don't send any
403 * and wait for the tt table request which will be fragmented
404 */
405 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
406 req_len = min_packet_len;
407
Sven Eckelmanna5130882012-05-16 20:23:16 +0200408 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
409 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800410
411 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200412 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800413}
414
Sven Eckelmann56303d32012-06-05 22:31:31 +0200415static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200416 unsigned char **packet_buff,
417 int *packet_buff_len,
418 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800419{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800421 int count = 0, tot_changes = 0, new_len;
422 unsigned char *tt_buff;
423
Sven Eckelmanna5130882012-05-16 20:23:16 +0200424 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
425 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800426
427 new_len = *packet_buff_len - min_packet_len;
428 tt_buff = *packet_buff + min_packet_len;
429
430 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200431 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
Sven Eckelmann807736f2012-07-15 22:26:51 +0200433 spin_lock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Sven Eckelmann807736f2012-07-15 22:26:51 +0200436 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100437 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200439 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200440 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 count++;
442 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443 list_del(&entry->list);
444 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200446 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
Antonio Quartullia73105b2011-04-27 14:27:44 +0200448 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200449 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
450 kfree(bat_priv->tt.last_changeset);
451 bat_priv->tt.last_changeset_len = 0;
452 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800453 /* check whether this new OGM has no changes due to size problems */
454 if (new_len > 0) {
455 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 * instead of providing the diff
457 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200458 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
459 if (bat_priv->tt.last_changeset) {
460 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
461 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200462 }
463 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200464 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
Marek Lindner08ad76e2012-04-23 16:32:55 +0800466 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467}
468
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200469int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470{
471 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200472 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200473 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200474 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100475 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200476 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000477 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200479 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100480 int last_seen_secs;
481 int last_seen_msecs;
482 unsigned long last_seen_jiffies;
483 bool no_purge;
484 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Marek Lindner30da63a2012-08-03 17:15:46 +0200486 primary_if = batadv_seq_print_text_primary_if_get(seq);
487 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200488 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100490 seq_printf(seq,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100491 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
492 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
493 bat_priv->tt.local_crc);
Antonio Quartulli85766a82012-11-08 22:16:16 +0100494 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
495 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 for (i = 0; i < hash->size; i++) {
498 head = &hash->table[i];
499
Marek Lindner7aadf882011-02-18 12:28:09 +0000500 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100501 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000502 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100503 tt_local = container_of(tt_common_entry,
504 struct batadv_tt_local_entry,
505 common);
506 last_seen_jiffies = jiffies - tt_local->last_seen;
507 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
508 last_seen_secs = last_seen_msecs / 1000;
509 last_seen_msecs = last_seen_msecs % 1000;
510
511 no_purge = tt_common_entry->flags & np_flag;
512
513 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100514 tt_common_entry->addr,
515 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200516 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100517 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100518 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200519 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100520 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200521 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100522 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100523 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
524 no_purge ? last_seen_secs : 0,
525 no_purge ? last_seen_msecs : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000527 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200529out:
530 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200531 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200532 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533}
534
Sven Eckelmann56303d32012-06-05 22:31:31 +0200535static void
536batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
537 struct batadv_tt_local_entry *tt_local_entry,
538 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200540 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
541 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Antonio Quartulli015758d2011-07-09 17:52:13 +0200543 /* The local client has to be marked as "pending to be removed" but has
544 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200545 * response issued before the net ttvn increment (consistency check)
546 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200547 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100548
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200549 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200550 "Local tt entry (%pM) pending to be removed: %s\n",
551 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552}
553
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200554/**
555 * batadv_tt_local_remove - logically remove an entry from the local table
556 * @bat_priv: the bat priv with all the soft interface information
557 * @addr: the MAC address of the client to remove
558 * @message: message to append to the log on deletion
559 * @roaming: true if the deletion is due to a roaming event
560 *
561 * Returns the flags assigned to the local entry before being deleted
562 */
563uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
564 const uint8_t *addr, const char *message,
565 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200567 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200568 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569
Sven Eckelmanna5130882012-05-16 20:23:16 +0200570 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200571 if (!tt_local_entry)
572 goto out;
573
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200574 curr_flags = tt_local_entry->common.flags;
575
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200576 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200577 /* if this global entry addition is due to a roaming, the node has to
578 * mark the local entry as "roamed" in order to correctly reroute
579 * packets later
580 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200581 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200582 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200583 /* mark the local client as ROAMed */
584 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
585 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200586
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200587 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
588 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
589 message);
590 goto out;
591 }
592 /* if this client has been added right now, it is possible to
593 * immediately purge it
594 */
595 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
596 curr_flags | BATADV_TT_CLIENT_DEL);
597 hlist_del_rcu(&tt_local_entry->common.hash_entry);
598 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200599
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200600out:
601 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200602 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200603
604 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605}
606
Sven Eckelmann56303d32012-06-05 22:31:31 +0200607static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200608 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610 struct batadv_tt_local_entry *tt_local_entry;
611 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000612 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200613
614 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
615 hash_entry) {
616 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200617 struct batadv_tt_local_entry,
618 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200619 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
620 continue;
621
622 /* entry already marked for deletion */
623 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
624 continue;
625
626 if (!batadv_has_timed_out(tt_local_entry->last_seen,
627 BATADV_TT_LOCAL_TIMEOUT))
628 continue;
629
630 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
631 BATADV_TT_CLIENT_DEL, "timed out");
632 }
633}
634
Sven Eckelmann56303d32012-06-05 22:31:31 +0200635static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200636{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200637 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200640 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642 for (i = 0; i < hash->size; i++) {
643 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200644 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200646 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200647 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649 }
650
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651}
652
Sven Eckelmann56303d32012-06-05 22:31:31 +0200653static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200655 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200657 struct batadv_tt_common_entry *tt_common_entry;
658 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200659 struct hlist_node *node, *node_tmp;
660 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200661 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662
Sven Eckelmann807736f2012-07-15 22:26:51 +0200663 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664 return;
665
Sven Eckelmann807736f2012-07-15 22:26:51 +0200666 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200667
668 for (i = 0; i < hash->size; i++) {
669 head = &hash->table[i];
670 list_lock = &hash->list_locks[i];
671
672 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100673 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200674 head, hash_entry) {
675 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200676 tt_local = container_of(tt_common_entry,
677 struct batadv_tt_local_entry,
678 common);
679 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200680 }
681 spin_unlock_bh(list_lock);
682 }
683
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200684 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200685
Sven Eckelmann807736f2012-07-15 22:26:51 +0200686 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687}
688
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200691 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200692 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
Sven Eckelmann807736f2012-07-15 22:26:51 +0200694 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695
Sven Eckelmann807736f2012-07-15 22:26:51 +0200696 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200697 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698
Sven Eckelmann5346c352012-05-05 13:27:28 +0200699 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700}
701
Sven Eckelmann56303d32012-06-05 22:31:31 +0200702static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200704 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705
Sven Eckelmann807736f2012-07-15 22:26:51 +0200706 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200707
Sven Eckelmann807736f2012-07-15 22:26:51 +0200708 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200709 list) {
710 list_del(&entry->list);
711 kfree(entry);
712 }
713
Sven Eckelmann807736f2012-07-15 22:26:51 +0200714 atomic_set(&bat_priv->tt.local_changes, 0);
715 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200716}
717
Antonio Quartullid657e622012-07-01 14:09:12 +0200718/* retrieves the orig_tt_list_entry belonging to orig_node from the
719 * batadv_tt_global_entry list
720 *
721 * returns it with an increased refcounter, NULL if not found
722 */
723static struct batadv_tt_orig_list_entry *
724batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
725 const struct batadv_orig_node *orig_node)
726{
727 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
728 const struct hlist_head *head;
729 struct hlist_node *node;
730
731 rcu_read_lock();
732 head = &entry->orig_list;
733 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
734 if (tmp_orig_entry->orig_node != orig_node)
735 continue;
736 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
737 continue;
738
739 orig_entry = tmp_orig_entry;
740 break;
741 }
742 rcu_read_unlock();
743
744 return orig_entry;
745}
746
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200747/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200748 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200749 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200750static bool
751batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
752 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753{
Antonio Quartullid657e622012-07-01 14:09:12 +0200754 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200755 bool found = false;
756
Antonio Quartullid657e622012-07-01 14:09:12 +0200757 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
758 if (orig_entry) {
759 found = true;
760 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200761 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200762
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200763 return found;
764}
765
Sven Eckelmanna5130882012-05-16 20:23:16 +0200766static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200767batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200768 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200769{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200770 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200771
Antonio Quartullid657e622012-07-01 14:09:12 +0200772 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200773 if (orig_entry) {
774 /* refresh the ttvn: the current value could be a bogus one that
775 * was added during a "temporary client detection"
776 */
777 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200778 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200779 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200780
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200781 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
782 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200783 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200784
785 INIT_HLIST_NODE(&orig_entry->list);
786 atomic_inc(&orig_node->refcount);
787 atomic_inc(&orig_node->tt_size);
788 orig_entry->orig_node = orig_node;
789 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200790 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200791
Antonio Quartullid657e622012-07-01 14:09:12 +0200792 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200793 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200794 &tt_global->orig_list);
795 spin_unlock_bh(&tt_global->list_lock);
796out:
797 if (orig_entry)
798 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200799}
800
Antonio Quartullia73105b2011-04-27 14:27:44 +0200801/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200802int batadv_tt_global_add(struct batadv_priv *bat_priv,
803 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200804 const unsigned char *tt_addr, uint8_t flags,
805 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200807 struct batadv_tt_global_entry *tt_global_entry;
808 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200809 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100810 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200811 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200812 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813
Sven Eckelmanna5130882012-05-16 20:23:16 +0200814 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200815 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
816
817 /* if the node already has a local client for this entry, it has to wait
818 * for a roaming advertisement instead of manually messing up the global
819 * table
820 */
821 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
822 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
823 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824
Antonio Quartullia73105b2011-04-27 14:27:44 +0200825 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200826 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200827 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200828 goto out;
829
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200830 common = &tt_global_entry->common;
831 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200832
Antonio Quartullid4f44692012-05-25 00:00:54 +0200833 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200834 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200835 /* node must store current time in case of roaming. This is
836 * needed to purge this entry out on timeout (if nobody claims
837 * it)
838 */
839 if (flags & BATADV_TT_CLIENT_ROAM)
840 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200841 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200842 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200843
844 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
845 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200846
Sven Eckelmann807736f2012-07-15 22:26:51 +0200847 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200848 batadv_compare_tt,
849 batadv_choose_orig, common,
850 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100851
852 if (unlikely(hash_added != 0)) {
853 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200854 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100855 goto out_remove;
856 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200857 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200858 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200859 /* If there is already a global entry, we can use this one for
860 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200861 * But if we are trying to add a temporary client then here are
862 * two options at this point:
863 * 1) the global client is not a temporary client: the global
864 * client has to be left as it is, temporary information
865 * should never override any already known client state
866 * 2) the global client is a temporary client: purge the
867 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200868 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200869 if (flags & BATADV_TT_CLIENT_TEMP) {
870 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
871 goto out;
872 if (batadv_tt_global_entry_has_orig(tt_global_entry,
873 orig_node))
874 goto out_remove;
875 batadv_tt_global_del_orig_list(tt_global_entry);
876 goto add_orig_entry;
877 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200878
879 /* if the client was temporary added before receiving the first
880 * OGM announcing it, we have to clear the TEMP flag
881 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200882 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200883
Antonio Quartullie9c001362012-11-07 15:05:33 +0100884 /* the change can carry possible "attribute" flags like the
885 * TT_CLIENT_WIFI, therefore they have to be copied in the
886 * client entry
887 */
888 tt_global_entry->common.flags |= flags;
889
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200890 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
891 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200892 * delete + roaming change for this originator.
893 *
894 * We should first delete the old originator before adding the
895 * new one.
896 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200897 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200898 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200899 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200900 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000901 }
902 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200903add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200904 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200905 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200906
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200907 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200908 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200909 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200910 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200911
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100912out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200913
Antonio Quartullia73105b2011-04-27 14:27:44 +0200914 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200915 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
916 "global tt received",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200917 !!(flags & BATADV_TT_CLIENT_ROAM));
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200918 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
919
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200920 if (!(flags & BATADV_TT_CLIENT_ROAM))
921 /* this is a normal global add. Therefore the client is not in a
922 * roaming state anymore.
923 */
924 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
925
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200926out:
927 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200928 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200929 if (tt_local_entry)
930 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200931 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000932}
933
Sven Eckelmann981d8902012-10-07 13:34:15 +0200934/* batadv_transtable_best_orig - Get best originator list entry from tt entry
935 * @tt_global_entry: global translation table entry to be analyzed
936 *
937 * This functon assumes the caller holds rcu_read_lock().
938 * Returns best originator list entry or NULL on errors.
939 */
940static struct batadv_tt_orig_list_entry *
941batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
942{
943 struct batadv_neigh_node *router = NULL;
944 struct hlist_head *head;
945 struct hlist_node *node;
946 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
947 int best_tq = 0;
948
949 head = &tt_global_entry->orig_list;
950 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
951 router = batadv_orig_node_get_router(orig_entry->orig_node);
952 if (!router)
953 continue;
954
955 if (router->tq_avg > best_tq) {
956 best_entry = orig_entry;
957 best_tq = router->tq_avg;
958 }
959
960 batadv_neigh_node_free_ref(router);
961 }
962
963 return best_entry;
964}
965
966/* batadv_tt_global_print_entry - print all orig nodes who announce the address
967 * for this global entry
968 * @tt_global_entry: global translation table entry to be printed
969 * @seq: debugfs table seq_file struct
970 *
971 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200972 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200973static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200975 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200976{
977 struct hlist_head *head;
978 struct hlist_node *node;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200979 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200980 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200981 uint16_t flags;
982 uint8_t last_ttvn;
983
984 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200985 flags = tt_common_entry->flags;
986
987 best_entry = batadv_transtable_best_orig(tt_global_entry);
988 if (best_entry) {
989 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100990 seq_printf(seq,
991 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +0200992 '*', tt_global_entry->common.addr,
993 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartullif9d8a532012-11-19 09:01:42 +0100994 last_ttvn, best_entry->orig_node->tt_crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +0200995 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
996 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
997 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
998 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200999
1000 head = &tt_global_entry->orig_list;
1001
1002 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001003 if (best_entry == orig_entry)
1004 continue;
1005
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001006 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001007 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1008 '+', tt_global_entry->common.addr,
1009 orig_entry->ttvn, orig_entry->orig_node->orig,
1010 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001011 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001012 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1013 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001014 }
1015}
1016
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001017int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018{
1019 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001021 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001022 struct batadv_tt_common_entry *tt_common_entry;
1023 struct batadv_tt_global_entry *tt_global;
1024 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +00001025 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001027 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028
Marek Lindner30da63a2012-08-03 17:15:46 +02001029 primary_if = batadv_seq_print_text_primary_if_get(seq);
1030 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001031 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001033 seq_printf(seq,
1034 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035 net_dev->name);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001036 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1037 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1038 "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040 for (i = 0; i < hash->size; i++) {
1041 head = &hash->table[i];
1042
Marek Lindner7aadf882011-02-18 12:28:09 +00001043 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001044 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +00001045 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001046 tt_global = container_of(tt_common_entry,
1047 struct batadv_tt_global_entry,
1048 common);
1049 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001051 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001052 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001053out:
1054 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001055 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001056 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001057}
1058
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001059/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001060static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001061batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001063 struct hlist_head *head;
1064 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001065 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001066
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001067 spin_lock_bh(&tt_global_entry->list_lock);
1068 head = &tt_global_entry->orig_list;
1069 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1070 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001071 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072 }
1073 spin_unlock_bh(&tt_global_entry->list_lock);
1074
1075}
1076
Sven Eckelmanna5130882012-05-16 20:23:16 +02001077static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001078batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1079 struct batadv_tt_global_entry *tt_global_entry,
1080 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001081 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001082{
1083 struct hlist_head *head;
1084 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001085 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001086
1087 spin_lock_bh(&tt_global_entry->list_lock);
1088 head = &tt_global_entry->orig_list;
1089 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1090 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001091 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001092 "Deleting %pM from global tt entry %pM: %s\n",
1093 orig_node->orig,
1094 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001095 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001096 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001097 }
1098 }
1099 spin_unlock_bh(&tt_global_entry->list_lock);
1100}
1101
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001102/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001103 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1104 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001105 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001106static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001107batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1108 struct batadv_tt_global_entry *tt_global_entry,
1109 struct batadv_orig_node *orig_node,
1110 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001111{
1112 bool last_entry = true;
1113 struct hlist_head *head;
1114 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001115 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001116
1117 /* no local entry exists, case 1:
1118 * Check if this is the last one or if other entries exist.
1119 */
1120
1121 rcu_read_lock();
1122 head = &tt_global_entry->orig_list;
1123 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1124 if (orig_entry->orig_node != orig_node) {
1125 last_entry = false;
1126 break;
1127 }
1128 }
1129 rcu_read_unlock();
1130
1131 if (last_entry) {
1132 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001133 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001134 tt_global_entry->roam_at = jiffies;
1135 } else
1136 /* there is another entry, we can simply delete this
1137 * one and can still use the other one.
1138 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001139 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1140 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001141}
1142
1143
1144
Sven Eckelmann56303d32012-06-05 22:31:31 +02001145static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1146 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001147 const unsigned char *addr,
1148 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001150 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001151 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001152
Sven Eckelmanna5130882012-05-16 20:23:16 +02001153 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001154 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001155 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001156
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001157 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001158 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1159 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001160
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001161 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001162 batadv_tt_global_free(bat_priv, tt_global_entry,
1163 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001164
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001165 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001166 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001167
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001168 /* if we are deleting a global entry due to a roam
1169 * event, there are two possibilities:
1170 * 1) the client roamed from node A to node B => if there
1171 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001172 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001173 * wait for node B to claim it. In case of timeout
1174 * the entry is purged.
1175 *
1176 * If there are other originators left, we directly delete
1177 * the originator.
1178 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001179 * the global entry, since it is useless now.
1180 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001181 local_entry = batadv_tt_local_hash_find(bat_priv,
1182 tt_global_entry->common.addr);
1183 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001184 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001185 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001186 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001187 } else
1188 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001189 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1190 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001191
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001192
Antonio Quartullicc47f662011-04-27 14:27:57 +02001193out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001194 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001195 batadv_tt_global_entry_free_ref(tt_global_entry);
1196 if (local_entry)
1197 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001198}
1199
Sven Eckelmann56303d32012-06-05 22:31:31 +02001200void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1201 struct batadv_orig_node *orig_node,
1202 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001203{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001204 struct batadv_tt_global_entry *tt_global;
1205 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001206 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001207 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001208 struct hlist_node *node, *safe;
1209 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001210 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001211
Simon Wunderlich6e801492011-10-19 10:28:26 +02001212 if (!hash)
1213 return;
1214
Antonio Quartullia73105b2011-04-27 14:27:44 +02001215 for (i = 0; i < hash->size; i++) {
1216 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001217 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001218
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001219 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001220 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001221 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001222 tt_global = container_of(tt_common_entry,
1223 struct batadv_tt_global_entry,
1224 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225
Sven Eckelmann56303d32012-06-05 22:31:31 +02001226 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001227 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001228
Sven Eckelmann56303d32012-06-05 22:31:31 +02001229 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001230 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001231 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001232 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001233 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001234 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001235 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001236 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001237 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001238 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001239 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001240}
1241
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001242static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1243 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001244{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001245 bool purge = false;
1246 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1247 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001248
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001249 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1250 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1251 purge = true;
1252 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001253 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001254
1255 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1256 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1257 purge = true;
1258 *msg = "Temporary client timeout\n";
1259 }
1260
1261 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001262}
1263
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001264static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001265{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001266 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001267 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001268 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001269 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001270 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001271 char *msg = NULL;
1272 struct batadv_tt_common_entry *tt_common;
1273 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001274
Antonio Quartullicc47f662011-04-27 14:27:57 +02001275 for (i = 0; i < hash->size; i++) {
1276 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001277 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001278
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001279 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001280 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1281 hash_entry) {
1282 tt_global = container_of(tt_common,
1283 struct batadv_tt_global_entry,
1284 common);
1285
1286 if (!batadv_tt_global_to_purge(tt_global, &msg))
1287 continue;
1288
1289 batadv_dbg(BATADV_DBG_TT, bat_priv,
1290 "Deleting global tt entry (%pM): %s\n",
1291 tt_global->common.addr, msg);
1292
1293 hlist_del_rcu(node);
1294
1295 batadv_tt_global_entry_free_ref(tt_global);
1296 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001297 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001298 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001299}
1300
Sven Eckelmann56303d32012-06-05 22:31:31 +02001301static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001302{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001303 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001305 struct batadv_tt_common_entry *tt_common_entry;
1306 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001307 struct hlist_node *node, *node_tmp;
1308 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001309 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001310
Sven Eckelmann807736f2012-07-15 22:26:51 +02001311 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001312 return;
1313
Sven Eckelmann807736f2012-07-15 22:26:51 +02001314 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001315
1316 for (i = 0; i < hash->size; i++) {
1317 head = &hash->table[i];
1318 list_lock = &hash->list_locks[i];
1319
1320 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001321 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001322 head, hash_entry) {
1323 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001324 tt_global = container_of(tt_common_entry,
1325 struct batadv_tt_global_entry,
1326 common);
1327 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001328 }
1329 spin_unlock_bh(list_lock);
1330 }
1331
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001332 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001333
Sven Eckelmann807736f2012-07-15 22:26:51 +02001334 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001335}
1336
Sven Eckelmann56303d32012-06-05 22:31:31 +02001337static bool
1338_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1339 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001340{
1341 bool ret = false;
1342
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001343 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1344 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001345 ret = true;
1346
1347 return ret;
1348}
1349
Sven Eckelmann56303d32012-06-05 22:31:31 +02001350struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1351 const uint8_t *src,
1352 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001353{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001354 struct batadv_tt_local_entry *tt_local_entry = NULL;
1355 struct batadv_tt_global_entry *tt_global_entry = NULL;
1356 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001357 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001358
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001359 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001360 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001361 if (!tt_local_entry ||
1362 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001363 goto out;
1364 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001365
Sven Eckelmanna5130882012-05-16 20:23:16 +02001366 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001367 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001368 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001369
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001370 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001371 * isolation
1372 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001373 if (tt_local_entry &&
1374 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001375 goto out;
1376
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001377 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001378 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001379 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001380 if (best_entry)
1381 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001382 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1383 orig_node = NULL;
1384 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001385
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001386out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001387 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001388 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001389 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001390 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001391
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001392 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001393}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001394
1395/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001396static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1397 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398{
1399 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001400 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001401 struct batadv_tt_common_entry *tt_common;
1402 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001403 struct hlist_node *node;
1404 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001405 uint32_t i;
1406 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001407
1408 for (i = 0; i < hash->size; i++) {
1409 head = &hash->table[i];
1410
1411 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001412 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001413 tt_global = container_of(tt_common,
1414 struct batadv_tt_global_entry,
1415 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001416 /* Roaming clients are in the global table for
1417 * consistency only. They don't have to be
1418 * taken into account while computing the
1419 * global crc
1420 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001421 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001422 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001423 /* Temporary clients have not been announced yet, so
1424 * they have to be skipped while computing the global
1425 * crc
1426 */
1427 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1428 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001429
1430 /* find out if this global entry is announced by this
1431 * originator
1432 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001433 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001434 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001435 continue;
1436
1437 total_one = 0;
1438 for (j = 0; j < ETH_ALEN; j++)
1439 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001440 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001441 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001442 }
1443 rcu_read_unlock();
1444 }
1445
1446 return total;
1447}
1448
1449/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001450static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001451{
1452 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001453 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001454 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455 struct hlist_node *node;
1456 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001457 uint32_t i;
1458 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001459
1460 for (i = 0; i < hash->size; i++) {
1461 head = &hash->table[i];
1462
1463 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001464 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001465 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001466 * account while computing the CRC
1467 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001468 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001469 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470 total_one = 0;
1471 for (j = 0; j < ETH_ALEN; j++)
1472 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001473 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 total ^= total_one;
1475 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 rcu_read_unlock();
1477 }
1478
1479 return total;
1480}
1481
Sven Eckelmann56303d32012-06-05 22:31:31 +02001482static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001484 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001485
Sven Eckelmann807736f2012-07-15 22:26:51 +02001486 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001487
Sven Eckelmann807736f2012-07-15 22:26:51 +02001488 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489 list_del(&node->list);
1490 kfree(node);
1491 }
1492
Sven Eckelmann807736f2012-07-15 22:26:51 +02001493 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001494}
1495
Sven Eckelmann56303d32012-06-05 22:31:31 +02001496static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1497 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001498 const unsigned char *tt_buff,
1499 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001501 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001502
1503 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001504 * last OGM (the OGM could carry no changes)
1505 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001506 spin_lock_bh(&orig_node->tt_buff_lock);
1507 if (tt_buff_len > 0) {
1508 kfree(orig_node->tt_buff);
1509 orig_node->tt_buff_len = 0;
1510 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1511 if (orig_node->tt_buff) {
1512 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1513 orig_node->tt_buff_len = tt_buff_len;
1514 }
1515 }
1516 spin_unlock_bh(&orig_node->tt_buff_lock);
1517}
1518
Sven Eckelmann56303d32012-06-05 22:31:31 +02001519static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001521 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001522
Sven Eckelmann807736f2012-07-15 22:26:51 +02001523 spin_lock_bh(&bat_priv->tt.req_list_lock);
1524 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001525 if (batadv_has_timed_out(node->issued_at,
1526 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527 list_del(&node->list);
1528 kfree(node);
1529 }
1530 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001531 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001532}
1533
1534/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001535 * has already been issued for this orig_node, NULL otherwise
1536 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001537static struct batadv_tt_req_node *
1538batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1539 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001540{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001541 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542
Sven Eckelmann807736f2012-07-15 22:26:51 +02001543 spin_lock_bh(&bat_priv->tt.req_list_lock);
1544 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001545 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1546 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001547 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001548 goto unlock;
1549 }
1550
1551 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1552 if (!tt_req_node)
1553 goto unlock;
1554
1555 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1556 tt_req_node->issued_at = jiffies;
1557
Sven Eckelmann807736f2012-07-15 22:26:51 +02001558 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001560 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561 return tt_req_node;
1562}
1563
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001564/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001565static int batadv_tt_local_valid_entry(const void *entry_ptr,
1566 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001567{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001568 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001569
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001570 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001571 return 0;
1572 return 1;
1573}
1574
Sven Eckelmanna5130882012-05-16 20:23:16 +02001575static int batadv_tt_global_valid(const void *entry_ptr,
1576 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001577{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001578 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1579 const struct batadv_tt_global_entry *tt_global_entry;
1580 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001582 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1583 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001584 return 0;
1585
Sven Eckelmann56303d32012-06-05 22:31:31 +02001586 tt_global_entry = container_of(tt_common_entry,
1587 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001588 common);
1589
Sven Eckelmanna5130882012-05-16 20:23:16 +02001590 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001591}
1592
Sven Eckelmanna5130882012-05-16 20:23:16 +02001593static struct sk_buff *
1594batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001595 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001596 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001597 int (*valid_cb)(const void *, const void *),
1598 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001600 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001601 struct batadv_tt_query_packet *tt_response;
1602 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001603 struct hlist_node *node;
1604 struct hlist_head *head;
1605 struct sk_buff *skb = NULL;
1606 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001607 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001608 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001609 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610
1611 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1612 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001613 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001615 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616
Sven Eckelmann96412692012-06-05 22:31:30 +02001617 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001618 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619 if (!skb)
1620 goto out;
1621
Sven Eckelmann5b246572012-11-04 17:11:45 +01001622 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001623 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001625
Sven Eckelmann96412692012-06-05 22:31:30 +02001626 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001627 tt_count = 0;
1628
1629 rcu_read_lock();
1630 for (i = 0; i < hash->size; i++) {
1631 head = &hash->table[i];
1632
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001633 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001634 head, hash_entry) {
1635 if (tt_count == tt_tot)
1636 break;
1637
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001638 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 continue;
1640
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001641 memcpy(tt_change->addr, tt_common_entry->addr,
1642 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001643 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644
1645 tt_count++;
1646 tt_change++;
1647 }
1648 }
1649 rcu_read_unlock();
1650
Antonio Quartulli9d852392011-10-17 14:25:13 +02001651 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001652 * copied
1653 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001654 tt_response->tt_data = htons(tt_count);
1655
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656out:
1657 return skb;
1658}
1659
Sven Eckelmann56303d32012-06-05 22:31:31 +02001660static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1661 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001662 uint8_t ttvn, uint16_t tt_crc,
1663 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001664{
1665 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001666 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001667 struct batadv_hard_iface *primary_if;
1668 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001669 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001670 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001671
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001672 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 if (!primary_if)
1674 goto out;
1675
1676 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001677 * reply from the same orig_node yet
1678 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001679 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001680 if (!tt_req_node)
1681 goto out;
1682
Sven Eckelmann5b246572012-11-04 17:11:45 +01001683 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684 if (!skb)
1685 goto out;
1686
Sven Eckelmann5b246572012-11-04 17:11:45 +01001687 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688
Sven Eckelmann96412692012-06-05 22:31:30 +02001689 tt_req_len = sizeof(*tt_request);
1690 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001692 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001693 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1695 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001696 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001698 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001699 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700
1701 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001702 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001704 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1705 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001707 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001708
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001709 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1710 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001711
1712out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001713 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001714 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 if (ret)
1716 kfree_skb(skb);
1717 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001718 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001720 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721 kfree(tt_req_node);
1722 }
1723 return ret;
1724}
1725
Sven Eckelmann96412692012-06-05 22:31:30 +02001726static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001727batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001728 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001729{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001730 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001731 struct batadv_orig_node *res_dst_orig_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001732 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001733 uint8_t orig_ttvn, req_ttvn, ttvn;
1734 int ret = false;
1735 unsigned char *tt_buff;
1736 bool full_table;
1737 uint16_t tt_len, tt_tot;
1738 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001739 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001740 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001741 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001743 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001744 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1745 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001746 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747
1748 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001749 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750 if (!req_dst_orig_node)
1751 goto out;
1752
Sven Eckelmannda641192012-05-12 13:48:56 +02001753 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754 if (!res_dst_orig_node)
1755 goto out;
1756
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001757 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001758 if (!primary_if)
1759 goto out;
1760
1761 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1762 req_ttvn = tt_request->ttvn;
1763
Antonio Quartulli015758d2011-07-09 17:52:13 +02001764 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001766 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001767 goto out;
1768
Antonio Quartulli015758d2011-07-09 17:52:13 +02001769 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001770 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 !req_dst_orig_node->tt_buff)
1772 full_table = true;
1773 else
1774 full_table = false;
1775
1776 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001777 * I'll send only one packet with as much TT entries as I can
1778 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001779 if (!full_table) {
1780 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1781 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001782 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783
Sven Eckelmann96412692012-06-05 22:31:30 +02001784 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001785 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001786 if (!skb)
1787 goto unlock;
1788
Sven Eckelmann5b246572012-11-04 17:11:45 +01001789 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001790 packet_pos = skb_put(skb, len);
1791 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001792 tt_response->ttvn = req_ttvn;
1793 tt_response->tt_data = htons(tt_tot);
1794
Sven Eckelmann96412692012-06-05 22:31:30 +02001795 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796 /* Copy the last orig_node's OGM buffer */
1797 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1798 req_dst_orig_node->tt_buff_len);
1799
1800 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1801 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001802 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1803 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001804 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1805
Sven Eckelmanna5130882012-05-16 20:23:16 +02001806 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001807 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001808 primary_if,
1809 batadv_tt_global_valid,
1810 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001811 if (!skb)
1812 goto out;
1813
Sven Eckelmann96412692012-06-05 22:31:30 +02001814 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 }
1816
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001817 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001818 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001819 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1821 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001822 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
1824 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001825 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001826
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001827 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001828 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1829 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001830
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001831 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001832
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001833 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1834 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001835 goto out;
1836
1837unlock:
1838 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1839
1840out:
1841 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001842 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001844 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001846 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001847 if (!ret)
1848 kfree_skb(skb);
1849 return ret;
1850
1851}
Sven Eckelmann96412692012-06-05 22:31:30 +02001852
1853static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001854batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001855 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001857 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001858 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859 uint8_t my_ttvn, req_ttvn, ttvn;
1860 int ret = false;
1861 unsigned char *tt_buff;
1862 bool full_table;
1863 uint16_t tt_len, tt_tot;
1864 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001865 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001866 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001867 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001869 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001870 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1871 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001872 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001873
1874
Sven Eckelmann807736f2012-07-15 22:26:51 +02001875 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876 req_ttvn = tt_request->ttvn;
1877
Sven Eckelmannda641192012-05-12 13:48:56 +02001878 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879 if (!orig_node)
1880 goto out;
1881
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001882 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 if (!primary_if)
1884 goto out;
1885
1886 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001887 * is too big send the whole local translation table
1888 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001889 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001890 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891 full_table = true;
1892 else
1893 full_table = false;
1894
1895 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001896 * I'll send only one packet with as much TT entries as I can
1897 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001899 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1900 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001901 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902
Sven Eckelmann96412692012-06-05 22:31:30 +02001903 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001904 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905 if (!skb)
1906 goto unlock;
1907
Sven Eckelmann5b246572012-11-04 17:11:45 +01001908 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001909 packet_pos = skb_put(skb, len);
1910 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001911 tt_response->ttvn = req_ttvn;
1912 tt_response->tt_data = htons(tt_tot);
1913
Sven Eckelmann96412692012-06-05 22:31:30 +02001914 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001915 memcpy(tt_buff, bat_priv->tt.last_changeset,
1916 bat_priv->tt.last_changeset_len);
1917 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001918 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001919 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001920 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001921 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922
Sven Eckelmanna5130882012-05-16 20:23:16 +02001923 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001924 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001925 primary_if,
1926 batadv_tt_local_valid_entry,
1927 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928 if (!skb)
1929 goto out;
1930
Sven Eckelmann96412692012-06-05 22:31:30 +02001931 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 }
1933
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001934 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001935 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001936 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1938 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001939 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001940
1941 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001942 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001943
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001944 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001945 "Sending TT_RESPONSE to %pM [%c]\n",
1946 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001947 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001948
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001949 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001950
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001951 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1952 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001953 goto out;
1954
1955unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001956 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001957out:
1958 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001959 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001960 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001961 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962 if (!ret)
1963 kfree_skb(skb);
1964 /* This packet was for me, so it doesn't need to be re-routed */
1965 return true;
1966}
1967
Sven Eckelmann56303d32012-06-05 22:31:31 +02001968bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001969 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001970{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001971 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001972 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001973 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001974 return true;
1975
Sven Eckelmanna5130882012-05-16 20:23:16 +02001976 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001977 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001978 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001979 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001980}
1981
Sven Eckelmann56303d32012-06-05 22:31:31 +02001982static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1983 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001984 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001985 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986{
1987 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001988 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001989
1990 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001991 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1992 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001993 batadv_tt_global_del(bat_priv, orig_node,
1994 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001995 "tt removed by changes",
1996 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001997 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001998 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001999 (tt_change + i)->addr,
2000 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001 /* In case of problem while storing a
2002 * global_entry, we stop the updating
2003 * procedure without committing the
2004 * ttvn change. This will avoid to send
2005 * corrupted data on tt_request
2006 */
2007 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002008 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002010 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011}
2012
Sven Eckelmann56303d32012-06-05 22:31:31 +02002013static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002014 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002015{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002016 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002017
Sven Eckelmannda641192012-05-12 13:48:56 +02002018 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019 if (!orig_node)
2020 goto out;
2021
2022 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002023 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002024
Sven Eckelmanna5130882012-05-16 20:23:16 +02002025 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002026 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002027 ntohs(tt_response->tt_data),
2028 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002029
2030 spin_lock_bh(&orig_node->tt_buff_lock);
2031 kfree(orig_node->tt_buff);
2032 orig_node->tt_buff_len = 0;
2033 orig_node->tt_buff = NULL;
2034 spin_unlock_bh(&orig_node->tt_buff_lock);
2035
2036 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2037
2038out:
2039 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002040 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002041}
2042
Sven Eckelmann56303d32012-06-05 22:31:31 +02002043static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2044 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002045 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002046 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002047{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002048 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2049 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050
Sven Eckelmanna5130882012-05-16 20:23:16 +02002051 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2052 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002053 atomic_set(&orig_node->last_ttvn, ttvn);
2054}
2055
Sven Eckelmann56303d32012-06-05 22:31:31 +02002056bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002057{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002058 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002059 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002060
Sven Eckelmanna5130882012-05-16 20:23:16 +02002061 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002062 if (!tt_local_entry)
2063 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002064 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002065 * consistency purpose)
2066 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002067 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2068 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002069 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002070 ret = true;
2071out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002073 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002074 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002075}
2076
Sven Eckelmann56303d32012-06-05 22:31:31 +02002077void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002078 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002079{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002080 struct batadv_tt_req_node *node, *safe;
2081 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002082 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002083
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002084 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002085 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2086 tt_response->src, tt_response->ttvn,
2087 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002088 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002089
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002090 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002091 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002092 goto out;
2093
Sven Eckelmannda641192012-05-12 13:48:56 +02002094 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002095 if (!orig_node)
2096 goto out;
2097
Sven Eckelmann96412692012-06-05 22:31:30 +02002098 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002099 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002100 } else {
2101 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002102 batadv_tt_update_changes(bat_priv, orig_node,
2103 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002104 tt_response->ttvn, tt_change);
2105 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106
2107 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002108 spin_lock_bh(&bat_priv->tt.req_list_lock);
2109 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002110 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111 continue;
2112 list_del(&node->list);
2113 kfree(node);
2114 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002115 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116
2117 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002118 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002119out:
2120 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002121 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122}
2123
Sven Eckelmann56303d32012-06-05 22:31:31 +02002124int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002125{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002126 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002127
Sven Eckelmanna5130882012-05-16 20:23:16 +02002128 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002129 if (ret < 0)
2130 return ret;
2131
Sven Eckelmanna5130882012-05-16 20:23:16 +02002132 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002133 if (ret < 0)
2134 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002135
Sven Eckelmanna5130882012-05-16 20:23:16 +02002136 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002137
2138 return 1;
2139}
2140
Sven Eckelmann56303d32012-06-05 22:31:31 +02002141static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002142{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002143 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144
Sven Eckelmann807736f2012-07-15 22:26:51 +02002145 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002146
Sven Eckelmann807736f2012-07-15 22:26:51 +02002147 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002148 list_del(&node->list);
2149 kfree(node);
2150 }
2151
Sven Eckelmann807736f2012-07-15 22:26:51 +02002152 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002153}
2154
Sven Eckelmann56303d32012-06-05 22:31:31 +02002155static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002156{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002157 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002158
Sven Eckelmann807736f2012-07-15 22:26:51 +02002159 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2160 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002161 if (!batadv_has_timed_out(node->first_time,
2162 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002163 continue;
2164
2165 list_del(&node->list);
2166 kfree(node);
2167 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002168 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002169}
2170
2171/* This function checks whether the client already reached the
2172 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2173 * will not be sent.
2174 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002175 * returns true if the ROAMING_ADV can be sent, false otherwise
2176 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002177static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002178 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002179{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002180 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002181 bool ret = false;
2182
Sven Eckelmann807736f2012-07-15 22:26:51 +02002183 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002184 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002185 * reply from the same orig_node yet
2186 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002187 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002188 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002189 continue;
2190
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002191 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002192 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002193 continue;
2194
Sven Eckelmann3e348192012-05-16 20:23:22 +02002195 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002196 /* Sorry, you roamed too many times! */
2197 goto unlock;
2198 ret = true;
2199 break;
2200 }
2201
2202 if (!ret) {
2203 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2204 if (!tt_roam_node)
2205 goto unlock;
2206
2207 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002208 atomic_set(&tt_roam_node->counter,
2209 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002210 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2211
Sven Eckelmann807736f2012-07-15 22:26:51 +02002212 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002213 ret = true;
2214 }
2215
2216unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002217 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002218 return ret;
2219}
2220
Sven Eckelmann56303d32012-06-05 22:31:31 +02002221static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2222 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002223{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002224 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002225 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002226 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002227 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002228 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002229
2230 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002231 * already roamed to us too many times
2232 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002233 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002234 goto out;
2235
Sven Eckelmann5b246572012-11-04 17:11:45 +01002236 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002237 if (!skb)
2238 goto out;
2239
Sven Eckelmann5b246572012-11-04 17:11:45 +01002240 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002241
Sven Eckelmann96412692012-06-05 22:31:30 +02002242 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002243
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002244 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002245 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002246 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002247 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002248 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002249 if (!primary_if)
2250 goto out;
2251 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002252 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002253 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2254 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2255
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002256 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002257 "Sending ROAMING_ADV to %pM (client %pM)\n",
2258 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002259
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002260 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002261
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002262 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2263 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002264
2265out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002266 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002267 kfree_skb(skb);
2268 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002269}
2270
Sven Eckelmanna5130882012-05-16 20:23:16 +02002271static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002272{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002273 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002274 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002275 struct batadv_priv *bat_priv;
2276
2277 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002278 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2279 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002280
Sven Eckelmanna5130882012-05-16 20:23:16 +02002281 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002282 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002283 batadv_tt_req_purge(bat_priv);
2284 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002285
Sven Eckelmanna5130882012-05-16 20:23:16 +02002286 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002287}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002288
Sven Eckelmann56303d32012-06-05 22:31:31 +02002289void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002290{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002291 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002292
Sven Eckelmanna5130882012-05-16 20:23:16 +02002293 batadv_tt_local_table_free(bat_priv);
2294 batadv_tt_global_table_free(bat_priv);
2295 batadv_tt_req_list_free(bat_priv);
2296 batadv_tt_changes_list_free(bat_priv);
2297 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002298
Sven Eckelmann807736f2012-07-15 22:26:51 +02002299 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002300}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002301
Antonio Quartulli697f2532011-11-07 16:47:01 +01002302/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002303 * in the given hash table and returns the number of modified entries
2304 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002305static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2306 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002307{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002308 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002309 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002310 struct hlist_head *head;
2311 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002312 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002313
2314 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002315 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002316
2317 for (i = 0; i < hash->size; i++) {
2318 head = &hash->table[i];
2319
2320 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002321 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002322 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002323 if (enable) {
2324 if ((tt_common_entry->flags & flags) == flags)
2325 continue;
2326 tt_common_entry->flags |= flags;
2327 } else {
2328 if (!(tt_common_entry->flags & flags))
2329 continue;
2330 tt_common_entry->flags &= ~flags;
2331 }
2332 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002333 }
2334 rcu_read_unlock();
2335 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002336out:
2337 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002338}
2339
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002340/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002341static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002342{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002343 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002344 struct batadv_tt_common_entry *tt_common;
2345 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002346 struct hlist_node *node, *node_tmp;
2347 struct hlist_head *head;
2348 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002349 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002350
2351 if (!hash)
2352 return;
2353
2354 for (i = 0; i < hash->size; i++) {
2355 head = &hash->table[i];
2356 list_lock = &hash->list_locks[i];
2357
2358 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002359 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2360 hash_entry) {
2361 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002362 continue;
2363
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002364 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002365 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002366 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002367
Sven Eckelmann807736f2012-07-15 22:26:51 +02002368 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002369 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002370 tt_local = container_of(tt_common,
2371 struct batadv_tt_local_entry,
2372 common);
2373 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002374 }
2375 spin_unlock_bh(list_lock);
2376 }
2377
2378}
2379
Sven Eckelmann56303d32012-06-05 22:31:31 +02002380static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002381 unsigned char **packet_buff,
2382 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002383{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002384 uint16_t changed_num = 0;
2385
Sven Eckelmann807736f2012-07-15 22:26:51 +02002386 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002387 return -ENOENT;
2388
Sven Eckelmann807736f2012-07-15 22:26:51 +02002389 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002390 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002391
2392 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002393 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002394 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002395 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002396
2397 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002398 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002399 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002400 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002401 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002402
2403 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002404 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002405
Sven Eckelmanna5130882012-05-16 20:23:16 +02002406 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2407 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002408}
2409
2410/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002411int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002412 unsigned char **packet_buff, int *packet_buff_len,
2413 int packet_min_len)
2414{
2415 int tt_num_changes;
2416
2417 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002418 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2419 packet_buff_len,
2420 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002421
2422 /* if the changes have been sent often enough */
2423 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002424 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002425 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2426 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002427 tt_num_changes = 0;
2428 }
2429
2430 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002431}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002432
Sven Eckelmann56303d32012-06-05 22:31:31 +02002433bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002434 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002435{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002436 struct batadv_tt_local_entry *tt_local_entry = NULL;
2437 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002438 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002439
2440 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002441 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002442
Sven Eckelmanna5130882012-05-16 20:23:16 +02002443 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002444 if (!tt_local_entry)
2445 goto out;
2446
Sven Eckelmanna5130882012-05-16 20:23:16 +02002447 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002448 if (!tt_global_entry)
2449 goto out;
2450
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002451 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002452 goto out;
2453
Marek Lindner5870adc2012-06-20 17:16:05 +02002454 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002455
2456out:
2457 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002458 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002459 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002460 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002461 return ret;
2462}
Marek Lindnera943cac2011-07-30 13:10:18 +02002463
Sven Eckelmann56303d32012-06-05 22:31:31 +02002464void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2465 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002466 const unsigned char *tt_buff, uint8_t tt_num_changes,
2467 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002468{
2469 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2470 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002471 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002472
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002473 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002474 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002475 return;
2476
Antonio Quartulli17071572011-11-07 16:36:40 +01002477 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002478 * increased by one -> we can apply the attached changes
2479 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002480 if ((!orig_node->tt_initialised && ttvn == 1) ||
2481 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002482 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002483 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2484 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002485 * In this case send a tt request
2486 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002487 if (!tt_num_changes) {
2488 full_table = false;
2489 goto request_table;
2490 }
2491
Sven Eckelmann96412692012-06-05 22:31:30 +02002492 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002493 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002494 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002495
2496 /* Even if we received the precomputed crc with the OGM, we
2497 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002498 * in the global table
2499 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002500 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002501
2502 /* The ttvn alone is not enough to guarantee consistency
2503 * because a single value could represent different states
2504 * (due to the wrap around). Thus a node has to check whether
2505 * the resulting table (after applying the changes) is still
2506 * consistent or not. E.g. a node could disconnect while its
2507 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2508 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002509 * inconsistency
2510 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002511 if (orig_node->tt_crc != tt_crc)
2512 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002513 } else {
2514 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002515 * in sync anymore -> request fresh tt data
2516 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002517 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2518 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002519request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002520 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002521 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2522 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2523 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002524 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2525 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002526 return;
2527 }
2528 }
2529}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002530
2531/* returns true whether we know that the client has moved from its old
2532 * originator to another one. This entry is kept is still kept for consistency
2533 * purposes
2534 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002535bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002536 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002537{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002538 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002539 bool ret = false;
2540
Sven Eckelmanna5130882012-05-16 20:23:16 +02002541 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002542 if (!tt_global_entry)
2543 goto out;
2544
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002545 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002546 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002547out:
2548 return ret;
2549}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002550
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002551/**
2552 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2553 * @bat_priv: the bat priv with all the soft interface information
2554 * @addr: the MAC address of the local client to query
2555 *
2556 * Returns true if the local client is known to be roaming (it is not served by
2557 * this node anymore) or not. If yes, the client is still present in the table
2558 * to keep the latter consistent with the node TTVN
2559 */
2560bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2561 uint8_t *addr)
2562{
2563 struct batadv_tt_local_entry *tt_local_entry;
2564 bool ret = false;
2565
2566 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2567 if (!tt_local_entry)
2568 goto out;
2569
2570 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2571 batadv_tt_local_entry_free_ref(tt_local_entry);
2572out:
2573 return ret;
2574
2575}
2576
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002577bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2578 struct batadv_orig_node *orig_node,
2579 const unsigned char *addr)
2580{
2581 bool ret = false;
2582
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002583 /* if the originator is a backbone node (meaning it belongs to the same
2584 * LAN of this node) the temporary client must not be added because to
2585 * reach such destination the node must use the LAN instead of the mesh
2586 */
2587 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2588 goto out;
2589
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002590 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2591 BATADV_TT_CLIENT_TEMP,
2592 atomic_read(&orig_node->last_ttvn)))
2593 goto out;
2594
2595 batadv_dbg(BATADV_DBG_TT, bat_priv,
2596 "Added temporary global client (addr: %pM orig: %pM)\n",
2597 addr, orig_node->orig);
2598 ret = true;
2599out:
2600 return ret;
2601}