blob: d7418511448da0be7a13a6554edbb4484f931d80 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
Sven Eckelmann56303d32012-06-05 22:31:31 +020032static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020034static void batadv_tt_purge(struct work_struct *work);
35static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020036batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020037static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041
Marek Lindner7aadf882011-02-18 12:28:09 +000042/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020043static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000044{
Sven Eckelmann56303d32012-06-05 22:31:31 +020045 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020046 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000047
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49}
50
Sven Eckelmann56303d32012-06-05 22:31:31 +020051static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052{
Sven Eckelmann807736f2012-07-15 22:26:51 +020053 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020055 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020059batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000060{
Marek Lindner7aadf882011-02-18 12:28:09 +000061 struct hlist_head *head;
62 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020063 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020065 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000066
67 if (!hash)
68 return NULL;
69
Sven Eckelmannda641192012-05-12 13:48:56 +020070 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000071 head = &hash->table[index];
72
73 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020075 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000076 continue;
77
Antonio Quartulli48100ba2011-10-30 12:17:33 +010078 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020079 continue;
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000082 break;
83 }
84 rcu_read_unlock();
85
Antonio Quartulli48100ba2011-10-30 12:17:33 +010086 return tt_common_entry_tmp;
87}
88
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_tt_local_entry *
90batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091{
Sven Eckelmann56303d32012-06-05 22:31:31 +020092 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010094
Sven Eckelmann807736f2012-07-15 22:26:51 +020095 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 struct batadv_tt_local_entry,
99 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101}
102
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103static struct batadv_tt_global_entry *
104batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000105{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Sven Eckelmann807736f2012-07-15 22:26:51 +0200109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_tt_global_entry,
113 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000115
Marek Lindner7aadf882011-02-18 12:28:09 +0000116}
117
Sven Eckelmanna5130882012-05-16 20:23:16 +0200118static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200119batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200120{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200123}
124
Sven Eckelmanna5130882012-05-16 20:23:16 +0200125static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200133
Simon Wunderlich531027f2011-10-19 11:02:25 +0200134 kfree(tt_global_entry);
135}
136
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200139{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200141 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 }
145}
146
Sven Eckelmanna5130882012-05-16 20:23:16 +0200147static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200152 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200167 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182
183 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000206 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 event_removed = true;
208 goto unlock;
209 }
210
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213
214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200217 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200218 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200219 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200223int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224{
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200226}
227
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Sven Eckelmann807736f2012-07-15 22:26:51 +0200233 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann807736f2012-07-15 22:26:51 +0200235 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239}
240
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200241void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
242 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 struct batadv_tt_local_entry *tt_local_entry = NULL;
246 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200247 struct hlist_head *head;
248 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200249 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100250 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Sven Eckelmanna5130882012-05-16 20:23:16 +0200252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200254 if (tt_local_entry) {
255 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200258 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 }
260
Sven Eckelmann704509b2011-05-14 23:14:54 +0200261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200262 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200263 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200265 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200267 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200270 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200271 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100273 atomic_set(&tt_local_entry->common.refcount, 2);
274 tt_local_entry->last_seen = jiffies;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200275 tt_local_entry->common.added_at = tt_local_entry->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
277 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200278 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200283 * (consistency check)
284 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100286
Sven Eckelmann807736f2012-07-15 22:26:51 +0200287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200288 batadv_choose_orig,
289 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200290 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100291
292 if (unlikely(hash_added != 0)) {
293 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200294 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100295 goto out;
296 }
297
Sven Eckelmanna5130882012-05-16 20:23:16 +0200298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200299
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartullicc47f662011-04-27 14:27:57 +0200303 /* Check whether it is a roaming! */
304 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200305 /* These node are probably going to update their tt table */
306 head = &tt_global_entry->orig_list;
307 rcu_read_lock();
308 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200309 batadv_send_roam_adv(bat_priv,
310 tt_global_entry->common.addr,
311 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200312 }
313 rcu_read_unlock();
314 /* The global entry has to be marked as ROAMING and
315 * has to be kept for consistency purpose
316 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200317 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100318 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200319 }
320out:
321 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200322 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200323 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200324 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325}
326
Sven Eckelmanna5130882012-05-16 20:23:16 +0200327static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
328 int *packet_buff_len,
329 int min_packet_len,
330 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800332 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800334 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
335
336 /* keep old buffer if kmalloc should fail */
337 if (new_buff) {
338 memcpy(new_buff, *packet_buff, min_packet_len);
339 kfree(*packet_buff);
340 *packet_buff = new_buff;
341 *packet_buff_len = new_packet_len;
342 }
343}
344
Sven Eckelmann56303d32012-06-05 22:31:31 +0200345static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200346 unsigned char **packet_buff,
347 int *packet_buff_len,
348 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800349{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200350 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800351 int req_len;
352
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200353 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800354
355 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200356 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800357
358 /* if we have too many changes for one packet don't send any
359 * and wait for the tt table request which will be fragmented
360 */
361 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
362 req_len = min_packet_len;
363
Sven Eckelmanna5130882012-05-16 20:23:16 +0200364 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
365 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800366
367 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200368 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800369}
370
Sven Eckelmann56303d32012-06-05 22:31:31 +0200371static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200372 unsigned char **packet_buff,
373 int *packet_buff_len,
374 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800375{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200376 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800377 int count = 0, tot_changes = 0, new_len;
378 unsigned char *tt_buff;
379
Sven Eckelmanna5130882012-05-16 20:23:16 +0200380 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
381 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800382
383 new_len = *packet_buff_len - min_packet_len;
384 tt_buff = *packet_buff + min_packet_len;
385
386 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200387 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Sven Eckelmann807736f2012-07-15 22:26:51 +0200389 spin_lock_bh(&bat_priv->tt.changes_list_lock);
390 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
Sven Eckelmann807736f2012-07-15 22:26:51 +0200392 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100393 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200394 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200395 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200396 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 count++;
398 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200399 list_del(&entry->list);
400 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200402 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403
Antonio Quartullia73105b2011-04-27 14:27:44 +0200404 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200405 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
406 kfree(bat_priv->tt.last_changeset);
407 bat_priv->tt.last_changeset_len = 0;
408 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800409 /* check whether this new OGM has no changes due to size problems */
410 if (new_len > 0) {
411 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200412 * instead of providing the diff
413 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200414 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
415 if (bat_priv->tt.last_changeset) {
416 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
417 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200418 }
419 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200420 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
Marek Lindner08ad76e2012-04-23 16:32:55 +0800422 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423}
424
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200425int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426{
427 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200428 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430 struct batadv_tt_common_entry *tt_common_entry;
431 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000432 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200434 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Marek Lindner30da63a2012-08-03 17:15:46 +0200436 primary_if = batadv_seq_print_text_primary_if_get(seq);
437 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200438 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100440 seq_printf(seq,
441 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200442 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 for (i = 0; i < hash->size; i++) {
445 head = &hash->table[i];
446
Marek Lindner7aadf882011-02-18 12:28:09 +0000447 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100448 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000449 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200450 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100451 tt_common_entry->addr,
452 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200453 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100454 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200455 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100456 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200457 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100458 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200459 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100460 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200461 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000463 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200465out:
466 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200467 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200468 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469}
470
Sven Eckelmann56303d32012-06-05 22:31:31 +0200471static void
472batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
473 struct batadv_tt_local_entry *tt_local_entry,
474 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200476 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
477 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Antonio Quartulli015758d2011-07-09 17:52:13 +0200479 /* The local client has to be marked as "pending to be removed" but has
480 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200481 * response issued before the net ttvn increment (consistency check)
482 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200483 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100484
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200485 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200486 "Local tt entry (%pM) pending to be removed: %s\n",
487 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488}
489
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200490/**
491 * batadv_tt_local_remove - logically remove an entry from the local table
492 * @bat_priv: the bat priv with all the soft interface information
493 * @addr: the MAC address of the client to remove
494 * @message: message to append to the log on deletion
495 * @roaming: true if the deletion is due to a roaming event
496 *
497 * Returns the flags assigned to the local entry before being deleted
498 */
499uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
500 const uint8_t *addr, const char *message,
501 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200503 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200504 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505
Sven Eckelmanna5130882012-05-16 20:23:16 +0200506 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200507 if (!tt_local_entry)
508 goto out;
509
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200510 curr_flags = tt_local_entry->common.flags;
511
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200512 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200513 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200514 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200515 /* mark the local client as ROAMed */
516 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
517 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200518
519 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200520
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200521out:
522 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200523 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200524
525 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526}
527
Sven Eckelmann56303d32012-06-05 22:31:31 +0200528static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200529 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531 struct batadv_tt_local_entry *tt_local_entry;
532 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000533 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200534
535 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
536 hash_entry) {
537 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200538 struct batadv_tt_local_entry,
539 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200540 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
541 continue;
542
543 /* entry already marked for deletion */
544 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
545 continue;
546
547 if (!batadv_has_timed_out(tt_local_entry->last_seen,
548 BATADV_TT_LOCAL_TIMEOUT))
549 continue;
550
551 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
552 BATADV_TT_CLIENT_DEL, "timed out");
553 }
554}
555
Sven Eckelmann56303d32012-06-05 22:31:31 +0200556static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200557{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200558 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200560 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200561 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 for (i = 0; i < hash->size; i++) {
564 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200565 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200567 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200568 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200569 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570 }
571
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572}
573
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200576 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200577 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578 struct batadv_tt_common_entry *tt_common_entry;
579 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200580 struct hlist_node *node, *node_tmp;
581 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200582 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200583
Sven Eckelmann807736f2012-07-15 22:26:51 +0200584 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 return;
586
Sven Eckelmann807736f2012-07-15 22:26:51 +0200587 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200588
589 for (i = 0; i < hash->size; i++) {
590 head = &hash->table[i];
591 list_lock = &hash->list_locks[i];
592
593 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100594 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200595 head, hash_entry) {
596 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200597 tt_local = container_of(tt_common_entry,
598 struct batadv_tt_local_entry,
599 common);
600 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200601 }
602 spin_unlock_bh(list_lock);
603 }
604
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200605 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200606
Sven Eckelmann807736f2012-07-15 22:26:51 +0200607 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608}
609
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200612 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200613 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614
Sven Eckelmann807736f2012-07-15 22:26:51 +0200615 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000616
Sven Eckelmann807736f2012-07-15 22:26:51 +0200617 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200618 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619
Sven Eckelmann5346c352012-05-05 13:27:28 +0200620 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621}
622
Sven Eckelmann56303d32012-06-05 22:31:31 +0200623static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200624{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200625 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200626
Sven Eckelmann807736f2012-07-15 22:26:51 +0200627 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628
Sven Eckelmann807736f2012-07-15 22:26:51 +0200629 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200630 list) {
631 list_del(&entry->list);
632 kfree(entry);
633 }
634
Sven Eckelmann807736f2012-07-15 22:26:51 +0200635 atomic_set(&bat_priv->tt.local_changes, 0);
636 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200637}
638
Antonio Quartullid657e622012-07-01 14:09:12 +0200639/* retrieves the orig_tt_list_entry belonging to orig_node from the
640 * batadv_tt_global_entry list
641 *
642 * returns it with an increased refcounter, NULL if not found
643 */
644static struct batadv_tt_orig_list_entry *
645batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
646 const struct batadv_orig_node *orig_node)
647{
648 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
649 const struct hlist_head *head;
650 struct hlist_node *node;
651
652 rcu_read_lock();
653 head = &entry->orig_list;
654 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
655 if (tmp_orig_entry->orig_node != orig_node)
656 continue;
657 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
658 continue;
659
660 orig_entry = tmp_orig_entry;
661 break;
662 }
663 rcu_read_unlock();
664
665 return orig_entry;
666}
667
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200668/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200669 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200670 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200671static bool
672batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
673 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200674{
Antonio Quartullid657e622012-07-01 14:09:12 +0200675 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200676 bool found = false;
677
Antonio Quartullid657e622012-07-01 14:09:12 +0200678 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
679 if (orig_entry) {
680 found = true;
681 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200682 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200683
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200684 return found;
685}
686
Sven Eckelmanna5130882012-05-16 20:23:16 +0200687static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200688batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200690{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200691 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200692
Antonio Quartullid657e622012-07-01 14:09:12 +0200693 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200694 if (orig_entry) {
695 /* refresh the ttvn: the current value could be a bogus one that
696 * was added during a "temporary client detection"
697 */
698 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200699 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200700 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200701
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200702 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
703 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200704 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200705
706 INIT_HLIST_NODE(&orig_entry->list);
707 atomic_inc(&orig_node->refcount);
708 atomic_inc(&orig_node->tt_size);
709 orig_entry->orig_node = orig_node;
710 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200711 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200712
Antonio Quartullid657e622012-07-01 14:09:12 +0200713 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200714 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200715 &tt_global->orig_list);
716 spin_unlock_bh(&tt_global->list_lock);
717out:
718 if (orig_entry)
719 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200720}
721
Antonio Quartullia73105b2011-04-27 14:27:44 +0200722/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200723int batadv_tt_global_add(struct batadv_priv *bat_priv,
724 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200725 const unsigned char *tt_addr, uint8_t flags,
726 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200728 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200729 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100730 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200731 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200732 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733
Sven Eckelmanna5130882012-05-16 20:23:16 +0200734 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200737 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200738 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200739 goto out;
740
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200741 common = &tt_global_entry->common;
742 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200743
Antonio Quartullid4f44692012-05-25 00:00:54 +0200744 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200745 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200746 /* node must store current time in case of roaming. This is
747 * needed to purge this entry out on timeout (if nobody claims
748 * it)
749 */
750 if (flags & BATADV_TT_CLIENT_ROAM)
751 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200752 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200753 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200754
755 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
756 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200757
Sven Eckelmann807736f2012-07-15 22:26:51 +0200758 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200759 batadv_compare_tt,
760 batadv_choose_orig, common,
761 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100762
763 if (unlikely(hash_added != 0)) {
764 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200765 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100766 goto out_remove;
767 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200768 } else {
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200769 /* If there is already a global entry, we can use this one for
770 * our processing.
771 * But if we are trying to add a temporary client we can exit
772 * directly because the temporary information should never
773 * override any already known client state (whatever it is)
774 */
775 if (flags & BATADV_TT_CLIENT_TEMP)
776 goto out;
777
778 /* if the client was temporary added before receiving the first
779 * OGM announcing it, we have to clear the TEMP flag
780 */
781 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200782
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200783 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
784 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200785 * delete + roaming change for this originator.
786 *
787 * We should first delete the old originator before adding the
788 * new one.
789 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200790 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200791 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200792 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200793 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 }
795 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200796 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200797 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200798
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200799 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200800 "Creating new global tt entry: %pM (via %pM)\n",
801 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200802 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200803
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100804out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200805
Antonio Quartullia73105b2011-04-27 14:27:44 +0200806 /* remove address from local hash if present */
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200807 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
808 "global tt received",
809 flags & BATADV_TT_CLIENT_ROAM);
810 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
811
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200812out:
813 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200814 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200815 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816}
817
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200818/* print all orig nodes who announce the address for this global entry.
819 * it is assumed that the caller holds rcu_read_lock();
820 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200821static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200822batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200823 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200824{
825 struct hlist_head *head;
826 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200827 struct batadv_tt_orig_list_entry *orig_entry;
828 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200829 uint16_t flags;
830 uint8_t last_ttvn;
831
832 tt_common_entry = &tt_global_entry->common;
833
834 head = &tt_global_entry->orig_list;
835
836 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
837 flags = tt_common_entry->flags;
838 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200839 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200840 tt_global_entry->common.addr, orig_entry->ttvn,
841 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200842 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200843 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
844 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200845 }
846}
847
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200848int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849{
850 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200851 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200852 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200853 struct batadv_tt_common_entry *tt_common_entry;
854 struct batadv_tt_global_entry *tt_global;
855 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000856 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200858 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
Marek Lindner30da63a2012-08-03 17:15:46 +0200860 primary_if = batadv_seq_print_text_primary_if_get(seq);
861 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200862 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200864 seq_printf(seq,
865 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200867 seq_printf(seq, " %-13s %s %-15s %s %s\n",
868 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000870 for (i = 0; i < hash->size; i++) {
871 head = &hash->table[i];
872
Marek Lindner7aadf882011-02-18 12:28:09 +0000873 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100874 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000875 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200876 tt_global = container_of(tt_common_entry,
877 struct batadv_tt_global_entry,
878 common);
879 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000881 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200883out:
884 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200885 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200886 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887}
888
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200889/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200890static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200891batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200893 struct hlist_head *head;
894 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200895 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200896
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200897 spin_lock_bh(&tt_global_entry->list_lock);
898 head = &tt_global_entry->orig_list;
899 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
900 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200901 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200902 }
903 spin_unlock_bh(&tt_global_entry->list_lock);
904
905}
906
Sven Eckelmanna5130882012-05-16 20:23:16 +0200907static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200908batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
909 struct batadv_tt_global_entry *tt_global_entry,
910 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200911 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200912{
913 struct hlist_head *head;
914 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200915 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200916
917 spin_lock_bh(&tt_global_entry->list_lock);
918 head = &tt_global_entry->orig_list;
919 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
920 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200921 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200922 "Deleting %pM from global tt entry %pM: %s\n",
923 orig_node->orig,
924 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200925 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200926 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200927 }
928 }
929 spin_unlock_bh(&tt_global_entry->list_lock);
930}
931
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932static void
933batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
934 struct batadv_tt_global_entry *tt_global_entry,
935 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200936{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200937 batadv_dbg(BATADV_DBG_TT, bat_priv,
938 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200939 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200940
Sven Eckelmann807736f2012-07-15 22:26:51 +0200941 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200942 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200943 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200944
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945}
946
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200947/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200948 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
949 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200950 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200951static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200952batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
953 struct batadv_tt_global_entry *tt_global_entry,
954 struct batadv_orig_node *orig_node,
955 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200956{
957 bool last_entry = true;
958 struct hlist_head *head;
959 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200960 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200961
962 /* no local entry exists, case 1:
963 * Check if this is the last one or if other entries exist.
964 */
965
966 rcu_read_lock();
967 head = &tt_global_entry->orig_list;
968 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
969 if (orig_entry->orig_node != orig_node) {
970 last_entry = false;
971 break;
972 }
973 }
974 rcu_read_unlock();
975
976 if (last_entry) {
977 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200978 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200979 tt_global_entry->roam_at = jiffies;
980 } else
981 /* there is another entry, we can simply delete this
982 * one and can still use the other one.
983 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200984 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
985 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200986}
987
988
989
Sven Eckelmann56303d32012-06-05 22:31:31 +0200990static void batadv_tt_global_del(struct batadv_priv *bat_priv,
991 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200992 const unsigned char *addr,
993 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200995 struct batadv_tt_global_entry *tt_global_entry = NULL;
996 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997
Sven Eckelmanna5130882012-05-16 20:23:16 +0200998 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200999 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001000 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001001
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001002 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001003 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1004 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001005
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001006 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +02001007 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
1008 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001009
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001010 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001011 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001012
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001013 /* if we are deleting a global entry due to a roam
1014 * event, there are two possibilities:
1015 * 1) the client roamed from node A to node B => if there
1016 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001017 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001018 * wait for node B to claim it. In case of timeout
1019 * the entry is purged.
1020 *
1021 * If there are other originators left, we directly delete
1022 * the originator.
1023 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001024 * the global entry, since it is useless now.
1025 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001026 local_entry = batadv_tt_local_hash_find(bat_priv,
1027 tt_global_entry->common.addr);
1028 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001029 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001030 batadv_tt_global_del_orig_list(tt_global_entry);
1031 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001032 } else
1033 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001034 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1035 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001036
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001037
Antonio Quartullicc47f662011-04-27 14:27:57 +02001038out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001039 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001040 batadv_tt_global_entry_free_ref(tt_global_entry);
1041 if (local_entry)
1042 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001043}
1044
Sven Eckelmann56303d32012-06-05 22:31:31 +02001045void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1046 struct batadv_orig_node *orig_node,
1047 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001048{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001049 struct batadv_tt_global_entry *tt_global;
1050 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001051 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001052 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001053 struct hlist_node *node, *safe;
1054 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001055 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001056
Simon Wunderlich6e801492011-10-19 10:28:26 +02001057 if (!hash)
1058 return;
1059
Antonio Quartullia73105b2011-04-27 14:27:44 +02001060 for (i = 0; i < hash->size; i++) {
1061 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001062 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001064 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001065 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001066 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001067 tt_global = container_of(tt_common_entry,
1068 struct batadv_tt_global_entry,
1069 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001070
Sven Eckelmann56303d32012-06-05 22:31:31 +02001071 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001072 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001073
Sven Eckelmann56303d32012-06-05 22:31:31 +02001074 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001075 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001076 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001077 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001078 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001079 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001080 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001081 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001082 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001084 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085}
1086
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001087static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1088 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001089{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001090 bool purge = false;
1091 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1092 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001093
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001094 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1095 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1096 purge = true;
1097 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001098 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001099
1100 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1101 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1102 purge = true;
1103 *msg = "Temporary client timeout\n";
1104 }
1105
1106 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001107}
1108
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001109static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001110{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001111 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001112 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001113 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001114 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001115 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001116 char *msg = NULL;
1117 struct batadv_tt_common_entry *tt_common;
1118 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001119
Antonio Quartullicc47f662011-04-27 14:27:57 +02001120 for (i = 0; i < hash->size; i++) {
1121 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001122 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001123
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001124 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001125 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1126 hash_entry) {
1127 tt_global = container_of(tt_common,
1128 struct batadv_tt_global_entry,
1129 common);
1130
1131 if (!batadv_tt_global_to_purge(tt_global, &msg))
1132 continue;
1133
1134 batadv_dbg(BATADV_DBG_TT, bat_priv,
1135 "Deleting global tt entry (%pM): %s\n",
1136 tt_global->common.addr, msg);
1137
1138 hlist_del_rcu(node);
1139
1140 batadv_tt_global_entry_free_ref(tt_global);
1141 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001142 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001143 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001144}
1145
Sven Eckelmann56303d32012-06-05 22:31:31 +02001146static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001148 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001149 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001150 struct batadv_tt_common_entry *tt_common_entry;
1151 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001152 struct hlist_node *node, *node_tmp;
1153 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001154 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001155
Sven Eckelmann807736f2012-07-15 22:26:51 +02001156 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001157 return;
1158
Sven Eckelmann807736f2012-07-15 22:26:51 +02001159 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001160
1161 for (i = 0; i < hash->size; i++) {
1162 head = &hash->table[i];
1163 list_lock = &hash->list_locks[i];
1164
1165 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001166 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001167 head, hash_entry) {
1168 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001169 tt_global = container_of(tt_common_entry,
1170 struct batadv_tt_global_entry,
1171 common);
1172 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001173 }
1174 spin_unlock_bh(list_lock);
1175 }
1176
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001177 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001178
Sven Eckelmann807736f2012-07-15 22:26:51 +02001179 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180}
1181
Sven Eckelmann56303d32012-06-05 22:31:31 +02001182static bool
1183_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1184 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001185{
1186 bool ret = false;
1187
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001188 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1189 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001190 ret = true;
1191
1192 return ret;
1193}
1194
Sven Eckelmann56303d32012-06-05 22:31:31 +02001195struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1196 const uint8_t *src,
1197 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001198{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199 struct batadv_tt_local_entry *tt_local_entry = NULL;
1200 struct batadv_tt_global_entry *tt_global_entry = NULL;
1201 struct batadv_orig_node *orig_node = NULL;
1202 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001203 struct hlist_head *head;
1204 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001205 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001206 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001208 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001209 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001210 if (!tt_local_entry)
1211 goto out;
1212 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001213
Sven Eckelmanna5130882012-05-16 20:23:16 +02001214 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001215 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001216 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001217
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001218 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001219 * isolation
1220 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001221 if (tt_local_entry &&
1222 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001223 goto out;
1224
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001226
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001227 rcu_read_lock();
1228 head = &tt_global_entry->orig_list;
1229 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001230 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001231 if (!router)
1232 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001233
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001234 if (router->tq_avg > best_tq) {
1235 orig_node = orig_entry->orig_node;
1236 best_tq = router->tq_avg;
1237 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001238 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001239 }
1240 /* found anything? */
1241 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1242 orig_node = NULL;
1243 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001244out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001245 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001246 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001247 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001248 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001249
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001250 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001251}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001252
1253/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001254static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1255 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001256{
1257 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001258 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001259 struct batadv_tt_common_entry *tt_common;
1260 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001261 struct hlist_node *node;
1262 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001263 uint32_t i;
1264 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001265
1266 for (i = 0; i < hash->size; i++) {
1267 head = &hash->table[i];
1268
1269 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001270 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001271 tt_global = container_of(tt_common,
1272 struct batadv_tt_global_entry,
1273 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001274 /* Roaming clients are in the global table for
1275 * consistency only. They don't have to be
1276 * taken into account while computing the
1277 * global crc
1278 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001279 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001280 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001281 /* Temporary clients have not been announced yet, so
1282 * they have to be skipped while computing the global
1283 * crc
1284 */
1285 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1286 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001287
1288 /* find out if this global entry is announced by this
1289 * originator
1290 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001291 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001292 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001293 continue;
1294
1295 total_one = 0;
1296 for (j = 0; j < ETH_ALEN; j++)
1297 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001298 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001299 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001300 }
1301 rcu_read_unlock();
1302 }
1303
1304 return total;
1305}
1306
1307/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001308static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001309{
1310 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001311 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001312 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001313 struct hlist_node *node;
1314 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001315 uint32_t i;
1316 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001317
1318 for (i = 0; i < hash->size; i++) {
1319 head = &hash->table[i];
1320
1321 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001322 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001323 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001324 * account while computing the CRC
1325 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001326 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001327 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001328 total_one = 0;
1329 for (j = 0; j < ETH_ALEN; j++)
1330 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001331 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001332 total ^= total_one;
1333 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001334 rcu_read_unlock();
1335 }
1336
1337 return total;
1338}
1339
Sven Eckelmann56303d32012-06-05 22:31:31 +02001340static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001342 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001343
Sven Eckelmann807736f2012-07-15 22:26:51 +02001344 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001345
Sven Eckelmann807736f2012-07-15 22:26:51 +02001346 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001347 list_del(&node->list);
1348 kfree(node);
1349 }
1350
Sven Eckelmann807736f2012-07-15 22:26:51 +02001351 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001352}
1353
Sven Eckelmann56303d32012-06-05 22:31:31 +02001354static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1355 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001356 const unsigned char *tt_buff,
1357 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001359 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001360
1361 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001362 * last OGM (the OGM could carry no changes)
1363 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001364 spin_lock_bh(&orig_node->tt_buff_lock);
1365 if (tt_buff_len > 0) {
1366 kfree(orig_node->tt_buff);
1367 orig_node->tt_buff_len = 0;
1368 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1369 if (orig_node->tt_buff) {
1370 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1371 orig_node->tt_buff_len = tt_buff_len;
1372 }
1373 }
1374 spin_unlock_bh(&orig_node->tt_buff_lock);
1375}
1376
Sven Eckelmann56303d32012-06-05 22:31:31 +02001377static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001378{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001380
Sven Eckelmann807736f2012-07-15 22:26:51 +02001381 spin_lock_bh(&bat_priv->tt.req_list_lock);
1382 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001383 if (batadv_has_timed_out(node->issued_at,
1384 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001385 list_del(&node->list);
1386 kfree(node);
1387 }
1388 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001389 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390}
1391
1392/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001393 * has already been issued for this orig_node, NULL otherwise
1394 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001395static struct batadv_tt_req_node *
1396batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1397 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001398{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001399 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001400
Sven Eckelmann807736f2012-07-15 22:26:51 +02001401 spin_lock_bh(&bat_priv->tt.req_list_lock);
1402 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001403 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1404 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001405 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001406 goto unlock;
1407 }
1408
1409 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1410 if (!tt_req_node)
1411 goto unlock;
1412
1413 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1414 tt_req_node->issued_at = jiffies;
1415
Sven Eckelmann807736f2012-07-15 22:26:51 +02001416 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001417unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001418 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001419 return tt_req_node;
1420}
1421
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001422/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001423static int batadv_tt_local_valid_entry(const void *entry_ptr,
1424 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001425{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001426 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001427
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001428 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001429 return 0;
1430 return 1;
1431}
1432
Sven Eckelmanna5130882012-05-16 20:23:16 +02001433static int batadv_tt_global_valid(const void *entry_ptr,
1434 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001436 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1437 const struct batadv_tt_global_entry *tt_global_entry;
1438 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001440 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1441 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001442 return 0;
1443
Sven Eckelmann56303d32012-06-05 22:31:31 +02001444 tt_global_entry = container_of(tt_common_entry,
1445 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001446 common);
1447
Sven Eckelmanna5130882012-05-16 20:23:16 +02001448 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001449}
1450
Sven Eckelmanna5130882012-05-16 20:23:16 +02001451static struct sk_buff *
1452batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001453 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001454 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001455 int (*valid_cb)(const void *, const void *),
1456 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001458 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001459 struct batadv_tt_query_packet *tt_response;
1460 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001461 struct hlist_node *node;
1462 struct hlist_head *head;
1463 struct sk_buff *skb = NULL;
1464 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001465 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001466 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001467 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001468
1469 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1470 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001471 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001473 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474
Sven Eckelmann96412692012-06-05 22:31:30 +02001475 len = tt_query_size + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001476 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477 if (!skb)
1478 goto out;
1479
Sven Eckelmann5b246572012-11-04 17:11:45 +01001480 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001481 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001483
Sven Eckelmann96412692012-06-05 22:31:30 +02001484 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001485 tt_count = 0;
1486
1487 rcu_read_lock();
1488 for (i = 0; i < hash->size; i++) {
1489 head = &hash->table[i];
1490
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001491 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001492 head, hash_entry) {
1493 if (tt_count == tt_tot)
1494 break;
1495
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001496 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497 continue;
1498
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001499 memcpy(tt_change->addr, tt_common_entry->addr,
1500 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001501 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001502
1503 tt_count++;
1504 tt_change++;
1505 }
1506 }
1507 rcu_read_unlock();
1508
Antonio Quartulli9d852392011-10-17 14:25:13 +02001509 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001510 * copied
1511 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001512 tt_response->tt_data = htons(tt_count);
1513
Antonio Quartullia73105b2011-04-27 14:27:44 +02001514out:
1515 return skb;
1516}
1517
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1519 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001520 uint8_t ttvn, uint16_t tt_crc,
1521 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001522{
1523 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001524 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001525 struct batadv_neigh_node *neigh_node = NULL;
1526 struct batadv_hard_iface *primary_if;
1527 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001528 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001529 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001531 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001532 if (!primary_if)
1533 goto out;
1534
1535 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001536 * reply from the same orig_node yet
1537 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001538 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539 if (!tt_req_node)
1540 goto out;
1541
Sven Eckelmann5b246572012-11-04 17:11:45 +01001542 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001543 if (!skb)
1544 goto out;
1545
Sven Eckelmann5b246572012-11-04 17:11:45 +01001546 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001547
Sven Eckelmann96412692012-06-05 22:31:30 +02001548 tt_req_len = sizeof(*tt_request);
1549 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001550
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001551 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001552 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001553 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1554 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001555 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001556 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001557 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001558 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001559
1560 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001561 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001563 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001564 if (!neigh_node)
1565 goto out;
1566
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001567 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001568 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1569 dst_orig_node->orig, neigh_node->addr,
1570 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001571
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001572 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001573
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001574 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001575 ret = 0;
1576
1577out:
1578 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001579 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001580 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001581 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001582 if (ret)
1583 kfree_skb(skb);
1584 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001585 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001586 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001587 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001588 kfree(tt_req_node);
1589 }
1590 return ret;
1591}
1592
Sven Eckelmann96412692012-06-05 22:31:30 +02001593static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001594batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001595 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001596{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001597 struct batadv_orig_node *req_dst_orig_node = NULL;
1598 struct batadv_orig_node *res_dst_orig_node = NULL;
1599 struct batadv_neigh_node *neigh_node = NULL;
1600 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001601 uint8_t orig_ttvn, req_ttvn, ttvn;
1602 int ret = false;
1603 unsigned char *tt_buff;
1604 bool full_table;
1605 uint16_t tt_len, tt_tot;
1606 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001607 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001608 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001609 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001611 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001612 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1613 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001614 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001615
1616 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001617 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001618 if (!req_dst_orig_node)
1619 goto out;
1620
Sven Eckelmannda641192012-05-12 13:48:56 +02001621 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001622 if (!res_dst_orig_node)
1623 goto out;
1624
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001625 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001626 if (!neigh_node)
1627 goto out;
1628
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001629 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001630 if (!primary_if)
1631 goto out;
1632
1633 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1634 req_ttvn = tt_request->ttvn;
1635
Antonio Quartulli015758d2011-07-09 17:52:13 +02001636 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001638 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639 goto out;
1640
Antonio Quartulli015758d2011-07-09 17:52:13 +02001641 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001642 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643 !req_dst_orig_node->tt_buff)
1644 full_table = true;
1645 else
1646 full_table = false;
1647
1648 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001649 * I'll send only one packet with as much TT entries as I can
1650 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001651 if (!full_table) {
1652 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1653 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001654 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001655
Sven Eckelmann96412692012-06-05 22:31:30 +02001656 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001657 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001658 if (!skb)
1659 goto unlock;
1660
Sven Eckelmann5b246572012-11-04 17:11:45 +01001661 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001662 packet_pos = skb_put(skb, len);
1663 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001664 tt_response->ttvn = req_ttvn;
1665 tt_response->tt_data = htons(tt_tot);
1666
Sven Eckelmann96412692012-06-05 22:31:30 +02001667 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001668 /* Copy the last orig_node's OGM buffer */
1669 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1670 req_dst_orig_node->tt_buff_len);
1671
1672 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1673 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001674 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1675 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1677
Sven Eckelmanna5130882012-05-16 20:23:16 +02001678 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001679 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001680 primary_if,
1681 batadv_tt_global_valid,
1682 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001683 if (!skb)
1684 goto out;
1685
Sven Eckelmann96412692012-06-05 22:31:30 +02001686 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001687 }
1688
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001689 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001690 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001691 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001692 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1693 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001694 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001695
1696 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001697 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001699 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001700 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1701 res_dst_orig_node->orig, neigh_node->addr,
1702 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001703
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001704 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001705
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001706 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 ret = true;
1708 goto out;
1709
1710unlock:
1711 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1712
1713out:
1714 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001715 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001716 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001717 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001718 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001719 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001720 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001721 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001722 if (!ret)
1723 kfree_skb(skb);
1724 return ret;
1725
1726}
Sven Eckelmann96412692012-06-05 22:31:30 +02001727
1728static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001730 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001731{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001732 struct batadv_orig_node *orig_node = NULL;
1733 struct batadv_neigh_node *neigh_node = NULL;
1734 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001735 uint8_t my_ttvn, req_ttvn, ttvn;
1736 int ret = false;
1737 unsigned char *tt_buff;
1738 bool full_table;
1739 uint16_t tt_len, tt_tot;
1740 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001741 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001742 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001743 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001744
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001745 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001746 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1747 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001748 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001749
1750
Sven Eckelmann807736f2012-07-15 22:26:51 +02001751 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001752 req_ttvn = tt_request->ttvn;
1753
Sven Eckelmannda641192012-05-12 13:48:56 +02001754 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001755 if (!orig_node)
1756 goto out;
1757
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001758 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001759 if (!neigh_node)
1760 goto out;
1761
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001762 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001763 if (!primary_if)
1764 goto out;
1765
1766 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001767 * is too big send the whole local translation table
1768 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001769 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001770 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 full_table = true;
1772 else
1773 full_table = false;
1774
1775 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001776 * I'll send only one packet with as much TT entries as I can
1777 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001778 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001779 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1780 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001781 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782
Sven Eckelmann96412692012-06-05 22:31:30 +02001783 len = sizeof(*tt_response) + tt_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +01001784 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785 if (!skb)
1786 goto unlock;
1787
Sven Eckelmann5b246572012-11-04 17:11:45 +01001788 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001789 packet_pos = skb_put(skb, len);
1790 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 tt_response->ttvn = req_ttvn;
1792 tt_response->tt_data = htons(tt_tot);
1793
Sven Eckelmann96412692012-06-05 22:31:30 +02001794 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001795 memcpy(tt_buff, bat_priv->tt.last_changeset,
1796 bat_priv->tt.last_changeset_len);
1797 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001798 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001799 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001800 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001801 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001802
Sven Eckelmanna5130882012-05-16 20:23:16 +02001803 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001804 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001805 primary_if,
1806 batadv_tt_local_valid_entry,
1807 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808 if (!skb)
1809 goto out;
1810
Sven Eckelmann96412692012-06-05 22:31:30 +02001811 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812 }
1813
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001814 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001815 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001816 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001817 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1818 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001819 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820
1821 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001822 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001823
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001824 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001825 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1826 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001827 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001828
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001829 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001830
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02001831 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832 ret = true;
1833 goto out;
1834
1835unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001836 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001837out:
1838 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001839 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001840 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001841 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001843 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001844 if (!ret)
1845 kfree_skb(skb);
1846 /* This packet was for me, so it doesn't need to be re-routed */
1847 return true;
1848}
1849
Sven Eckelmann56303d32012-06-05 22:31:31 +02001850bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001851 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001852{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001853 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001854 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001855 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001856 return true;
1857
Sven Eckelmanna5130882012-05-16 20:23:16 +02001858 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001859 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001860 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001861 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001862}
1863
Sven Eckelmann56303d32012-06-05 22:31:31 +02001864static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1865 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001866 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001867 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001868{
1869 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001870 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001871
1872 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001873 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1874 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001875 batadv_tt_global_del(bat_priv, orig_node,
1876 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001877 "tt removed by changes",
1878 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001879 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001880 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001881 (tt_change + i)->addr,
1882 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001883 /* In case of problem while storing a
1884 * global_entry, we stop the updating
1885 * procedure without committing the
1886 * ttvn change. This will avoid to send
1887 * corrupted data on tt_request
1888 */
1889 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001890 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001892 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001893}
1894
Sven Eckelmann56303d32012-06-05 22:31:31 +02001895static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001896 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001897{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001898 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899
Sven Eckelmannda641192012-05-12 13:48:56 +02001900 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001901 if (!orig_node)
1902 goto out;
1903
1904 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001905 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001906
Sven Eckelmanna5130882012-05-16 20:23:16 +02001907 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001908 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001909 ntohs(tt_response->tt_data),
1910 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001911
1912 spin_lock_bh(&orig_node->tt_buff_lock);
1913 kfree(orig_node->tt_buff);
1914 orig_node->tt_buff_len = 0;
1915 orig_node->tt_buff = NULL;
1916 spin_unlock_bh(&orig_node->tt_buff_lock);
1917
1918 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1919
1920out:
1921 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001922 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001923}
1924
Sven Eckelmann56303d32012-06-05 22:31:31 +02001925static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1926 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001927 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001928 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001929{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001930 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1931 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932
Sven Eckelmanna5130882012-05-16 20:23:16 +02001933 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1934 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001935 atomic_set(&orig_node->last_ttvn, ttvn);
1936}
1937
Sven Eckelmann56303d32012-06-05 22:31:31 +02001938bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001939{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001940 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001941 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001942
Sven Eckelmanna5130882012-05-16 20:23:16 +02001943 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001944 if (!tt_local_entry)
1945 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001946 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001947 * consistency purpose)
1948 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001949 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
1950 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001951 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001952 ret = true;
1953out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001954 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001955 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001956 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001957}
1958
Sven Eckelmann56303d32012-06-05 22:31:31 +02001959void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001960 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001961{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001962 struct batadv_tt_req_node *node, *safe;
1963 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001964 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001965
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001966 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001967 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1968 tt_response->src, tt_response->ttvn,
1969 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001970 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001971
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001972 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001973 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001974 goto out;
1975
Sven Eckelmannda641192012-05-12 13:48:56 +02001976 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001977 if (!orig_node)
1978 goto out;
1979
Sven Eckelmann96412692012-06-05 22:31:30 +02001980 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001981 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001982 } else {
1983 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001984 batadv_tt_update_changes(bat_priv, orig_node,
1985 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001986 tt_response->ttvn, tt_change);
1987 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001988
1989 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001990 spin_lock_bh(&bat_priv->tt.req_list_lock);
1991 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001992 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001993 continue;
1994 list_del(&node->list);
1995 kfree(node);
1996 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001997 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001998
1999 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002000 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002001out:
2002 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002003 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002004}
2005
Sven Eckelmann56303d32012-06-05 22:31:31 +02002006int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007{
Sven Eckelmann5346c352012-05-05 13:27:28 +02002008 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009
Sven Eckelmanna5130882012-05-16 20:23:16 +02002010 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002011 if (ret < 0)
2012 return ret;
2013
Sven Eckelmanna5130882012-05-16 20:23:16 +02002014 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02002015 if (ret < 0)
2016 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002017
Sven Eckelmanna5130882012-05-16 20:23:16 +02002018 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002019
2020 return 1;
2021}
2022
Sven Eckelmann56303d32012-06-05 22:31:31 +02002023static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002024{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002025 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002026
Sven Eckelmann807736f2012-07-15 22:26:51 +02002027 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002028
Sven Eckelmann807736f2012-07-15 22:26:51 +02002029 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002030 list_del(&node->list);
2031 kfree(node);
2032 }
2033
Sven Eckelmann807736f2012-07-15 22:26:51 +02002034 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002035}
2036
Sven Eckelmann56303d32012-06-05 22:31:31 +02002037static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002039 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002040
Sven Eckelmann807736f2012-07-15 22:26:51 +02002041 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2042 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002043 if (!batadv_has_timed_out(node->first_time,
2044 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002045 continue;
2046
2047 list_del(&node->list);
2048 kfree(node);
2049 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002050 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002051}
2052
2053/* This function checks whether the client already reached the
2054 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2055 * will not be sent.
2056 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002057 * returns true if the ROAMING_ADV can be sent, false otherwise
2058 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002059static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002060 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002061{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002062 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002063 bool ret = false;
2064
Sven Eckelmann807736f2012-07-15 22:26:51 +02002065 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002066 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002067 * reply from the same orig_node yet
2068 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002069 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002070 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002071 continue;
2072
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002073 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002074 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002075 continue;
2076
Sven Eckelmann3e348192012-05-16 20:23:22 +02002077 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002078 /* Sorry, you roamed too many times! */
2079 goto unlock;
2080 ret = true;
2081 break;
2082 }
2083
2084 if (!ret) {
2085 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2086 if (!tt_roam_node)
2087 goto unlock;
2088
2089 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002090 atomic_set(&tt_roam_node->counter,
2091 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002092 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2093
Sven Eckelmann807736f2012-07-15 22:26:51 +02002094 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002095 ret = true;
2096 }
2097
2098unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002099 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002100 return ret;
2101}
2102
Sven Eckelmann56303d32012-06-05 22:31:31 +02002103static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2104 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002105{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002106 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002107 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002108 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002109 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002110 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002111 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002112
2113 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002114 * already roamed to us too many times
2115 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002116 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002117 goto out;
2118
Sven Eckelmann5b246572012-11-04 17:11:45 +01002119 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002120 if (!skb)
2121 goto out;
2122
Sven Eckelmann5b246572012-11-04 17:11:45 +01002123 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002124
Sven Eckelmann96412692012-06-05 22:31:30 +02002125 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002126
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002127 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002128 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002129 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002130 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002131 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002132 if (!primary_if)
2133 goto out;
2134 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002135 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002136 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2137 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2138
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002139 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002140 if (!neigh_node)
2141 goto out;
2142
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002143 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002144 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2145 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002146
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002147 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002148
Sven Eckelmann9455e34cb2012-05-12 02:09:37 +02002149 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002150 ret = 0;
2151
2152out:
2153 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002154 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002155 if (ret)
2156 kfree_skb(skb);
2157 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002158}
2159
Sven Eckelmanna5130882012-05-16 20:23:16 +02002160static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002161{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002162 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002163 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002164 struct batadv_priv *bat_priv;
2165
2166 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002167 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2168 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002169
Sven Eckelmanna5130882012-05-16 20:23:16 +02002170 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002171 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002172 batadv_tt_req_purge(bat_priv);
2173 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002174
Sven Eckelmanna5130882012-05-16 20:23:16 +02002175 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002176}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002177
Sven Eckelmann56303d32012-06-05 22:31:31 +02002178void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002179{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002180 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002181
Sven Eckelmanna5130882012-05-16 20:23:16 +02002182 batadv_tt_local_table_free(bat_priv);
2183 batadv_tt_global_table_free(bat_priv);
2184 batadv_tt_req_list_free(bat_priv);
2185 batadv_tt_changes_list_free(bat_priv);
2186 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002187
Sven Eckelmann807736f2012-07-15 22:26:51 +02002188 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002189}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002190
Antonio Quartulli697f2532011-11-07 16:47:01 +01002191/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002192 * in the given hash table and returns the number of modified entries
2193 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002194static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2195 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002196{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002197 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002198 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002199 struct hlist_head *head;
2200 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002201 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002202
2203 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002204 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002205
2206 for (i = 0; i < hash->size; i++) {
2207 head = &hash->table[i];
2208
2209 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002210 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002211 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002212 if (enable) {
2213 if ((tt_common_entry->flags & flags) == flags)
2214 continue;
2215 tt_common_entry->flags |= flags;
2216 } else {
2217 if (!(tt_common_entry->flags & flags))
2218 continue;
2219 tt_common_entry->flags &= ~flags;
2220 }
2221 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002222 }
2223 rcu_read_unlock();
2224 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002225out:
2226 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002227}
2228
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002229/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002230static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002231{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002232 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002233 struct batadv_tt_common_entry *tt_common;
2234 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002235 struct hlist_node *node, *node_tmp;
2236 struct hlist_head *head;
2237 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002238 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002239
2240 if (!hash)
2241 return;
2242
2243 for (i = 0; i < hash->size; i++) {
2244 head = &hash->table[i];
2245 list_lock = &hash->list_locks[i];
2246
2247 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002248 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2249 hash_entry) {
2250 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002251 continue;
2252
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002253 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002254 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002255 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002256
Sven Eckelmann807736f2012-07-15 22:26:51 +02002257 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002258 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002259 tt_local = container_of(tt_common,
2260 struct batadv_tt_local_entry,
2261 common);
2262 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002263 }
2264 spin_unlock_bh(list_lock);
2265 }
2266
2267}
2268
Sven Eckelmann56303d32012-06-05 22:31:31 +02002269static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002270 unsigned char **packet_buff,
2271 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002272{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002273 uint16_t changed_num = 0;
2274
Sven Eckelmann807736f2012-07-15 22:26:51 +02002275 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002276 return -ENOENT;
2277
Sven Eckelmann807736f2012-07-15 22:26:51 +02002278 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002279 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002280
2281 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002282 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002283 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002284 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002285
2286 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002287 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002288 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002289 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002290 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002291
2292 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002293 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002294
Sven Eckelmanna5130882012-05-16 20:23:16 +02002295 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2296 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002297}
2298
2299/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002300int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002301 unsigned char **packet_buff, int *packet_buff_len,
2302 int packet_min_len)
2303{
2304 int tt_num_changes;
2305
2306 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002307 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2308 packet_buff_len,
2309 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002310
2311 /* if the changes have been sent often enough */
2312 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002313 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002314 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2315 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002316 tt_num_changes = 0;
2317 }
2318
2319 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002320}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002321
Sven Eckelmann56303d32012-06-05 22:31:31 +02002322bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002323 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002324{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002325 struct batadv_tt_local_entry *tt_local_entry = NULL;
2326 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002327 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002328
2329 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002330 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002331
Sven Eckelmanna5130882012-05-16 20:23:16 +02002332 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002333 if (!tt_local_entry)
2334 goto out;
2335
Sven Eckelmanna5130882012-05-16 20:23:16 +02002336 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002337 if (!tt_global_entry)
2338 goto out;
2339
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002340 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002341 goto out;
2342
Marek Lindner5870adc2012-06-20 17:16:05 +02002343 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002344
2345out:
2346 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002347 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002348 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002349 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002350 return ret;
2351}
Marek Lindnera943cac2011-07-30 13:10:18 +02002352
Sven Eckelmann56303d32012-06-05 22:31:31 +02002353void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2354 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002355 const unsigned char *tt_buff, uint8_t tt_num_changes,
2356 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002357{
2358 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2359 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002360 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002361
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002362 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002363 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002364 return;
2365
Antonio Quartulli17071572011-11-07 16:36:40 +01002366 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002367 * increased by one -> we can apply the attached changes
2368 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002369 if ((!orig_node->tt_initialised && ttvn == 1) ||
2370 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002371 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002372 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2373 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002374 * In this case send a tt request
2375 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002376 if (!tt_num_changes) {
2377 full_table = false;
2378 goto request_table;
2379 }
2380
Sven Eckelmann96412692012-06-05 22:31:30 +02002381 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002382 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002383 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002384
2385 /* Even if we received the precomputed crc with the OGM, we
2386 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002387 * in the global table
2388 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002389 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002390
2391 /* The ttvn alone is not enough to guarantee consistency
2392 * because a single value could represent different states
2393 * (due to the wrap around). Thus a node has to check whether
2394 * the resulting table (after applying the changes) is still
2395 * consistent or not. E.g. a node could disconnect while its
2396 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2397 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002398 * inconsistency
2399 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002400 if (orig_node->tt_crc != tt_crc)
2401 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02002402 } else {
2403 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002404 * in sync anymore -> request fresh tt data
2405 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002406 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2407 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002408request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002409 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002410 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2411 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2412 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002413 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2414 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002415 return;
2416 }
2417 }
2418}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002419
2420/* returns true whether we know that the client has moved from its old
2421 * originator to another one. This entry is kept is still kept for consistency
2422 * purposes
2423 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002424bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002425 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002426{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002427 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002428 bool ret = false;
2429
Sven Eckelmanna5130882012-05-16 20:23:16 +02002430 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002431 if (!tt_global_entry)
2432 goto out;
2433
Antonio Quartulli9f9ff082012-08-27 09:35:54 +02002434 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002435 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002436out:
2437 return ret;
2438}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002439
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002440/**
2441 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2442 * @bat_priv: the bat priv with all the soft interface information
2443 * @addr: the MAC address of the local client to query
2444 *
2445 * Returns true if the local client is known to be roaming (it is not served by
2446 * this node anymore) or not. If yes, the client is still present in the table
2447 * to keep the latter consistent with the node TTVN
2448 */
2449bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2450 uint8_t *addr)
2451{
2452 struct batadv_tt_local_entry *tt_local_entry;
2453 bool ret = false;
2454
2455 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2456 if (!tt_local_entry)
2457 goto out;
2458
2459 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2460 batadv_tt_local_entry_free_ref(tt_local_entry);
2461out:
2462 return ret;
2463
2464}
2465
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002466bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2467 struct batadv_orig_node *orig_node,
2468 const unsigned char *addr)
2469{
2470 bool ret = false;
2471
2472 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2473 BATADV_TT_CLIENT_TEMP,
2474 atomic_read(&orig_node->last_ttvn)))
2475 goto out;
2476
2477 batadv_dbg(BATADV_DBG_TT, bat_priv,
2478 "Added temporary global client (addr: %pM orig: %pM)\n",
2479 addr, orig_node->orig);
2480 ret = true;
2481out:
2482 return ret;
2483}