blob: 426a3ae477887b5b9679700d257e9c942efee5e4 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
Sven Eckelmann56303d32012-06-05 22:31:31 +020032static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020034static void batadv_tt_purge(struct work_struct *work);
35static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020036batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020037static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041
Marek Lindner7aadf882011-02-18 12:28:09 +000042/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020043static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000044{
Sven Eckelmann56303d32012-06-05 22:31:31 +020045 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020046 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000047
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49}
50
Sven Eckelmann56303d32012-06-05 22:31:31 +020051static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052{
Sven Eckelmann807736f2012-07-15 22:26:51 +020053 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020055 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020059batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000060{
Marek Lindner7aadf882011-02-18 12:28:09 +000061 struct hlist_head *head;
62 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020063 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020065 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000066
67 if (!hash)
68 return NULL;
69
Sven Eckelmannda641192012-05-12 13:48:56 +020070 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000071 head = &hash->table[index];
72
73 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020075 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000076 continue;
77
Antonio Quartulli48100ba2011-10-30 12:17:33 +010078 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020079 continue;
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000082 break;
83 }
84 rcu_read_unlock();
85
Antonio Quartulli48100ba2011-10-30 12:17:33 +010086 return tt_common_entry_tmp;
87}
88
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_tt_local_entry *
90batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091{
Sven Eckelmann56303d32012-06-05 22:31:31 +020092 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010094
Sven Eckelmann807736f2012-07-15 22:26:51 +020095 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 struct batadv_tt_local_entry,
99 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101}
102
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103static struct batadv_tt_global_entry *
104batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000105{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Sven Eckelmann807736f2012-07-15 22:26:51 +0200109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_tt_global_entry,
113 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000115
Marek Lindner7aadf882011-02-18 12:28:09 +0000116}
117
Sven Eckelmanna5130882012-05-16 20:23:16 +0200118static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200119batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200120{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200123}
124
Sven Eckelmanna5130882012-05-16 20:23:16 +0200125static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200133
Simon Wunderlich531027f2011-10-19 11:02:25 +0200134 kfree(tt_global_entry);
135}
136
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200139{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200141 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 }
145}
146
Sven Eckelmanna5130882012-05-16 20:23:16 +0200147static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200152 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200167 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182
183 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000206 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 event_removed = true;
208 goto unlock;
209 }
210
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213
214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200217 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200218 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200219 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200223int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224{
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200226}
227
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Sven Eckelmann807736f2012-07-15 22:26:51 +0200233 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann807736f2012-07-15 22:26:51 +0200235 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239}
240
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200241static void batadv_tt_global_free(struct batadv_priv *bat_priv,
242 struct batadv_tt_global_entry *tt_global,
243 const char *message)
244{
245 batadv_dbg(BATADV_DBG_TT, bat_priv,
246 "Deleting global tt entry %pM: %s\n",
247 tt_global->common.addr, message);
248
249 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
250 batadv_choose_orig, tt_global->common.addr);
251 batadv_tt_global_entry_free_ref(tt_global);
252
253}
254
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200255void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
256 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200258 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200259 struct batadv_tt_local_entry *tt_local;
260 struct batadv_tt_global_entry *tt_global;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200261 struct hlist_head *head;
262 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200263 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100264 int hash_added;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200265 bool roamed_back = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Antonio Quartulli47c94652012-09-23 22:38:35 +0200267 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200268 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartulli47c94652012-09-23 22:38:35 +0200270 if (tt_local) {
271 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200272 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
273 batadv_dbg(BATADV_DBG_TT, bat_priv,
274 "Re-adding pending client %pM\n", addr);
275 /* whatever the reason why the PENDING flag was set,
276 * this is a client which was enqueued to be removed in
277 * this orig_interval. Since it popped up again, the
278 * flag can be reset like it was never enqueued
279 */
280 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
281 goto add_event;
282 }
283
284 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
285 batadv_dbg(BATADV_DBG_TT, bat_priv,
286 "Roaming client %pM came back to its original location\n",
287 addr);
288 /* the ROAM flag is set because this client roamed away
289 * and the node got a roaming_advertisement message. Now
290 * that the client popped up again at its original
291 * location such flag can be unset
292 */
293 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
294 roamed_back = true;
295 }
296 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 }
298
Antonio Quartulli47c94652012-09-23 22:38:35 +0200299 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
300 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200301 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200302
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200303 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200304 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200305 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
Antonio Quartulli47c94652012-09-23 22:38:35 +0200307 memcpy(tt_local->common.addr, addr, ETH_ALEN);
308 tt_local->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200309 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200310 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311 atomic_set(&tt_local->common.refcount, 2);
312 tt_local->last_seen = jiffies;
313 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314
315 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200316 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200317 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100319 /* The local entry has to be marked as NEW to avoid to send it in
320 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200321 * (consistency check)
322 */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200323 tt_local->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100324
Sven Eckelmann807736f2012-07-15 22:26:51 +0200325 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200326 batadv_choose_orig, &tt_local->common,
327 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100328
329 if (unlikely(hash_added != 0)) {
330 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200331 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100332 goto out;
333 }
334
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200335add_event:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200336 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200337
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200338check_roaming:
339 /* Check whether it is a roaming, but don't do anything if the roaming
340 * process has already been handled
341 */
342 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200343 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200344 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200345 rcu_read_lock();
346 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200347 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200348 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200349 }
350 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200351 if (roamed_back) {
352 batadv_tt_global_free(bat_priv, tt_global,
353 "Roaming canceled");
354 tt_global = NULL;
355 } else {
356 /* The global entry has to be marked as ROAMING and
357 * has to be kept for consistency purpose
358 */
359 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
360 tt_global->roam_at = jiffies;
361 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200362 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200363
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200364out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200365 if (tt_local)
366 batadv_tt_local_entry_free_ref(tt_local);
367 if (tt_global)
368 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369}
370
Sven Eckelmanna5130882012-05-16 20:23:16 +0200371static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
372 int *packet_buff_len,
373 int min_packet_len,
374 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800376 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800378 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
379
380 /* keep old buffer if kmalloc should fail */
381 if (new_buff) {
382 memcpy(new_buff, *packet_buff, min_packet_len);
383 kfree(*packet_buff);
384 *packet_buff = new_buff;
385 *packet_buff_len = new_packet_len;
386 }
387}
388
Sven Eckelmann56303d32012-06-05 22:31:31 +0200389static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200390 unsigned char **packet_buff,
391 int *packet_buff_len,
392 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800393{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200394 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800395 int req_len;
396
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200397 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800398
399 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200400 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800401
402 /* if we have too many changes for one packet don't send any
403 * and wait for the tt table request which will be fragmented
404 */
405 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
406 req_len = min_packet_len;
407
Sven Eckelmanna5130882012-05-16 20:23:16 +0200408 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
409 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800410
411 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200412 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800413}
414
Sven Eckelmann56303d32012-06-05 22:31:31 +0200415static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200416 unsigned char **packet_buff,
417 int *packet_buff_len,
418 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800419{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800421 int count = 0, tot_changes = 0, new_len;
422 unsigned char *tt_buff;
423
Sven Eckelmanna5130882012-05-16 20:23:16 +0200424 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
425 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800426
427 new_len = *packet_buff_len - min_packet_len;
428 tt_buff = *packet_buff + min_packet_len;
429
430 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200431 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
Sven Eckelmann807736f2012-07-15 22:26:51 +0200433 spin_lock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Sven Eckelmann807736f2012-07-15 22:26:51 +0200436 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100437 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200439 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200440 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 count++;
442 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443 list_del(&entry->list);
444 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200446 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
Antonio Quartullia73105b2011-04-27 14:27:44 +0200448 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200449 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
450 kfree(bat_priv->tt.last_changeset);
451 bat_priv->tt.last_changeset_len = 0;
452 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800453 /* check whether this new OGM has no changes due to size problems */
454 if (new_len > 0) {
455 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456 * instead of providing the diff
457 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200458 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
459 if (bat_priv->tt.last_changeset) {
460 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
461 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200462 }
463 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200464 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
Marek Lindner08ad76e2012-04-23 16:32:55 +0800466 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467}
468
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200469int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470{
471 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200472 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200473 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200474 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100475 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200476 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000477 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200479 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100480 int last_seen_secs;
481 int last_seen_msecs;
482 unsigned long last_seen_jiffies;
483 bool no_purge;
484 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Marek Lindner30da63a2012-08-03 17:15:46 +0200486 primary_if = batadv_seq_print_text_primary_if_get(seq);
487 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200488 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100490 seq_printf(seq,
491 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200492 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartulli85766a82012-11-08 22:16:16 +0100493 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
494 "Last seen");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496 for (i = 0; i < hash->size; i++) {
497 head = &hash->table[i];
498
Marek Lindner7aadf882011-02-18 12:28:09 +0000499 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100500 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000501 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100502 tt_local = container_of(tt_common_entry,
503 struct batadv_tt_local_entry,
504 common);
505 last_seen_jiffies = jiffies - tt_local->last_seen;
506 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
507 last_seen_secs = last_seen_msecs / 1000;
508 last_seen_msecs = last_seen_msecs % 1000;
509
510 no_purge = tt_common_entry->flags & np_flag;
511
512 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100513 tt_common_entry->addr,
514 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200515 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100516 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100517 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200518 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100519 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200520 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100521 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100522 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
523 no_purge ? last_seen_secs : 0,
524 no_purge ? last_seen_msecs : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000526 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200528out:
529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200530 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200531 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532}
533
Sven Eckelmann56303d32012-06-05 22:31:31 +0200534static void
535batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
536 struct batadv_tt_local_entry *tt_local_entry,
537 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200539 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
540 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541
Antonio Quartulli015758d2011-07-09 17:52:13 +0200542 /* The local client has to be marked as "pending to be removed" but has
543 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200544 * response issued before the net ttvn increment (consistency check)
545 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200546 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100547
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200548 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200549 "Local tt entry (%pM) pending to be removed: %s\n",
550 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551}
552
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200553/**
554 * batadv_tt_local_remove - logically remove an entry from the local table
555 * @bat_priv: the bat priv with all the soft interface information
556 * @addr: the MAC address of the client to remove
557 * @message: message to append to the log on deletion
558 * @roaming: true if the deletion is due to a roaming event
559 *
560 * Returns the flags assigned to the local entry before being deleted
561 */
562uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
563 const uint8_t *addr, const char *message,
564 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200566 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200567 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568
Sven Eckelmanna5130882012-05-16 20:23:16 +0200569 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200570 if (!tt_local_entry)
571 goto out;
572
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200573 curr_flags = tt_local_entry->common.flags;
574
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200575 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200576 /* if this global entry addition is due to a roaming, the node has to
577 * mark the local entry as "roamed" in order to correctly reroute
578 * packets later
579 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200580 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200581 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200582 /* mark the local client as ROAMed */
583 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
584 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200585
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200586 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
587 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
588 message);
589 goto out;
590 }
591 /* if this client has been added right now, it is possible to
592 * immediately purge it
593 */
594 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
595 curr_flags | BATADV_TT_CLIENT_DEL);
596 hlist_del_rcu(&tt_local_entry->common.hash_entry);
597 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200598
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200599out:
600 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200601 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200602
603 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604}
605
Sven Eckelmann56303d32012-06-05 22:31:31 +0200606static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200607 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200609 struct batadv_tt_local_entry *tt_local_entry;
610 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000611 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200612
613 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
614 hash_entry) {
615 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200616 struct batadv_tt_local_entry,
617 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200618 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
619 continue;
620
621 /* entry already marked for deletion */
622 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
623 continue;
624
625 if (!batadv_has_timed_out(tt_local_entry->last_seen,
626 BATADV_TT_LOCAL_TIMEOUT))
627 continue;
628
629 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
630 BATADV_TT_CLIENT_DEL, "timed out");
631 }
632}
633
Sven Eckelmann56303d32012-06-05 22:31:31 +0200634static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200635{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200636 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200639 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 for (i = 0; i < hash->size; i++) {
642 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200643 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200645 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200646 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648 }
649
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650}
651
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200654 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200655 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200656 struct batadv_tt_common_entry *tt_common_entry;
657 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200658 struct hlist_node *node, *node_tmp;
659 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200660 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200661
Sven Eckelmann807736f2012-07-15 22:26:51 +0200662 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 return;
664
Sven Eckelmann807736f2012-07-15 22:26:51 +0200665 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200666
667 for (i = 0; i < hash->size; i++) {
668 head = &hash->table[i];
669 list_lock = &hash->list_locks[i];
670
671 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100672 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200673 head, hash_entry) {
674 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675 tt_local = container_of(tt_common_entry,
676 struct batadv_tt_local_entry,
677 common);
678 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200679 }
680 spin_unlock_bh(list_lock);
681 }
682
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200683 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200684
Sven Eckelmann807736f2012-07-15 22:26:51 +0200685 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686}
687
Sven Eckelmann56303d32012-06-05 22:31:31 +0200688static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200690 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200691 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692
Sven Eckelmann807736f2012-07-15 22:26:51 +0200693 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
Sven Eckelmann807736f2012-07-15 22:26:51 +0200695 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200696 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Sven Eckelmann5346c352012-05-05 13:27:28 +0200698 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699}
700
Sven Eckelmann56303d32012-06-05 22:31:31 +0200701static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200702{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200703 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200704
Sven Eckelmann807736f2012-07-15 22:26:51 +0200705 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200706
Sven Eckelmann807736f2012-07-15 22:26:51 +0200707 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200708 list) {
709 list_del(&entry->list);
710 kfree(entry);
711 }
712
Sven Eckelmann807736f2012-07-15 22:26:51 +0200713 atomic_set(&bat_priv->tt.local_changes, 0);
714 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200715}
716
Antonio Quartullid657e622012-07-01 14:09:12 +0200717/* retrieves the orig_tt_list_entry belonging to orig_node from the
718 * batadv_tt_global_entry list
719 *
720 * returns it with an increased refcounter, NULL if not found
721 */
722static struct batadv_tt_orig_list_entry *
723batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
724 const struct batadv_orig_node *orig_node)
725{
726 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
727 const struct hlist_head *head;
728 struct hlist_node *node;
729
730 rcu_read_lock();
731 head = &entry->orig_list;
732 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
733 if (tmp_orig_entry->orig_node != orig_node)
734 continue;
735 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
736 continue;
737
738 orig_entry = tmp_orig_entry;
739 break;
740 }
741 rcu_read_unlock();
742
743 return orig_entry;
744}
745
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200746/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200747 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200748 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200749static bool
750batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
751 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200752{
Antonio Quartullid657e622012-07-01 14:09:12 +0200753 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200754 bool found = false;
755
Antonio Quartullid657e622012-07-01 14:09:12 +0200756 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
757 if (orig_entry) {
758 found = true;
759 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200760 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200761
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200762 return found;
763}
764
Sven Eckelmanna5130882012-05-16 20:23:16 +0200765static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200766batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200767 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200768{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200769 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200770
Antonio Quartullid657e622012-07-01 14:09:12 +0200771 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200772 if (orig_entry) {
773 /* refresh the ttvn: the current value could be a bogus one that
774 * was added during a "temporary client detection"
775 */
776 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200777 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200778 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200779
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200780 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
781 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200782 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200783
784 INIT_HLIST_NODE(&orig_entry->list);
785 atomic_inc(&orig_node->refcount);
786 atomic_inc(&orig_node->tt_size);
787 orig_entry->orig_node = orig_node;
788 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200789 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200790
Antonio Quartullid657e622012-07-01 14:09:12 +0200791 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200792 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200793 &tt_global->orig_list);
794 spin_unlock_bh(&tt_global->list_lock);
795out:
796 if (orig_entry)
797 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200798}
799
Antonio Quartullia73105b2011-04-27 14:27:44 +0200800/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200801int batadv_tt_global_add(struct batadv_priv *bat_priv,
802 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200803 const unsigned char *tt_addr, uint8_t flags,
804 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200806 struct batadv_tt_global_entry *tt_global_entry;
807 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200808 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100809 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200810 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200811 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812
Sven Eckelmanna5130882012-05-16 20:23:16 +0200813 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200814 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
815
816 /* if the node already has a local client for this entry, it has to wait
817 * for a roaming advertisement instead of manually messing up the global
818 * table
819 */
820 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
821 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
822 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
Antonio Quartullia73105b2011-04-27 14:27:44 +0200824 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200825 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200826 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200827 goto out;
828
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200829 common = &tt_global_entry->common;
830 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200831
Antonio Quartullid4f44692012-05-25 00:00:54 +0200832 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200833 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200834 /* node must store current time in case of roaming. This is
835 * needed to purge this entry out on timeout (if nobody claims
836 * it)
837 */
838 if (flags & BATADV_TT_CLIENT_ROAM)
839 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200840 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200841 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200842
843 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
844 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200845
Sven Eckelmann807736f2012-07-15 22:26:51 +0200846 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200847 batadv_compare_tt,
848 batadv_choose_orig, common,
849 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100850
851 if (unlikely(hash_added != 0)) {
852 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200853 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100854 goto out_remove;
855 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200856 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200857 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200858 /* If there is already a global entry, we can use this one for
859 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200860 * But if we are trying to add a temporary client then here are
861 * two options at this point:
862 * 1) the global client is not a temporary client: the global
863 * client has to be left as it is, temporary information
864 * should never override any already known client state
865 * 2) the global client is a temporary client: purge the
866 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200867 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200868 if (flags & BATADV_TT_CLIENT_TEMP) {
869 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
870 goto out;
871 if (batadv_tt_global_entry_has_orig(tt_global_entry,
872 orig_node))
873 goto out_remove;
874 batadv_tt_global_del_orig_list(tt_global_entry);
875 goto add_orig_entry;
876 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200877
878 /* if the client was temporary added before receiving the first
879 * OGM announcing it, we have to clear the TEMP flag
880 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200881 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200882
Antonio Quartullie9c001362012-11-07 15:05:33 +0100883 /* the change can carry possible "attribute" flags like the
884 * TT_CLIENT_WIFI, therefore they have to be copied in the
885 * client entry
886 */
887 tt_global_entry->common.flags |= flags;
888
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200889 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
890 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200891 * delete + roaming change for this originator.
892 *
893 * We should first delete the old originator before adding the
894 * new one.
895 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200896 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200897 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200898 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200899 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900 }
901 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200902add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200903 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200904 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200905
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200906 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200907 "Creating new global tt entry: %pM (via %pM)\n",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200908 common->addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200909 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200910
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100911out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200912
Antonio Quartullia73105b2011-04-27 14:27:44 +0200913 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200914 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
915 "global tt received",
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200916 !!(flags & BATADV_TT_CLIENT_ROAM));
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200917 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
918
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200919 if (!(flags & BATADV_TT_CLIENT_ROAM))
920 /* this is a normal global add. Therefore the client is not in a
921 * roaming state anymore.
922 */
923 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
924
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200925out:
926 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200927 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200928 if (tt_local_entry)
929 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200930 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931}
932
Sven Eckelmann981d8902012-10-07 13:34:15 +0200933/* batadv_transtable_best_orig - Get best originator list entry from tt entry
934 * @tt_global_entry: global translation table entry to be analyzed
935 *
936 * This functon assumes the caller holds rcu_read_lock().
937 * Returns best originator list entry or NULL on errors.
938 */
939static struct batadv_tt_orig_list_entry *
940batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
941{
942 struct batadv_neigh_node *router = NULL;
943 struct hlist_head *head;
944 struct hlist_node *node;
945 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
946 int best_tq = 0;
947
948 head = &tt_global_entry->orig_list;
949 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
950 router = batadv_orig_node_get_router(orig_entry->orig_node);
951 if (!router)
952 continue;
953
954 if (router->tq_avg > best_tq) {
955 best_entry = orig_entry;
956 best_tq = router->tq_avg;
957 }
958
959 batadv_neigh_node_free_ref(router);
960 }
961
962 return best_entry;
963}
964
965/* batadv_tt_global_print_entry - print all orig nodes who announce the address
966 * for this global entry
967 * @tt_global_entry: global translation table entry to be printed
968 * @seq: debugfs table seq_file struct
969 *
970 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200971 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200972static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200973batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200974 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200975{
976 struct hlist_head *head;
977 struct hlist_node *node;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200978 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200979 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200980 uint16_t flags;
981 uint8_t last_ttvn;
982
983 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +0200984 flags = tt_common_entry->flags;
985
986 best_entry = batadv_transtable_best_orig(tt_global_entry);
987 if (best_entry) {
988 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
989 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
990 '*', tt_global_entry->common.addr,
991 best_entry->ttvn, best_entry->orig_node->orig,
992 last_ttvn,
993 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
994 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
995 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
996 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200997
998 head = &tt_global_entry->orig_list;
999
1000 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001001 if (best_entry == orig_entry)
1002 continue;
1003
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001004 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001005 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1006 '+', tt_global_entry->common.addr,
1007 orig_entry->ttvn, orig_entry->orig_node->orig,
1008 last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001009 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001010 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1011 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001012 }
1013}
1014
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001015int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016{
1017 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001018 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001019 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 struct batadv_tt_common_entry *tt_common_entry;
1021 struct batadv_tt_global_entry *tt_global;
1022 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +00001023 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001025 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026
Marek Lindner30da63a2012-08-03 17:15:46 +02001027 primary_if = batadv_seq_print_text_primary_if_get(seq);
1028 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001029 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001031 seq_printf(seq,
1032 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +02001034 seq_printf(seq, " %-13s %s %-15s %s %s\n",
1035 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037 for (i = 0; i < hash->size; i++) {
1038 head = &hash->table[i];
1039
Marek Lindner7aadf882011-02-18 12:28:09 +00001040 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001041 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +00001042 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043 tt_global = container_of(tt_common_entry,
1044 struct batadv_tt_global_entry,
1045 common);
1046 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001048 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001050out:
1051 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001052 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001053 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054}
1055
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001056/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001057static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001058batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001060 struct hlist_head *head;
1061 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001063
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001064 spin_lock_bh(&tt_global_entry->list_lock);
1065 head = &tt_global_entry->orig_list;
1066 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1067 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001068 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001069 }
1070 spin_unlock_bh(&tt_global_entry->list_lock);
1071
1072}
1073
Sven Eckelmanna5130882012-05-16 20:23:16 +02001074static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1076 struct batadv_tt_global_entry *tt_global_entry,
1077 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001078 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001079{
1080 struct hlist_head *head;
1081 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001082 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001083
1084 spin_lock_bh(&tt_global_entry->list_lock);
1085 head = &tt_global_entry->orig_list;
1086 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1087 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001088 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001089 "Deleting %pM from global tt entry %pM: %s\n",
1090 orig_node->orig,
1091 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001092 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001093 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001094 }
1095 }
1096 spin_unlock_bh(&tt_global_entry->list_lock);
1097}
1098
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001099/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001100 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1101 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001102 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001103static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001104batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1105 struct batadv_tt_global_entry *tt_global_entry,
1106 struct batadv_orig_node *orig_node,
1107 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001108{
1109 bool last_entry = true;
1110 struct hlist_head *head;
1111 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001112 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001113
1114 /* no local entry exists, case 1:
1115 * Check if this is the last one or if other entries exist.
1116 */
1117
1118 rcu_read_lock();
1119 head = &tt_global_entry->orig_list;
1120 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1121 if (orig_entry->orig_node != orig_node) {
1122 last_entry = false;
1123 break;
1124 }
1125 }
1126 rcu_read_unlock();
1127
1128 if (last_entry) {
1129 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001130 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001131 tt_global_entry->roam_at = jiffies;
1132 } else
1133 /* there is another entry, we can simply delete this
1134 * one and can still use the other one.
1135 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001136 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1137 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001138}
1139
1140
1141
Sven Eckelmann56303d32012-06-05 22:31:31 +02001142static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1143 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001144 const unsigned char *addr,
1145 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001147 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001148 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149
Sven Eckelmanna5130882012-05-16 20:23:16 +02001150 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001151 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001152 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001153
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001154 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001155 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1156 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001157
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001158 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001159 batadv_tt_global_free(bat_priv, tt_global_entry,
1160 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001161
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001162 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001164
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001165 /* if we are deleting a global entry due to a roam
1166 * event, there are two possibilities:
1167 * 1) the client roamed from node A to node B => if there
1168 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001169 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001170 * wait for node B to claim it. In case of timeout
1171 * the entry is purged.
1172 *
1173 * If there are other originators left, we directly delete
1174 * the originator.
1175 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001176 * the global entry, since it is useless now.
1177 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001178 local_entry = batadv_tt_local_hash_find(bat_priv,
1179 tt_global_entry->common.addr);
1180 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001182 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001183 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001184 } else
1185 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001186 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1187 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001188
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001189
Antonio Quartullicc47f662011-04-27 14:27:57 +02001190out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001191 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001192 batadv_tt_global_entry_free_ref(tt_global_entry);
1193 if (local_entry)
1194 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001195}
1196
Sven Eckelmann56303d32012-06-05 22:31:31 +02001197void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1198 struct batadv_orig_node *orig_node,
1199 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001200{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001201 struct batadv_tt_global_entry *tt_global;
1202 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001203 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001204 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001205 struct hlist_node *node, *safe;
1206 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001207 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001208
Simon Wunderlich6e801492011-10-19 10:28:26 +02001209 if (!hash)
1210 return;
1211
Antonio Quartullia73105b2011-04-27 14:27:44 +02001212 for (i = 0; i < hash->size; i++) {
1213 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001214 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001216 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001217 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001218 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001219 tt_global = container_of(tt_common_entry,
1220 struct batadv_tt_global_entry,
1221 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001222
Sven Eckelmann56303d32012-06-05 22:31:31 +02001223 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001224 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225
Sven Eckelmann56303d32012-06-05 22:31:31 +02001226 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001227 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001228 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001229 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001230 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001231 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001232 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001233 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001234 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001235 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001236 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237}
1238
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001239static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1240 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001241{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001242 bool purge = false;
1243 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1244 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001245
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001246 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1247 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1248 purge = true;
1249 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001250 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001251
1252 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1253 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1254 purge = true;
1255 *msg = "Temporary client timeout\n";
1256 }
1257
1258 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001259}
1260
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001261static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001262{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001263 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001264 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001265 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001266 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001267 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001268 char *msg = NULL;
1269 struct batadv_tt_common_entry *tt_common;
1270 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001271
Antonio Quartullicc47f662011-04-27 14:27:57 +02001272 for (i = 0; i < hash->size; i++) {
1273 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001274 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001275
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001276 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001277 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1278 hash_entry) {
1279 tt_global = container_of(tt_common,
1280 struct batadv_tt_global_entry,
1281 common);
1282
1283 if (!batadv_tt_global_to_purge(tt_global, &msg))
1284 continue;
1285
1286 batadv_dbg(BATADV_DBG_TT, bat_priv,
1287 "Deleting global tt entry (%pM): %s\n",
1288 tt_global->common.addr, msg);
1289
1290 hlist_del_rcu(node);
1291
1292 batadv_tt_global_entry_free_ref(tt_global);
1293 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001294 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001295 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001296}
1297
Sven Eckelmann56303d32012-06-05 22:31:31 +02001298static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001299{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001300 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001301 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001302 struct batadv_tt_common_entry *tt_common_entry;
1303 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001304 struct hlist_node *node, *node_tmp;
1305 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001306 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001307
Sven Eckelmann807736f2012-07-15 22:26:51 +02001308 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001309 return;
1310
Sven Eckelmann807736f2012-07-15 22:26:51 +02001311 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001312
1313 for (i = 0; i < hash->size; i++) {
1314 head = &hash->table[i];
1315 list_lock = &hash->list_locks[i];
1316
1317 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001318 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001319 head, hash_entry) {
1320 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 tt_global = container_of(tt_common_entry,
1322 struct batadv_tt_global_entry,
1323 common);
1324 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001325 }
1326 spin_unlock_bh(list_lock);
1327 }
1328
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001329 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001330
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001332}
1333
Sven Eckelmann56303d32012-06-05 22:31:31 +02001334static bool
1335_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1336 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001337{
1338 bool ret = false;
1339
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001340 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1341 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001342 ret = true;
1343
1344 return ret;
1345}
1346
Sven Eckelmann56303d32012-06-05 22:31:31 +02001347struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1348 const uint8_t *src,
1349 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001351 struct batadv_tt_local_entry *tt_local_entry = NULL;
1352 struct batadv_tt_global_entry *tt_global_entry = NULL;
1353 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001354 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001356 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001357 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001358 if (!tt_local_entry ||
1359 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001360 goto out;
1361 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001362
Sven Eckelmanna5130882012-05-16 20:23:16 +02001363 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001364 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001365 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001366
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001367 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001368 * isolation
1369 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001370 if (tt_local_entry &&
1371 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001372 goto out;
1373
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001374 rcu_read_lock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001375 best_entry = batadv_transtable_best_orig(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001376 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001377 if (best_entry)
1378 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001379 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1380 orig_node = NULL;
1381 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001382
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001383out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001384 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001385 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001386 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001387 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001388
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001389 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001390}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001391
1392/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001393static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1394 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395{
1396 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001397 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398 struct batadv_tt_common_entry *tt_common;
1399 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400 struct hlist_node *node;
1401 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001402 uint32_t i;
1403 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404
1405 for (i = 0; i < hash->size; i++) {
1406 head = &hash->table[i];
1407
1408 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001409 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001410 tt_global = container_of(tt_common,
1411 struct batadv_tt_global_entry,
1412 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001413 /* Roaming clients are in the global table for
1414 * consistency only. They don't have to be
1415 * taken into account while computing the
1416 * global crc
1417 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001418 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001419 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001420 /* Temporary clients have not been announced yet, so
1421 * they have to be skipped while computing the global
1422 * crc
1423 */
1424 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1425 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001426
1427 /* find out if this global entry is announced by this
1428 * originator
1429 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001430 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001431 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001432 continue;
1433
1434 total_one = 0;
1435 for (j = 0; j < ETH_ALEN; j++)
1436 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001437 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001438 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439 }
1440 rcu_read_unlock();
1441 }
1442
1443 return total;
1444}
1445
1446/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001447static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448{
1449 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001450 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001451 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 struct hlist_node *node;
1453 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001454 uint32_t i;
1455 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456
1457 for (i = 0; i < hash->size; i++) {
1458 head = &hash->table[i];
1459
1460 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001461 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001462 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001463 * account while computing the CRC
1464 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001465 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001466 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 total_one = 0;
1468 for (j = 0; j < ETH_ALEN; j++)
1469 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001470 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 total ^= total_one;
1472 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473 rcu_read_unlock();
1474 }
1475
1476 return total;
1477}
1478
Sven Eckelmann56303d32012-06-05 22:31:31 +02001479static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001481 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484
Sven Eckelmann807736f2012-07-15 22:26:51 +02001485 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001486 list_del(&node->list);
1487 kfree(node);
1488 }
1489
Sven Eckelmann807736f2012-07-15 22:26:51 +02001490 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491}
1492
Sven Eckelmann56303d32012-06-05 22:31:31 +02001493static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1494 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001495 const unsigned char *tt_buff,
1496 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001498 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499
1500 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001501 * last OGM (the OGM could carry no changes)
1502 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001503 spin_lock_bh(&orig_node->tt_buff_lock);
1504 if (tt_buff_len > 0) {
1505 kfree(orig_node->tt_buff);
1506 orig_node->tt_buff_len = 0;
1507 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1508 if (orig_node->tt_buff) {
1509 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1510 orig_node->tt_buff_len = tt_buff_len;
1511 }
1512 }
1513 spin_unlock_bh(&orig_node->tt_buff_lock);
1514}
1515
Sven Eckelmann56303d32012-06-05 22:31:31 +02001516static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001517{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519
Sven Eckelmann807736f2012-07-15 22:26:51 +02001520 spin_lock_bh(&bat_priv->tt.req_list_lock);
1521 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001522 if (batadv_has_timed_out(node->issued_at,
1523 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001524 list_del(&node->list);
1525 kfree(node);
1526 }
1527 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001528 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529}
1530
1531/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001532 * has already been issued for this orig_node, NULL otherwise
1533 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001534static struct batadv_tt_req_node *
1535batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1536 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001537{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001538 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539
Sven Eckelmann807736f2012-07-15 22:26:51 +02001540 spin_lock_bh(&bat_priv->tt.req_list_lock);
1541 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001542 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1543 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001544 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001545 goto unlock;
1546 }
1547
1548 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1549 if (!tt_req_node)
1550 goto unlock;
1551
1552 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1553 tt_req_node->issued_at = jiffies;
1554
Sven Eckelmann807736f2012-07-15 22:26:51 +02001555 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001557 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558 return tt_req_node;
1559}
1560
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001561/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001562static int batadv_tt_local_valid_entry(const void *entry_ptr,
1563 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001564{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001565 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001566
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001567 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001568 return 0;
1569 return 1;
1570}
1571
Sven Eckelmanna5130882012-05-16 20:23:16 +02001572static int batadv_tt_global_valid(const void *entry_ptr,
1573 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001575 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1576 const struct batadv_tt_global_entry *tt_global_entry;
1577 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001579 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1580 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001581 return 0;
1582
Sven Eckelmann56303d32012-06-05 22:31:31 +02001583 tt_global_entry = container_of(tt_common_entry,
1584 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001585 common);
1586
Sven Eckelmanna5130882012-05-16 20:23:16 +02001587 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588}
1589
Sven Eckelmanna5130882012-05-16 20:23:16 +02001590static struct sk_buff *
1591batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001592 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001593 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001594 int (*valid_cb)(const void *, const void *),
1595 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001597 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001598 struct batadv_tt_query_packet *tt_response;
1599 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001600 struct hlist_node *node;
1601 struct hlist_head *head;
1602 struct sk_buff *skb = NULL;
1603 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001604 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001605 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001606 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607
1608 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1609 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001610 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001611 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001612 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613
Sven Eckelmann96412692012-06-05 22:31:30 +02001614 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001615 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616 if (!skb)
1617 goto out;
1618
Sven Eckelmann5b246572012-11-04 17:11:45 +01001619 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001620 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001622
Sven Eckelmann96412692012-06-05 22:31:30 +02001623 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 tt_count = 0;
1625
1626 rcu_read_lock();
1627 for (i = 0; i < hash->size; i++) {
1628 head = &hash->table[i];
1629
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001630 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631 head, hash_entry) {
1632 if (tt_count == tt_tot)
1633 break;
1634
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001635 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636 continue;
1637
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001638 memcpy(tt_change->addr, tt_common_entry->addr,
1639 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01001640 tt_change->flags = tt_common_entry->flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641
1642 tt_count++;
1643 tt_change++;
1644 }
1645 }
1646 rcu_read_unlock();
1647
Antonio Quartulli9d852392011-10-17 14:25:13 +02001648 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001649 * copied
1650 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001651 tt_response->tt_data = htons(tt_count);
1652
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653out:
1654 return skb;
1655}
1656
Sven Eckelmann56303d32012-06-05 22:31:31 +02001657static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1658 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001659 uint8_t ttvn, uint16_t tt_crc,
1660 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001661{
1662 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001663 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001664 struct batadv_hard_iface *primary_if;
1665 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001667 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001669 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001670 if (!primary_if)
1671 goto out;
1672
1673 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001674 * reply from the same orig_node yet
1675 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001676 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001677 if (!tt_req_node)
1678 goto out;
1679
Sven Eckelmann5b246572012-11-04 17:11:45 +01001680 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001681 if (!skb)
1682 goto out;
1683
Sven Eckelmann5b246572012-11-04 17:11:45 +01001684 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685
Sven Eckelmann96412692012-06-05 22:31:30 +02001686 tt_req_len = sizeof(*tt_request);
1687 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001688
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001689 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001690 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1692 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001693 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001695 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001696 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697
1698 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001699 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001701 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1702 dst_orig_node->orig, (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001704 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001705
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001706 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1707 ret = 0;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001708
1709out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001710 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001711 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001712 if (ret)
1713 kfree_skb(skb);
1714 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001715 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 kfree(tt_req_node);
1719 }
1720 return ret;
1721}
1722
Sven Eckelmann96412692012-06-05 22:31:30 +02001723static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001724batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001725 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001726{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001727 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001728 struct batadv_orig_node *res_dst_orig_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730 uint8_t orig_ttvn, req_ttvn, ttvn;
1731 int ret = false;
1732 unsigned char *tt_buff;
1733 bool full_table;
1734 uint16_t tt_len, tt_tot;
1735 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001736 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001737 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001738 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001740 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001741 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1742 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001743 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744
1745 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001746 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 if (!req_dst_orig_node)
1748 goto out;
1749
Sven Eckelmannda641192012-05-12 13:48:56 +02001750 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 if (!res_dst_orig_node)
1752 goto out;
1753
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001754 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755 if (!primary_if)
1756 goto out;
1757
1758 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1759 req_ttvn = tt_request->ttvn;
1760
Antonio Quartulli015758d2011-07-09 17:52:13 +02001761 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001763 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001764 goto out;
1765
Antonio Quartulli015758d2011-07-09 17:52:13 +02001766 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001767 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001768 !req_dst_orig_node->tt_buff)
1769 full_table = true;
1770 else
1771 full_table = false;
1772
1773 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001774 * I'll send only one packet with as much TT entries as I can
1775 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 if (!full_table) {
1777 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1778 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001779 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Sven Eckelmann96412692012-06-05 22:31:30 +02001781 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001782 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783 if (!skb)
1784 goto unlock;
1785
Sven Eckelmann5b246572012-11-04 17:11:45 +01001786 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001787 packet_pos = skb_put(skb, len);
1788 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 tt_response->ttvn = req_ttvn;
1790 tt_response->tt_data = htons(tt_tot);
1791
Sven Eckelmann96412692012-06-05 22:31:30 +02001792 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001793 /* Copy the last orig_node's OGM buffer */
1794 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1795 req_dst_orig_node->tt_buff_len);
1796
1797 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1798 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001799 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1800 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1802
Sven Eckelmanna5130882012-05-16 20:23:16 +02001803 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001804 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001805 primary_if,
1806 batadv_tt_global_valid,
1807 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808 if (!skb)
1809 goto out;
1810
Sven Eckelmann96412692012-06-05 22:31:30 +02001811 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812 }
1813
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001814 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001815 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001816 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001817 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1818 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001819 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820
1821 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001822 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001824 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001825 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1826 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001827
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001828 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001829
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001830 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1831 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832 goto out;
1833
1834unlock:
1835 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1836
1837out:
1838 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001839 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001841 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001843 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 if (!ret)
1845 kfree_skb(skb);
1846 return ret;
1847
1848}
Sven Eckelmann96412692012-06-05 22:31:30 +02001849
1850static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001851batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001852 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001853{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001854 struct batadv_orig_node *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001855 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001856 uint8_t my_ttvn, req_ttvn, ttvn;
1857 int ret = false;
1858 unsigned char *tt_buff;
1859 bool full_table;
1860 uint16_t tt_len, tt_tot;
1861 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001862 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001863 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001864 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001866 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001867 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1868 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001869 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001870
1871
Sven Eckelmann807736f2012-07-15 22:26:51 +02001872 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001873 req_ttvn = tt_request->ttvn;
1874
Sven Eckelmannda641192012-05-12 13:48:56 +02001875 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001876 if (!orig_node)
1877 goto out;
1878
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001879 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001880 if (!primary_if)
1881 goto out;
1882
1883 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001884 * is too big send the whole local translation table
1885 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001886 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001887 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001888 full_table = true;
1889 else
1890 full_table = false;
1891
1892 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001893 * I'll send only one packet with as much TT entries as I can
1894 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001896 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1897 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001898 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899
Sven Eckelmann96412692012-06-05 22:31:30 +02001900 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001901 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001902 if (!skb)
1903 goto unlock;
1904
Sven Eckelmann5b246572012-11-04 17:11:45 +01001905 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001906 packet_pos = skb_put(skb, len);
1907 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908 tt_response->ttvn = req_ttvn;
1909 tt_response->tt_data = htons(tt_tot);
1910
Sven Eckelmann96412692012-06-05 22:31:30 +02001911 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001912 memcpy(tt_buff, bat_priv->tt.last_changeset,
1913 bat_priv->tt.last_changeset_len);
1914 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001916 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001917 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001918 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001919
Sven Eckelmanna5130882012-05-16 20:23:16 +02001920 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001921 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001922 primary_if,
1923 batadv_tt_local_valid_entry,
1924 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001925 if (!skb)
1926 goto out;
1927
Sven Eckelmann96412692012-06-05 22:31:30 +02001928 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001929 }
1930
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001931 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001932 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001933 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1935 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001936 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937
1938 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001939 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001940
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001941 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001942 "Sending TT_RESPONSE to %pM [%c]\n",
1943 orig_node->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001944 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001945
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001946 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001947
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02001948 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1949 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001950 goto out;
1951
1952unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001953 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001954out:
1955 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001956 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001957 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001958 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959 if (!ret)
1960 kfree_skb(skb);
1961 /* This packet was for me, so it doesn't need to be re-routed */
1962 return true;
1963}
1964
Sven Eckelmann56303d32012-06-05 22:31:31 +02001965bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001966 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001968 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001969 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001970 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001971 return true;
1972
Sven Eckelmanna5130882012-05-16 20:23:16 +02001973 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001974 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001975 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001976 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001977}
1978
Sven Eckelmann56303d32012-06-05 22:31:31 +02001979static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1980 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001981 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001982 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001983{
1984 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001985 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986
1987 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001988 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1989 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001990 batadv_tt_global_del(bat_priv, orig_node,
1991 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001992 "tt removed by changes",
1993 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001994 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001995 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001996 (tt_change + i)->addr,
1997 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998 /* In case of problem while storing a
1999 * global_entry, we stop the updating
2000 * procedure without committing the
2001 * ttvn change. This will avoid to send
2002 * corrupted data on tt_request
2003 */
2004 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002005 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002006 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002007 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002008}
2009
Sven Eckelmann56303d32012-06-05 22:31:31 +02002010static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002011 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002012{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002013 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014
Sven Eckelmannda641192012-05-12 13:48:56 +02002015 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002016 if (!orig_node)
2017 goto out;
2018
2019 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002020 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021
Sven Eckelmanna5130882012-05-16 20:23:16 +02002022 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02002023 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02002024 ntohs(tt_response->tt_data),
2025 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
2027 spin_lock_bh(&orig_node->tt_buff_lock);
2028 kfree(orig_node->tt_buff);
2029 orig_node->tt_buff_len = 0;
2030 orig_node->tt_buff = NULL;
2031 spin_unlock_bh(&orig_node->tt_buff_lock);
2032
2033 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2034
2035out:
2036 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002037 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002038}
2039
Sven Eckelmann56303d32012-06-05 22:31:31 +02002040static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2041 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002042 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02002043 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002044{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002045 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2046 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002047
Sven Eckelmanna5130882012-05-16 20:23:16 +02002048 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2049 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002050 atomic_set(&orig_node->last_ttvn, ttvn);
2051}
2052
Sven Eckelmann56303d32012-06-05 22:31:31 +02002053bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002054{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002055 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002056 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002057
Sven Eckelmanna5130882012-05-16 20:23:16 +02002058 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002059 if (!tt_local_entry)
2060 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002061 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002062 * consistency purpose)
2063 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002064 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2065 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002066 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002067 ret = true;
2068out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002069 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002070 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002071 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002072}
2073
Sven Eckelmann56303d32012-06-05 22:31:31 +02002074void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02002075 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002076{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002077 struct batadv_tt_req_node *node, *safe;
2078 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002079 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002081 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002082 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2083 tt_response->src, tt_response->ttvn,
2084 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002085 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002086
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002087 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002088 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002089 goto out;
2090
Sven Eckelmannda641192012-05-12 13:48:56 +02002091 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002092 if (!orig_node)
2093 goto out;
2094
Sven Eckelmann96412692012-06-05 22:31:30 +02002095 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002096 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02002097 } else {
2098 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002099 batadv_tt_update_changes(bat_priv, orig_node,
2100 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02002101 tt_response->ttvn, tt_change);
2102 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002103
2104 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002105 spin_lock_bh(&bat_priv->tt.req_list_lock);
2106 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002107 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002108 continue;
2109 list_del(&node->list);
2110 kfree(node);
2111 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002112 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002113
2114 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002115 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116out:
2117 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002118 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002119}
2120
Sven Eckelmann56303d32012-06-05 22:31:31 +02002121int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002123 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124
Sven Eckelmanna5130882012-05-16 20:23:16 +02002125 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002126 if (ret < 0)
2127 return ret;
2128
Sven Eckelmanna5130882012-05-16 20:23:16 +02002129 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002130 if (ret < 0)
2131 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002132
Sven Eckelmanna5130882012-05-16 20:23:16 +02002133 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002134
2135 return 1;
2136}
2137
Sven Eckelmann56303d32012-06-05 22:31:31 +02002138static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002139{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002140 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002141
Sven Eckelmann807736f2012-07-15 22:26:51 +02002142 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002143
Sven Eckelmann807736f2012-07-15 22:26:51 +02002144 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002145 list_del(&node->list);
2146 kfree(node);
2147 }
2148
Sven Eckelmann807736f2012-07-15 22:26:51 +02002149 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002150}
2151
Sven Eckelmann56303d32012-06-05 22:31:31 +02002152static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002153{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002155
Sven Eckelmann807736f2012-07-15 22:26:51 +02002156 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2157 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002158 if (!batadv_has_timed_out(node->first_time,
2159 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002160 continue;
2161
2162 list_del(&node->list);
2163 kfree(node);
2164 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002165 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002166}
2167
2168/* This function checks whether the client already reached the
2169 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2170 * will not be sent.
2171 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002172 * returns true if the ROAMING_ADV can be sent, false otherwise
2173 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002174static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002175 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002176{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002177 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002178 bool ret = false;
2179
Sven Eckelmann807736f2012-07-15 22:26:51 +02002180 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002181 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002182 * reply from the same orig_node yet
2183 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002184 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002185 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002186 continue;
2187
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002188 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002189 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002190 continue;
2191
Sven Eckelmann3e348192012-05-16 20:23:22 +02002192 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002193 /* Sorry, you roamed too many times! */
2194 goto unlock;
2195 ret = true;
2196 break;
2197 }
2198
2199 if (!ret) {
2200 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2201 if (!tt_roam_node)
2202 goto unlock;
2203
2204 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002205 atomic_set(&tt_roam_node->counter,
2206 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002207 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2208
Sven Eckelmann807736f2012-07-15 22:26:51 +02002209 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002210 ret = true;
2211 }
2212
2213unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002214 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002215 return ret;
2216}
2217
Sven Eckelmann56303d32012-06-05 22:31:31 +02002218static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2219 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002220{
Antonio Quartullicc47f662011-04-27 14:27:57 +02002221 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002222 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002223 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002224 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002225 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002226
2227 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002228 * already roamed to us too many times
2229 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002230 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002231 goto out;
2232
Sven Eckelmann5b246572012-11-04 17:11:45 +01002233 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002234 if (!skb)
2235 goto out;
2236
Sven Eckelmann5b246572012-11-04 17:11:45 +01002237 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002238
Sven Eckelmann96412692012-06-05 22:31:30 +02002239 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002240
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002241 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002242 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002243 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002244 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002245 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002246 if (!primary_if)
2247 goto out;
2248 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002249 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002250 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2251 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2252
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002253 batadv_dbg(BATADV_DBG_TT, bat_priv,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002254 "Sending ROAMING_ADV to %pM (client %pM)\n",
2255 orig_node->orig, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002256
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002257 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002258
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002259 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2260 ret = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002261
2262out:
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002263 if (ret && skb)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002264 kfree_skb(skb);
2265 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002266}
2267
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002269{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002270 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002271 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002272 struct batadv_priv *bat_priv;
2273
2274 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002275 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2276 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002277
Sven Eckelmanna5130882012-05-16 20:23:16 +02002278 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002279 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002280 batadv_tt_req_purge(bat_priv);
2281 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002282
Sven Eckelmanna5130882012-05-16 20:23:16 +02002283 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002284}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002285
Sven Eckelmann56303d32012-06-05 22:31:31 +02002286void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002287{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002288 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002289
Sven Eckelmanna5130882012-05-16 20:23:16 +02002290 batadv_tt_local_table_free(bat_priv);
2291 batadv_tt_global_table_free(bat_priv);
2292 batadv_tt_req_list_free(bat_priv);
2293 batadv_tt_changes_list_free(bat_priv);
2294 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002295
Sven Eckelmann807736f2012-07-15 22:26:51 +02002296 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002297}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002298
Antonio Quartulli697f2532011-11-07 16:47:01 +01002299/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002300 * in the given hash table and returns the number of modified entries
2301 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002302static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2303 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002304{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002305 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002306 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002307 struct hlist_head *head;
2308 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002309 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002310
2311 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002312 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002313
2314 for (i = 0; i < hash->size; i++) {
2315 head = &hash->table[i];
2316
2317 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002318 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002319 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002320 if (enable) {
2321 if ((tt_common_entry->flags & flags) == flags)
2322 continue;
2323 tt_common_entry->flags |= flags;
2324 } else {
2325 if (!(tt_common_entry->flags & flags))
2326 continue;
2327 tt_common_entry->flags &= ~flags;
2328 }
2329 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002330 }
2331 rcu_read_unlock();
2332 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002333out:
2334 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002335}
2336
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002337/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002338static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002339{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002340 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002341 struct batadv_tt_common_entry *tt_common;
2342 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002343 struct hlist_node *node, *node_tmp;
2344 struct hlist_head *head;
2345 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002346 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002347
2348 if (!hash)
2349 return;
2350
2351 for (i = 0; i < hash->size; i++) {
2352 head = &hash->table[i];
2353 list_lock = &hash->list_locks[i];
2354
2355 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002356 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2357 hash_entry) {
2358 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002359 continue;
2360
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002361 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002362 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002363 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002364
Sven Eckelmann807736f2012-07-15 22:26:51 +02002365 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002366 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002367 tt_local = container_of(tt_common,
2368 struct batadv_tt_local_entry,
2369 common);
2370 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002371 }
2372 spin_unlock_bh(list_lock);
2373 }
2374
2375}
2376
Sven Eckelmann56303d32012-06-05 22:31:31 +02002377static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002378 unsigned char **packet_buff,
2379 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002380{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002381 uint16_t changed_num = 0;
2382
Sven Eckelmann807736f2012-07-15 22:26:51 +02002383 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002384 return -ENOENT;
2385
Sven Eckelmann807736f2012-07-15 22:26:51 +02002386 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002387 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002388
2389 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002390 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002391 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002392 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002393
2394 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002395 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002396 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002397 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002398 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002399
2400 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002401 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002402
Sven Eckelmanna5130882012-05-16 20:23:16 +02002403 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2404 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002405}
2406
2407/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002408int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002409 unsigned char **packet_buff, int *packet_buff_len,
2410 int packet_min_len)
2411{
2412 int tt_num_changes;
2413
2414 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002415 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2416 packet_buff_len,
2417 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002418
2419 /* if the changes have been sent often enough */
2420 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002421 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002422 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2423 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002424 tt_num_changes = 0;
2425 }
2426
2427 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002428}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002429
Sven Eckelmann56303d32012-06-05 22:31:31 +02002430bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002431 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002432{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002433 struct batadv_tt_local_entry *tt_local_entry = NULL;
2434 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002435 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002436
2437 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002438 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002439
Sven Eckelmanna5130882012-05-16 20:23:16 +02002440 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002441 if (!tt_local_entry)
2442 goto out;
2443
Sven Eckelmanna5130882012-05-16 20:23:16 +02002444 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002445 if (!tt_global_entry)
2446 goto out;
2447
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002448 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002449 goto out;
2450
Marek Lindner5870adc2012-06-20 17:16:05 +02002451 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002452
2453out:
2454 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002455 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002456 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002457 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002458 return ret;
2459}
Marek Lindnera943cac2011-07-30 13:10:18 +02002460
Sven Eckelmann56303d32012-06-05 22:31:31 +02002461void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2462 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002463 const unsigned char *tt_buff, uint8_t tt_num_changes,
2464 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002465{
2466 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2467 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002468 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002469
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002470 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002471 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002472 return;
2473
Antonio Quartulli17071572011-11-07 16:36:40 +01002474 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002475 * increased by one -> we can apply the attached changes
2476 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002477 if ((!orig_node->tt_initialised && ttvn == 1) ||
2478 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002479 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002480 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2481 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002482 * In this case send a tt request
2483 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002484 if (!tt_num_changes) {
2485 full_table = false;
2486 goto request_table;
2487 }
2488
Sven Eckelmann96412692012-06-05 22:31:30 +02002489 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002490 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002491 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002492
2493 /* Even if we received the precomputed crc with the OGM, we
2494 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002495 * in the global table
2496 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002497 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002498
2499 /* The ttvn alone is not enough to guarantee consistency
2500 * because a single value could represent different states
2501 * (due to the wrap around). Thus a node has to check whether
2502 * the resulting table (after applying the changes) is still
2503 * consistent or not. E.g. a node could disconnect while its
2504 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2505 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002506 * inconsistency
2507 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002508 if (orig_node->tt_crc != tt_crc)
2509 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002510 } else {
2511 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002512 * in sync anymore -> request fresh tt data
2513 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002514 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2515 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002516request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002517 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002518 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2519 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2520 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002521 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2522 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002523 return;
2524 }
2525 }
2526}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002527
2528/* returns true whether we know that the client has moved from its old
2529 * originator to another one. This entry is kept is still kept for consistency
2530 * purposes
2531 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002532bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002533 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002534{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002535 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002536 bool ret = false;
2537
Sven Eckelmanna5130882012-05-16 20:23:16 +02002538 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002539 if (!tt_global_entry)
2540 goto out;
2541
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002542 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002543 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002544out:
2545 return ret;
2546}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002547
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002548/**
2549 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2550 * @bat_priv: the bat priv with all the soft interface information
2551 * @addr: the MAC address of the local client to query
2552 *
2553 * Returns true if the local client is known to be roaming (it is not served by
2554 * this node anymore) or not. If yes, the client is still present in the table
2555 * to keep the latter consistent with the node TTVN
2556 */
2557bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2558 uint8_t *addr)
2559{
2560 struct batadv_tt_local_entry *tt_local_entry;
2561 bool ret = false;
2562
2563 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2564 if (!tt_local_entry)
2565 goto out;
2566
2567 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2568 batadv_tt_local_entry_free_ref(tt_local_entry);
2569out:
2570 return ret;
2571
2572}
2573
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002574bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2575 struct batadv_orig_node *orig_node,
2576 const unsigned char *addr)
2577{
2578 bool ret = false;
2579
Antonio Quartulli1f36aeb2012-11-08 21:55:29 +01002580 /* if the originator is a backbone node (meaning it belongs to the same
2581 * LAN of this node) the temporary client must not be added because to
2582 * reach such destination the node must use the LAN instead of the mesh
2583 */
2584 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2585 goto out;
2586
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002587 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2588 BATADV_TT_CLIENT_TEMP,
2589 atomic_read(&orig_node->last_ttvn)))
2590 goto out;
2591
2592 batadv_dbg(BATADV_DBG_TT, bat_priv,
2593 "Added temporary global client (addr: %pM orig: %pM)\n",
2594 addr, orig_node->orig);
2595 ret = true;
2596out:
2597 return ret;
2598}