blob: a570d957a5a057c60401abd3a107e71a5f731cca [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
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200241void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
242 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 struct batadv_tt_local_entry *tt_local_entry = NULL;
246 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200247 struct hlist_head *head;
248 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200249 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100250 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Sven Eckelmanna5130882012-05-16 20:23:16 +0200252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200254 if (tt_local_entry) {
255 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200258 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 }
260
Sven Eckelmann704509b2011-05-14 23:14:54 +0200261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200262 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200263 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200265 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200267 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200270 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200271 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100273 atomic_set(&tt_local_entry->common.refcount, 2);
274 tt_local_entry->last_seen = jiffies;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200275 tt_local_entry->common.added_at = tt_local_entry->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
277 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200278 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200283 * (consistency check)
284 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100286
Sven Eckelmann807736f2012-07-15 22:26:51 +0200287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200288 batadv_choose_orig,
289 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200290 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100291
292 if (unlikely(hash_added != 0)) {
293 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200294 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100295 goto out;
296 }
297
Sven Eckelmanna5130882012-05-16 20:23:16 +0200298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200299
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartullicc47f662011-04-27 14:27:57 +0200303 /* Check whether it is a roaming! */
304 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200305 /* These node are probably going to update their tt table */
306 head = &tt_global_entry->orig_list;
307 rcu_read_lock();
308 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
309 orig_entry->orig_node->tt_poss_change = true;
310
Sven Eckelmanna5130882012-05-16 20:23:16 +0200311 batadv_send_roam_adv(bat_priv,
312 tt_global_entry->common.addr,
313 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200314 }
315 rcu_read_unlock();
316 /* The global entry has to be marked as ROAMING and
317 * has to be kept for consistency purpose
318 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200319 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100320 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200321 }
322out:
323 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200324 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200325 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200326 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327}
328
Sven Eckelmanna5130882012-05-16 20:23:16 +0200329static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
330 int *packet_buff_len,
331 int min_packet_len,
332 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800334 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800336 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
337
338 /* keep old buffer if kmalloc should fail */
339 if (new_buff) {
340 memcpy(new_buff, *packet_buff, min_packet_len);
341 kfree(*packet_buff);
342 *packet_buff = new_buff;
343 *packet_buff_len = new_packet_len;
344 }
345}
346
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200348 unsigned char **packet_buff,
349 int *packet_buff_len,
350 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800351{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800353 int req_len;
354
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200355 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800356
357 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200358 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800359
360 /* if we have too many changes for one packet don't send any
361 * and wait for the tt table request which will be fragmented
362 */
363 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
364 req_len = min_packet_len;
365
Sven Eckelmanna5130882012-05-16 20:23:16 +0200366 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
367 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800368
369 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200370 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800371}
372
Sven Eckelmann56303d32012-06-05 22:31:31 +0200373static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200374 unsigned char **packet_buff,
375 int *packet_buff_len,
376 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800377{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200378 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800379 int count = 0, tot_changes = 0, new_len;
380 unsigned char *tt_buff;
381
Sven Eckelmanna5130882012-05-16 20:23:16 +0200382 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
383 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800384
385 new_len = *packet_buff_len - min_packet_len;
386 tt_buff = *packet_buff + min_packet_len;
387
388 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200389 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
Sven Eckelmann807736f2012-07-15 22:26:51 +0200391 spin_lock_bh(&bat_priv->tt.changes_list_lock);
392 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Sven Eckelmann807736f2012-07-15 22:26:51 +0200394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100395 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200397 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200398 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 count++;
400 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200401 list_del(&entry->list);
402 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200404 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Antonio Quartullia73105b2011-04-27 14:27:44 +0200406 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200407 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
408 kfree(bat_priv->tt.last_changeset);
409 bat_priv->tt.last_changeset_len = 0;
410 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800411 /* check whether this new OGM has no changes due to size problems */
412 if (new_len > 0) {
413 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200414 * instead of providing the diff
415 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200416 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
417 if (bat_priv->tt.last_changeset) {
418 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
419 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200420 }
421 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200422 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Marek Lindner08ad76e2012-04-23 16:32:55 +0800424 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425}
426
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200427int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428{
429 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200432 struct batadv_tt_common_entry *tt_common_entry;
433 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000434 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200436 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Marek Lindner30da63a2012-08-03 17:15:46 +0200438 primary_if = batadv_seq_print_text_primary_if_get(seq);
439 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200440 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100442 seq_printf(seq,
443 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200444 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448
Marek Lindner7aadf882011-02-18 12:28:09 +0000449 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100450 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000451 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200452 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100453 tt_common_entry->addr,
454 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200455 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100456 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200457 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100458 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200459 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100460 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200461 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100462 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200463 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000465 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200467out:
468 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200469 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200470 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471}
472
Sven Eckelmann56303d32012-06-05 22:31:31 +0200473static void
474batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
475 struct batadv_tt_local_entry *tt_local_entry,
476 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200478 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
479 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Antonio Quartulli015758d2011-07-09 17:52:13 +0200481 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200483 * response issued before the net ttvn increment (consistency check)
484 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200485 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100486
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200487 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200488 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200492/**
493 * batadv_tt_local_remove - logically remove an entry from the local table
494 * @bat_priv: the bat priv with all the soft interface information
495 * @addr: the MAC address of the client to remove
496 * @message: message to append to the log on deletion
497 * @roaming: true if the deletion is due to a roaming event
498 *
499 * Returns the flags assigned to the local entry before being deleted
500 */
501uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
502 const uint8_t *addr, const char *message,
503 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200505 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200506 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Sven Eckelmanna5130882012-05-16 20:23:16 +0200508 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200509 if (!tt_local_entry)
510 goto out;
511
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200512 curr_flags = tt_local_entry->common.flags;
513
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200514 flags = BATADV_TT_CLIENT_DEL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200515 if (roaming)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200516 flags |= BATADV_TT_CLIENT_ROAM;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200517
518 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200519
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200520out:
521 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200522 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200523
524 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525}
526
Sven Eckelmann56303d32012-06-05 22:31:31 +0200527static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200528 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200530 struct batadv_tt_local_entry *tt_local_entry;
531 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000532 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200533
534 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
535 hash_entry) {
536 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200537 struct batadv_tt_local_entry,
538 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200539 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
540 continue;
541
542 /* entry already marked for deletion */
543 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
544 continue;
545
546 if (!batadv_has_timed_out(tt_local_entry->last_seen,
547 BATADV_TT_LOCAL_TIMEOUT))
548 continue;
549
550 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
551 BATADV_TT_CLIENT_DEL, "timed out");
552 }
553}
554
Sven Eckelmann56303d32012-06-05 22:31:31 +0200555static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200556{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200557 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200559 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200560 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562 for (i = 0; i < hash->size; i++) {
563 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200564 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200566 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200567 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200568 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 }
570
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571}
572
Sven Eckelmann56303d32012-06-05 22:31:31 +0200573static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200575 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200576 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200577 struct batadv_tt_common_entry *tt_common_entry;
578 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200579 struct hlist_node *node, *node_tmp;
580 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200581 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200582
Sven Eckelmann807736f2012-07-15 22:26:51 +0200583 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 return;
585
Sven Eckelmann807736f2012-07-15 22:26:51 +0200586 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200587
588 for (i = 0; i < hash->size; i++) {
589 head = &hash->table[i];
590 list_lock = &hash->list_locks[i];
591
592 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100593 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200594 head, hash_entry) {
595 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200596 tt_local = container_of(tt_common_entry,
597 struct batadv_tt_local_entry,
598 common);
599 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200600 }
601 spin_unlock_bh(list_lock);
602 }
603
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200604 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200605
Sven Eckelmann807736f2012-07-15 22:26:51 +0200606 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607}
608
Sven Eckelmann56303d32012-06-05 22:31:31 +0200609static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200611 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200612 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613
Sven Eckelmann807736f2012-07-15 22:26:51 +0200614 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615
Sven Eckelmann807736f2012-07-15 22:26:51 +0200616 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200617 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Sven Eckelmann5346c352012-05-05 13:27:28 +0200619 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620}
621
Sven Eckelmann56303d32012-06-05 22:31:31 +0200622static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200623{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200624 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200625
Sven Eckelmann807736f2012-07-15 22:26:51 +0200626 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200627
Sven Eckelmann807736f2012-07-15 22:26:51 +0200628 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200629 list) {
630 list_del(&entry->list);
631 kfree(entry);
632 }
633
Sven Eckelmann807736f2012-07-15 22:26:51 +0200634 atomic_set(&bat_priv->tt.local_changes, 0);
635 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200636}
637
Antonio Quartullid657e622012-07-01 14:09:12 +0200638/* retrieves the orig_tt_list_entry belonging to orig_node from the
639 * batadv_tt_global_entry list
640 *
641 * returns it with an increased refcounter, NULL if not found
642 */
643static struct batadv_tt_orig_list_entry *
644batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
645 const struct batadv_orig_node *orig_node)
646{
647 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
648 const struct hlist_head *head;
649 struct hlist_node *node;
650
651 rcu_read_lock();
652 head = &entry->orig_list;
653 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
654 if (tmp_orig_entry->orig_node != orig_node)
655 continue;
656 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
657 continue;
658
659 orig_entry = tmp_orig_entry;
660 break;
661 }
662 rcu_read_unlock();
663
664 return orig_entry;
665}
666
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200667/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200668 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200669 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200670static bool
671batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
672 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200673{
Antonio Quartullid657e622012-07-01 14:09:12 +0200674 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200675 bool found = false;
676
Antonio Quartullid657e622012-07-01 14:09:12 +0200677 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
678 if (orig_entry) {
679 found = true;
680 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200681 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200682
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200683 return found;
684}
685
Sven Eckelmanna5130882012-05-16 20:23:16 +0200686static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200687batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200688 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200689{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200690 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200691
Antonio Quartullid657e622012-07-01 14:09:12 +0200692 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200693 if (orig_entry) {
694 /* refresh the ttvn: the current value could be a bogus one that
695 * was added during a "temporary client detection"
696 */
697 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200698 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200699 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200700
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200701 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
702 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200703 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200704
705 INIT_HLIST_NODE(&orig_entry->list);
706 atomic_inc(&orig_node->refcount);
707 atomic_inc(&orig_node->tt_size);
708 orig_entry->orig_node = orig_node;
709 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200710 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200711
Antonio Quartullid657e622012-07-01 14:09:12 +0200712 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200713 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200714 &tt_global->orig_list);
715 spin_unlock_bh(&tt_global->list_lock);
716out:
717 if (orig_entry)
718 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200719}
720
Antonio Quartullia73105b2011-04-27 14:27:44 +0200721/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200722int batadv_tt_global_add(struct batadv_priv *bat_priv,
723 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200724 const unsigned char *tt_addr, uint8_t flags,
725 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200727 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200728 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100729 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200730 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200731 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732
Sven Eckelmanna5130882012-05-16 20:23:16 +0200733 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734
Antonio Quartullia73105b2011-04-27 14:27:44 +0200735 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200736 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200737 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200738 goto out;
739
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200740 common = &tt_global_entry->common;
741 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200742
Antonio Quartullid4f44692012-05-25 00:00:54 +0200743 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200744 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200745 /* node must store current time in case of roaming. This is
746 * needed to purge this entry out on timeout (if nobody claims
747 * it)
748 */
749 if (flags & BATADV_TT_CLIENT_ROAM)
750 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200751 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200752 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200753
754 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
755 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200756
Sven Eckelmann807736f2012-07-15 22:26:51 +0200757 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200758 batadv_compare_tt,
759 batadv_choose_orig, common,
760 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100761
762 if (unlikely(hash_added != 0)) {
763 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200764 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100765 goto out_remove;
766 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200767 } else {
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200768 /* If there is already a global entry, we can use this one for
769 * our processing.
770 * But if we are trying to add a temporary client we can exit
771 * directly because the temporary information should never
772 * override any already known client state (whatever it is)
773 */
774 if (flags & BATADV_TT_CLIENT_TEMP)
775 goto out;
776
777 /* if the client was temporary added before receiving the first
778 * OGM announcing it, we have to clear the TEMP flag
779 */
780 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200781
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200782 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
783 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200784 * delete + roaming change for this originator.
785 *
786 * We should first delete the old originator before adding the
787 * new one.
788 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200789 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200790 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200791 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200792 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793 }
794 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200795 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200796 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200797
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200798 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200799 "Creating new global tt entry: %pM (via %pM)\n",
800 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200801 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200802
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100803out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200804
Antonio Quartullia73105b2011-04-27 14:27:44 +0200805 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200806 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
807 "global tt received",
808 flags & BATADV_TT_CLIENT_ROAM);
809 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
810
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200811out:
812 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200813 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200814 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815}
816
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200817/* print all orig nodes who announce the address for this global entry.
818 * it is assumed that the caller holds rcu_read_lock();
819 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200820static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200821batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200822 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200823{
824 struct hlist_head *head;
825 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200826 struct batadv_tt_orig_list_entry *orig_entry;
827 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200828 uint16_t flags;
829 uint8_t last_ttvn;
830
831 tt_common_entry = &tt_global_entry->common;
832
833 head = &tt_global_entry->orig_list;
834
835 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
836 flags = tt_common_entry->flags;
837 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200838 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200839 tt_global_entry->common.addr, orig_entry->ttvn,
840 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200841 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200842 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
843 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200844 }
845}
846
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200847int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848{
849 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200850 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200851 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200852 struct batadv_tt_common_entry *tt_common_entry;
853 struct batadv_tt_global_entry *tt_global;
854 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000855 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200857 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858
Marek Lindner30da63a2012-08-03 17:15:46 +0200859 primary_if = batadv_seq_print_text_primary_if_get(seq);
860 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200861 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200863 seq_printf(seq,
864 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200866 seq_printf(seq, " %-13s %s %-15s %s %s\n",
867 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869 for (i = 0; i < hash->size; i++) {
870 head = &hash->table[i];
871
Marek Lindner7aadf882011-02-18 12:28:09 +0000872 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100873 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000874 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200875 tt_global = container_of(tt_common_entry,
876 struct batadv_tt_global_entry,
877 common);
878 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000880 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200882out:
883 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200884 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200885 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000886}
887
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200888/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200889static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200890batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200892 struct hlist_head *head;
893 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200894 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200895
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200896 spin_lock_bh(&tt_global_entry->list_lock);
897 head = &tt_global_entry->orig_list;
898 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
899 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200900 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200901 }
902 spin_unlock_bh(&tt_global_entry->list_lock);
903
904}
905
Sven Eckelmanna5130882012-05-16 20:23:16 +0200906static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200907batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
908 struct batadv_tt_global_entry *tt_global_entry,
909 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200910 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200911{
912 struct hlist_head *head;
913 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200914 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200915
916 spin_lock_bh(&tt_global_entry->list_lock);
917 head = &tt_global_entry->orig_list;
918 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
919 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200920 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200921 "Deleting %pM from global tt entry %pM: %s\n",
922 orig_node->orig,
923 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200924 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200925 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200926 }
927 }
928 spin_unlock_bh(&tt_global_entry->list_lock);
929}
930
Sven Eckelmann56303d32012-06-05 22:31:31 +0200931static void
932batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
933 struct batadv_tt_global_entry *tt_global_entry,
934 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200935{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200936 batadv_dbg(BATADV_DBG_TT, bat_priv,
937 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200938 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200939
Sven Eckelmann807736f2012-07-15 22:26:51 +0200940 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200941 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200942 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200943
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000944}
945
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200946/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200947 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
948 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200949 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200950static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200951batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
952 struct batadv_tt_global_entry *tt_global_entry,
953 struct batadv_orig_node *orig_node,
954 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200955{
956 bool last_entry = true;
957 struct hlist_head *head;
958 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200959 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200960
961 /* no local entry exists, case 1:
962 * Check if this is the last one or if other entries exist.
963 */
964
965 rcu_read_lock();
966 head = &tt_global_entry->orig_list;
967 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
968 if (orig_entry->orig_node != orig_node) {
969 last_entry = false;
970 break;
971 }
972 }
973 rcu_read_unlock();
974
975 if (last_entry) {
976 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200977 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200978 tt_global_entry->roam_at = jiffies;
979 } else
980 /* there is another entry, we can simply delete this
981 * one and can still use the other one.
982 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200983 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
984 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200985}
986
987
988
Sven Eckelmann56303d32012-06-05 22:31:31 +0200989static void batadv_tt_global_del(struct batadv_priv *bat_priv,
990 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200991 const unsigned char *addr,
992 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000993{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200994 struct batadv_tt_global_entry *tt_global_entry = NULL;
995 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000996
Sven Eckelmanna5130882012-05-16 20:23:16 +0200997 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200998 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200999 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001000
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001001 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001002 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1003 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001004
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001005 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +02001006 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
1007 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001008
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001009 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001010 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001011
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001012 /* if we are deleting a global entry due to a roam
1013 * event, there are two possibilities:
1014 * 1) the client roamed from node A to node B => if there
1015 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001016 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001017 * wait for node B to claim it. In case of timeout
1018 * the entry is purged.
1019 *
1020 * If there are other originators left, we directly delete
1021 * the originator.
1022 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001023 * the global entry, since it is useless now.
1024 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001025 local_entry = batadv_tt_local_hash_find(bat_priv,
1026 tt_global_entry->common.addr);
1027 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001028 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001029 batadv_tt_global_del_orig_list(tt_global_entry);
1030 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001031 } else
1032 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001033 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1034 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001035
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001036
Antonio Quartullicc47f662011-04-27 14:27:57 +02001037out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001038 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001039 batadv_tt_global_entry_free_ref(tt_global_entry);
1040 if (local_entry)
1041 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001042}
1043
Sven Eckelmann56303d32012-06-05 22:31:31 +02001044void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1045 struct batadv_orig_node *orig_node,
1046 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001047{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001048 struct batadv_tt_global_entry *tt_global;
1049 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001050 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001051 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001052 struct hlist_node *node, *safe;
1053 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001054 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001055
Simon Wunderlich6e801492011-10-19 10:28:26 +02001056 if (!hash)
1057 return;
1058
Antonio Quartullia73105b2011-04-27 14:27:44 +02001059 for (i = 0; i < hash->size; i++) {
1060 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001061 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001063 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001064 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001065 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001066 tt_global = container_of(tt_common_entry,
1067 struct batadv_tt_global_entry,
1068 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001069
Sven Eckelmann56303d32012-06-05 22:31:31 +02001070 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001071 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001072
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001074 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001075 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001076 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001077 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001078 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001079 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001080 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001081 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001083 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084}
1085
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001086static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1087 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001088{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001089 bool purge = false;
1090 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1091 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001092
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001093 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1094 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1095 purge = true;
1096 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001097 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001098
1099 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1100 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1101 purge = true;
1102 *msg = "Temporary client timeout\n";
1103 }
1104
1105 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001106}
1107
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001108static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001109{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001110 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001111 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001112 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001113 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001114 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001115 char *msg = NULL;
1116 struct batadv_tt_common_entry *tt_common;
1117 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001118
Antonio Quartullicc47f662011-04-27 14:27:57 +02001119 for (i = 0; i < hash->size; i++) {
1120 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001121 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001122
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001123 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001124 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1125 hash_entry) {
1126 tt_global = container_of(tt_common,
1127 struct batadv_tt_global_entry,
1128 common);
1129
1130 if (!batadv_tt_global_to_purge(tt_global, &msg))
1131 continue;
1132
1133 batadv_dbg(BATADV_DBG_TT, bat_priv,
1134 "Deleting global tt entry (%pM): %s\n",
1135 tt_global->common.addr, msg);
1136
1137 hlist_del_rcu(node);
1138
1139 batadv_tt_global_entry_free_ref(tt_global);
1140 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001141 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001142 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001143}
1144
Sven Eckelmann56303d32012-06-05 22:31:31 +02001145static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001147 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001148 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001149 struct batadv_tt_common_entry *tt_common_entry;
1150 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001151 struct hlist_node *node, *node_tmp;
1152 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001153 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001154
Sven Eckelmann807736f2012-07-15 22:26:51 +02001155 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156 return;
1157
Sven Eckelmann807736f2012-07-15 22:26:51 +02001158 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001159
1160 for (i = 0; i < hash->size; i++) {
1161 head = &hash->table[i];
1162 list_lock = &hash->list_locks[i];
1163
1164 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001165 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001166 head, hash_entry) {
1167 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001168 tt_global = container_of(tt_common_entry,
1169 struct batadv_tt_global_entry,
1170 common);
1171 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001172 }
1173 spin_unlock_bh(list_lock);
1174 }
1175
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001176 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001177
Sven Eckelmann807736f2012-07-15 22:26:51 +02001178 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001179}
1180
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181static bool
1182_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1183 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001184{
1185 bool ret = false;
1186
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001187 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1188 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001189 ret = true;
1190
1191 return ret;
1192}
1193
Sven Eckelmann56303d32012-06-05 22:31:31 +02001194struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1195 const uint8_t *src,
1196 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001197{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001198 struct batadv_tt_local_entry *tt_local_entry = NULL;
1199 struct batadv_tt_global_entry *tt_global_entry = NULL;
1200 struct batadv_orig_node *orig_node = NULL;
1201 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001202 struct hlist_head *head;
1203 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001204 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001205 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001206
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001207 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001208 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001209 if (!tt_local_entry)
1210 goto out;
1211 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001212
Sven Eckelmanna5130882012-05-16 20:23:16 +02001213 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001214 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001215 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001216
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001217 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001218 * isolation
1219 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001220 if (tt_local_entry &&
1221 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001222 goto out;
1223
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001224 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001225
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001226 rcu_read_lock();
1227 head = &tt_global_entry->orig_list;
1228 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001229 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001230 if (!router)
1231 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001232
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001233 if (router->tq_avg > best_tq) {
1234 orig_node = orig_entry->orig_node;
1235 best_tq = router->tq_avg;
1236 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001237 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001238 }
1239 /* found anything? */
1240 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1241 orig_node = NULL;
1242 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001243out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001244 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001245 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001246 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001247 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001248
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001249 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001250}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001251
1252/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001253static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1254 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001255{
1256 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001257 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001258 struct batadv_tt_common_entry *tt_common;
1259 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260 struct hlist_node *node;
1261 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001262 uint32_t i;
1263 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001264
1265 for (i = 0; i < hash->size; i++) {
1266 head = &hash->table[i];
1267
1268 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001269 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001270 tt_global = container_of(tt_common,
1271 struct batadv_tt_global_entry,
1272 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001273 /* Roaming clients are in the global table for
1274 * consistency only. They don't have to be
1275 * taken into account while computing the
1276 * global crc
1277 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001278 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001279 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001280 /* Temporary clients have not been announced yet, so
1281 * they have to be skipped while computing the global
1282 * crc
1283 */
1284 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1285 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001286
1287 /* find out if this global entry is announced by this
1288 * originator
1289 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001290 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001291 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001292 continue;
1293
1294 total_one = 0;
1295 for (j = 0; j < ETH_ALEN; j++)
1296 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001297 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001298 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001299 }
1300 rcu_read_unlock();
1301 }
1302
1303 return total;
1304}
1305
1306/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001307static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001308{
1309 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001310 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001311 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001312 struct hlist_node *node;
1313 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001314 uint32_t i;
1315 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001316
1317 for (i = 0; i < hash->size; i++) {
1318 head = &hash->table[i];
1319
1320 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001321 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001322 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001323 * account while computing the CRC
1324 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001325 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001326 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001327 total_one = 0;
1328 for (j = 0; j < ETH_ALEN; j++)
1329 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001330 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331 total ^= total_one;
1332 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001333 rcu_read_unlock();
1334 }
1335
1336 return total;
1337}
1338
Sven Eckelmann56303d32012-06-05 22:31:31 +02001339static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001341 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001342
Sven Eckelmann807736f2012-07-15 22:26:51 +02001343 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001344
Sven Eckelmann807736f2012-07-15 22:26:51 +02001345 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001346 list_del(&node->list);
1347 kfree(node);
1348 }
1349
Sven Eckelmann807736f2012-07-15 22:26:51 +02001350 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001351}
1352
Sven Eckelmann56303d32012-06-05 22:31:31 +02001353static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1354 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001355 const unsigned char *tt_buff,
1356 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001357{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001358 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001359
1360 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001361 * last OGM (the OGM could carry no changes)
1362 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001363 spin_lock_bh(&orig_node->tt_buff_lock);
1364 if (tt_buff_len > 0) {
1365 kfree(orig_node->tt_buff);
1366 orig_node->tt_buff_len = 0;
1367 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1368 if (orig_node->tt_buff) {
1369 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1370 orig_node->tt_buff_len = tt_buff_len;
1371 }
1372 }
1373 spin_unlock_bh(&orig_node->tt_buff_lock);
1374}
1375
Sven Eckelmann56303d32012-06-05 22:31:31 +02001376static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001377{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001378 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001379
Sven Eckelmann807736f2012-07-15 22:26:51 +02001380 spin_lock_bh(&bat_priv->tt.req_list_lock);
1381 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001382 if (batadv_has_timed_out(node->issued_at,
1383 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001384 list_del(&node->list);
1385 kfree(node);
1386 }
1387 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001388 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001389}
1390
1391/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001392 * has already been issued for this orig_node, NULL otherwise
1393 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001394static struct batadv_tt_req_node *
1395batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1396 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399
Sven Eckelmann807736f2012-07-15 22:26:51 +02001400 spin_lock_bh(&bat_priv->tt.req_list_lock);
1401 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001402 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1403 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001404 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001405 goto unlock;
1406 }
1407
1408 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1409 if (!tt_req_node)
1410 goto unlock;
1411
1412 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1413 tt_req_node->issued_at = jiffies;
1414
Sven Eckelmann807736f2012-07-15 22:26:51 +02001415 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001417 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001418 return tt_req_node;
1419}
1420
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001421/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001422static int batadv_tt_local_valid_entry(const void *entry_ptr,
1423 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001424{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001425 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001426
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001427 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001428 return 0;
1429 return 1;
1430}
1431
Sven Eckelmanna5130882012-05-16 20:23:16 +02001432static int batadv_tt_global_valid(const void *entry_ptr,
1433 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001434{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001435 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1436 const struct batadv_tt_global_entry *tt_global_entry;
1437 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001438
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001439 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1440 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001441 return 0;
1442
Sven Eckelmann56303d32012-06-05 22:31:31 +02001443 tt_global_entry = container_of(tt_common_entry,
1444 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001445 common);
1446
Sven Eckelmanna5130882012-05-16 20:23:16 +02001447 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448}
1449
Sven Eckelmanna5130882012-05-16 20:23:16 +02001450static struct sk_buff *
1451batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001452 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001453 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001454 int (*valid_cb)(const void *, const void *),
1455 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001456{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001457 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001458 struct batadv_tt_query_packet *tt_response;
1459 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001460 struct hlist_node *node;
1461 struct hlist_head *head;
1462 struct sk_buff *skb = NULL;
1463 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001464 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001465 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001466 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467
1468 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1469 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001470 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001472 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001473
Sven Eckelmann96412692012-06-05 22:31:30 +02001474 len = tt_query_size + tt_len;
1475 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001476 if (!skb)
1477 goto out;
1478
1479 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001480 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482
Sven Eckelmann96412692012-06-05 22:31:30 +02001483 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001484 tt_count = 0;
1485
1486 rcu_read_lock();
1487 for (i = 0; i < hash->size; i++) {
1488 head = &hash->table[i];
1489
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001490 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001491 head, hash_entry) {
1492 if (tt_count == tt_tot)
1493 break;
1494
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001495 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496 continue;
1497
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001498 memcpy(tt_change->addr, tt_common_entry->addr,
1499 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001500 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001501
1502 tt_count++;
1503 tt_change++;
1504 }
1505 }
1506 rcu_read_unlock();
1507
Antonio Quartulli9d852392011-10-17 14:25:13 +02001508 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001509 * copied
1510 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001511 tt_response->tt_data = htons(tt_count);
1512
Antonio Quartullia73105b2011-04-27 14:27:44 +02001513out:
1514 return skb;
1515}
1516
Sven Eckelmann56303d32012-06-05 22:31:31 +02001517static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1518 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001519 uint8_t ttvn, uint16_t tt_crc,
1520 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521{
1522 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001523 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001524 struct batadv_neigh_node *neigh_node = NULL;
1525 struct batadv_hard_iface *primary_if;
1526 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001528 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001530 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001531 if (!primary_if)
1532 goto out;
1533
1534 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001535 * reply from the same orig_node yet
1536 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001537 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001538 if (!tt_req_node)
1539 goto out;
1540
Sven Eckelmann96412692012-06-05 22:31:30 +02001541 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542 if (!skb)
1543 goto out;
1544
1545 skb_reserve(skb, ETH_HLEN);
1546
Sven Eckelmann96412692012-06-05 22:31:30 +02001547 tt_req_len = sizeof(*tt_request);
1548 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001549
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001550 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001551 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001552 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1553 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001554 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001556 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001557 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558
1559 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001560 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001562 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001563 if (!neigh_node)
1564 goto out;
1565
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001566 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001567 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1568 dst_orig_node->orig, neigh_node->addr,
1569 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001570
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001571 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001572
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001573 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001574 ret = 0;
1575
1576out:
1577 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001578 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001579 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001580 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581 if (ret)
1582 kfree_skb(skb);
1583 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001584 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001585 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001586 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001587 kfree(tt_req_node);
1588 }
1589 return ret;
1590}
1591
Sven Eckelmann96412692012-06-05 22:31:30 +02001592static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001593batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001594 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001596 struct batadv_orig_node *req_dst_orig_node = NULL;
1597 struct batadv_orig_node *res_dst_orig_node = NULL;
1598 struct batadv_neigh_node *neigh_node = NULL;
1599 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001600 uint8_t orig_ttvn, req_ttvn, ttvn;
1601 int ret = false;
1602 unsigned char *tt_buff;
1603 bool full_table;
1604 uint16_t tt_len, tt_tot;
1605 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001606 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001607 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001608 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001609
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001610 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001611 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1612 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001613 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001614
1615 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001616 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 if (!req_dst_orig_node)
1618 goto out;
1619
Sven Eckelmannda641192012-05-12 13:48:56 +02001620 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001621 if (!res_dst_orig_node)
1622 goto out;
1623
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001624 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001625 if (!neigh_node)
1626 goto out;
1627
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001628 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001629 if (!primary_if)
1630 goto out;
1631
1632 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1633 req_ttvn = tt_request->ttvn;
1634
Antonio Quartulli015758d2011-07-09 17:52:13 +02001635 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001637 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001638 goto out;
1639
Antonio Quartulli015758d2011-07-09 17:52:13 +02001640 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001641 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001642 !req_dst_orig_node->tt_buff)
1643 full_table = true;
1644 else
1645 full_table = false;
1646
1647 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001648 * I'll send only one packet with as much TT entries as I can
1649 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001650 if (!full_table) {
1651 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1652 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001653 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001654
Sven Eckelmann96412692012-06-05 22:31:30 +02001655 len = sizeof(*tt_response) + tt_len;
1656 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001657 if (!skb)
1658 goto unlock;
1659
1660 skb_reserve(skb, ETH_HLEN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001661 packet_pos = skb_put(skb, len);
1662 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001663 tt_response->ttvn = req_ttvn;
1664 tt_response->tt_data = htons(tt_tot);
1665
Sven Eckelmann96412692012-06-05 22:31:30 +02001666 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001667 /* Copy the last orig_node's OGM buffer */
1668 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1669 req_dst_orig_node->tt_buff_len);
1670
1671 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1672 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001673 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1674 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001675 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1676
Sven Eckelmanna5130882012-05-16 20:23:16 +02001677 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001678 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001679 primary_if,
1680 batadv_tt_global_valid,
1681 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001682 if (!skb)
1683 goto out;
1684
Sven Eckelmann96412692012-06-05 22:31:30 +02001685 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 }
1687
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001688 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001689 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001690 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001691 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1692 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001693 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694
1695 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001696 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001697
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001698 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001699 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1700 res_dst_orig_node->orig, neigh_node->addr,
1701 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001703 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001704
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001705 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001706 ret = true;
1707 goto out;
1708
1709unlock:
1710 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1711
1712out:
1713 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001714 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001716 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001718 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001720 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001721 if (!ret)
1722 kfree_skb(skb);
1723 return ret;
1724
1725}
Sven Eckelmann96412692012-06-05 22:31:30 +02001726
1727static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001728batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001729 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001730{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001731 struct batadv_orig_node *orig_node = NULL;
1732 struct batadv_neigh_node *neigh_node = NULL;
1733 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001734 uint8_t my_ttvn, req_ttvn, ttvn;
1735 int ret = false;
1736 unsigned char *tt_buff;
1737 bool full_table;
1738 uint16_t tt_len, tt_tot;
1739 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001740 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001741 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001742 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001744 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001745 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1746 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001747 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001748
1749
Sven Eckelmann807736f2012-07-15 22:26:51 +02001750 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 req_ttvn = tt_request->ttvn;
1752
Sven Eckelmannda641192012-05-12 13:48:56 +02001753 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001754 if (!orig_node)
1755 goto out;
1756
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001757 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001758 if (!neigh_node)
1759 goto out;
1760
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001761 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762 if (!primary_if)
1763 goto out;
1764
1765 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001766 * is too big send the whole local translation table
1767 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001768 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001769 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001770 full_table = true;
1771 else
1772 full_table = false;
1773
1774 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001775 * I'll send only one packet with as much TT entries as I can
1776 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001777 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001778 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1779 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001780 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001781
Sven Eckelmann96412692012-06-05 22:31:30 +02001782 len = sizeof(*tt_response) + tt_len;
1783 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001784 if (!skb)
1785 goto unlock;
1786
1787 skb_reserve(skb, ETH_HLEN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001788 packet_pos = skb_put(skb, len);
1789 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001790 tt_response->ttvn = req_ttvn;
1791 tt_response->tt_data = htons(tt_tot);
1792
Sven Eckelmann96412692012-06-05 22:31:30 +02001793 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001794 memcpy(tt_buff, bat_priv->tt.last_changeset,
1795 bat_priv->tt.last_changeset_len);
1796 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001797 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001798 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001799 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001800 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001801
Sven Eckelmanna5130882012-05-16 20:23:16 +02001802 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001803 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001804 primary_if,
1805 batadv_tt_local_valid_entry,
1806 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001807 if (!skb)
1808 goto out;
1809
Sven Eckelmann96412692012-06-05 22:31:30 +02001810 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001811 }
1812
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001813 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001814 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001815 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1817 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001818 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001819
1820 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001821 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001822
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001823 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001824 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1825 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001826 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
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
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001830 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001831 ret = true;
1832 goto out;
1833
1834unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001835 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001836out:
1837 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001838 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001840 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001842 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 if (!ret)
1844 kfree_skb(skb);
1845 /* This packet was for me, so it doesn't need to be re-routed */
1846 return true;
1847}
1848
Sven Eckelmann56303d32012-06-05 22:31:31 +02001849bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001850 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001852 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001853 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001854 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001855 return true;
1856
Sven Eckelmanna5130882012-05-16 20:23:16 +02001857 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001858 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001859 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001860 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001861}
1862
Sven Eckelmann56303d32012-06-05 22:31:31 +02001863static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1864 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001865 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001866 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001867{
1868 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001869 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001870
1871 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001872 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1873 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001874 batadv_tt_global_del(bat_priv, orig_node,
1875 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001876 "tt removed by changes",
1877 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001878 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001879 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001880 (tt_change + i)->addr,
1881 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001882 /* In case of problem while storing a
1883 * global_entry, we stop the updating
1884 * procedure without committing the
1885 * ttvn change. This will avoid to send
1886 * corrupted data on tt_request
1887 */
1888 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001889 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001890 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001891 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892}
1893
Sven Eckelmann56303d32012-06-05 22:31:31 +02001894static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001895 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001896{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001897 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898
Sven Eckelmannda641192012-05-12 13:48:56 +02001899 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001900 if (!orig_node)
1901 goto out;
1902
1903 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001904 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905
Sven Eckelmanna5130882012-05-16 20:23:16 +02001906 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001907 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001908 ntohs(tt_response->tt_data),
1909 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001910
1911 spin_lock_bh(&orig_node->tt_buff_lock);
1912 kfree(orig_node->tt_buff);
1913 orig_node->tt_buff_len = 0;
1914 orig_node->tt_buff = NULL;
1915 spin_unlock_bh(&orig_node->tt_buff_lock);
1916
1917 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1918
1919out:
1920 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001921 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922}
1923
Sven Eckelmann56303d32012-06-05 22:31:31 +02001924static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1925 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001926 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001927 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001928{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001929 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1930 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001931
Sven Eckelmanna5130882012-05-16 20:23:16 +02001932 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1933 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001934 atomic_set(&orig_node->last_ttvn, ttvn);
1935}
1936
Sven Eckelmann56303d32012-06-05 22:31:31 +02001937bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001938{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001939 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001940 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001941
Sven Eckelmanna5130882012-05-16 20:23:16 +02001942 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001943 if (!tt_local_entry)
1944 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001945 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001946 * consistency purpose)
1947 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001948 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001949 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001950 ret = true;
1951out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001953 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001954 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955}
1956
Sven Eckelmann56303d32012-06-05 22:31:31 +02001957void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001958 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001960 struct batadv_tt_req_node *node, *safe;
1961 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001962 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001964 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001965 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1966 tt_response->src, tt_response->ttvn,
1967 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001968 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001969
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001970 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001971 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001972 goto out;
1973
Sven Eckelmannda641192012-05-12 13:48:56 +02001974 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001975 if (!orig_node)
1976 goto out;
1977
Sven Eckelmann96412692012-06-05 22:31:30 +02001978 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001979 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001980 } else {
1981 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001982 batadv_tt_update_changes(bat_priv, orig_node,
1983 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001984 tt_response->ttvn, tt_change);
1985 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986
1987 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001988 spin_lock_bh(&bat_priv->tt.req_list_lock);
1989 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001990 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991 continue;
1992 list_del(&node->list);
1993 kfree(node);
1994 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001995 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996
1997 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001998 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001999 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002000 * unset the flag
2001 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02002002 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002003out:
2004 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002005 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002006}
2007
Sven Eckelmann56303d32012-06-05 22:31:31 +02002008int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002010 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011
Sven Eckelmanna5130882012-05-16 20:23:16 +02002012 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002013 if (ret < 0)
2014 return ret;
2015
Sven Eckelmanna5130882012-05-16 20:23:16 +02002016 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002017 if (ret < 0)
2018 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019
Sven Eckelmanna5130882012-05-16 20:23:16 +02002020 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021
2022 return 1;
2023}
2024
Sven Eckelmann56303d32012-06-05 22:31:31 +02002025static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002027 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028
Sven Eckelmann807736f2012-07-15 22:26:51 +02002029 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002030
Sven Eckelmann807736f2012-07-15 22:26:51 +02002031 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002032 list_del(&node->list);
2033 kfree(node);
2034 }
2035
Sven Eckelmann807736f2012-07-15 22:26:51 +02002036 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002037}
2038
Sven Eckelmann56303d32012-06-05 22:31:31 +02002039static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002040{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002041 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002042
Sven Eckelmann807736f2012-07-15 22:26:51 +02002043 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2044 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002045 if (!batadv_has_timed_out(node->first_time,
2046 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002047 continue;
2048
2049 list_del(&node->list);
2050 kfree(node);
2051 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002052 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002053}
2054
2055/* This function checks whether the client already reached the
2056 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2057 * will not be sent.
2058 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002059 * returns true if the ROAMING_ADV can be sent, false otherwise
2060 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002061static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002062 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002063{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002064 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002065 bool ret = false;
2066
Sven Eckelmann807736f2012-07-15 22:26:51 +02002067 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002068 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002069 * reply from the same orig_node yet
2070 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002071 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002072 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002073 continue;
2074
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002075 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002076 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002077 continue;
2078
Sven Eckelmann3e348192012-05-16 20:23:22 +02002079 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002080 /* Sorry, you roamed too many times! */
2081 goto unlock;
2082 ret = true;
2083 break;
2084 }
2085
2086 if (!ret) {
2087 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2088 if (!tt_roam_node)
2089 goto unlock;
2090
2091 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002092 atomic_set(&tt_roam_node->counter,
2093 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002094 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2095
Sven Eckelmann807736f2012-07-15 22:26:51 +02002096 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002097 ret = true;
2098 }
2099
2100unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002101 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002102 return ret;
2103}
2104
Sven Eckelmann56303d32012-06-05 22:31:31 +02002105static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2106 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002107{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002108 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002109 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002110 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002111 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002112 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002113 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002114
2115 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002116 * already roamed to us too many times
2117 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002118 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002119 goto out;
2120
Sven Eckelmann96412692012-06-05 22:31:30 +02002121 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002122 if (!skb)
2123 goto out;
2124
2125 skb_reserve(skb, ETH_HLEN);
2126
Sven Eckelmann96412692012-06-05 22:31:30 +02002127 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002128
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002129 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002130 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002131 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002132 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002133 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002134 if (!primary_if)
2135 goto out;
2136 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002137 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002138 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2139 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2140
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002141 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002142 if (!neigh_node)
2143 goto out;
2144
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002145 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002146 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2147 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002148
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002149 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002150
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02002151 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002152 ret = 0;
2153
2154out:
2155 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002156 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002157 if (ret)
2158 kfree_skb(skb);
2159 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002160}
2161
Sven Eckelmanna5130882012-05-16 20:23:16 +02002162static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002163{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002164 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002165 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002166 struct batadv_priv *bat_priv;
2167
2168 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002169 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2170 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002171
Sven Eckelmanna5130882012-05-16 20:23:16 +02002172 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002173 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002174 batadv_tt_req_purge(bat_priv);
2175 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002176
Sven Eckelmanna5130882012-05-16 20:23:16 +02002177 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002178}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002179
Sven Eckelmann56303d32012-06-05 22:31:31 +02002180void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002181{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002182 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002183
Sven Eckelmanna5130882012-05-16 20:23:16 +02002184 batadv_tt_local_table_free(bat_priv);
2185 batadv_tt_global_table_free(bat_priv);
2186 batadv_tt_req_list_free(bat_priv);
2187 batadv_tt_changes_list_free(bat_priv);
2188 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002189
Sven Eckelmann807736f2012-07-15 22:26:51 +02002190 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002191}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002192
Antonio Quartulli697f2532011-11-07 16:47:01 +01002193/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002194 * in the given hash table and returns the number of modified entries
2195 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002196static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2197 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002198{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002199 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002200 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002201 struct hlist_head *head;
2202 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002203 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002204
2205 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002206 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002207
2208 for (i = 0; i < hash->size; i++) {
2209 head = &hash->table[i];
2210
2211 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002212 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002213 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002214 if (enable) {
2215 if ((tt_common_entry->flags & flags) == flags)
2216 continue;
2217 tt_common_entry->flags |= flags;
2218 } else {
2219 if (!(tt_common_entry->flags & flags))
2220 continue;
2221 tt_common_entry->flags &= ~flags;
2222 }
2223 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002224 }
2225 rcu_read_unlock();
2226 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002227out:
2228 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002229}
2230
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002231/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002232static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002233{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002234 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002235 struct batadv_tt_common_entry *tt_common;
2236 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002237 struct hlist_node *node, *node_tmp;
2238 struct hlist_head *head;
2239 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002240 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002241
2242 if (!hash)
2243 return;
2244
2245 for (i = 0; i < hash->size; i++) {
2246 head = &hash->table[i];
2247 list_lock = &hash->list_locks[i];
2248
2249 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002250 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2251 hash_entry) {
2252 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002253 continue;
2254
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002255 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002256 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002257 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002258
Sven Eckelmann807736f2012-07-15 22:26:51 +02002259 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002260 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261 tt_local = container_of(tt_common,
2262 struct batadv_tt_local_entry,
2263 common);
2264 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002265 }
2266 spin_unlock_bh(list_lock);
2267 }
2268
2269}
2270
Sven Eckelmann56303d32012-06-05 22:31:31 +02002271static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002272 unsigned char **packet_buff,
2273 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002274{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002275 uint16_t changed_num = 0;
2276
Sven Eckelmann807736f2012-07-15 22:26:51 +02002277 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002278 return -ENOENT;
2279
Sven Eckelmann807736f2012-07-15 22:26:51 +02002280 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002281 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002282
2283 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002284 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002285 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002286 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002287
2288 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002289 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002290 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002291 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002292 (uint8_t)atomic_read(&bat_priv->tt.vn));
2293 bat_priv->tt.poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002294
2295 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002296 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002297
Sven Eckelmanna5130882012-05-16 20:23:16 +02002298 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2299 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002300}
2301
2302/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002303int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002304 unsigned char **packet_buff, int *packet_buff_len,
2305 int packet_min_len)
2306{
2307 int tt_num_changes;
2308
2309 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002310 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2311 packet_buff_len,
2312 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002313
2314 /* if the changes have been sent often enough */
2315 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002316 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002317 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2318 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002319 tt_num_changes = 0;
2320 }
2321
2322 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002323}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002324
Sven Eckelmann56303d32012-06-05 22:31:31 +02002325bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002326 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002327{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002328 struct batadv_tt_local_entry *tt_local_entry = NULL;
2329 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002330 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002331
2332 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002333 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002334
Sven Eckelmanna5130882012-05-16 20:23:16 +02002335 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002336 if (!tt_local_entry)
2337 goto out;
2338
Sven Eckelmanna5130882012-05-16 20:23:16 +02002339 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002340 if (!tt_global_entry)
2341 goto out;
2342
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002343 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002344 goto out;
2345
Marek Lindner5870adc2012-06-20 17:16:05 +02002346 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002347
2348out:
2349 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002350 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002351 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002352 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002353 return ret;
2354}
Marek Lindnera943cac2011-07-30 13:10:18 +02002355
Sven Eckelmann56303d32012-06-05 22:31:31 +02002356void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2357 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002358 const unsigned char *tt_buff, uint8_t tt_num_changes,
2359 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002360{
2361 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2362 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002363 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002364
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002365 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002366 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002367 return;
2368
Antonio Quartulli17071572011-11-07 16:36:40 +01002369 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002370 * increased by one -> we can apply the attached changes
2371 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002372 if ((!orig_node->tt_initialised && ttvn == 1) ||
2373 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002374 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002375 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2376 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002377 * In this case send a tt request
2378 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002379 if (!tt_num_changes) {
2380 full_table = false;
2381 goto request_table;
2382 }
2383
Sven Eckelmann96412692012-06-05 22:31:30 +02002384 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002385 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002386 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002387
2388 /* Even if we received the precomputed crc with the OGM, we
2389 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002390 * in the global table
2391 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002392 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002393
2394 /* The ttvn alone is not enough to guarantee consistency
2395 * because a single value could represent different states
2396 * (due to the wrap around). Thus a node has to check whether
2397 * the resulting table (after applying the changes) is still
2398 * consistent or not. E.g. a node could disconnect while its
2399 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2400 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002401 * inconsistency
2402 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002403 if (orig_node->tt_crc != tt_crc)
2404 goto request_table;
2405
2406 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002407 * unset the flag
2408 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002409 orig_node->tt_poss_change = false;
2410 } else {
2411 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002412 * in sync anymore -> request fresh tt data
2413 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002414 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2415 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002416request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002417 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002418 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2419 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2420 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002421 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2422 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002423 return;
2424 }
2425 }
2426}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002427
2428/* returns true whether we know that the client has moved from its old
2429 * originator to another one. This entry is kept is still kept for consistency
2430 * purposes
2431 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002432bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002433 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002434{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002435 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002436 bool ret = false;
2437
Sven Eckelmanna5130882012-05-16 20:23:16 +02002438 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002439 if (!tt_global_entry)
2440 goto out;
2441
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002442 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002443 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002444out:
2445 return ret;
2446}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002447
2448bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2449 struct batadv_orig_node *orig_node,
2450 const unsigned char *addr)
2451{
2452 bool ret = false;
2453
2454 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2455 BATADV_TT_CLIENT_TEMP,
2456 atomic_read(&orig_node->last_ttvn)))
2457 goto out;
2458
2459 batadv_dbg(BATADV_DBG_TT, bat_priv,
2460 "Added temporary global client (addr: %pM orig: %pM)\n",
2461 addr, orig_node->orig);
2462 ret = true;
2463out:
2464 return ret;
2465}