blob: c24e60425289d6ecdd0e2cf9df9ac2f1ca0fc47e [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
Sven Eckelmann56303d32012-06-05 22:31:31 +0200930static void
931batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
932 struct batadv_tt_global_entry *tt_global_entry,
933 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200934{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200935 batadv_dbg(BATADV_DBG_TT, bat_priv,
936 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200937 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200938
Sven Eckelmann807736f2012-07-15 22:26:51 +0200939 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200940 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200941 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200942
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943}
944
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200945/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200946 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
947 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200948 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200949static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200950batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
951 struct batadv_tt_global_entry *tt_global_entry,
952 struct batadv_orig_node *orig_node,
953 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200954{
955 bool last_entry = true;
956 struct hlist_head *head;
957 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200958 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200959
960 /* no local entry exists, case 1:
961 * Check if this is the last one or if other entries exist.
962 */
963
964 rcu_read_lock();
965 head = &tt_global_entry->orig_list;
966 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
967 if (orig_entry->orig_node != orig_node) {
968 last_entry = false;
969 break;
970 }
971 }
972 rcu_read_unlock();
973
974 if (last_entry) {
975 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200976 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200977 tt_global_entry->roam_at = jiffies;
978 } else
979 /* there is another entry, we can simply delete this
980 * one and can still use the other one.
981 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200982 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
983 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200984}
985
986
987
Sven Eckelmann56303d32012-06-05 22:31:31 +0200988static void batadv_tt_global_del(struct batadv_priv *bat_priv,
989 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200990 const unsigned char *addr,
991 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000992{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200993 struct batadv_tt_global_entry *tt_global_entry = NULL;
994 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995
Sven Eckelmanna5130882012-05-16 20:23:16 +0200996 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200997 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200998 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200999
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001000 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001001 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1002 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001003
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001004 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +02001005 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
1006 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001007
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001008 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001009 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001010
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001011 /* if we are deleting a global entry due to a roam
1012 * event, there are two possibilities:
1013 * 1) the client roamed from node A to node B => if there
1014 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001015 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001016 * wait for node B to claim it. In case of timeout
1017 * the entry is purged.
1018 *
1019 * If there are other originators left, we directly delete
1020 * the originator.
1021 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001022 * the global entry, since it is useless now.
1023 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001024 local_entry = batadv_tt_local_hash_find(bat_priv,
1025 tt_global_entry->common.addr);
1026 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001027 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001028 batadv_tt_global_del_orig_list(tt_global_entry);
1029 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001030 } else
1031 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001032 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1033 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001034
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001035
Antonio Quartullicc47f662011-04-27 14:27:57 +02001036out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001037 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001038 batadv_tt_global_entry_free_ref(tt_global_entry);
1039 if (local_entry)
1040 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001041}
1042
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1044 struct batadv_orig_node *orig_node,
1045 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001046{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001047 struct batadv_tt_global_entry *tt_global;
1048 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001049 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001050 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001051 struct hlist_node *node, *safe;
1052 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001053 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001054
Simon Wunderlich6e801492011-10-19 10:28:26 +02001055 if (!hash)
1056 return;
1057
Antonio Quartullia73105b2011-04-27 14:27:44 +02001058 for (i = 0; i < hash->size; i++) {
1059 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001060 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001062 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001063 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001064 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001065 tt_global = container_of(tt_common_entry,
1066 struct batadv_tt_global_entry,
1067 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001068
Sven Eckelmann56303d32012-06-05 22:31:31 +02001069 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001070 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001071
Sven Eckelmann56303d32012-06-05 22:31:31 +02001072 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001073 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001074 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001075 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001076 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001077 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001078 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001079 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001080 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001082 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083}
1084
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001085static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1086 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001087{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001088 bool purge = false;
1089 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1090 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001091
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001092 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1093 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1094 purge = true;
1095 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001096 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001097
1098 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1099 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1100 purge = true;
1101 *msg = "Temporary client timeout\n";
1102 }
1103
1104 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001105}
1106
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001107static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001108{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001109 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001110 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001111 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001112 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001113 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001114 char *msg = NULL;
1115 struct batadv_tt_common_entry *tt_common;
1116 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001117
Antonio Quartullicc47f662011-04-27 14:27:57 +02001118 for (i = 0; i < hash->size; i++) {
1119 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001120 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001121
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001122 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001123 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1124 hash_entry) {
1125 tt_global = container_of(tt_common,
1126 struct batadv_tt_global_entry,
1127 common);
1128
1129 if (!batadv_tt_global_to_purge(tt_global, &msg))
1130 continue;
1131
1132 batadv_dbg(BATADV_DBG_TT, bat_priv,
1133 "Deleting global tt entry (%pM): %s\n",
1134 tt_global->common.addr, msg);
1135
1136 hlist_del_rcu(node);
1137
1138 batadv_tt_global_entry_free_ref(tt_global);
1139 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001140 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001141 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001142}
1143
Sven Eckelmann56303d32012-06-05 22:31:31 +02001144static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001146 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001147 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001148 struct batadv_tt_common_entry *tt_common_entry;
1149 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001150 struct hlist_node *node, *node_tmp;
1151 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001152 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001153
Sven Eckelmann807736f2012-07-15 22:26:51 +02001154 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001155 return;
1156
Sven Eckelmann807736f2012-07-15 22:26:51 +02001157 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001158
1159 for (i = 0; i < hash->size; i++) {
1160 head = &hash->table[i];
1161 list_lock = &hash->list_locks[i];
1162
1163 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001164 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001165 head, hash_entry) {
1166 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001167 tt_global = container_of(tt_common_entry,
1168 struct batadv_tt_global_entry,
1169 common);
1170 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001171 }
1172 spin_unlock_bh(list_lock);
1173 }
1174
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001175 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001176
Sven Eckelmann807736f2012-07-15 22:26:51 +02001177 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178}
1179
Sven Eckelmann56303d32012-06-05 22:31:31 +02001180static bool
1181_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1182 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001183{
1184 bool ret = false;
1185
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001186 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1187 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001188 ret = true;
1189
1190 return ret;
1191}
1192
Sven Eckelmann56303d32012-06-05 22:31:31 +02001193struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1194 const uint8_t *src,
1195 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001197 struct batadv_tt_local_entry *tt_local_entry = NULL;
1198 struct batadv_tt_global_entry *tt_global_entry = NULL;
1199 struct batadv_orig_node *orig_node = NULL;
1200 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001201 struct hlist_head *head;
1202 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001203 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001204 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001205
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001206 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001207 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001208 if (!tt_local_entry)
1209 goto out;
1210 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001211
Sven Eckelmanna5130882012-05-16 20:23:16 +02001212 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001213 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001214 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001216 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001217 * isolation
1218 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001219 if (tt_local_entry &&
1220 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001221 goto out;
1222
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001223 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001224
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225 rcu_read_lock();
1226 head = &tt_global_entry->orig_list;
1227 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001228 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001229 if (!router)
1230 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001231
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001232 if (router->tq_avg > best_tq) {
1233 orig_node = orig_entry->orig_node;
1234 best_tq = router->tq_avg;
1235 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001236 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001237 }
1238 /* found anything? */
1239 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1240 orig_node = NULL;
1241 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001242out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001243 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001244 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001245 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001246 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001247
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001248 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001249}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001250
1251/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001252static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1253 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001254{
1255 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001256 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001257 struct batadv_tt_common_entry *tt_common;
1258 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001259 struct hlist_node *node;
1260 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001261 uint32_t i;
1262 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001263
1264 for (i = 0; i < hash->size; i++) {
1265 head = &hash->table[i];
1266
1267 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001268 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001269 tt_global = container_of(tt_common,
1270 struct batadv_tt_global_entry,
1271 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001272 /* Roaming clients are in the global table for
1273 * consistency only. They don't have to be
1274 * taken into account while computing the
1275 * global crc
1276 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001277 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001278 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001279 /* Temporary clients have not been announced yet, so
1280 * they have to be skipped while computing the global
1281 * crc
1282 */
1283 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1284 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001285
1286 /* find out if this global entry is announced by this
1287 * originator
1288 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001289 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001290 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001291 continue;
1292
1293 total_one = 0;
1294 for (j = 0; j < ETH_ALEN; j++)
1295 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001296 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001297 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001298 }
1299 rcu_read_unlock();
1300 }
1301
1302 return total;
1303}
1304
1305/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001306static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001307{
1308 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001309 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001310 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001311 struct hlist_node *node;
1312 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001313 uint32_t i;
1314 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001315
1316 for (i = 0; i < hash->size; i++) {
1317 head = &hash->table[i];
1318
1319 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001320 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001321 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001322 * account while computing the CRC
1323 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001324 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001325 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001326 total_one = 0;
1327 for (j = 0; j < ETH_ALEN; j++)
1328 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001329 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001330 total ^= total_one;
1331 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001332 rcu_read_unlock();
1333 }
1334
1335 return total;
1336}
1337
Sven Eckelmann56303d32012-06-05 22:31:31 +02001338static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001339{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001340 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341
Sven Eckelmann807736f2012-07-15 22:26:51 +02001342 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001343
Sven Eckelmann807736f2012-07-15 22:26:51 +02001344 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001345 list_del(&node->list);
1346 kfree(node);
1347 }
1348
Sven Eckelmann807736f2012-07-15 22:26:51 +02001349 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001350}
1351
Sven Eckelmann56303d32012-06-05 22:31:31 +02001352static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1353 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001354 const unsigned char *tt_buff,
1355 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001356{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001357 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358
1359 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001360 * last OGM (the OGM could carry no changes)
1361 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001362 spin_lock_bh(&orig_node->tt_buff_lock);
1363 if (tt_buff_len > 0) {
1364 kfree(orig_node->tt_buff);
1365 orig_node->tt_buff_len = 0;
1366 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1367 if (orig_node->tt_buff) {
1368 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1369 orig_node->tt_buff_len = tt_buff_len;
1370 }
1371 }
1372 spin_unlock_bh(&orig_node->tt_buff_lock);
1373}
1374
Sven Eckelmann56303d32012-06-05 22:31:31 +02001375static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001376{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001377 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001378
Sven Eckelmann807736f2012-07-15 22:26:51 +02001379 spin_lock_bh(&bat_priv->tt.req_list_lock);
1380 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001381 if (batadv_has_timed_out(node->issued_at,
1382 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001383 list_del(&node->list);
1384 kfree(node);
1385 }
1386 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001387 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001388}
1389
1390/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001391 * has already been issued for this orig_node, NULL otherwise
1392 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001393static struct batadv_tt_req_node *
1394batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1395 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001397 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398
Sven Eckelmann807736f2012-07-15 22:26:51 +02001399 spin_lock_bh(&bat_priv->tt.req_list_lock);
1400 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001401 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1402 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001403 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404 goto unlock;
1405 }
1406
1407 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1408 if (!tt_req_node)
1409 goto unlock;
1410
1411 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1412 tt_req_node->issued_at = jiffies;
1413
Sven Eckelmann807736f2012-07-15 22:26:51 +02001414 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001416 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001417 return tt_req_node;
1418}
1419
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001420/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001421static int batadv_tt_local_valid_entry(const void *entry_ptr,
1422 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001423{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001424 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001425
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001426 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001427 return 0;
1428 return 1;
1429}
1430
Sven Eckelmanna5130882012-05-16 20:23:16 +02001431static int batadv_tt_global_valid(const void *entry_ptr,
1432 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001433{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001434 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1435 const struct batadv_tt_global_entry *tt_global_entry;
1436 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001438 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1439 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001440 return 0;
1441
Sven Eckelmann56303d32012-06-05 22:31:31 +02001442 tt_global_entry = container_of(tt_common_entry,
1443 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001444 common);
1445
Sven Eckelmanna5130882012-05-16 20:23:16 +02001446 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001447}
1448
Sven Eckelmanna5130882012-05-16 20:23:16 +02001449static struct sk_buff *
1450batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001451 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001452 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001453 int (*valid_cb)(const void *, const void *),
1454 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001455{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001456 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001457 struct batadv_tt_query_packet *tt_response;
1458 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001459 struct hlist_node *node;
1460 struct hlist_head *head;
1461 struct sk_buff *skb = NULL;
1462 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001463 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001464 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001465 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001466
1467 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1468 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001469 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001470 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001471 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472
Sven Eckelmann96412692012-06-05 22:31:30 +02001473 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001474 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001475 if (!skb)
1476 goto out;
1477
Sven Eckelmann5b246572012-11-04 17:11:45 +01001478 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001479 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001480 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001481
Sven Eckelmann96412692012-06-05 22:31:30 +02001482 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483 tt_count = 0;
1484
1485 rcu_read_lock();
1486 for (i = 0; i < hash->size; i++) {
1487 head = &hash->table[i];
1488
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001489 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490 head, hash_entry) {
1491 if (tt_count == tt_tot)
1492 break;
1493
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001494 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495 continue;
1496
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001497 memcpy(tt_change->addr, tt_common_entry->addr,
1498 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001499 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500
1501 tt_count++;
1502 tt_change++;
1503 }
1504 }
1505 rcu_read_unlock();
1506
Antonio Quartulli9d852392011-10-17 14:25:13 +02001507 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001508 * copied
1509 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001510 tt_response->tt_data = htons(tt_count);
1511
Antonio Quartullia73105b2011-04-27 14:27:44 +02001512out:
1513 return skb;
1514}
1515
Sven Eckelmann56303d32012-06-05 22:31:31 +02001516static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1517 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001518 uint8_t ttvn, uint16_t tt_crc,
1519 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520{
1521 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001522 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001523 struct batadv_neigh_node *neigh_node = NULL;
1524 struct batadv_hard_iface *primary_if;
1525 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001526 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001527 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001529 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530 if (!primary_if)
1531 goto out;
1532
1533 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001534 * reply from the same orig_node yet
1535 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001536 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001537 if (!tt_req_node)
1538 goto out;
1539
Sven Eckelmann5b246572012-11-04 17:11:45 +01001540 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001541 if (!skb)
1542 goto out;
1543
Sven Eckelmann5b246572012-11-04 17:11:45 +01001544 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001545
Sven Eckelmann96412692012-06-05 22:31:30 +02001546 tt_req_len = sizeof(*tt_request);
1547 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001548
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001549 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001550 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001551 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1552 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001553 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001554 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001555 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001556 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001557
1558 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001559 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001560
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001561 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562 if (!neigh_node)
1563 goto out;
1564
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001565 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001566 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1567 dst_orig_node->orig, neigh_node->addr,
1568 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001570 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001571
Sven Eckelmann9455e342012-05-12 02:09:37 +02001572 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001573 ret = 0;
1574
1575out:
1576 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001577 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001578 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001579 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001580 if (ret)
1581 kfree_skb(skb);
1582 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001583 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001584 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001585 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586 kfree(tt_req_node);
1587 }
1588 return ret;
1589}
1590
Sven Eckelmann96412692012-06-05 22:31:30 +02001591static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001592batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001593 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001594{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001595 struct batadv_orig_node *req_dst_orig_node = NULL;
1596 struct batadv_orig_node *res_dst_orig_node = NULL;
1597 struct batadv_neigh_node *neigh_node = NULL;
1598 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001599 uint8_t orig_ttvn, req_ttvn, ttvn;
1600 int ret = false;
1601 unsigned char *tt_buff;
1602 bool full_table;
1603 uint16_t tt_len, tt_tot;
1604 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001605 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001606 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001607 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001608
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001609 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001610 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1611 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001612 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001613
1614 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001615 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001616 if (!req_dst_orig_node)
1617 goto out;
1618
Sven Eckelmannda641192012-05-12 13:48:56 +02001619 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620 if (!res_dst_orig_node)
1621 goto out;
1622
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001623 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624 if (!neigh_node)
1625 goto out;
1626
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001627 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001628 if (!primary_if)
1629 goto out;
1630
1631 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1632 req_ttvn = tt_request->ttvn;
1633
Antonio Quartulli015758d2011-07-09 17:52:13 +02001634 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001636 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637 goto out;
1638
Antonio Quartulli015758d2011-07-09 17:52:13 +02001639 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001640 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001641 !req_dst_orig_node->tt_buff)
1642 full_table = true;
1643 else
1644 full_table = false;
1645
1646 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001647 * I'll send only one packet with as much TT entries as I can
1648 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001649 if (!full_table) {
1650 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1651 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001652 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653
Sven Eckelmann96412692012-06-05 22:31:30 +02001654 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001655 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656 if (!skb)
1657 goto unlock;
1658
Sven Eckelmann5b246572012-11-04 17:11:45 +01001659 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001660 packet_pos = skb_put(skb, len);
1661 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001662 tt_response->ttvn = req_ttvn;
1663 tt_response->tt_data = htons(tt_tot);
1664
Sven Eckelmann96412692012-06-05 22:31:30 +02001665 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001666 /* Copy the last orig_node's OGM buffer */
1667 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1668 req_dst_orig_node->tt_buff_len);
1669
1670 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1671 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001672 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1673 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001674 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1675
Sven Eckelmanna5130882012-05-16 20:23:16 +02001676 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001677 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001678 primary_if,
1679 batadv_tt_global_valid,
1680 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001681 if (!skb)
1682 goto out;
1683
Sven Eckelmann96412692012-06-05 22:31:30 +02001684 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001685 }
1686
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001687 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001688 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001689 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001690 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1691 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001692 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001693
1694 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001695 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001697 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001698 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1699 res_dst_orig_node->orig, neigh_node->addr,
1700 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001702 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001703
Sven Eckelmann9455e342012-05-12 02:09:37 +02001704 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705 ret = true;
1706 goto out;
1707
1708unlock:
1709 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1710
1711out:
1712 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001713 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001715 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001717 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001719 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001720 if (!ret)
1721 kfree_skb(skb);
1722 return ret;
1723
1724}
Sven Eckelmann96412692012-06-05 22:31:30 +02001725
1726static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001727batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001728 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001729{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001730 struct batadv_orig_node *orig_node = NULL;
1731 struct batadv_neigh_node *neigh_node = NULL;
1732 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001733 uint8_t my_ttvn, req_ttvn, ttvn;
1734 int ret = false;
1735 unsigned char *tt_buff;
1736 bool full_table;
1737 uint16_t tt_len, tt_tot;
1738 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001739 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001740 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001741 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001742
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001743 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001744 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1745 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001746 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747
1748
Sven Eckelmann807736f2012-07-15 22:26:51 +02001749 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001750 req_ttvn = tt_request->ttvn;
1751
Sven Eckelmannda641192012-05-12 13:48:56 +02001752 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001753 if (!orig_node)
1754 goto out;
1755
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001756 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001757 if (!neigh_node)
1758 goto out;
1759
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001760 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001761 if (!primary_if)
1762 goto out;
1763
1764 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001765 * is too big send the whole local translation table
1766 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001767 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001768 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001769 full_table = true;
1770 else
1771 full_table = false;
1772
1773 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001774 * I'll send only one packet with as much TT entries as I can
1775 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001776 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001777 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1778 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001779 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001780
Sven Eckelmann96412692012-06-05 22:31:30 +02001781 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001782 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001783 if (!skb)
1784 goto unlock;
1785
Sven Eckelmann5b246572012-11-04 17:11:45 +01001786 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001787 packet_pos = skb_put(skb, len);
1788 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001789 tt_response->ttvn = req_ttvn;
1790 tt_response->tt_data = htons(tt_tot);
1791
Sven Eckelmann96412692012-06-05 22:31:30 +02001792 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001793 memcpy(tt_buff, bat_priv->tt.last_changeset,
1794 bat_priv->tt.last_changeset_len);
1795 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001796 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001797 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001798 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001799 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001800
Sven Eckelmanna5130882012-05-16 20:23:16 +02001801 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001802 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001803 primary_if,
1804 batadv_tt_local_valid_entry,
1805 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001806 if (!skb)
1807 goto out;
1808
Sven Eckelmann96412692012-06-05 22:31:30 +02001809 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001810 }
1811
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001812 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001813 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001814 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001815 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1816 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001817 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001818
1819 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001820 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001821
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001822 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001823 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1824 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001825 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001826
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001827 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001828
Sven Eckelmann9455e342012-05-12 02:09:37 +02001829 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001830 ret = true;
1831 goto out;
1832
1833unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001834 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001835out:
1836 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001837 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001838 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001839 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001841 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842 if (!ret)
1843 kfree_skb(skb);
1844 /* This packet was for me, so it doesn't need to be re-routed */
1845 return true;
1846}
1847
Sven Eckelmann56303d32012-06-05 22:31:31 +02001848bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001849 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001850{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001851 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001852 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001853 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001854 return true;
1855
Sven Eckelmanna5130882012-05-16 20:23:16 +02001856 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001857 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001858 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001859 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001860}
1861
Sven Eckelmann56303d32012-06-05 22:31:31 +02001862static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1863 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001864 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001865 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001866{
1867 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001868 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001869
1870 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001871 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1872 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001873 batadv_tt_global_del(bat_priv, orig_node,
1874 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001875 "tt removed by changes",
1876 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001877 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001878 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001879 (tt_change + i)->addr,
1880 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881 /* In case of problem while storing a
1882 * global_entry, we stop the updating
1883 * procedure without committing the
1884 * ttvn change. This will avoid to send
1885 * corrupted data on tt_request
1886 */
1887 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001888 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001889 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001890 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891}
1892
Sven Eckelmann56303d32012-06-05 22:31:31 +02001893static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001894 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001895{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001896 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001897
Sven Eckelmannda641192012-05-12 13:48:56 +02001898 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899 if (!orig_node)
1900 goto out;
1901
1902 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001903 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001904
Sven Eckelmanna5130882012-05-16 20:23:16 +02001905 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001906 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001907 ntohs(tt_response->tt_data),
1908 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909
1910 spin_lock_bh(&orig_node->tt_buff_lock);
1911 kfree(orig_node->tt_buff);
1912 orig_node->tt_buff_len = 0;
1913 orig_node->tt_buff = NULL;
1914 spin_unlock_bh(&orig_node->tt_buff_lock);
1915
1916 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1917
1918out:
1919 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001920 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001921}
1922
Sven Eckelmann56303d32012-06-05 22:31:31 +02001923static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1924 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001925 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001926 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001928 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1929 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001930
Sven Eckelmanna5130882012-05-16 20:23:16 +02001931 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1932 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933 atomic_set(&orig_node->last_ttvn, ttvn);
1934}
1935
Sven Eckelmann56303d32012-06-05 22:31:31 +02001936bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001937{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001938 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001939 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001940
Sven Eckelmanna5130882012-05-16 20:23:16 +02001941 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001942 if (!tt_local_entry)
1943 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001944 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001945 * consistency purpose)
1946 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001947 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
1948 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001949 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001950 ret = true;
1951out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001952 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001953 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001954 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955}
1956
Sven Eckelmann56303d32012-06-05 22:31:31 +02001957void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001958 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001960 struct batadv_tt_req_node *node, *safe;
1961 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001962 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001963
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001964 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001965 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1966 tt_response->src, tt_response->ttvn,
1967 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001968 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001969
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001970 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001971 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001972 goto out;
1973
Sven Eckelmannda641192012-05-12 13:48:56 +02001974 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001975 if (!orig_node)
1976 goto out;
1977
Sven Eckelmann96412692012-06-05 22:31:30 +02001978 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001979 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001980 } else {
1981 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001982 batadv_tt_update_changes(bat_priv, orig_node,
1983 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001984 tt_response->ttvn, tt_change);
1985 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986
1987 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001988 spin_lock_bh(&bat_priv->tt.req_list_lock);
1989 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001990 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001991 continue;
1992 list_del(&node->list);
1993 kfree(node);
1994 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001995 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996
1997 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001998 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001999out:
2000 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002001 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002002}
2003
Sven Eckelmann56303d32012-06-05 22:31:31 +02002004int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002005{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002006 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007
Sven Eckelmanna5130882012-05-16 20:23:16 +02002008 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002009 if (ret < 0)
2010 return ret;
2011
Sven Eckelmanna5130882012-05-16 20:23:16 +02002012 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002013 if (ret < 0)
2014 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002015
Sven Eckelmanna5130882012-05-16 20:23:16 +02002016 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002017
2018 return 1;
2019}
2020
Sven Eckelmann56303d32012-06-05 22:31:31 +02002021static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002022{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002023 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002024
Sven Eckelmann807736f2012-07-15 22:26:51 +02002025 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
Sven Eckelmann807736f2012-07-15 22:26:51 +02002027 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028 list_del(&node->list);
2029 kfree(node);
2030 }
2031
Sven Eckelmann807736f2012-07-15 22:26:51 +02002032 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002033}
2034
Sven Eckelmann56303d32012-06-05 22:31:31 +02002035static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002036{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002037 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038
Sven Eckelmann807736f2012-07-15 22:26:51 +02002039 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2040 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002041 if (!batadv_has_timed_out(node->first_time,
2042 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002043 continue;
2044
2045 list_del(&node->list);
2046 kfree(node);
2047 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002048 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002049}
2050
2051/* This function checks whether the client already reached the
2052 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2053 * will not be sent.
2054 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002055 * returns true if the ROAMING_ADV can be sent, false otherwise
2056 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002057static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002058 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002059{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002060 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002061 bool ret = false;
2062
Sven Eckelmann807736f2012-07-15 22:26:51 +02002063 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002064 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002065 * reply from the same orig_node yet
2066 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002067 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002068 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002069 continue;
2070
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002071 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002072 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002073 continue;
2074
Sven Eckelmann3e348192012-05-16 20:23:22 +02002075 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002076 /* Sorry, you roamed too many times! */
2077 goto unlock;
2078 ret = true;
2079 break;
2080 }
2081
2082 if (!ret) {
2083 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2084 if (!tt_roam_node)
2085 goto unlock;
2086
2087 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002088 atomic_set(&tt_roam_node->counter,
2089 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002090 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2091
Sven Eckelmann807736f2012-07-15 22:26:51 +02002092 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002093 ret = true;
2094 }
2095
2096unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002097 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002098 return ret;
2099}
2100
Sven Eckelmann56303d32012-06-05 22:31:31 +02002101static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2102 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002103{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002104 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002105 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002106 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002107 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002108 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002109 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002110
2111 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002112 * already roamed to us too many times
2113 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002114 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002115 goto out;
2116
Sven Eckelmann5b246572012-11-04 17:11:45 +01002117 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002118 if (!skb)
2119 goto out;
2120
Sven Eckelmann5b246572012-11-04 17:11:45 +01002121 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002122
Sven Eckelmann96412692012-06-05 22:31:30 +02002123 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002124
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002125 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002126 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002127 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002128 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002129 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002130 if (!primary_if)
2131 goto out;
2132 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002133 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002134 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2135 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2136
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002137 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002138 if (!neigh_node)
2139 goto out;
2140
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002141 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002142 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2143 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002144
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002145 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002146
Sven Eckelmann9455e342012-05-12 02:09:37 +02002147 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002148 ret = 0;
2149
2150out:
2151 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002152 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002153 if (ret)
2154 kfree_skb(skb);
2155 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002156}
2157
Sven Eckelmanna5130882012-05-16 20:23:16 +02002158static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002160 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002161 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002162 struct batadv_priv *bat_priv;
2163
2164 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002165 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2166 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167
Sven Eckelmanna5130882012-05-16 20:23:16 +02002168 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002169 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002170 batadv_tt_req_purge(bat_priv);
2171 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002172
Sven Eckelmanna5130882012-05-16 20:23:16 +02002173 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002174}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002175
Sven Eckelmann56303d32012-06-05 22:31:31 +02002176void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002177{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002178 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002179
Sven Eckelmanna5130882012-05-16 20:23:16 +02002180 batadv_tt_local_table_free(bat_priv);
2181 batadv_tt_global_table_free(bat_priv);
2182 batadv_tt_req_list_free(bat_priv);
2183 batadv_tt_changes_list_free(bat_priv);
2184 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002185
Sven Eckelmann807736f2012-07-15 22:26:51 +02002186 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002187}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002188
Antonio Quartulli697f2532011-11-07 16:47:01 +01002189/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002190 * in the given hash table and returns the number of modified entries
2191 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002192static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2193 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002194{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002195 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002196 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002197 struct hlist_head *head;
2198 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002199 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002200
2201 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002202 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002203
2204 for (i = 0; i < hash->size; i++) {
2205 head = &hash->table[i];
2206
2207 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002208 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002209 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002210 if (enable) {
2211 if ((tt_common_entry->flags & flags) == flags)
2212 continue;
2213 tt_common_entry->flags |= flags;
2214 } else {
2215 if (!(tt_common_entry->flags & flags))
2216 continue;
2217 tt_common_entry->flags &= ~flags;
2218 }
2219 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002220 }
2221 rcu_read_unlock();
2222 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002223out:
2224 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002225}
2226
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002227/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002228static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002229{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002230 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002231 struct batadv_tt_common_entry *tt_common;
2232 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002233 struct hlist_node *node, *node_tmp;
2234 struct hlist_head *head;
2235 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002236 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002237
2238 if (!hash)
2239 return;
2240
2241 for (i = 0; i < hash->size; i++) {
2242 head = &hash->table[i];
2243 list_lock = &hash->list_locks[i];
2244
2245 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002246 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2247 hash_entry) {
2248 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002249 continue;
2250
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002251 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002252 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002253 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002254
Sven Eckelmann807736f2012-07-15 22:26:51 +02002255 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002256 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002257 tt_local = container_of(tt_common,
2258 struct batadv_tt_local_entry,
2259 common);
2260 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002261 }
2262 spin_unlock_bh(list_lock);
2263 }
2264
2265}
2266
Sven Eckelmann56303d32012-06-05 22:31:31 +02002267static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268 unsigned char **packet_buff,
2269 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002270{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002271 uint16_t changed_num = 0;
2272
Sven Eckelmann807736f2012-07-15 22:26:51 +02002273 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002274 return -ENOENT;
2275
Sven Eckelmann807736f2012-07-15 22:26:51 +02002276 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002277 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002278
2279 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002280 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002281 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002282 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002283
2284 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002285 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002286 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002287 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002288 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002289
2290 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002291 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002292
Sven Eckelmanna5130882012-05-16 20:23:16 +02002293 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2294 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002295}
2296
2297/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002298int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002299 unsigned char **packet_buff, int *packet_buff_len,
2300 int packet_min_len)
2301{
2302 int tt_num_changes;
2303
2304 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002305 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2306 packet_buff_len,
2307 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002308
2309 /* if the changes have been sent often enough */
2310 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002311 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002312 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2313 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002314 tt_num_changes = 0;
2315 }
2316
2317 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002318}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002319
Sven Eckelmann56303d32012-06-05 22:31:31 +02002320bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002321 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002322{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002323 struct batadv_tt_local_entry *tt_local_entry = NULL;
2324 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002325 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002326
2327 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002328 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002329
Sven Eckelmanna5130882012-05-16 20:23:16 +02002330 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002331 if (!tt_local_entry)
2332 goto out;
2333
Sven Eckelmanna5130882012-05-16 20:23:16 +02002334 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002335 if (!tt_global_entry)
2336 goto out;
2337
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002338 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002339 goto out;
2340
Marek Lindner5870adc2012-06-20 17:16:05 +02002341 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002342
2343out:
2344 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002345 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002346 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002347 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002348 return ret;
2349}
Marek Lindnera943cac2011-07-30 13:10:18 +02002350
Sven Eckelmann56303d32012-06-05 22:31:31 +02002351void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2352 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002353 const unsigned char *tt_buff, uint8_t tt_num_changes,
2354 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002355{
2356 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2357 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002358 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002359
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002360 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002361 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002362 return;
2363
Antonio Quartulli17071572011-11-07 16:36:40 +01002364 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002365 * increased by one -> we can apply the attached changes
2366 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002367 if ((!orig_node->tt_initialised && ttvn == 1) ||
2368 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002369 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002370 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2371 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002372 * In this case send a tt request
2373 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002374 if (!tt_num_changes) {
2375 full_table = false;
2376 goto request_table;
2377 }
2378
Sven Eckelmann96412692012-06-05 22:31:30 +02002379 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002380 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002381 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002382
2383 /* Even if we received the precomputed crc with the OGM, we
2384 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002385 * in the global table
2386 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002387 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002388
2389 /* The ttvn alone is not enough to guarantee consistency
2390 * because a single value could represent different states
2391 * (due to the wrap around). Thus a node has to check whether
2392 * the resulting table (after applying the changes) is still
2393 * consistent or not. E.g. a node could disconnect while its
2394 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2395 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002396 * inconsistency
2397 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002398 if (orig_node->tt_crc != tt_crc)
2399 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002400 } else {
2401 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002402 * in sync anymore -> request fresh tt data
2403 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002404 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2405 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002406request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002407 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002408 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2409 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2410 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002411 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2412 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002413 return;
2414 }
2415 }
2416}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002417
2418/* returns true whether we know that the client has moved from its old
2419 * originator to another one. This entry is kept is still kept for consistency
2420 * purposes
2421 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002422bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002423 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002424{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002425 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002426 bool ret = false;
2427
Sven Eckelmanna5130882012-05-16 20:23:16 +02002428 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002429 if (!tt_global_entry)
2430 goto out;
2431
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002432 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002433 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002434out:
2435 return ret;
2436}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002437
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002438/**
2439 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2440 * @bat_priv: the bat priv with all the soft interface information
2441 * @addr: the MAC address of the local client to query
2442 *
2443 * Returns true if the local client is known to be roaming (it is not served by
2444 * this node anymore) or not. If yes, the client is still present in the table
2445 * to keep the latter consistent with the node TTVN
2446 */
2447bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2448 uint8_t *addr)
2449{
2450 struct batadv_tt_local_entry *tt_local_entry;
2451 bool ret = false;
2452
2453 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2454 if (!tt_local_entry)
2455 goto out;
2456
2457 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2458 batadv_tt_local_entry_free_ref(tt_local_entry);
2459out:
2460 return ret;
2461
2462}
2463
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002464bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2465 struct batadv_orig_node *orig_node,
2466 const unsigned char *addr)
2467{
2468 bool ret = false;
2469
2470 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2471 BATADV_TT_CLIENT_TEMP,
2472 atomic_read(&orig_node->last_ttvn)))
2473 goto out;
2474
2475 batadv_dbg(BATADV_DBG_TT, bat_priv,
2476 "Added temporary global client (addr: %pM orig: %pM)\n",
2477 addr, orig_node->orig);
2478 ret = true;
2479out:
2480 return ret;
2481}