blob: cb8433aceed8e7bcb0b7c1f28b6450d39e7b2edd [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);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200245 struct batadv_tt_local_entry *tt_local = NULL;
246 struct batadv_tt_global_entry *tt_global = 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
Antonio Quartulli47c94652012-09-23 22:38:35 +0200252 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartulli47c94652012-09-23 22:38:35 +0200254 if (tt_local) {
255 tt_local->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200257 tt_local->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
Antonio Quartulli47c94652012-09-23 22:38:35 +0200261 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
262 if (!tt_local)
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 Quartulli47c94652012-09-23 22:38:35 +0200269 memcpy(tt_local->common.addr, addr, ETH_ALEN);
270 tt_local->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200271 if (batadv_is_wifi_iface(ifindex))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200272 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
273 atomic_set(&tt_local->common.refcount, 2);
274 tt_local->last_seen = jiffies;
275 tt_local->common.added_at = tt_local->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))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200279 tt_local->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 */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200285 tt_local->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,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200288 batadv_choose_orig, &tt_local->common,
289 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100290
291 if (unlikely(hash_added != 0)) {
292 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200293 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100294 goto out;
295 }
296
Antonio Quartulli47c94652012-09-23 22:38:35 +0200297 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200298
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299 /* remove address from global hash if present */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200300 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301
Antonio Quartullicc47f662011-04-27 14:27:57 +0200302 /* Check whether it is a roaming! */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200303 if (tt_global) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200304 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200305 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200306 rcu_read_lock();
307 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200308 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200309 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200310 }
311 rcu_read_unlock();
312 /* The global entry has to be marked as ROAMING and
313 * has to be kept for consistency purpose
314 */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200315 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
316 tt_global->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200317 }
318out:
Antonio Quartulli47c94652012-09-23 22:38:35 +0200319 if (tt_local)
320 batadv_tt_local_entry_free_ref(tt_local);
321 if (tt_global)
322 batadv_tt_global_entry_free_ref(tt_global);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323}
324
Sven Eckelmanna5130882012-05-16 20:23:16 +0200325static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
326 int *packet_buff_len,
327 int min_packet_len,
328 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800330 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800332 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
333
334 /* keep old buffer if kmalloc should fail */
335 if (new_buff) {
336 memcpy(new_buff, *packet_buff, min_packet_len);
337 kfree(*packet_buff);
338 *packet_buff = new_buff;
339 *packet_buff_len = new_packet_len;
340 }
341}
342
Sven Eckelmann56303d32012-06-05 22:31:31 +0200343static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200344 unsigned char **packet_buff,
345 int *packet_buff_len,
346 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800347{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200348 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800349 int req_len;
350
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200351 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800352
353 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200354 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800355
356 /* if we have too many changes for one packet don't send any
357 * and wait for the tt table request which will be fragmented
358 */
359 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
360 req_len = min_packet_len;
361
Sven Eckelmanna5130882012-05-16 20:23:16 +0200362 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
363 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800364
365 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200366 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800367}
368
Sven Eckelmann56303d32012-06-05 22:31:31 +0200369static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200370 unsigned char **packet_buff,
371 int *packet_buff_len,
372 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800373{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200374 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800375 int count = 0, tot_changes = 0, new_len;
376 unsigned char *tt_buff;
377
Sven Eckelmanna5130882012-05-16 20:23:16 +0200378 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
379 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800380
381 new_len = *packet_buff_len - min_packet_len;
382 tt_buff = *packet_buff + min_packet_len;
383
384 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200385 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Sven Eckelmann807736f2012-07-15 22:26:51 +0200387 spin_lock_bh(&bat_priv->tt.changes_list_lock);
388 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmann807736f2012-07-15 22:26:51 +0200390 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100391 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200392 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200393 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200394 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 count++;
396 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397 list_del(&entry->list);
398 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200400 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Antonio Quartullia73105b2011-04-27 14:27:44 +0200402 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200403 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
404 kfree(bat_priv->tt.last_changeset);
405 bat_priv->tt.last_changeset_len = 0;
406 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800407 /* check whether this new OGM has no changes due to size problems */
408 if (new_len > 0) {
409 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200410 * instead of providing the diff
411 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200412 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
413 if (bat_priv->tt.last_changeset) {
414 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
415 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200416 }
417 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200418 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419
Marek Lindner08ad76e2012-04-23 16:32:55 +0800420 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421}
422
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200423int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424{
425 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200426 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200427 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200428 struct batadv_tt_common_entry *tt_common_entry;
429 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000430 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200432 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433
Marek Lindner30da63a2012-08-03 17:15:46 +0200434 primary_if = batadv_seq_print_text_primary_if_get(seq);
435 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200436 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100438 seq_printf(seq,
439 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200440 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 for (i = 0; i < hash->size; i++) {
443 head = &hash->table[i];
444
Marek Lindner7aadf882011-02-18 12:28:09 +0000445 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100446 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000447 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200448 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100449 tt_common_entry->addr,
450 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200451 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100452 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200453 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100454 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200455 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100456 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200457 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100458 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200459 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000461 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200463out:
464 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200465 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200466 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467}
468
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469static void
470batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
471 struct batadv_tt_local_entry *tt_local_entry,
472 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200474 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
475 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Antonio Quartulli015758d2011-07-09 17:52:13 +0200477 /* The local client has to be marked as "pending to be removed" but has
478 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200479 * response issued before the net ttvn increment (consistency check)
480 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200481 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100482
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200483 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200484 "Local tt entry (%pM) pending to be removed: %s\n",
485 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486}
487
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200488/**
489 * batadv_tt_local_remove - logically remove an entry from the local table
490 * @bat_priv: the bat priv with all the soft interface information
491 * @addr: the MAC address of the client to remove
492 * @message: message to append to the log on deletion
493 * @roaming: true if the deletion is due to a roaming event
494 *
495 * Returns the flags assigned to the local entry before being deleted
496 */
497uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
498 const uint8_t *addr, const char *message,
499 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200501 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200502 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Sven Eckelmanna5130882012-05-16 20:23:16 +0200504 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200505 if (!tt_local_entry)
506 goto out;
507
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200508 curr_flags = tt_local_entry->common.flags;
509
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200510 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200511 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200513 /* mark the local client as ROAMed */
514 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
515 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200516
517 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200518
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200519out:
520 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200521 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200522
523 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524}
525
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200527 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200529 struct batadv_tt_local_entry *tt_local_entry;
530 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000531 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200532
533 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
534 hash_entry) {
535 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200536 struct batadv_tt_local_entry,
537 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200538 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
539 continue;
540
541 /* entry already marked for deletion */
542 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
543 continue;
544
545 if (!batadv_has_timed_out(tt_local_entry->last_seen,
546 BATADV_TT_LOCAL_TIMEOUT))
547 continue;
548
549 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
550 BATADV_TT_CLIENT_DEL, "timed out");
551 }
552}
553
Sven Eckelmann56303d32012-06-05 22:31:31 +0200554static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200555{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200556 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200558 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200559 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 for (i = 0; i < hash->size; i++) {
562 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200563 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200565 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200566 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200567 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 }
569
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570}
571
Sven Eckelmann56303d32012-06-05 22:31:31 +0200572static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200574 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200575 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200576 struct batadv_tt_common_entry *tt_common_entry;
577 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200578 struct hlist_node *node, *node_tmp;
579 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200580 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200581
Sven Eckelmann807736f2012-07-15 22:26:51 +0200582 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 return;
584
Sven Eckelmann807736f2012-07-15 22:26:51 +0200585 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200586
587 for (i = 0; i < hash->size; i++) {
588 head = &hash->table[i];
589 list_lock = &hash->list_locks[i];
590
591 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100592 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200593 head, hash_entry) {
594 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200595 tt_local = container_of(tt_common_entry,
596 struct batadv_tt_local_entry,
597 common);
598 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200599 }
600 spin_unlock_bh(list_lock);
601 }
602
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200603 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200604
Sven Eckelmann807736f2012-07-15 22:26:51 +0200605 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606}
607
Sven Eckelmann56303d32012-06-05 22:31:31 +0200608static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200610 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200611 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
Sven Eckelmann807736f2012-07-15 22:26:51 +0200613 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614
Sven Eckelmann807736f2012-07-15 22:26:51 +0200615 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200616 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Sven Eckelmann5346c352012-05-05 13:27:28 +0200618 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619}
620
Sven Eckelmann56303d32012-06-05 22:31:31 +0200621static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200623 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624
Sven Eckelmann807736f2012-07-15 22:26:51 +0200625 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200626
Sven Eckelmann807736f2012-07-15 22:26:51 +0200627 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628 list) {
629 list_del(&entry->list);
630 kfree(entry);
631 }
632
Sven Eckelmann807736f2012-07-15 22:26:51 +0200633 atomic_set(&bat_priv->tt.local_changes, 0);
634 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200635}
636
Antonio Quartullid657e622012-07-01 14:09:12 +0200637/* retrieves the orig_tt_list_entry belonging to orig_node from the
638 * batadv_tt_global_entry list
639 *
640 * returns it with an increased refcounter, NULL if not found
641 */
642static struct batadv_tt_orig_list_entry *
643batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
644 const struct batadv_orig_node *orig_node)
645{
646 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
647 const struct hlist_head *head;
648 struct hlist_node *node;
649
650 rcu_read_lock();
651 head = &entry->orig_list;
652 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
653 if (tmp_orig_entry->orig_node != orig_node)
654 continue;
655 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
656 continue;
657
658 orig_entry = tmp_orig_entry;
659 break;
660 }
661 rcu_read_unlock();
662
663 return orig_entry;
664}
665
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200666/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200667 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200668 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200669static bool
670batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
671 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200672{
Antonio Quartullid657e622012-07-01 14:09:12 +0200673 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200674 bool found = false;
675
Antonio Quartullid657e622012-07-01 14:09:12 +0200676 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
677 if (orig_entry) {
678 found = true;
679 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200680 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200681
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200682 return found;
683}
684
Sven Eckelmanna5130882012-05-16 20:23:16 +0200685static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200686batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200688{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200690
Antonio Quartullid657e622012-07-01 14:09:12 +0200691 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200692 if (orig_entry) {
693 /* refresh the ttvn: the current value could be a bogus one that
694 * was added during a "temporary client detection"
695 */
696 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200697 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200698 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200699
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200700 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
701 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200702 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200703
704 INIT_HLIST_NODE(&orig_entry->list);
705 atomic_inc(&orig_node->refcount);
706 atomic_inc(&orig_node->tt_size);
707 orig_entry->orig_node = orig_node;
708 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200709 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200710
Antonio Quartullid657e622012-07-01 14:09:12 +0200711 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200712 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200713 &tt_global->orig_list);
714 spin_unlock_bh(&tt_global->list_lock);
715out:
716 if (orig_entry)
717 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200718}
719
Antonio Quartullia73105b2011-04-27 14:27:44 +0200720/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200721int batadv_tt_global_add(struct batadv_priv *bat_priv,
722 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200723 const unsigned char *tt_addr, uint8_t flags,
724 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200726 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200727 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100728 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200729 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200730 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731
Sven Eckelmanna5130882012-05-16 20:23:16 +0200732 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733
Antonio Quartullia73105b2011-04-27 14:27:44 +0200734 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200735 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200737 goto out;
738
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200739 common = &tt_global_entry->common;
740 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200741
Antonio Quartullid4f44692012-05-25 00:00:54 +0200742 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200743 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200744 /* node must store current time in case of roaming. This is
745 * needed to purge this entry out on timeout (if nobody claims
746 * it)
747 */
748 if (flags & BATADV_TT_CLIENT_ROAM)
749 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200750 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200751 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200752
753 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
754 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200755
Sven Eckelmann807736f2012-07-15 22:26:51 +0200756 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200757 batadv_compare_tt,
758 batadv_choose_orig, common,
759 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100760
761 if (unlikely(hash_added != 0)) {
762 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200763 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100764 goto out_remove;
765 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200766 } else {
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200767 /* If there is already a global entry, we can use this one for
768 * our processing.
769 * But if we are trying to add a temporary client we can exit
770 * directly because the temporary information should never
771 * override any already known client state (whatever it is)
772 */
773 if (flags & BATADV_TT_CLIENT_TEMP)
774 goto out;
775
776 /* if the client was temporary added before receiving the first
777 * OGM announcing it, we have to clear the TEMP flag
778 */
779 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200780
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200781 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
782 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200783 * delete + roaming change for this originator.
784 *
785 * We should first delete the old originator before adding the
786 * new one.
787 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200788 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200789 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200790 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200791 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000792 }
793 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200794 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200795 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200796
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200797 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200798 "Creating new global tt entry: %pM (via %pM)\n",
799 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200800 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200801
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100802out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200803
Antonio Quartullia73105b2011-04-27 14:27:44 +0200804 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200805 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
806 "global tt received",
807 flags & BATADV_TT_CLIENT_ROAM);
808 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
809
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200810out:
811 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200812 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200813 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814}
815
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200816/* print all orig nodes who announce the address for this global entry.
817 * it is assumed that the caller holds rcu_read_lock();
818 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200819static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200820batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200821 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200822{
823 struct hlist_head *head;
824 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200825 struct batadv_tt_orig_list_entry *orig_entry;
826 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200827 uint16_t flags;
828 uint8_t last_ttvn;
829
830 tt_common_entry = &tt_global_entry->common;
831
832 head = &tt_global_entry->orig_list;
833
834 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
835 flags = tt_common_entry->flags;
836 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200837 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200838 tt_global_entry->common.addr, orig_entry->ttvn,
839 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200840 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200841 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
842 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200843 }
844}
845
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200846int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000847{
848 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200849 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200850 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200851 struct batadv_tt_common_entry *tt_common_entry;
852 struct batadv_tt_global_entry *tt_global;
853 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000854 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200856 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857
Marek Lindner30da63a2012-08-03 17:15:46 +0200858 primary_if = batadv_seq_print_text_primary_if_get(seq);
859 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200860 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200862 seq_printf(seq,
863 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200865 seq_printf(seq, " %-13s %s %-15s %s %s\n",
866 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868 for (i = 0; i < hash->size; i++) {
869 head = &hash->table[i];
870
Marek Lindner7aadf882011-02-18 12:28:09 +0000871 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100872 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000873 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200874 tt_global = container_of(tt_common_entry,
875 struct batadv_tt_global_entry,
876 common);
877 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000879 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200881out:
882 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200883 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200884 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885}
886
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200887/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200888static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200889batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200891 struct hlist_head *head;
892 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200893 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200895 spin_lock_bh(&tt_global_entry->list_lock);
896 head = &tt_global_entry->orig_list;
897 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
898 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200899 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200900 }
901 spin_unlock_bh(&tt_global_entry->list_lock);
902
903}
904
Sven Eckelmanna5130882012-05-16 20:23:16 +0200905static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200906batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
907 struct batadv_tt_global_entry *tt_global_entry,
908 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200909 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200910{
911 struct hlist_head *head;
912 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200913 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200914
915 spin_lock_bh(&tt_global_entry->list_lock);
916 head = &tt_global_entry->orig_list;
917 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
918 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200919 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200920 "Deleting %pM from global tt entry %pM: %s\n",
921 orig_node->orig,
922 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200923 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200924 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200925 }
926 }
927 spin_unlock_bh(&tt_global_entry->list_lock);
928}
929
Antonio Quartullibe73b482012-09-23 22:38:36 +0200930static void batadv_tt_global_free(struct batadv_priv *bat_priv,
931 struct batadv_tt_global_entry *tt_global,
932 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200933{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200934 batadv_dbg(BATADV_DBG_TT, bat_priv,
935 "Deleting global tt entry %pM: %s\n",
Antonio Quartullibe73b482012-09-23 22:38:36 +0200936 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200937
Sven Eckelmann807736f2012-07-15 22:26:51 +0200938 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullibe73b482012-09-23 22:38:36 +0200939 batadv_choose_orig, tt_global->common.addr);
940 batadv_tt_global_entry_free_ref(tt_global);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200941
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942}
943
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200944/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200945 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
946 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200947 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200948static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200949batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
950 struct batadv_tt_global_entry *tt_global_entry,
951 struct batadv_orig_node *orig_node,
952 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200953{
954 bool last_entry = true;
955 struct hlist_head *head;
956 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200957 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200958
959 /* no local entry exists, case 1:
960 * Check if this is the last one or if other entries exist.
961 */
962
963 rcu_read_lock();
964 head = &tt_global_entry->orig_list;
965 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
966 if (orig_entry->orig_node != orig_node) {
967 last_entry = false;
968 break;
969 }
970 }
971 rcu_read_unlock();
972
973 if (last_entry) {
974 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200975 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200976 tt_global_entry->roam_at = jiffies;
977 } else
978 /* there is another entry, we can simply delete this
979 * one and can still use the other one.
980 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200981 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
982 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200983}
984
985
986
Sven Eckelmann56303d32012-06-05 22:31:31 +0200987static void batadv_tt_global_del(struct batadv_priv *bat_priv,
988 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200989 const unsigned char *addr,
990 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000991{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200992 struct batadv_tt_global_entry *tt_global_entry = NULL;
993 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994
Sven Eckelmanna5130882012-05-16 20:23:16 +0200995 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200996 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200997 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200998
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200999 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001000 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1001 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001002
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001003 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001004 batadv_tt_global_free(bat_priv, tt_global_entry,
1005 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001006
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001007 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001008 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001009
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001010 /* if we are deleting a global entry due to a roam
1011 * event, there are two possibilities:
1012 * 1) the client roamed from node A to node B => if there
1013 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001014 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001015 * wait for node B to claim it. In case of timeout
1016 * the entry is purged.
1017 *
1018 * If there are other originators left, we directly delete
1019 * the originator.
1020 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001021 * the global entry, since it is useless now.
1022 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001023 local_entry = batadv_tt_local_hash_find(bat_priv,
1024 tt_global_entry->common.addr);
1025 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001026 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001027 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001028 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001029 } else
1030 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001031 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1032 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001033
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001034
Antonio Quartullicc47f662011-04-27 14:27:57 +02001035out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001036 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001037 batadv_tt_global_entry_free_ref(tt_global_entry);
1038 if (local_entry)
1039 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040}
1041
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1043 struct batadv_orig_node *orig_node,
1044 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001045{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001046 struct batadv_tt_global_entry *tt_global;
1047 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001048 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001049 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001050 struct hlist_node *node, *safe;
1051 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001052 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001053
Simon Wunderlich6e801492011-10-19 10:28:26 +02001054 if (!hash)
1055 return;
1056
Antonio Quartullia73105b2011-04-27 14:27:44 +02001057 for (i = 0; i < hash->size; i++) {
1058 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001059 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001061 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001062 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001063 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001064 tt_global = container_of(tt_common_entry,
1065 struct batadv_tt_global_entry,
1066 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001067
Sven Eckelmann56303d32012-06-05 22:31:31 +02001068 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001069 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070
Sven Eckelmann56303d32012-06-05 22:31:31 +02001071 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001072 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001073 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001074 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001075 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001076 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001077 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001078 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001079 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001080 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001081 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082}
1083
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001084static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1085 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001086{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001087 bool purge = false;
1088 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1089 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001090
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001091 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1092 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1093 purge = true;
1094 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001095 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001096
1097 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1098 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1099 purge = true;
1100 *msg = "Temporary client timeout\n";
1101 }
1102
1103 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001104}
1105
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001106static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001107{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001108 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001109 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001110 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001111 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001112 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001113 char *msg = NULL;
1114 struct batadv_tt_common_entry *tt_common;
1115 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001116
Antonio Quartullicc47f662011-04-27 14:27:57 +02001117 for (i = 0; i < hash->size; i++) {
1118 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001119 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001120
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001121 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001122 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1123 hash_entry) {
1124 tt_global = container_of(tt_common,
1125 struct batadv_tt_global_entry,
1126 common);
1127
1128 if (!batadv_tt_global_to_purge(tt_global, &msg))
1129 continue;
1130
1131 batadv_dbg(BATADV_DBG_TT, bat_priv,
1132 "Deleting global tt entry (%pM): %s\n",
1133 tt_global->common.addr, msg);
1134
1135 hlist_del_rcu(node);
1136
1137 batadv_tt_global_entry_free_ref(tt_global);
1138 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001139 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001140 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001141}
1142
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001145 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001146 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001147 struct batadv_tt_common_entry *tt_common_entry;
1148 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001149 struct hlist_node *node, *node_tmp;
1150 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001151 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001152
Sven Eckelmann807736f2012-07-15 22:26:51 +02001153 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001154 return;
1155
Sven Eckelmann807736f2012-07-15 22:26:51 +02001156 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001157
1158 for (i = 0; i < hash->size; i++) {
1159 head = &hash->table[i];
1160 list_lock = &hash->list_locks[i];
1161
1162 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001163 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001164 head, hash_entry) {
1165 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001166 tt_global = container_of(tt_common_entry,
1167 struct batadv_tt_global_entry,
1168 common);
1169 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001170 }
1171 spin_unlock_bh(list_lock);
1172 }
1173
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001174 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001175
Sven Eckelmann807736f2012-07-15 22:26:51 +02001176 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001177}
1178
Sven Eckelmann56303d32012-06-05 22:31:31 +02001179static bool
1180_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1181 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001182{
1183 bool ret = false;
1184
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001185 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1186 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001187 ret = true;
1188
1189 return ret;
1190}
1191
Sven Eckelmann56303d32012-06-05 22:31:31 +02001192struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1193 const uint8_t *src,
1194 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001195{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001196 struct batadv_tt_local_entry *tt_local_entry = NULL;
1197 struct batadv_tt_global_entry *tt_global_entry = NULL;
1198 struct batadv_orig_node *orig_node = NULL;
1199 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001200 struct hlist_head *head;
1201 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001202 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001203 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001204
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001205 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001206 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001207 if (!tt_local_entry)
1208 goto out;
1209 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001210
Sven Eckelmanna5130882012-05-16 20:23:16 +02001211 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001212 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001213 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001214
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001215 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001216 * isolation
1217 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001218 if (tt_local_entry &&
1219 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001220 goto out;
1221
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001222 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001223
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001224 rcu_read_lock();
1225 head = &tt_global_entry->orig_list;
1226 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001227 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001228 if (!router)
1229 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001230
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001231 if (router->tq_avg > best_tq) {
1232 orig_node = orig_entry->orig_node;
1233 best_tq = router->tq_avg;
1234 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001235 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001236 }
1237 /* found anything? */
1238 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1239 orig_node = NULL;
1240 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001241out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001242 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001243 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001244 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001245 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001246
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001247 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001248}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001249
1250/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001251static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1252 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001253{
1254 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001255 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001256 struct batadv_tt_common_entry *tt_common;
1257 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001258 struct hlist_node *node;
1259 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001260 uint32_t i;
1261 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001262
1263 for (i = 0; i < hash->size; i++) {
1264 head = &hash->table[i];
1265
1266 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001267 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001268 tt_global = container_of(tt_common,
1269 struct batadv_tt_global_entry,
1270 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001271 /* Roaming clients are in the global table for
1272 * consistency only. They don't have to be
1273 * taken into account while computing the
1274 * global crc
1275 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001276 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001278 /* Temporary clients have not been announced yet, so
1279 * they have to be skipped while computing the global
1280 * crc
1281 */
1282 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1283 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001284
1285 /* find out if this global entry is announced by this
1286 * originator
1287 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001288 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001289 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001290 continue;
1291
1292 total_one = 0;
1293 for (j = 0; j < ETH_ALEN; j++)
1294 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001295 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001296 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001297 }
1298 rcu_read_unlock();
1299 }
1300
1301 return total;
1302}
1303
1304/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001305static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001306{
1307 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001308 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001309 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001310 struct hlist_node *node;
1311 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001312 uint32_t i;
1313 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001314
1315 for (i = 0; i < hash->size; i++) {
1316 head = &hash->table[i];
1317
1318 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001319 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001320 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001321 * account while computing the CRC
1322 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001323 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001324 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001325 total_one = 0;
1326 for (j = 0; j < ETH_ALEN; j++)
1327 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001328 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001329 total ^= total_one;
1330 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001331 rcu_read_unlock();
1332 }
1333
1334 return total;
1335}
1336
Sven Eckelmann56303d32012-06-05 22:31:31 +02001337static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001338{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001339 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340
Sven Eckelmann807736f2012-07-15 22:26:51 +02001341 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001342
Sven Eckelmann807736f2012-07-15 22:26:51 +02001343 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001344 list_del(&node->list);
1345 kfree(node);
1346 }
1347
Sven Eckelmann807736f2012-07-15 22:26:51 +02001348 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001349}
1350
Sven Eckelmann56303d32012-06-05 22:31:31 +02001351static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1352 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001353 const unsigned char *tt_buff,
1354 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001355{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001356 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001357
1358 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001359 * last OGM (the OGM could carry no changes)
1360 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001361 spin_lock_bh(&orig_node->tt_buff_lock);
1362 if (tt_buff_len > 0) {
1363 kfree(orig_node->tt_buff);
1364 orig_node->tt_buff_len = 0;
1365 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1366 if (orig_node->tt_buff) {
1367 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1368 orig_node->tt_buff_len = tt_buff_len;
1369 }
1370 }
1371 spin_unlock_bh(&orig_node->tt_buff_lock);
1372}
1373
Sven Eckelmann56303d32012-06-05 22:31:31 +02001374static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001375{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001376 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001377
Sven Eckelmann807736f2012-07-15 22:26:51 +02001378 spin_lock_bh(&bat_priv->tt.req_list_lock);
1379 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001380 if (batadv_has_timed_out(node->issued_at,
1381 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001382 list_del(&node->list);
1383 kfree(node);
1384 }
1385 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001386 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001387}
1388
1389/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001390 * has already been issued for this orig_node, NULL otherwise
1391 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001392static struct batadv_tt_req_node *
1393batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1394 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001395{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001396 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397
Sven Eckelmann807736f2012-07-15 22:26:51 +02001398 spin_lock_bh(&bat_priv->tt.req_list_lock);
1399 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001400 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1401 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001402 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001403 goto unlock;
1404 }
1405
1406 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1407 if (!tt_req_node)
1408 goto unlock;
1409
1410 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1411 tt_req_node->issued_at = jiffies;
1412
Sven Eckelmann807736f2012-07-15 22:26:51 +02001413 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001414unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001415 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001416 return tt_req_node;
1417}
1418
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001419/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001420static int batadv_tt_local_valid_entry(const void *entry_ptr,
1421 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001422{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001423 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001424
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001425 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001426 return 0;
1427 return 1;
1428}
1429
Sven Eckelmanna5130882012-05-16 20:23:16 +02001430static int batadv_tt_global_valid(const void *entry_ptr,
1431 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001432{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001433 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1434 const struct batadv_tt_global_entry *tt_global_entry;
1435 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001436
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001437 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1438 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001439 return 0;
1440
Sven Eckelmann56303d32012-06-05 22:31:31 +02001441 tt_global_entry = container_of(tt_common_entry,
1442 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001443 common);
1444
Sven Eckelmanna5130882012-05-16 20:23:16 +02001445 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001446}
1447
Sven Eckelmanna5130882012-05-16 20:23:16 +02001448static struct sk_buff *
1449batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001450 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001451 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001452 int (*valid_cb)(const void *, const void *),
1453 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001455 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001456 struct batadv_tt_query_packet *tt_response;
1457 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001458 struct hlist_node *node;
1459 struct hlist_head *head;
1460 struct sk_buff *skb = NULL;
1461 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001462 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001463 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001464 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001465
1466 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1467 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001468 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001469 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001470 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001471
Sven Eckelmann96412692012-06-05 22:31:30 +02001472 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001473 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 if (!skb)
1475 goto out;
1476
Sven Eckelmann5b246572012-11-04 17:11:45 +01001477 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001478 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001479 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480
Sven Eckelmann96412692012-06-05 22:31:30 +02001481 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482 tt_count = 0;
1483
1484 rcu_read_lock();
1485 for (i = 0; i < hash->size; i++) {
1486 head = &hash->table[i];
1487
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001488 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001489 head, hash_entry) {
1490 if (tt_count == tt_tot)
1491 break;
1492
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001493 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001494 continue;
1495
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001496 memcpy(tt_change->addr, tt_common_entry->addr,
1497 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001498 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499
1500 tt_count++;
1501 tt_change++;
1502 }
1503 }
1504 rcu_read_unlock();
1505
Antonio Quartulli9d852392011-10-17 14:25:13 +02001506 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001507 * copied
1508 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001509 tt_response->tt_data = htons(tt_count);
1510
Antonio Quartullia73105b2011-04-27 14:27:44 +02001511out:
1512 return skb;
1513}
1514
Sven Eckelmann56303d32012-06-05 22:31:31 +02001515static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1516 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001517 uint8_t ttvn, uint16_t tt_crc,
1518 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519{
1520 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001521 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001522 struct batadv_neigh_node *neigh_node = NULL;
1523 struct batadv_hard_iface *primary_if;
1524 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001525 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001526 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001528 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001529 if (!primary_if)
1530 goto out;
1531
1532 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001533 * reply from the same orig_node yet
1534 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001535 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001536 if (!tt_req_node)
1537 goto out;
1538
Sven Eckelmann5b246572012-11-04 17:11:45 +01001539 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001540 if (!skb)
1541 goto out;
1542
Sven Eckelmann5b246572012-11-04 17:11:45 +01001543 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001544
Sven Eckelmann96412692012-06-05 22:31:30 +02001545 tt_req_len = sizeof(*tt_request);
1546 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001547
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001548 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001549 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001550 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1551 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001552 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001554 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001555 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556
1557 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001558 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001560 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561 if (!neigh_node)
1562 goto out;
1563
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001564 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001565 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1566 dst_orig_node->orig, neigh_node->addr,
1567 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001568
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001569 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001570
Sven Eckelmann9455e342012-05-12 02:09:37 +02001571 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001572 ret = 0;
1573
1574out:
1575 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001576 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001577 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001578 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001579 if (ret)
1580 kfree_skb(skb);
1581 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001582 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001583 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001584 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001585 kfree(tt_req_node);
1586 }
1587 return ret;
1588}
1589
Sven Eckelmann96412692012-06-05 22:31:30 +02001590static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001591batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001592 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001593{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001594 struct batadv_orig_node *req_dst_orig_node = NULL;
1595 struct batadv_orig_node *res_dst_orig_node = NULL;
1596 struct batadv_neigh_node *neigh_node = NULL;
1597 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001598 uint8_t orig_ttvn, req_ttvn, ttvn;
1599 int ret = false;
1600 unsigned char *tt_buff;
1601 bool full_table;
1602 uint16_t tt_len, tt_tot;
1603 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001604 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001605 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001606 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001607
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001608 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001609 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1610 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001611 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001612
1613 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001614 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615 if (!req_dst_orig_node)
1616 goto out;
1617
Sven Eckelmannda641192012-05-12 13:48:56 +02001618 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619 if (!res_dst_orig_node)
1620 goto out;
1621
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001622 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623 if (!neigh_node)
1624 goto out;
1625
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001626 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001627 if (!primary_if)
1628 goto out;
1629
1630 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1631 req_ttvn = tt_request->ttvn;
1632
Antonio Quartulli015758d2011-07-09 17:52:13 +02001633 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001634 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001635 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001636 goto out;
1637
Antonio Quartulli015758d2011-07-09 17:52:13 +02001638 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001639 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001640 !req_dst_orig_node->tt_buff)
1641 full_table = true;
1642 else
1643 full_table = false;
1644
1645 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001646 * I'll send only one packet with as much TT entries as I can
1647 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648 if (!full_table) {
1649 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1650 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001651 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001652
Sven Eckelmann96412692012-06-05 22:31:30 +02001653 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001654 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001655 if (!skb)
1656 goto unlock;
1657
Sven Eckelmann5b246572012-11-04 17:11:45 +01001658 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001659 packet_pos = skb_put(skb, len);
1660 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001661 tt_response->ttvn = req_ttvn;
1662 tt_response->tt_data = htons(tt_tot);
1663
Sven Eckelmann96412692012-06-05 22:31:30 +02001664 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001665 /* Copy the last orig_node's OGM buffer */
1666 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1667 req_dst_orig_node->tt_buff_len);
1668
1669 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1670 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001671 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1672 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001673 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1674
Sven Eckelmanna5130882012-05-16 20:23:16 +02001675 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001676 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001677 primary_if,
1678 batadv_tt_global_valid,
1679 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001680 if (!skb)
1681 goto out;
1682
Sven Eckelmann96412692012-06-05 22:31:30 +02001683 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001684 }
1685
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001686 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001687 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001688 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001689 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1690 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001691 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692
1693 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001694 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001695
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001696 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001697 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1698 res_dst_orig_node->orig, neigh_node->addr,
1699 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001701 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001702
Sven Eckelmann9455e342012-05-12 02:09:37 +02001703 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001704 ret = true;
1705 goto out;
1706
1707unlock:
1708 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1709
1710out:
1711 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001712 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001713 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001714 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001716 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001717 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001718 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719 if (!ret)
1720 kfree_skb(skb);
1721 return ret;
1722
1723}
Sven Eckelmann96412692012-06-05 22:31:30 +02001724
1725static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001726batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001727 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729 struct batadv_orig_node *orig_node = NULL;
1730 struct batadv_neigh_node *neigh_node = NULL;
1731 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001732 uint8_t my_ttvn, req_ttvn, ttvn;
1733 int ret = false;
1734 unsigned char *tt_buff;
1735 bool full_table;
1736 uint16_t tt_len, tt_tot;
1737 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001738 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001739 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001740 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001741
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001742 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001743 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1744 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001745 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746
1747
Sven Eckelmann807736f2012-07-15 22:26:51 +02001748 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001749 req_ttvn = tt_request->ttvn;
1750
Sven Eckelmannda641192012-05-12 13:48:56 +02001751 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001752 if (!orig_node)
1753 goto out;
1754
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001755 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001756 if (!neigh_node)
1757 goto out;
1758
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001759 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001760 if (!primary_if)
1761 goto out;
1762
1763 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001764 * is too big send the whole local translation table
1765 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001766 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001767 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001768 full_table = true;
1769 else
1770 full_table = false;
1771
1772 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001773 * I'll send only one packet with as much TT entries as I can
1774 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001775 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001776 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1777 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001778 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001779
Sven Eckelmann96412692012-06-05 22:31:30 +02001780 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001781 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782 if (!skb)
1783 goto unlock;
1784
Sven Eckelmann5b246572012-11-04 17:11:45 +01001785 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001786 packet_pos = skb_put(skb, len);
1787 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788 tt_response->ttvn = req_ttvn;
1789 tt_response->tt_data = htons(tt_tot);
1790
Sven Eckelmann96412692012-06-05 22:31:30 +02001791 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001792 memcpy(tt_buff, bat_priv->tt.last_changeset,
1793 bat_priv->tt.last_changeset_len);
1794 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001795 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001796 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001797 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001798 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001799
Sven Eckelmanna5130882012-05-16 20:23:16 +02001800 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001801 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001802 primary_if,
1803 batadv_tt_local_valid_entry,
1804 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001805 if (!skb)
1806 goto out;
1807
Sven Eckelmann96412692012-06-05 22:31:30 +02001808 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001809 }
1810
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001811 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001812 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001813 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001814 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1815 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001816 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001817
1818 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001819 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001821 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001822 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1823 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001824 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001826 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001827
Sven Eckelmann9455e342012-05-12 02:09:37 +02001828 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001829 ret = true;
1830 goto out;
1831
1832unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001833 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001834out:
1835 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001836 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001837 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001838 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001839 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001840 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001841 if (!ret)
1842 kfree_skb(skb);
1843 /* This packet was for me, so it doesn't need to be re-routed */
1844 return true;
1845}
1846
Sven Eckelmann56303d32012-06-05 22:31:31 +02001847bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001848 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001849{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001850 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001851 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001852 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001853 return true;
1854
Sven Eckelmanna5130882012-05-16 20:23:16 +02001855 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001856 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001857 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001858 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001859}
1860
Sven Eckelmann56303d32012-06-05 22:31:31 +02001861static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1862 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001863 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001864 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001865{
1866 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001867 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868
1869 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001870 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1871 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001872 batadv_tt_global_del(bat_priv, orig_node,
1873 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001874 "tt removed by changes",
1875 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001876 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001877 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001878 (tt_change + i)->addr,
1879 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001880 /* In case of problem while storing a
1881 * global_entry, we stop the updating
1882 * procedure without committing the
1883 * ttvn change. This will avoid to send
1884 * corrupted data on tt_request
1885 */
1886 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001887 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001888 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001889 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001890}
1891
Sven Eckelmann56303d32012-06-05 22:31:31 +02001892static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001893 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001894{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001895 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001896
Sven Eckelmannda641192012-05-12 13:48:56 +02001897 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898 if (!orig_node)
1899 goto out;
1900
1901 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001902 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903
Sven Eckelmanna5130882012-05-16 20:23:16 +02001904 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001905 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001906 ntohs(tt_response->tt_data),
1907 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001908
1909 spin_lock_bh(&orig_node->tt_buff_lock);
1910 kfree(orig_node->tt_buff);
1911 orig_node->tt_buff_len = 0;
1912 orig_node->tt_buff = NULL;
1913 spin_unlock_bh(&orig_node->tt_buff_lock);
1914
1915 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1916
1917out:
1918 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001919 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001920}
1921
Sven Eckelmann56303d32012-06-05 22:31:31 +02001922static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1923 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001924 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001925 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001926{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001927 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1928 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001929
Sven Eckelmanna5130882012-05-16 20:23:16 +02001930 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1931 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 atomic_set(&orig_node->last_ttvn, ttvn);
1933}
1934
Sven Eckelmann56303d32012-06-05 22:31:31 +02001935bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001936{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001937 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001938 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939
Sven Eckelmanna5130882012-05-16 20:23:16 +02001940 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001941 if (!tt_local_entry)
1942 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001943 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001944 * consistency purpose)
1945 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001946 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
1947 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001948 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001949 ret = true;
1950out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001951 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001952 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001953 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001954}
1955
Sven Eckelmann56303d32012-06-05 22:31:31 +02001956void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001957 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001958{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001959 struct batadv_tt_req_node *node, *safe;
1960 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001961 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001963 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001964 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1965 tt_response->src, tt_response->ttvn,
1966 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001967 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001968
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001969 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001970 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001971 goto out;
1972
Sven Eckelmannda641192012-05-12 13:48:56 +02001973 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001974 if (!orig_node)
1975 goto out;
1976
Sven Eckelmann96412692012-06-05 22:31:30 +02001977 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001978 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001979 } else {
1980 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001981 batadv_tt_update_changes(bat_priv, orig_node,
1982 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001983 tt_response->ttvn, tt_change);
1984 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001985
1986 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001987 spin_lock_bh(&bat_priv->tt.req_list_lock);
1988 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001989 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001990 continue;
1991 list_del(&node->list);
1992 kfree(node);
1993 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001994 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001995
1996 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001997 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998out:
1999 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002000 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001}
2002
Sven Eckelmann56303d32012-06-05 22:31:31 +02002003int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002004{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002005 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002006
Sven Eckelmanna5130882012-05-16 20:23:16 +02002007 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002008 if (ret < 0)
2009 return ret;
2010
Sven Eckelmanna5130882012-05-16 20:23:16 +02002011 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002012 if (ret < 0)
2013 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002014
Sven Eckelmanna5130882012-05-16 20:23:16 +02002015 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002016
2017 return 1;
2018}
2019
Sven Eckelmann56303d32012-06-05 22:31:31 +02002020static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002021{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002022 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023
Sven Eckelmann807736f2012-07-15 22:26:51 +02002024 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002025
Sven Eckelmann807736f2012-07-15 22:26:51 +02002026 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002027 list_del(&node->list);
2028 kfree(node);
2029 }
2030
Sven Eckelmann807736f2012-07-15 22:26:51 +02002031 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002032}
2033
Sven Eckelmann56303d32012-06-05 22:31:31 +02002034static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002035{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002036 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002037
Sven Eckelmann807736f2012-07-15 22:26:51 +02002038 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2039 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002040 if (!batadv_has_timed_out(node->first_time,
2041 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002042 continue;
2043
2044 list_del(&node->list);
2045 kfree(node);
2046 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002047 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002048}
2049
2050/* This function checks whether the client already reached the
2051 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2052 * will not be sent.
2053 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002054 * returns true if the ROAMING_ADV can be sent, false otherwise
2055 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002056static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002057 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002058{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002059 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002060 bool ret = false;
2061
Sven Eckelmann807736f2012-07-15 22:26:51 +02002062 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002063 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002064 * reply from the same orig_node yet
2065 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002066 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002067 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002068 continue;
2069
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002070 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002071 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002072 continue;
2073
Sven Eckelmann3e348192012-05-16 20:23:22 +02002074 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002075 /* Sorry, you roamed too many times! */
2076 goto unlock;
2077 ret = true;
2078 break;
2079 }
2080
2081 if (!ret) {
2082 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2083 if (!tt_roam_node)
2084 goto unlock;
2085
2086 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002087 atomic_set(&tt_roam_node->counter,
2088 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002089 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2090
Sven Eckelmann807736f2012-07-15 22:26:51 +02002091 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002092 ret = true;
2093 }
2094
2095unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002096 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002097 return ret;
2098}
2099
Sven Eckelmann56303d32012-06-05 22:31:31 +02002100static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2101 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002102{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002103 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002104 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002105 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002106 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002107 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002108 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002109
2110 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002111 * already roamed to us too many times
2112 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002113 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002114 goto out;
2115
Sven Eckelmann5b246572012-11-04 17:11:45 +01002116 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002117 if (!skb)
2118 goto out;
2119
Sven Eckelmann5b246572012-11-04 17:11:45 +01002120 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002121
Sven Eckelmann96412692012-06-05 22:31:30 +02002122 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002123
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002124 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002125 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002126 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002127 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002128 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002129 if (!primary_if)
2130 goto out;
2131 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002132 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002133 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2134 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2135
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002136 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002137 if (!neigh_node)
2138 goto out;
2139
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002140 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002141 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2142 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002143
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002144 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002145
Sven Eckelmann9455e342012-05-12 02:09:37 +02002146 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002147 ret = 0;
2148
2149out:
2150 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002151 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002152 if (ret)
2153 kfree_skb(skb);
2154 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002155}
2156
Sven Eckelmanna5130882012-05-16 20:23:16 +02002157static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002158{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002159 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002160 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002161 struct batadv_priv *bat_priv;
2162
2163 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002164 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2165 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166
Sven Eckelmanna5130882012-05-16 20:23:16 +02002167 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002168 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002169 batadv_tt_req_purge(bat_priv);
2170 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002171
Sven Eckelmanna5130882012-05-16 20:23:16 +02002172 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002173}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002174
Sven Eckelmann56303d32012-06-05 22:31:31 +02002175void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002176{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002177 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002178
Sven Eckelmanna5130882012-05-16 20:23:16 +02002179 batadv_tt_local_table_free(bat_priv);
2180 batadv_tt_global_table_free(bat_priv);
2181 batadv_tt_req_list_free(bat_priv);
2182 batadv_tt_changes_list_free(bat_priv);
2183 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002184
Sven Eckelmann807736f2012-07-15 22:26:51 +02002185 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002186}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002187
Antonio Quartulli697f2532011-11-07 16:47:01 +01002188/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002189 * in the given hash table and returns the number of modified entries
2190 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002191static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2192 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002193{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002194 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002195 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002196 struct hlist_head *head;
2197 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002198 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002199
2200 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002201 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002202
2203 for (i = 0; i < hash->size; i++) {
2204 head = &hash->table[i];
2205
2206 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002207 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002208 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002209 if (enable) {
2210 if ((tt_common_entry->flags & flags) == flags)
2211 continue;
2212 tt_common_entry->flags |= flags;
2213 } else {
2214 if (!(tt_common_entry->flags & flags))
2215 continue;
2216 tt_common_entry->flags &= ~flags;
2217 }
2218 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002219 }
2220 rcu_read_unlock();
2221 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002222out:
2223 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002224}
2225
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002226/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002227static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002228{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002229 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002230 struct batadv_tt_common_entry *tt_common;
2231 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002232 struct hlist_node *node, *node_tmp;
2233 struct hlist_head *head;
2234 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002235 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002236
2237 if (!hash)
2238 return;
2239
2240 for (i = 0; i < hash->size; i++) {
2241 head = &hash->table[i];
2242 list_lock = &hash->list_locks[i];
2243
2244 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002245 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2246 hash_entry) {
2247 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002248 continue;
2249
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002250 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002251 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002252 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002253
Sven Eckelmann807736f2012-07-15 22:26:51 +02002254 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002255 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002256 tt_local = container_of(tt_common,
2257 struct batadv_tt_local_entry,
2258 common);
2259 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002260 }
2261 spin_unlock_bh(list_lock);
2262 }
2263
2264}
2265
Sven Eckelmann56303d32012-06-05 22:31:31 +02002266static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002267 unsigned char **packet_buff,
2268 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002269{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002270 uint16_t changed_num = 0;
2271
Sven Eckelmann807736f2012-07-15 22:26:51 +02002272 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002273 return -ENOENT;
2274
Sven Eckelmann807736f2012-07-15 22:26:51 +02002275 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002276 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002277
2278 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002279 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002280 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002281 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002282
2283 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002284 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002285 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002286 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002288
2289 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002290 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002291
Sven Eckelmanna5130882012-05-16 20:23:16 +02002292 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2293 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002294}
2295
2296/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002297int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002298 unsigned char **packet_buff, int *packet_buff_len,
2299 int packet_min_len)
2300{
2301 int tt_num_changes;
2302
2303 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002304 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2305 packet_buff_len,
2306 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002307
2308 /* if the changes have been sent often enough */
2309 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002310 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002311 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2312 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002313 tt_num_changes = 0;
2314 }
2315
2316 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002317}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002318
Sven Eckelmann56303d32012-06-05 22:31:31 +02002319bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002320 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002321{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002322 struct batadv_tt_local_entry *tt_local_entry = NULL;
2323 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002324 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002325
2326 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002327 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002328
Sven Eckelmanna5130882012-05-16 20:23:16 +02002329 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002330 if (!tt_local_entry)
2331 goto out;
2332
Sven Eckelmanna5130882012-05-16 20:23:16 +02002333 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002334 if (!tt_global_entry)
2335 goto out;
2336
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002337 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002338 goto out;
2339
Marek Lindner5870adc2012-06-20 17:16:05 +02002340 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002341
2342out:
2343 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002344 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002345 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002346 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002347 return ret;
2348}
Marek Lindnera943cac2011-07-30 13:10:18 +02002349
Sven Eckelmann56303d32012-06-05 22:31:31 +02002350void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2351 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002352 const unsigned char *tt_buff, uint8_t tt_num_changes,
2353 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002354{
2355 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2356 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002357 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002358
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002359 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002360 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002361 return;
2362
Antonio Quartulli17071572011-11-07 16:36:40 +01002363 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002364 * increased by one -> we can apply the attached changes
2365 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002366 if ((!orig_node->tt_initialised && ttvn == 1) ||
2367 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002368 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002369 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2370 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002371 * In this case send a tt request
2372 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002373 if (!tt_num_changes) {
2374 full_table = false;
2375 goto request_table;
2376 }
2377
Sven Eckelmann96412692012-06-05 22:31:30 +02002378 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002379 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002380 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002381
2382 /* Even if we received the precomputed crc with the OGM, we
2383 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002384 * in the global table
2385 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002386 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002387
2388 /* The ttvn alone is not enough to guarantee consistency
2389 * because a single value could represent different states
2390 * (due to the wrap around). Thus a node has to check whether
2391 * the resulting table (after applying the changes) is still
2392 * consistent or not. E.g. a node could disconnect while its
2393 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2394 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002395 * inconsistency
2396 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002397 if (orig_node->tt_crc != tt_crc)
2398 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002399 } else {
2400 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002401 * in sync anymore -> request fresh tt data
2402 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002403 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2404 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002405request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002406 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002407 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2408 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2409 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002410 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2411 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002412 return;
2413 }
2414 }
2415}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002416
2417/* returns true whether we know that the client has moved from its old
2418 * originator to another one. This entry is kept is still kept for consistency
2419 * purposes
2420 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002421bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002422 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002423{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002424 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002425 bool ret = false;
2426
Sven Eckelmanna5130882012-05-16 20:23:16 +02002427 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002428 if (!tt_global_entry)
2429 goto out;
2430
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002431 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002432 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002433out:
2434 return ret;
2435}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002436
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002437/**
2438 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2439 * @bat_priv: the bat priv with all the soft interface information
2440 * @addr: the MAC address of the local client to query
2441 *
2442 * Returns true if the local client is known to be roaming (it is not served by
2443 * this node anymore) or not. If yes, the client is still present in the table
2444 * to keep the latter consistent with the node TTVN
2445 */
2446bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2447 uint8_t *addr)
2448{
2449 struct batadv_tt_local_entry *tt_local_entry;
2450 bool ret = false;
2451
2452 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2453 if (!tt_local_entry)
2454 goto out;
2455
2456 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2457 batadv_tt_local_entry_free_ref(tt_local_entry);
2458out:
2459 return ret;
2460
2461}
2462
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002463bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2464 struct batadv_orig_node *orig_node,
2465 const unsigned char *addr)
2466{
2467 bool ret = false;
2468
2469 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2470 BATADV_TT_CLIENT_TEMP,
2471 atomic_read(&orig_node->last_ttvn)))
2472 goto out;
2473
2474 batadv_dbg(BATADV_DBG_TT, bat_priv,
2475 "Added temporary global client (addr: %pM orig: %pM)\n",
2476 addr, orig_node->orig);
2477 ret = true;
2478out:
2479 return ret;
2480}