blob: cdad824a0014a4b0d9951cca0131419088776f66 [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;
475 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000476 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200478 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479
Marek Lindner30da63a2012-08-03 17:15:46 +0200480 primary_if = batadv_seq_print_text_primary_if_get(seq);
481 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200482 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100484 seq_printf(seq,
485 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200486 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488 for (i = 0; i < hash->size; i++) {
489 head = &hash->table[i];
490
Marek Lindner7aadf882011-02-18 12:28:09 +0000491 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100492 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000493 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200494 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100495 tt_common_entry->addr,
496 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200497 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100498 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200499 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100500 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200501 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100502 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200503 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100504 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200505 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000507 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200509out:
510 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200511 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200512 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513}
514
Sven Eckelmann56303d32012-06-05 22:31:31 +0200515static void
516batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
517 struct batadv_tt_local_entry *tt_local_entry,
518 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200520 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
521 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522
Antonio Quartulli015758d2011-07-09 17:52:13 +0200523 /* The local client has to be marked as "pending to be removed" but has
524 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200525 * response issued before the net ttvn increment (consistency check)
526 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200527 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100528
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200529 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200530 "Local tt entry (%pM) pending to be removed: %s\n",
531 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532}
533
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200534/**
535 * batadv_tt_local_remove - logically remove an entry from the local table
536 * @bat_priv: the bat priv with all the soft interface information
537 * @addr: the MAC address of the client to remove
538 * @message: message to append to the log on deletion
539 * @roaming: true if the deletion is due to a roaming event
540 *
541 * Returns the flags assigned to the local entry before being deleted
542 */
543uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
544 const uint8_t *addr, const char *message,
545 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200547 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200548 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
Sven Eckelmanna5130882012-05-16 20:23:16 +0200550 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200551 if (!tt_local_entry)
552 goto out;
553
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200554 curr_flags = tt_local_entry->common.flags;
555
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200556 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200557 /* if this global entry addition is due to a roaming, the node has to
558 * mark the local entry as "roamed" in order to correctly reroute
559 * packets later
560 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200561 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200562 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200563 /* mark the local client as ROAMed */
564 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
565 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200566
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200567 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
568 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
569 message);
570 goto out;
571 }
572 /* if this client has been added right now, it is possible to
573 * immediately purge it
574 */
575 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
576 curr_flags | BATADV_TT_CLIENT_DEL);
577 hlist_del_rcu(&tt_local_entry->common.hash_entry);
578 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200579
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200580out:
581 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200582 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200583
584 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585}
586
Sven Eckelmann56303d32012-06-05 22:31:31 +0200587static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200588 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200590 struct batadv_tt_local_entry *tt_local_entry;
591 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000592 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200593
594 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
595 hash_entry) {
596 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200597 struct batadv_tt_local_entry,
598 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200599 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
600 continue;
601
602 /* entry already marked for deletion */
603 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
604 continue;
605
606 if (!batadv_has_timed_out(tt_local_entry->last_seen,
607 BATADV_TT_LOCAL_TIMEOUT))
608 continue;
609
610 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
611 BATADV_TT_CLIENT_DEL, "timed out");
612 }
613}
614
Sven Eckelmann56303d32012-06-05 22:31:31 +0200615static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200616{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200617 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200619 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200620 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 for (i = 0; i < hash->size; i++) {
623 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200624 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200626 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200627 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200628 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 }
630
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631}
632
Sven Eckelmann56303d32012-06-05 22:31:31 +0200633static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200635 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200636 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200637 struct batadv_tt_common_entry *tt_common_entry;
638 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200639 struct hlist_node *node, *node_tmp;
640 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200641 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200642
Sven Eckelmann807736f2012-07-15 22:26:51 +0200643 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644 return;
645
Sven Eckelmann807736f2012-07-15 22:26:51 +0200646 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200647
648 for (i = 0; i < hash->size; i++) {
649 head = &hash->table[i];
650 list_lock = &hash->list_locks[i];
651
652 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100653 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200654 head, hash_entry) {
655 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200656 tt_local = container_of(tt_common_entry,
657 struct batadv_tt_local_entry,
658 common);
659 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200660 }
661 spin_unlock_bh(list_lock);
662 }
663
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200664 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200665
Sven Eckelmann807736f2012-07-15 22:26:51 +0200666 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667}
668
Sven Eckelmann56303d32012-06-05 22:31:31 +0200669static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200671 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200672 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673
Sven Eckelmann807736f2012-07-15 22:26:51 +0200674 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675
Sven Eckelmann807736f2012-07-15 22:26:51 +0200676 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200677 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678
Sven Eckelmann5346c352012-05-05 13:27:28 +0200679 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680}
681
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200683{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200685
Sven Eckelmann807736f2012-07-15 22:26:51 +0200686 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200687
Sven Eckelmann807736f2012-07-15 22:26:51 +0200688 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200689 list) {
690 list_del(&entry->list);
691 kfree(entry);
692 }
693
Sven Eckelmann807736f2012-07-15 22:26:51 +0200694 atomic_set(&bat_priv->tt.local_changes, 0);
695 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200696}
697
Antonio Quartullid657e622012-07-01 14:09:12 +0200698/* retrieves the orig_tt_list_entry belonging to orig_node from the
699 * batadv_tt_global_entry list
700 *
701 * returns it with an increased refcounter, NULL if not found
702 */
703static struct batadv_tt_orig_list_entry *
704batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
705 const struct batadv_orig_node *orig_node)
706{
707 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
708 const struct hlist_head *head;
709 struct hlist_node *node;
710
711 rcu_read_lock();
712 head = &entry->orig_list;
713 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
714 if (tmp_orig_entry->orig_node != orig_node)
715 continue;
716 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
717 continue;
718
719 orig_entry = tmp_orig_entry;
720 break;
721 }
722 rcu_read_unlock();
723
724 return orig_entry;
725}
726
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200727/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200728 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200729 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200730static bool
731batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
732 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200733{
Antonio Quartullid657e622012-07-01 14:09:12 +0200734 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200735 bool found = false;
736
Antonio Quartullid657e622012-07-01 14:09:12 +0200737 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
738 if (orig_entry) {
739 found = true;
740 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200742
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200743 return found;
744}
745
Sven Eckelmanna5130882012-05-16 20:23:16 +0200746static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200747batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200748 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200749{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200750 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200751
Antonio Quartullid657e622012-07-01 14:09:12 +0200752 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200753 if (orig_entry) {
754 /* refresh the ttvn: the current value could be a bogus one that
755 * was added during a "temporary client detection"
756 */
757 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200758 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200759 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200760
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200761 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
762 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200763 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200764
765 INIT_HLIST_NODE(&orig_entry->list);
766 atomic_inc(&orig_node->refcount);
767 atomic_inc(&orig_node->tt_size);
768 orig_entry->orig_node = orig_node;
769 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200770 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200771
Antonio Quartullid657e622012-07-01 14:09:12 +0200772 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200773 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200774 &tt_global->orig_list);
775 spin_unlock_bh(&tt_global->list_lock);
776out:
777 if (orig_entry)
778 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200779}
780
Antonio Quartullia73105b2011-04-27 14:27:44 +0200781/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200782int batadv_tt_global_add(struct batadv_priv *bat_priv,
783 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200784 const unsigned char *tt_addr, uint8_t flags,
785 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200787 struct batadv_tt_global_entry *tt_global_entry;
788 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200789 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100790 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200791 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200792 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793
Sven Eckelmanna5130882012-05-16 20:23:16 +0200794 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200795 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
796
797 /* if the node already has a local client for this entry, it has to wait
798 * for a roaming advertisement instead of manually messing up the global
799 * table
800 */
801 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
802 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
803 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804
Antonio Quartullia73105b2011-04-27 14:27:44 +0200805 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200806 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200807 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200808 goto out;
809
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200810 common = &tt_global_entry->common;
811 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200812
Antonio Quartullid4f44692012-05-25 00:00:54 +0200813 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200814 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200815 /* node must store current time in case of roaming. This is
816 * needed to purge this entry out on timeout (if nobody claims
817 * it)
818 */
819 if (flags & BATADV_TT_CLIENT_ROAM)
820 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200821 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200822 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200823
824 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
825 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200826
Sven Eckelmann807736f2012-07-15 22:26:51 +0200827 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200828 batadv_compare_tt,
829 batadv_choose_orig, common,
830 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100831
832 if (unlikely(hash_added != 0)) {
833 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200834 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100835 goto out_remove;
836 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200837 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200838 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200839 /* If there is already a global entry, we can use this one for
840 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200841 * But if we are trying to add a temporary client then here are
842 * two options at this point:
843 * 1) the global client is not a temporary client: the global
844 * client has to be left as it is, temporary information
845 * should never override any already known client state
846 * 2) the global client is a temporary client: purge the
847 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200848 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200849 if (flags & BATADV_TT_CLIENT_TEMP) {
850 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
851 goto out;
852 if (batadv_tt_global_entry_has_orig(tt_global_entry,
853 orig_node))
854 goto out_remove;
855 batadv_tt_global_del_orig_list(tt_global_entry);
856 goto add_orig_entry;
857 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200858
859 /* if the client was temporary added before receiving the first
860 * OGM announcing it, we have to clear the TEMP flag
861 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200862 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200863
Antonio Quartullie9c001362012-11-07 15:05:33 +0100864 /* the change can carry possible "attribute" flags like the
865 * TT_CLIENT_WIFI, therefore they have to be copied in the
866 * client entry
867 */
868 tt_global_entry->common.flags |= flags;
869
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200870 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
871 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200872 * delete + roaming change for this originator.
873 *
874 * We should first delete the old originator before adding the
875 * new one.
876 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200877 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200878 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200879 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200880 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881 }
882 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200883add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200884 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200885 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200886
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200887 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200888 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200889 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200890 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200891
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100892out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200893
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200895 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
896 "global tt received",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200897 !!(flags & BATADV_TT_CLIENT_ROAM));
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200898 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
899
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200900 if (!(flags & BATADV_TT_CLIENT_ROAM))
901 /* this is a normal global add. Therefore the client is not in a
902 * roaming state anymore.
903 */
904 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
905
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200906out:
907 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200908 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200909 if (tt_local_entry)
910 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200911 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912}
913
Sven Eckelmann981d8902012-10-07 13:34:15 +0200914/* batadv_transtable_best_orig - Get best originator list entry from tt entry
915 * @tt_global_entry: global translation table entry to be analyzed
916 *
917 * This functon assumes the caller holds rcu_read_lock().
918 * Returns best originator list entry or NULL on errors.
919 */
920static struct batadv_tt_orig_list_entry *
921batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
922{
923 struct batadv_neigh_node *router = NULL;
924 struct hlist_head *head;
925 struct hlist_node *node;
926 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
927 int best_tq = 0;
928
929 head = &tt_global_entry->orig_list;
930 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
931 router = batadv_orig_node_get_router(orig_entry->orig_node);
932 if (!router)
933 continue;
934
935 if (router->tq_avg > best_tq) {
936 best_entry = orig_entry;
937 best_tq = router->tq_avg;
938 }
939
940 batadv_neigh_node_free_ref(router);
941 }
942
943 return best_entry;
944}
945
946/* batadv_tt_global_print_entry - print all orig nodes who announce the address
947 * for this global entry
948 * @tt_global_entry: global translation table entry to be printed
949 * @seq: debugfs table seq_file struct
950 *
951 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200952 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200953static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200954batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200955 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200956{
957 struct hlist_head *head;
958 struct hlist_node *node;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200959 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200960 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200961 uint16_t flags;
962 uint8_t last_ttvn;
963
964 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200965 flags = tt_common_entry->flags;
966
967 best_entry = batadv_transtable_best_orig(tt_global_entry);
968 if (best_entry) {
969 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
970 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
971 '*', tt_global_entry->common.addr,
972 best_entry->ttvn, best_entry->orig_node->orig,
973 last_ttvn,
974 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
975 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
976 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
977 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200978
979 head = &tt_global_entry->orig_list;
980
981 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +0200982 if (best_entry == orig_entry)
983 continue;
984
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200985 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +0200986 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
987 '+', tt_global_entry->common.addr,
988 orig_entry->ttvn, orig_entry->orig_node->orig,
989 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200990 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200991 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
992 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200993 }
994}
995
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200996int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997{
998 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200999 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001000 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001001 struct batadv_tt_common_entry *tt_common_entry;
1002 struct batadv_tt_global_entry *tt_global;
1003 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +00001004 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001005 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001006 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001007
Marek Lindner30da63a2012-08-03 17:15:46 +02001008 primary_if = batadv_seq_print_text_primary_if_get(seq);
1009 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001010 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001012 seq_printf(seq,
1013 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +02001015 seq_printf(seq, " %-13s %s %-15s %s %s\n",
1016 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018 for (i = 0; i < hash->size; i++) {
1019 head = &hash->table[i];
1020
Marek Lindner7aadf882011-02-18 12:28:09 +00001021 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001022 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +00001023 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 tt_global = container_of(tt_common_entry,
1025 struct batadv_tt_global_entry,
1026 common);
1027 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001029 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001031out:
1032 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001033 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001034 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035}
1036
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001037/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001038static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001039batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001041 struct hlist_head *head;
1042 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001044
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001045 spin_lock_bh(&tt_global_entry->list_lock);
1046 head = &tt_global_entry->orig_list;
1047 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1048 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001049 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001050 }
1051 spin_unlock_bh(&tt_global_entry->list_lock);
1052
1053}
1054
Sven Eckelmanna5130882012-05-16 20:23:16 +02001055static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001056batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1057 struct batadv_tt_global_entry *tt_global_entry,
1058 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001059 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001060{
1061 struct hlist_head *head;
1062 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001063 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001064
1065 spin_lock_bh(&tt_global_entry->list_lock);
1066 head = &tt_global_entry->orig_list;
1067 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1068 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001069 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001070 "Deleting %pM from global tt entry %pM: %s\n",
1071 orig_node->orig,
1072 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001073 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001074 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001075 }
1076 }
1077 spin_unlock_bh(&tt_global_entry->list_lock);
1078}
1079
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001080/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001081 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1082 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001083 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001084static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001085batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1086 struct batadv_tt_global_entry *tt_global_entry,
1087 struct batadv_orig_node *orig_node,
1088 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001089{
1090 bool last_entry = true;
1091 struct hlist_head *head;
1092 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001093 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094
1095 /* no local entry exists, case 1:
1096 * Check if this is the last one or if other entries exist.
1097 */
1098
1099 rcu_read_lock();
1100 head = &tt_global_entry->orig_list;
1101 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1102 if (orig_entry->orig_node != orig_node) {
1103 last_entry = false;
1104 break;
1105 }
1106 }
1107 rcu_read_unlock();
1108
1109 if (last_entry) {
1110 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001111 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001112 tt_global_entry->roam_at = jiffies;
1113 } else
1114 /* there is another entry, we can simply delete this
1115 * one and can still use the other one.
1116 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001117 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1118 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001119}
1120
1121
1122
Sven Eckelmann56303d32012-06-05 22:31:31 +02001123static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1124 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001125 const unsigned char *addr,
1126 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001127{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001128 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001129 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130
Sven Eckelmanna5130882012-05-16 20:23:16 +02001131 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001132 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001133 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001134
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001135 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001136 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1137 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001138
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001139 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001140 batadv_tt_global_free(bat_priv, tt_global_entry,
1141 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001142
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001143 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001144 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001145
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001146 /* if we are deleting a global entry due to a roam
1147 * event, there are two possibilities:
1148 * 1) the client roamed from node A to node B => if there
1149 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001150 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151 * wait for node B to claim it. In case of timeout
1152 * the entry is purged.
1153 *
1154 * If there are other originators left, we directly delete
1155 * the originator.
1156 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001157 * the global entry, since it is useless now.
1158 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001159 local_entry = batadv_tt_local_hash_find(bat_priv,
1160 tt_global_entry->common.addr);
1161 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001162 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001163 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001164 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001165 } else
1166 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001167 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1168 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001169
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001170
Antonio Quartullicc47f662011-04-27 14:27:57 +02001171out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001172 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001173 batadv_tt_global_entry_free_ref(tt_global_entry);
1174 if (local_entry)
1175 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001176}
1177
Sven Eckelmann56303d32012-06-05 22:31:31 +02001178void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1179 struct batadv_orig_node *orig_node,
1180 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001181{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001182 struct batadv_tt_global_entry *tt_global;
1183 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001184 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001185 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001186 struct hlist_node *node, *safe;
1187 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001188 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001189
Simon Wunderlich6e801492011-10-19 10:28:26 +02001190 if (!hash)
1191 return;
1192
Antonio Quartullia73105b2011-04-27 14:27:44 +02001193 for (i = 0; i < hash->size; i++) {
1194 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001195 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001197 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001198 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001199 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001200 tt_global = container_of(tt_common_entry,
1201 struct batadv_tt_global_entry,
1202 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001203
Sven Eckelmann56303d32012-06-05 22:31:31 +02001204 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001205 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001206
Sven Eckelmann56303d32012-06-05 22:31:31 +02001207 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001208 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001209 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001210 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001211 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001212 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001213 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001214 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001215 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001216 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001217 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001218}
1219
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001220static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1221 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001222{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001223 bool purge = false;
1224 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1225 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001226
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001227 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1228 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1229 purge = true;
1230 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001231 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001232
1233 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1234 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1235 purge = true;
1236 *msg = "Temporary client timeout\n";
1237 }
1238
1239 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001240}
1241
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001242static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001243{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001244 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001245 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001246 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001247 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001248 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001249 char *msg = NULL;
1250 struct batadv_tt_common_entry *tt_common;
1251 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001252
Antonio Quartullicc47f662011-04-27 14:27:57 +02001253 for (i = 0; i < hash->size; i++) {
1254 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001255 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001256
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001257 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001258 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1259 hash_entry) {
1260 tt_global = container_of(tt_common,
1261 struct batadv_tt_global_entry,
1262 common);
1263
1264 if (!batadv_tt_global_to_purge(tt_global, &msg))
1265 continue;
1266
1267 batadv_dbg(BATADV_DBG_TT, bat_priv,
1268 "Deleting global tt entry (%pM): %s\n",
1269 tt_global->common.addr, msg);
1270
1271 hlist_del_rcu(node);
1272
1273 batadv_tt_global_entry_free_ref(tt_global);
1274 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001275 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001276 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001277}
1278
Sven Eckelmann56303d32012-06-05 22:31:31 +02001279static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001280{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001281 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001282 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001283 struct batadv_tt_common_entry *tt_common_entry;
1284 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001285 struct hlist_node *node, *node_tmp;
1286 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001287 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001288
Sven Eckelmann807736f2012-07-15 22:26:51 +02001289 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001290 return;
1291
Sven Eckelmann807736f2012-07-15 22:26:51 +02001292 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001293
1294 for (i = 0; i < hash->size; i++) {
1295 head = &hash->table[i];
1296 list_lock = &hash->list_locks[i];
1297
1298 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001299 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001300 head, hash_entry) {
1301 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001302 tt_global = container_of(tt_common_entry,
1303 struct batadv_tt_global_entry,
1304 common);
1305 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001306 }
1307 spin_unlock_bh(list_lock);
1308 }
1309
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001310 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001311
Sven Eckelmann807736f2012-07-15 22:26:51 +02001312 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001313}
1314
Sven Eckelmann56303d32012-06-05 22:31:31 +02001315static bool
1316_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1317 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001318{
1319 bool ret = false;
1320
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001321 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1322 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001323 ret = true;
1324
1325 return ret;
1326}
1327
Sven Eckelmann56303d32012-06-05 22:31:31 +02001328struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1329 const uint8_t *src,
1330 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001331{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001332 struct batadv_tt_local_entry *tt_local_entry = NULL;
1333 struct batadv_tt_global_entry *tt_global_entry = NULL;
1334 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001335 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001336
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001337 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001338 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001339 if (!tt_local_entry ||
1340 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001341 goto out;
1342 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001343
Sven Eckelmanna5130882012-05-16 20:23:16 +02001344 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001345 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001346 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001348 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001349 * isolation
1350 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001351 if (tt_local_entry &&
1352 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001353 goto out;
1354
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001355 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001356 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001357 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001358 if (best_entry)
1359 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001360 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1361 orig_node = NULL;
1362 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001363
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001364out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001365 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001366 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001367 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001368 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001369
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001370 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001371}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001372
1373/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001374static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1375 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001376{
1377 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001378 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379 struct batadv_tt_common_entry *tt_common;
1380 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001381 struct hlist_node *node;
1382 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001383 uint32_t i;
1384 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385
1386 for (i = 0; i < hash->size; i++) {
1387 head = &hash->table[i];
1388
1389 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001390 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001391 tt_global = container_of(tt_common,
1392 struct batadv_tt_global_entry,
1393 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001394 /* Roaming clients are in the global table for
1395 * consistency only. They don't have to be
1396 * taken into account while computing the
1397 * global crc
1398 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001399 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001400 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001401 /* Temporary clients have not been announced yet, so
1402 * they have to be skipped while computing the global
1403 * crc
1404 */
1405 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1406 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001407
1408 /* find out if this global entry is announced by this
1409 * originator
1410 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001411 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001412 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001413 continue;
1414
1415 total_one = 0;
1416 for (j = 0; j < ETH_ALEN; j++)
1417 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001418 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001420 }
1421 rcu_read_unlock();
1422 }
1423
1424 return total;
1425}
1426
1427/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001428static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001429{
1430 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001432 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433 struct hlist_node *node;
1434 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001435 uint32_t i;
1436 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437
1438 for (i = 0; i < hash->size; i++) {
1439 head = &hash->table[i];
1440
1441 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001442 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001443 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001444 * account while computing the CRC
1445 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001446 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001447 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448 total_one = 0;
1449 for (j = 0; j < ETH_ALEN; j++)
1450 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001451 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 total ^= total_one;
1453 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454 rcu_read_unlock();
1455 }
1456
1457 return total;
1458}
1459
Sven Eckelmann56303d32012-06-05 22:31:31 +02001460static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001461{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001462 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463
Sven Eckelmann807736f2012-07-15 22:26:51 +02001464 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001465
Sven Eckelmann807736f2012-07-15 22:26:51 +02001466 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 list_del(&node->list);
1468 kfree(node);
1469 }
1470
Sven Eckelmann807736f2012-07-15 22:26:51 +02001471 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472}
1473
Sven Eckelmann56303d32012-06-05 22:31:31 +02001474static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1475 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001476 const unsigned char *tt_buff,
1477 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001479 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480
1481 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001482 * last OGM (the OGM could carry no changes)
1483 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484 spin_lock_bh(&orig_node->tt_buff_lock);
1485 if (tt_buff_len > 0) {
1486 kfree(orig_node->tt_buff);
1487 orig_node->tt_buff_len = 0;
1488 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1489 if (orig_node->tt_buff) {
1490 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1491 orig_node->tt_buff_len = tt_buff_len;
1492 }
1493 }
1494 spin_unlock_bh(&orig_node->tt_buff_lock);
1495}
1496
Sven Eckelmann56303d32012-06-05 22:31:31 +02001497static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001498{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001499 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500
Sven Eckelmann807736f2012-07-15 22:26:51 +02001501 spin_lock_bh(&bat_priv->tt.req_list_lock);
1502 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001503 if (batadv_has_timed_out(node->issued_at,
1504 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001505 list_del(&node->list);
1506 kfree(node);
1507 }
1508 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001509 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510}
1511
1512/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001513 * has already been issued for this orig_node, NULL otherwise
1514 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001515static struct batadv_tt_req_node *
1516batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1517 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001519 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520
Sven Eckelmann807736f2012-07-15 22:26:51 +02001521 spin_lock_bh(&bat_priv->tt.req_list_lock);
1522 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001523 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1524 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001525 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001526 goto unlock;
1527 }
1528
1529 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1530 if (!tt_req_node)
1531 goto unlock;
1532
1533 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1534 tt_req_node->issued_at = jiffies;
1535
Sven Eckelmann807736f2012-07-15 22:26:51 +02001536 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001537unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001538 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539 return tt_req_node;
1540}
1541
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001542/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001543static int batadv_tt_local_valid_entry(const void *entry_ptr,
1544 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001545{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001546 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001547
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001548 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001549 return 0;
1550 return 1;
1551}
1552
Sven Eckelmanna5130882012-05-16 20:23:16 +02001553static int batadv_tt_global_valid(const void *entry_ptr,
1554 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001556 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1557 const struct batadv_tt_global_entry *tt_global_entry;
1558 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001560 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1561 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001562 return 0;
1563
Sven Eckelmann56303d32012-06-05 22:31:31 +02001564 tt_global_entry = container_of(tt_common_entry,
1565 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001566 common);
1567
Sven Eckelmanna5130882012-05-16 20:23:16 +02001568 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569}
1570
Sven Eckelmanna5130882012-05-16 20:23:16 +02001571static struct sk_buff *
1572batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001573 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001574 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001575 int (*valid_cb)(const void *, const void *),
1576 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001577{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001578 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001579 struct batadv_tt_query_packet *tt_response;
1580 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581 struct hlist_node *node;
1582 struct hlist_head *head;
1583 struct sk_buff *skb = NULL;
1584 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001585 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001586 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001587 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588
1589 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1590 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001591 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001592 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001593 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001594
Sven Eckelmann96412692012-06-05 22:31:30 +02001595 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001596 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001597 if (!skb)
1598 goto out;
1599
Sven Eckelmann5b246572012-11-04 17:11:45 +01001600 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001601 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001602 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001603
Sven Eckelmann96412692012-06-05 22:31:30 +02001604 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001605 tt_count = 0;
1606
1607 rcu_read_lock();
1608 for (i = 0; i < hash->size; i++) {
1609 head = &hash->table[i];
1610
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001611 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001612 head, hash_entry) {
1613 if (tt_count == tt_tot)
1614 break;
1615
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001616 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 continue;
1618
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001619 memcpy(tt_change->addr, tt_common_entry->addr,
1620 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001621 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001622
1623 tt_count++;
1624 tt_change++;
1625 }
1626 }
1627 rcu_read_unlock();
1628
Antonio Quartulli9d852392011-10-17 14:25:13 +02001629 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001630 * copied
1631 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001632 tt_response->tt_data = htons(tt_count);
1633
Antonio Quartullia73105b2011-04-27 14:27:44 +02001634out:
1635 return skb;
1636}
1637
Sven Eckelmann56303d32012-06-05 22:31:31 +02001638static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1639 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001640 uint8_t ttvn, uint16_t tt_crc,
1641 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642{
1643 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001644 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001645 struct batadv_neigh_node *neigh_node = NULL;
1646 struct batadv_hard_iface *primary_if;
1647 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001649 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001650
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001651 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001652 if (!primary_if)
1653 goto out;
1654
1655 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001656 * reply from the same orig_node yet
1657 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001658 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001659 if (!tt_req_node)
1660 goto out;
1661
Sven Eckelmann5b246572012-11-04 17:11:45 +01001662 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001663 if (!skb)
1664 goto out;
1665
Sven Eckelmann5b246572012-11-04 17:11:45 +01001666 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001667
Sven Eckelmann96412692012-06-05 22:31:30 +02001668 tt_req_len = sizeof(*tt_request);
1669 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001670
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001671 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001672 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1674 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001675 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001677 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001678 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001679
1680 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001681 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001683 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684 if (!neigh_node)
1685 goto out;
1686
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001687 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001688 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1689 dst_orig_node->orig, neigh_node->addr,
1690 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001692 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001693
Sven Eckelmann9455e342012-05-12 02:09:37 +02001694 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001695 ret = 0;
1696
1697out:
1698 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001699 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001701 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702 if (ret)
1703 kfree_skb(skb);
1704 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001705 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001707 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708 kfree(tt_req_node);
1709 }
1710 return ret;
1711}
1712
Sven Eckelmann96412692012-06-05 22:31:30 +02001713static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001714batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001715 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001717 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001718 struct batadv_orig_node *res_dst_orig_node = NULL;
1719 struct batadv_neigh_node *neigh_node = NULL;
1720 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721 uint8_t orig_ttvn, req_ttvn, ttvn;
1722 int ret = false;
1723 unsigned char *tt_buff;
1724 bool full_table;
1725 uint16_t tt_len, tt_tot;
1726 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001727 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001728 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001729 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001731 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001732 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1733 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001734 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001735
1736 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001737 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001738 if (!req_dst_orig_node)
1739 goto out;
1740
Sven Eckelmannda641192012-05-12 13:48:56 +02001741 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742 if (!res_dst_orig_node)
1743 goto out;
1744
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001745 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746 if (!neigh_node)
1747 goto out;
1748
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001749 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750 if (!primary_if)
1751 goto out;
1752
1753 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1754 req_ttvn = tt_request->ttvn;
1755
Antonio Quartulli015758d2011-07-09 17:52:13 +02001756 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001758 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 goto out;
1760
Antonio Quartulli015758d2011-07-09 17:52:13 +02001761 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001762 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 !req_dst_orig_node->tt_buff)
1764 full_table = true;
1765 else
1766 full_table = false;
1767
1768 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001769 * I'll send only one packet with as much TT entries as I can
1770 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 if (!full_table) {
1772 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1773 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001774 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001775
Sven Eckelmann96412692012-06-05 22:31:30 +02001776 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001777 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001778 if (!skb)
1779 goto unlock;
1780
Sven Eckelmann5b246572012-11-04 17:11:45 +01001781 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001782 packet_pos = skb_put(skb, len);
1783 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 tt_response->ttvn = req_ttvn;
1785 tt_response->tt_data = htons(tt_tot);
1786
Sven Eckelmann96412692012-06-05 22:31:30 +02001787 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788 /* Copy the last orig_node's OGM buffer */
1789 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1790 req_dst_orig_node->tt_buff_len);
1791
1792 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1793 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001794 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1795 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1797
Sven Eckelmanna5130882012-05-16 20:23:16 +02001798 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001799 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001800 primary_if,
1801 batadv_tt_global_valid,
1802 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001803 if (!skb)
1804 goto out;
1805
Sven Eckelmann96412692012-06-05 22:31:30 +02001806 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001807 }
1808
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001809 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001810 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001811 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1813 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001814 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815
1816 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001817 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001818
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001819 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001820 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1821 res_dst_orig_node->orig, neigh_node->addr,
1822 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001824 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001825
Sven Eckelmann9455e342012-05-12 02:09:37 +02001826 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827 ret = true;
1828 goto out;
1829
1830unlock:
1831 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1832
1833out:
1834 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001835 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001837 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001838 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001839 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001841 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842 if (!ret)
1843 kfree_skb(skb);
1844 return ret;
1845
1846}
Sven Eckelmann96412692012-06-05 22:31:30 +02001847
1848static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001849batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001850 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001852 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001853 struct batadv_neigh_node *neigh_node = NULL;
1854 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001855 uint8_t my_ttvn, req_ttvn, ttvn;
1856 int ret = false;
1857 unsigned char *tt_buff;
1858 bool full_table;
1859 uint16_t tt_len, tt_tot;
1860 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001861 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001862 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001863 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001864
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001865 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001866 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1867 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001868 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869
1870
Sven Eckelmann807736f2012-07-15 22:26:51 +02001871 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001872 req_ttvn = tt_request->ttvn;
1873
Sven Eckelmannda641192012-05-12 13:48:56 +02001874 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001875 if (!orig_node)
1876 goto out;
1877
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001878 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879 if (!neigh_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,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001945 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1946 orig_node->orig, neigh_node->addr,
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
Sven Eckelmann9455e342012-05-12 02:09:37 +02001951 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952 ret = true;
1953 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 (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001961 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001963 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001964 if (!ret)
1965 kfree_skb(skb);
1966 /* This packet was for me, so it doesn't need to be re-routed */
1967 return true;
1968}
1969
Sven Eckelmann56303d32012-06-05 22:31:31 +02001970bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001971 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001973 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001974 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001975 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001976 return true;
1977
Sven Eckelmanna5130882012-05-16 20:23:16 +02001978 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001979 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001980 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001981 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001982}
1983
Sven Eckelmann56303d32012-06-05 22:31:31 +02001984static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1985 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001986 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001987 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001988{
1989 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001990 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991
1992 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001993 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1994 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001995 batadv_tt_global_del(bat_priv, orig_node,
1996 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001997 "tt removed by changes",
1998 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001999 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002000 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002001 (tt_change + i)->addr,
2002 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003 /* In case of problem while storing a
2004 * global_entry, we stop the updating
2005 * procedure without committing the
2006 * ttvn change. This will avoid to send
2007 * corrupted data on tt_request
2008 */
2009 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002010 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002012 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013}
2014
Sven Eckelmann56303d32012-06-05 22:31:31 +02002015static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002016 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002017{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002018 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019
Sven Eckelmannda641192012-05-12 13:48:56 +02002020 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021 if (!orig_node)
2022 goto out;
2023
2024 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002025 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
Sven Eckelmanna5130882012-05-16 20:23:16 +02002027 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002028 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002029 ntohs(tt_response->tt_data),
2030 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002031
2032 spin_lock_bh(&orig_node->tt_buff_lock);
2033 kfree(orig_node->tt_buff);
2034 orig_node->tt_buff_len = 0;
2035 orig_node->tt_buff = NULL;
2036 spin_unlock_bh(&orig_node->tt_buff_lock);
2037
2038 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2039
2040out:
2041 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002042 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002043}
2044
Sven Eckelmann56303d32012-06-05 22:31:31 +02002045static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2046 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002047 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002048 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002049{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002050 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2051 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002052
Sven Eckelmanna5130882012-05-16 20:23:16 +02002053 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2054 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002055 atomic_set(&orig_node->last_ttvn, ttvn);
2056}
2057
Sven Eckelmann56303d32012-06-05 22:31:31 +02002058bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002060 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002061 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002062
Sven Eckelmanna5130882012-05-16 20:23:16 +02002063 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002064 if (!tt_local_entry)
2065 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002066 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002067 * consistency purpose)
2068 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002069 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2070 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002071 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002072 ret = true;
2073out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002074 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002075 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002076 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002077}
2078
Sven Eckelmann56303d32012-06-05 22:31:31 +02002079void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002080 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002081{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002082 struct batadv_tt_req_node *node, *safe;
2083 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002084 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002085
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002086 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002087 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2088 tt_response->src, tt_response->ttvn,
2089 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002090 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002091
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002092 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002093 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002094 goto out;
2095
Sven Eckelmannda641192012-05-12 13:48:56 +02002096 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097 if (!orig_node)
2098 goto out;
2099
Sven Eckelmann96412692012-06-05 22:31:30 +02002100 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002101 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002102 } else {
2103 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002104 batadv_tt_update_changes(bat_priv, orig_node,
2105 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002106 tt_response->ttvn, tt_change);
2107 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002108
2109 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002110 spin_lock_bh(&bat_priv->tt.req_list_lock);
2111 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002112 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002113 continue;
2114 list_del(&node->list);
2115 kfree(node);
2116 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002117 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002118
2119 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002120 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121out:
2122 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002123 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124}
2125
Sven Eckelmann56303d32012-06-05 22:31:31 +02002126int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002127{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002128 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002129
Sven Eckelmanna5130882012-05-16 20:23:16 +02002130 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002131 if (ret < 0)
2132 return ret;
2133
Sven Eckelmanna5130882012-05-16 20:23:16 +02002134 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002135 if (ret < 0)
2136 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002137
Sven Eckelmanna5130882012-05-16 20:23:16 +02002138 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002139
2140 return 1;
2141}
2142
Sven Eckelmann56303d32012-06-05 22:31:31 +02002143static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002145 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002146
Sven Eckelmann807736f2012-07-15 22:26:51 +02002147 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002148
Sven Eckelmann807736f2012-07-15 22:26:51 +02002149 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002150 list_del(&node->list);
2151 kfree(node);
2152 }
2153
Sven Eckelmann807736f2012-07-15 22:26:51 +02002154 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002155}
2156
Sven Eckelmann56303d32012-06-05 22:31:31 +02002157static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002158{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002159 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002160
Sven Eckelmann807736f2012-07-15 22:26:51 +02002161 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2162 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002163 if (!batadv_has_timed_out(node->first_time,
2164 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002165 continue;
2166
2167 list_del(&node->list);
2168 kfree(node);
2169 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002170 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002171}
2172
2173/* This function checks whether the client already reached the
2174 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2175 * will not be sent.
2176 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002177 * returns true if the ROAMING_ADV can be sent, false otherwise
2178 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002179static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002180 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002181{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002182 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002183 bool ret = false;
2184
Sven Eckelmann807736f2012-07-15 22:26:51 +02002185 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002186 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002187 * reply from the same orig_node yet
2188 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002189 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002190 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002191 continue;
2192
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002193 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002194 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002195 continue;
2196
Sven Eckelmann3e348192012-05-16 20:23:22 +02002197 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002198 /* Sorry, you roamed too many times! */
2199 goto unlock;
2200 ret = true;
2201 break;
2202 }
2203
2204 if (!ret) {
2205 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2206 if (!tt_roam_node)
2207 goto unlock;
2208
2209 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002210 atomic_set(&tt_roam_node->counter,
2211 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002212 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2213
Sven Eckelmann807736f2012-07-15 22:26:51 +02002214 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002215 ret = true;
2216 }
2217
2218unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002219 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002220 return ret;
2221}
2222
Sven Eckelmann56303d32012-06-05 22:31:31 +02002223static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2224 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002225{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002226 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002227 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002228 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002229 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002230 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002231 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002232
2233 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002234 * already roamed to us too many times
2235 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002236 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002237 goto out;
2238
Sven Eckelmann5b246572012-11-04 17:11:45 +01002239 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002240 if (!skb)
2241 goto out;
2242
Sven Eckelmann5b246572012-11-04 17:11:45 +01002243 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002244
Sven Eckelmann96412692012-06-05 22:31:30 +02002245 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002246
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002247 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002248 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002249 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002250 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002251 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002252 if (!primary_if)
2253 goto out;
2254 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002255 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002256 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2257 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2258
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002259 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002260 if (!neigh_node)
2261 goto out;
2262
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002263 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002264 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2265 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002266
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002267 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002268
Sven Eckelmann9455e342012-05-12 02:09:37 +02002269 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002270 ret = 0;
2271
2272out:
2273 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002274 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002275 if (ret)
2276 kfree_skb(skb);
2277 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002278}
2279
Sven Eckelmanna5130882012-05-16 20:23:16 +02002280static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002281{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002282 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002283 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002284 struct batadv_priv *bat_priv;
2285
2286 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2288 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002289
Sven Eckelmanna5130882012-05-16 20:23:16 +02002290 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002291 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002292 batadv_tt_req_purge(bat_priv);
2293 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002294
Sven Eckelmanna5130882012-05-16 20:23:16 +02002295 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002296}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002297
Sven Eckelmann56303d32012-06-05 22:31:31 +02002298void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002299{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002300 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002301
Sven Eckelmanna5130882012-05-16 20:23:16 +02002302 batadv_tt_local_table_free(bat_priv);
2303 batadv_tt_global_table_free(bat_priv);
2304 batadv_tt_req_list_free(bat_priv);
2305 batadv_tt_changes_list_free(bat_priv);
2306 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002307
Sven Eckelmann807736f2012-07-15 22:26:51 +02002308 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002309}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002310
Antonio Quartulli697f2532011-11-07 16:47:01 +01002311/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002312 * in the given hash table and returns the number of modified entries
2313 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002314static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2315 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002316{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002317 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002318 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002319 struct hlist_head *head;
2320 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002321 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002322
2323 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002324 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002325
2326 for (i = 0; i < hash->size; i++) {
2327 head = &hash->table[i];
2328
2329 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002330 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002331 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002332 if (enable) {
2333 if ((tt_common_entry->flags & flags) == flags)
2334 continue;
2335 tt_common_entry->flags |= flags;
2336 } else {
2337 if (!(tt_common_entry->flags & flags))
2338 continue;
2339 tt_common_entry->flags &= ~flags;
2340 }
2341 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002342 }
2343 rcu_read_unlock();
2344 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002345out:
2346 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002347}
2348
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002349/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002350static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002351{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002352 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002353 struct batadv_tt_common_entry *tt_common;
2354 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002355 struct hlist_node *node, *node_tmp;
2356 struct hlist_head *head;
2357 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002358 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002359
2360 if (!hash)
2361 return;
2362
2363 for (i = 0; i < hash->size; i++) {
2364 head = &hash->table[i];
2365 list_lock = &hash->list_locks[i];
2366
2367 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002368 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2369 hash_entry) {
2370 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002371 continue;
2372
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002373 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002374 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002375 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002376
Sven Eckelmann807736f2012-07-15 22:26:51 +02002377 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002378 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002379 tt_local = container_of(tt_common,
2380 struct batadv_tt_local_entry,
2381 common);
2382 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002383 }
2384 spin_unlock_bh(list_lock);
2385 }
2386
2387}
2388
Sven Eckelmann56303d32012-06-05 22:31:31 +02002389static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002390 unsigned char **packet_buff,
2391 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002392{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002393 uint16_t changed_num = 0;
2394
Sven Eckelmann807736f2012-07-15 22:26:51 +02002395 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002396 return -ENOENT;
2397
Sven Eckelmann807736f2012-07-15 22:26:51 +02002398 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002399 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002400
2401 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002402 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002403 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002404 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002405
2406 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002407 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002408 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002409 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002410 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002411
2412 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002413 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002414
Sven Eckelmanna5130882012-05-16 20:23:16 +02002415 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2416 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002417}
2418
2419/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002420int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002421 unsigned char **packet_buff, int *packet_buff_len,
2422 int packet_min_len)
2423{
2424 int tt_num_changes;
2425
2426 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002427 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2428 packet_buff_len,
2429 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002430
2431 /* if the changes have been sent often enough */
2432 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002433 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002434 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2435 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002436 tt_num_changes = 0;
2437 }
2438
2439 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002440}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002441
Sven Eckelmann56303d32012-06-05 22:31:31 +02002442bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002443 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002444{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002445 struct batadv_tt_local_entry *tt_local_entry = NULL;
2446 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002447 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002448
2449 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002450 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002451
Sven Eckelmanna5130882012-05-16 20:23:16 +02002452 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002453 if (!tt_local_entry)
2454 goto out;
2455
Sven Eckelmanna5130882012-05-16 20:23:16 +02002456 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002457 if (!tt_global_entry)
2458 goto out;
2459
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002460 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002461 goto out;
2462
Marek Lindner5870adc2012-06-20 17:16:05 +02002463 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002464
2465out:
2466 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002467 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002468 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002469 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002470 return ret;
2471}
Marek Lindnera943cac2011-07-30 13:10:18 +02002472
Sven Eckelmann56303d32012-06-05 22:31:31 +02002473void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2474 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002475 const unsigned char *tt_buff, uint8_t tt_num_changes,
2476 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002477{
2478 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2479 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002480 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002481
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002482 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002483 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002484 return;
2485
Antonio Quartulli17071572011-11-07 16:36:40 +01002486 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002487 * increased by one -> we can apply the attached changes
2488 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002489 if ((!orig_node->tt_initialised && ttvn == 1) ||
2490 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002491 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002492 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2493 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002494 * In this case send a tt request
2495 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002496 if (!tt_num_changes) {
2497 full_table = false;
2498 goto request_table;
2499 }
2500
Sven Eckelmann96412692012-06-05 22:31:30 +02002501 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002502 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002503 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002504
2505 /* Even if we received the precomputed crc with the OGM, we
2506 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002507 * in the global table
2508 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002509 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002510
2511 /* The ttvn alone is not enough to guarantee consistency
2512 * because a single value could represent different states
2513 * (due to the wrap around). Thus a node has to check whether
2514 * the resulting table (after applying the changes) is still
2515 * consistent or not. E.g. a node could disconnect while its
2516 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2517 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002518 * inconsistency
2519 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002520 if (orig_node->tt_crc != tt_crc)
2521 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002522 } else {
2523 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002524 * in sync anymore -> request fresh tt data
2525 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002526 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2527 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002528request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002529 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002530 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2531 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2532 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002533 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2534 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002535 return;
2536 }
2537 }
2538}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002539
2540/* returns true whether we know that the client has moved from its old
2541 * originator to another one. This entry is kept is still kept for consistency
2542 * purposes
2543 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002544bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002545 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002546{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002547 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002548 bool ret = false;
2549
Sven Eckelmanna5130882012-05-16 20:23:16 +02002550 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002551 if (!tt_global_entry)
2552 goto out;
2553
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002554 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002555 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002556out:
2557 return ret;
2558}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002559
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002560/**
2561 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2562 * @bat_priv: the bat priv with all the soft interface information
2563 * @addr: the MAC address of the local client to query
2564 *
2565 * Returns true if the local client is known to be roaming (it is not served by
2566 * this node anymore) or not. If yes, the client is still present in the table
2567 * to keep the latter consistent with the node TTVN
2568 */
2569bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2570 uint8_t *addr)
2571{
2572 struct batadv_tt_local_entry *tt_local_entry;
2573 bool ret = false;
2574
2575 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2576 if (!tt_local_entry)
2577 goto out;
2578
2579 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2580 batadv_tt_local_entry_free_ref(tt_local_entry);
2581out:
2582 return ret;
2583
2584}
2585
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002586bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2587 struct batadv_orig_node *orig_node,
2588 const unsigned char *addr)
2589{
2590 bool ret = false;
2591
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002592 /* if the originator is a backbone node (meaning it belongs to the same
2593 * LAN of this node) the temporary client must not be added because to
2594 * reach such destination the node must use the LAN instead of the mesh
2595 */
2596 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2597 goto out;
2598
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002599 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2600 BATADV_TT_CLIENT_TEMP,
2601 atomic_read(&orig_node->last_ttvn)))
2602 goto out;
2603
2604 batadv_dbg(BATADV_DBG_TT, bat_priv,
2605 "Added temporary global client (addr: %pM orig: %pM)\n",
2606 addr, orig_node->orig);
2607 ret = true;
2608out:
2609 return ret;
2610}