blob: 0ac39d5f77d8071047aa4d32cf421212654526bc [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020023#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020024#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020027#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010028#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Antonio Quartullia73105b2011-04-27 14:27:44 +020030#include <linux/crc16.h>
31
Sven Eckelmann56303d32012-06-05 22:31:31 +020032static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020034static void batadv_tt_purge(struct work_struct *work);
35static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020036batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020037static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041
Marek Lindner7aadf882011-02-18 12:28:09 +000042/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020043static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000044{
Sven Eckelmann56303d32012-06-05 22:31:31 +020045 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020046 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000047
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49}
50
Sven Eckelmann56303d32012-06-05 22:31:31 +020051static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052{
Sven Eckelmann807736f2012-07-15 22:26:51 +020053 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
Antonio Quartullia73105b2011-04-27 14:27:44 +020055 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static struct batadv_tt_common_entry *
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020059batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000060{
Marek Lindner7aadf882011-02-18 12:28:09 +000061 struct hlist_head *head;
62 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +020063 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020065 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000066
67 if (!hash)
68 return NULL;
69
Sven Eckelmannda641192012-05-12 13:48:56 +020070 index = batadv_choose_orig(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +000071 head = &hash->table[index];
72
73 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +010074 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020075 if (!batadv_compare_eth(tt_common_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000076 continue;
77
Antonio Quartulli48100ba2011-10-30 12:17:33 +010078 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020079 continue;
80
Antonio Quartulli48100ba2011-10-30 12:17:33 +010081 tt_common_entry_tmp = tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000082 break;
83 }
84 rcu_read_unlock();
85
Antonio Quartulli48100ba2011-10-30 12:17:33 +010086 return tt_common_entry_tmp;
87}
88
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_tt_local_entry *
90batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
Antonio Quartulli48100ba2011-10-30 12:17:33 +010091{
Sven Eckelmann56303d32012-06-05 22:31:31 +020092 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +010094
Sven Eckelmann807736f2012-07-15 22:26:51 +020095 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +010096 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +020098 struct batadv_tt_local_entry,
99 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100100 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101}
102
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103static struct batadv_tt_global_entry *
104batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +0000105{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108
Sven Eckelmann807736f2012-07-15 22:26:51 +0200109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_tt_global_entry,
113 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100114 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000115
Marek Lindner7aadf882011-02-18 12:28:09 +0000116}
117
Sven Eckelmanna5130882012-05-16 20:23:16 +0200118static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200119batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200120{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200123}
124
Sven Eckelmanna5130882012-05-16 20:23:16 +0200125static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlich531027f2011-10-19 11:02:25 +0200126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
Simon Wunderlich531027f2011-10-19 11:02:25 +0200129
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
Simon Wunderlich531027f2011-10-19 11:02:25 +0200133
Simon Wunderlich531027f2011-10-19 11:02:25 +0200134 kfree(tt_global_entry);
135}
136
Sven Eckelmanna5130882012-05-16 20:23:16 +0200137static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200139{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200141 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 call_rcu(&tt_global_entry->common.rcu,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200143 batadv_tt_global_entry_free_rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200144 }
145}
146
Sven Eckelmanna5130882012-05-16 20:23:16 +0200147static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200152 batadv_orig_node_free_ref(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200153 kfree(orig_entry);
154}
155
Sven Eckelmanna5130882012-05-16 20:23:16 +0200156static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200158{
Antonio Quartullid657e622012-07-01 14:09:12 +0200159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
Antonio Quartulli29cb99d2012-06-25 20:49:51 +0000161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200167 const uint8_t *addr, uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
Antonio Quartulliff66c972011-06-30 01:14:00 +0200178 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200182
183 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000206 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200207 event_removed = true;
208 goto unlock;
209 }
210
Antonio Quartullia73105b2011-04-27 14:27:44 +0200211 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200213
214unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200217 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200218 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200219 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200221}
222
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200223int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200224{
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 return changes_num * sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200226}
227
Sven Eckelmann56303d32012-06-05 22:31:31 +0200228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200231 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Sven Eckelmann807736f2012-07-15 22:26:51 +0200233 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann807736f2012-07-15 22:26:51 +0200235 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200236 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239}
240
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200241void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
242 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 struct batadv_tt_local_entry *tt_local_entry = NULL;
246 struct batadv_tt_global_entry *tt_global_entry = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200247 struct hlist_head *head;
248 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200249 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100250 int hash_added;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Sven Eckelmanna5130882012-05-16 20:23:16 +0200252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200254 if (tt_local_entry) {
255 tt_local_entry->last_seen = jiffies;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200258 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 }
260
Sven Eckelmann704509b2011-05-14 23:14:54 +0200261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200262 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200263 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200265 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200267 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200270 tt_local_entry->common.flags = BATADV_NO_FLAGS;
Sven Eckelmann95638772012-05-12 02:09:31 +0200271 if (batadv_is_wifi_iface(ifindex))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100273 atomic_set(&tt_local_entry->common.refcount, 2);
274 tt_local_entry->last_seen = jiffies;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200275 tt_local_entry->common.added_at = tt_local_entry->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
277 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200278 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200283 * (consistency check)
284 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
Antonio Quartullic40ed2b2012-01-06 21:31:33 +0100286
Sven Eckelmann807736f2012-07-15 22:26:51 +0200287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200288 batadv_choose_orig,
289 &tt_local_entry->common,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200290 &tt_local_entry->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100291
292 if (unlikely(hash_added != 0)) {
293 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200294 batadv_tt_local_entry_free_ref(tt_local_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100295 goto out;
296 }
297
Sven Eckelmanna5130882012-05-16 20:23:16 +0200298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200299
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 /* remove address from global hash if present */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Antonio Quartullicc47f662011-04-27 14:27:57 +0200303 /* Check whether it is a roaming! */
304 if (tt_global_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200305 /* These node are probably going to update their tt table */
306 head = &tt_global_entry->orig_list;
307 rcu_read_lock();
308 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
309 orig_entry->orig_node->tt_poss_change = true;
310
Sven Eckelmanna5130882012-05-16 20:23:16 +0200311 batadv_send_roam_adv(bat_priv,
312 tt_global_entry->common.addr,
313 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200314 }
315 rcu_read_unlock();
316 /* The global entry has to be marked as ROAMING and
317 * has to be kept for consistency purpose
318 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200319 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli03fc3072011-12-04 12:26:50 +0100320 tt_global_entry->roam_at = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200321 }
322out:
323 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200324 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200325 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200326 batadv_tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327}
328
Sven Eckelmanna5130882012-05-16 20:23:16 +0200329static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
330 int *packet_buff_len,
331 int min_packet_len,
332 int new_packet_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800334 unsigned char *new_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800336 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
337
338 /* keep old buffer if kmalloc should fail */
339 if (new_buff) {
340 memcpy(new_buff, *packet_buff, min_packet_len);
341 kfree(*packet_buff);
342 *packet_buff = new_buff;
343 *packet_buff_len = new_packet_len;
344 }
345}
346
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200348 unsigned char **packet_buff,
349 int *packet_buff_len,
350 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800351{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_hard_iface *primary_if;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800353 int req_len;
354
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200355 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800356
357 req_len = min_packet_len;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200358 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800359
360 /* if we have too many changes for one packet don't send any
361 * and wait for the tt table request which will be fragmented
362 */
363 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
364 req_len = min_packet_len;
365
Sven Eckelmanna5130882012-05-16 20:23:16 +0200366 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
367 min_packet_len, req_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800368
369 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200370 batadv_hardif_free_ref(primary_if);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800371}
372
Sven Eckelmann56303d32012-06-05 22:31:31 +0200373static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200374 unsigned char **packet_buff,
375 int *packet_buff_len,
376 int min_packet_len)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800377{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200378 struct batadv_tt_change_node *entry, *safe;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800379 int count = 0, tot_changes = 0, new_len;
380 unsigned char *tt_buff;
381
Sven Eckelmanna5130882012-05-16 20:23:16 +0200382 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
383 packet_buff_len, min_packet_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800384
385 new_len = *packet_buff_len - min_packet_len;
386 tt_buff = *packet_buff + min_packet_len;
387
388 if (new_len > 0)
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200389 tot_changes = new_len / batadv_tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
Sven Eckelmann807736f2012-07-15 22:26:51 +0200391 spin_lock_bh(&bat_priv->tt.changes_list_lock);
392 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Sven Eckelmann807736f2012-07-15 22:26:51 +0200394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100395 list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396 if (count < tot_changes) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200397 memcpy(tt_buff + batadv_tt_len(count),
Sven Eckelmann96412692012-06-05 22:31:30 +0200398 &entry->change, sizeof(struct batadv_tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399 count++;
400 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200401 list_del(&entry->list);
402 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200404 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Antonio Quartullia73105b2011-04-27 14:27:44 +0200406 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200407 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
408 kfree(bat_priv->tt.last_changeset);
409 bat_priv->tt.last_changeset_len = 0;
410 bat_priv->tt.last_changeset = NULL;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800411 /* check whether this new OGM has no changes due to size problems */
412 if (new_len > 0) {
413 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200414 * instead of providing the diff
415 */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200416 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
417 if (bat_priv->tt.last_changeset) {
418 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
419 bat_priv->tt.last_changeset_len = new_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200420 }
421 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200422 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Marek Lindner08ad76e2012-04-23 16:32:55 +0800424 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425}
426
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200427int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428{
429 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200432 struct batadv_tt_common_entry *tt_common_entry;
433 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000434 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200436 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Marek Lindner30da63a2012-08-03 17:15:46 +0200438 primary_if = batadv_seq_print_text_primary_if_get(seq);
439 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200440 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100442 seq_printf(seq,
443 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +0200444 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448
Marek Lindner7aadf882011-02-18 12:28:09 +0000449 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100450 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000451 head, hash_entry) {
Simon Wunderlichd099c2c2011-10-22 18:15:26 +0200452 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100453 tt_common_entry->addr,
454 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200455 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100456 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200457 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100458 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200459 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100460 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200461 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100462 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200463 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000465 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200467out:
468 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200469 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200470 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471}
472
Sven Eckelmann56303d32012-06-05 22:31:31 +0200473static void
474batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
475 struct batadv_tt_local_entry *tt_local_entry,
476 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477{
Sven Eckelmanna5130882012-05-16 20:23:16 +0200478 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
479 tt_local_entry->common.flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Antonio Quartulli015758d2011-07-09 17:52:13 +0200481 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200483 * response issued before the net ttvn increment (consistency check)
484 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200485 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100486
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200487 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200488 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry->common.addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490}
491
Sven Eckelmann56303d32012-06-05 22:31:31 +0200492void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200493 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200495 struct batadv_tt_local_entry *tt_local_entry = NULL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200496 uint16_t flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497
Sven Eckelmanna5130882012-05-16 20:23:16 +0200498 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200499 if (!tt_local_entry)
500 goto out;
501
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200502 flags = BATADV_TT_CLIENT_DEL;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200503 if (roaming)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200504 flags |= BATADV_TT_CLIENT_ROAM;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200505
506 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200507out:
508 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200509 batadv_tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510}
511
Sven Eckelmann56303d32012-06-05 22:31:31 +0200512static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200513 struct hlist_head *head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200515 struct batadv_tt_local_entry *tt_local_entry;
516 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000517 struct hlist_node *node, *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200518
519 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
520 hash_entry) {
521 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200522 struct batadv_tt_local_entry,
523 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200524 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
525 continue;
526
527 /* entry already marked for deletion */
528 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
529 continue;
530
531 if (!batadv_has_timed_out(tt_local_entry->last_seen,
532 BATADV_TT_LOCAL_TIMEOUT))
533 continue;
534
535 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
536 BATADV_TT_CLIENT_DEL, "timed out");
537 }
538}
539
Sven Eckelmann56303d32012-06-05 22:31:31 +0200540static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200541{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200542 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200544 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +0200545 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547 for (i = 0; i < hash->size; i++) {
548 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200549 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200551 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200552 batadv_tt_local_purge_list(bat_priv, head);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200553 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554 }
555
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556}
557
Sven Eckelmann56303d32012-06-05 22:31:31 +0200558static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200560 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200561 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200562 struct batadv_tt_common_entry *tt_common_entry;
563 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200564 struct hlist_node *node, *node_tmp;
565 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200566 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200567
Sven Eckelmann807736f2012-07-15 22:26:51 +0200568 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 return;
570
Sven Eckelmann807736f2012-07-15 22:26:51 +0200571 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200572
573 for (i = 0; i < hash->size; i++) {
574 head = &hash->table[i];
575 list_lock = &hash->list_locks[i];
576
577 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100578 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200579 head, hash_entry) {
580 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200581 tt_local = container_of(tt_common_entry,
582 struct batadv_tt_local_entry,
583 common);
584 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200585 }
586 spin_unlock_bh(list_lock);
587 }
588
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200589 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200590
Sven Eckelmann807736f2012-07-15 22:26:51 +0200591 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592}
593
Sven Eckelmann56303d32012-06-05 22:31:31 +0200594static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200596 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200597 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Sven Eckelmann807736f2012-07-15 22:26:51 +0200599 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600
Sven Eckelmann807736f2012-07-15 22:26:51 +0200601 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200602 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Sven Eckelmann5346c352012-05-05 13:27:28 +0200604 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605}
606
Sven Eckelmann56303d32012-06-05 22:31:31 +0200607static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200608{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200609 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200610
Sven Eckelmann807736f2012-07-15 22:26:51 +0200611 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200612
Sven Eckelmann807736f2012-07-15 22:26:51 +0200613 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200614 list) {
615 list_del(&entry->list);
616 kfree(entry);
617 }
618
Sven Eckelmann807736f2012-07-15 22:26:51 +0200619 atomic_set(&bat_priv->tt.local_changes, 0);
620 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200621}
622
Antonio Quartullid657e622012-07-01 14:09:12 +0200623/* retrieves the orig_tt_list_entry belonging to orig_node from the
624 * batadv_tt_global_entry list
625 *
626 * returns it with an increased refcounter, NULL if not found
627 */
628static struct batadv_tt_orig_list_entry *
629batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
630 const struct batadv_orig_node *orig_node)
631{
632 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
633 const struct hlist_head *head;
634 struct hlist_node *node;
635
636 rcu_read_lock();
637 head = &entry->orig_list;
638 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
639 if (tmp_orig_entry->orig_node != orig_node)
640 continue;
641 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
642 continue;
643
644 orig_entry = tmp_orig_entry;
645 break;
646 }
647 rcu_read_unlock();
648
649 return orig_entry;
650}
651
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200652/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +0200653 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200654 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200655static bool
656batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
657 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200658{
Antonio Quartullid657e622012-07-01 14:09:12 +0200659 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200660 bool found = false;
661
Antonio Quartullid657e622012-07-01 14:09:12 +0200662 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
663 if (orig_entry) {
664 found = true;
665 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200666 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200667
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200668 return found;
669}
670
Sven Eckelmanna5130882012-05-16 20:23:16 +0200671static void
Antonio Quartullid657e622012-07-01 14:09:12 +0200672batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200673 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200674{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200676
Antonio Quartullid657e622012-07-01 14:09:12 +0200677 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200678 if (orig_entry) {
679 /* refresh the ttvn: the current value could be a bogus one that
680 * was added during a "temporary client detection"
681 */
682 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200683 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200684 }
Antonio Quartullid657e622012-07-01 14:09:12 +0200685
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200686 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
687 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +0200688 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200689
690 INIT_HLIST_NODE(&orig_entry->list);
691 atomic_inc(&orig_node->refcount);
692 atomic_inc(&orig_node->tt_size);
693 orig_entry->orig_node = orig_node;
694 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +0200695 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200696
Antonio Quartullid657e622012-07-01 14:09:12 +0200697 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200698 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +0200699 &tt_global->orig_list);
700 spin_unlock_bh(&tt_global->list_lock);
701out:
702 if (orig_entry)
703 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200704}
705
Antonio Quartullia73105b2011-04-27 14:27:44 +0200706/* caller must hold orig_node refcount */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200707int batadv_tt_global_add(struct batadv_priv *bat_priv,
708 struct batadv_orig_node *orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200709 const unsigned char *tt_addr, uint8_t flags,
710 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200712 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200713 int ret = 0;
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100714 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200715 struct batadv_tt_common_entry *common;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Sven Eckelmanna5130882012-05-16 20:23:16 +0200717 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718
Antonio Quartullia73105b2011-04-27 14:27:44 +0200719 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +0200720 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200721 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200722 goto out;
723
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200724 common = &tt_global_entry->common;
725 memcpy(common->addr, tt_addr, ETH_ALEN);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200726
Antonio Quartullid4f44692012-05-25 00:00:54 +0200727 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200728 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +0200729 /* node must store current time in case of roaming. This is
730 * needed to purge this entry out on timeout (if nobody claims
731 * it)
732 */
733 if (flags & BATADV_TT_CLIENT_ROAM)
734 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200735 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200736 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200737
738 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
739 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200740
Sven Eckelmann807736f2012-07-15 22:26:51 +0200741 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200742 batadv_compare_tt,
743 batadv_choose_orig, common,
744 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100745
746 if (unlikely(hash_added != 0)) {
747 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200748 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100749 goto out_remove;
750 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200751 } else {
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200752 /* If there is already a global entry, we can use this one for
753 * our processing.
754 * But if we are trying to add a temporary client we can exit
755 * directly because the temporary information should never
756 * override any already known client state (whatever it is)
757 */
758 if (flags & BATADV_TT_CLIENT_TEMP)
759 goto out;
760
761 /* if the client was temporary added before receiving the first
762 * OGM announcing it, we have to clear the TEMP flag
763 */
764 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200765
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200766 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
767 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200768 * delete + roaming change for this originator.
769 *
770 * We should first delete the old originator before adding the
771 * new one.
772 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200773 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200774 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200775 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200776 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777 }
778 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200779 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +0200780 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200781
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200782 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200783 "Creating new global tt entry: %pM (via %pM)\n",
784 tt_global_entry->common.addr, orig_node->orig);
Antonio Quartullic10dba02012-08-11 11:11:00 +0200785 ret = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200786
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100787out_remove:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200788 /* remove address from local hash if present */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200789 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200790 "global tt received",
791 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200792out:
793 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200794 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200795 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796}
797
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200798/* print all orig nodes who announce the address for this global entry.
799 * it is assumed that the caller holds rcu_read_lock();
800 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200801static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200802batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200803 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200804{
805 struct hlist_head *head;
806 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200807 struct batadv_tt_orig_list_entry *orig_entry;
808 struct batadv_tt_common_entry *tt_common_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200809 uint16_t flags;
810 uint8_t last_ttvn;
811
812 tt_common_entry = &tt_global_entry->common;
813
814 head = &tt_global_entry->orig_list;
815
816 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
817 flags = tt_common_entry->flags;
818 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200819 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200820 tt_global_entry->common.addr, orig_entry->ttvn,
821 orig_entry->orig_node->orig, last_ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200822 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +0200823 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
824 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200825 }
826}
827
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200828int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000829{
830 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200831 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200832 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200833 struct batadv_tt_common_entry *tt_common_entry;
834 struct batadv_tt_global_entry *tt_global;
835 struct batadv_hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000836 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200838 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839
Marek Lindner30da63a2012-08-03 17:15:46 +0200840 primary_if = batadv_seq_print_text_primary_if_get(seq);
841 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200842 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000843
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200844 seq_printf(seq,
845 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200847 seq_printf(seq, " %-13s %s %-15s %s %s\n",
848 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 for (i = 0; i < hash->size; i++) {
851 head = &hash->table[i];
852
Marek Lindner7aadf882011-02-18 12:28:09 +0000853 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100854 hlist_for_each_entry_rcu(tt_common_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000855 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200856 tt_global = container_of(tt_common_entry,
857 struct batadv_tt_global_entry,
858 common);
859 batadv_tt_global_print_entry(tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000860 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000861 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200863out:
864 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200865 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200866 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867}
868
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200869/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200870static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200871batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200873 struct hlist_head *head;
874 struct hlist_node *node, *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200875 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200876
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200877 spin_lock_bh(&tt_global_entry->list_lock);
878 head = &tt_global_entry->orig_list;
879 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
880 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200881 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200882 }
883 spin_unlock_bh(&tt_global_entry->list_lock);
884
885}
886
Sven Eckelmanna5130882012-05-16 20:23:16 +0200887static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200888batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
889 struct batadv_tt_global_entry *tt_global_entry,
890 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200891 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200892{
893 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;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200896
897 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 if (orig_entry->orig_node == orig_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200901 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200902 "Deleting %pM from global tt entry %pM: %s\n",
903 orig_node->orig,
904 tt_global_entry->common.addr, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200905 hlist_del_rcu(node);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200906 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200907 }
908 }
909 spin_unlock_bh(&tt_global_entry->list_lock);
910}
911
Sven Eckelmann56303d32012-06-05 22:31:31 +0200912static void
913batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
914 struct batadv_tt_global_entry *tt_global_entry,
915 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200916{
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200917 batadv_dbg(BATADV_DBG_TT, bat_priv,
918 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200919 tt_global_entry->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200920
Sven Eckelmann807736f2012-07-15 22:26:51 +0200921 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Sven Eckelmannda641192012-05-12 13:48:56 +0200922 batadv_choose_orig, tt_global_entry->common.addr);
Sven Eckelmanna5130882012-05-16 20:23:16 +0200923 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200924
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000925}
926
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200927/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200928 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
929 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200930 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200931static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
933 struct batadv_tt_global_entry *tt_global_entry,
934 struct batadv_orig_node *orig_node,
935 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200936{
937 bool last_entry = true;
938 struct hlist_head *head;
939 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200940 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200941
942 /* no local entry exists, case 1:
943 * Check if this is the last one or if other entries exist.
944 */
945
946 rcu_read_lock();
947 head = &tt_global_entry->orig_list;
948 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
949 if (orig_entry->orig_node != orig_node) {
950 last_entry = false;
951 break;
952 }
953 }
954 rcu_read_unlock();
955
956 if (last_entry) {
957 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200958 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200959 tt_global_entry->roam_at = jiffies;
960 } else
961 /* there is another entry, we can simply delete this
962 * one and can still use the other one.
963 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200964 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
965 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200966}
967
968
969
Sven Eckelmann56303d32012-06-05 22:31:31 +0200970static void batadv_tt_global_del(struct batadv_priv *bat_priv,
971 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200972 const unsigned char *addr,
973 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000974{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200975 struct batadv_tt_global_entry *tt_global_entry = NULL;
976 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977
Sven Eckelmanna5130882012-05-16 20:23:16 +0200978 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200979 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200980 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200981
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200982 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200983 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
984 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800985
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200986 if (hlist_empty(&tt_global_entry->orig_list))
Sven Eckelmanna5130882012-05-16 20:23:16 +0200987 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
988 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200989
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800990 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200991 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +0800992
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200993 /* if we are deleting a global entry due to a roam
994 * event, there are two possibilities:
995 * 1) the client roamed from node A to node B => if there
996 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200997 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200998 * wait for node B to claim it. In case of timeout
999 * the entry is purged.
1000 *
1001 * If there are other originators left, we directly delete
1002 * the originator.
1003 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001004 * the global entry, since it is useless now.
1005 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001006 local_entry = batadv_tt_local_hash_find(bat_priv,
1007 tt_global_entry->common.addr);
1008 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001009 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001010 batadv_tt_global_del_orig_list(tt_global_entry);
1011 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001012 } else
1013 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001014 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1015 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001016
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001017
Antonio Quartullicc47f662011-04-27 14:27:57 +02001018out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001019 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001020 batadv_tt_global_entry_free_ref(tt_global_entry);
1021 if (local_entry)
1022 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001023}
1024
Sven Eckelmann56303d32012-06-05 22:31:31 +02001025void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1026 struct batadv_orig_node *orig_node,
1027 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001028{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001029 struct batadv_tt_global_entry *tt_global;
1030 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001031 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001032 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001033 struct hlist_node *node, *safe;
1034 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001035 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001036
Simon Wunderlich6e801492011-10-19 10:28:26 +02001037 if (!hash)
1038 return;
1039
Antonio Quartullia73105b2011-04-27 14:27:44 +02001040 for (i = 0; i < hash->size; i++) {
1041 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001042 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001044 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001045 hlist_for_each_entry_safe(tt_common_entry, node, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001046 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001047 tt_global = container_of(tt_common_entry,
1048 struct batadv_tt_global_entry,
1049 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001050
Sven Eckelmann56303d32012-06-05 22:31:31 +02001051 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001052 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001053
Sven Eckelmann56303d32012-06-05 22:31:31 +02001054 if (hlist_empty(&tt_global->orig_list)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001055 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001056 "Deleting global tt entry %pM: %s\n",
Sven Eckelmann56303d32012-06-05 22:31:31 +02001057 tt_global->common.addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001058 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001059 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001060 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001061 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001062 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001064 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001065}
1066
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001067static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1068 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001069{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001070 bool purge = false;
1071 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1072 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001073
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001074 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1075 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1076 purge = true;
1077 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001078 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001079
1080 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1081 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1082 purge = true;
1083 *msg = "Temporary client timeout\n";
1084 }
1085
1086 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001087}
1088
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001089static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001090{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001091 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001092 struct hlist_head *head;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001093 struct hlist_node *node, *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001094 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001095 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001096 char *msg = NULL;
1097 struct batadv_tt_common_entry *tt_common;
1098 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001099
Antonio Quartullicc47f662011-04-27 14:27:57 +02001100 for (i = 0; i < hash->size; i++) {
1101 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001102 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001103
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001104 spin_lock_bh(list_lock);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001105 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1106 hash_entry) {
1107 tt_global = container_of(tt_common,
1108 struct batadv_tt_global_entry,
1109 common);
1110
1111 if (!batadv_tt_global_to_purge(tt_global, &msg))
1112 continue;
1113
1114 batadv_dbg(BATADV_DBG_TT, bat_priv,
1115 "Deleting global tt entry (%pM): %s\n",
1116 tt_global->common.addr, msg);
1117
1118 hlist_del_rcu(node);
1119
1120 batadv_tt_global_entry_free_ref(tt_global);
1121 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001122 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001123 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001124}
1125
Sven Eckelmann56303d32012-06-05 22:31:31 +02001126static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001127{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001128 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001129 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001130 struct batadv_tt_common_entry *tt_common_entry;
1131 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001132 struct hlist_node *node, *node_tmp;
1133 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001134 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001135
Sven Eckelmann807736f2012-07-15 22:26:51 +02001136 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001137 return;
1138
Sven Eckelmann807736f2012-07-15 22:26:51 +02001139 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001140
1141 for (i = 0; i < hash->size; i++) {
1142 head = &hash->table[i];
1143 list_lock = &hash->list_locks[i];
1144
1145 spin_lock_bh(list_lock);
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001146 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001147 head, hash_entry) {
1148 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001149 tt_global = container_of(tt_common_entry,
1150 struct batadv_tt_global_entry,
1151 common);
1152 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001153 }
1154 spin_unlock_bh(list_lock);
1155 }
1156
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001157 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001158
Sven Eckelmann807736f2012-07-15 22:26:51 +02001159 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001160}
1161
Sven Eckelmann56303d32012-06-05 22:31:31 +02001162static bool
1163_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1164 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001165{
1166 bool ret = false;
1167
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001168 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1169 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001170 ret = true;
1171
1172 return ret;
1173}
1174
Sven Eckelmann56303d32012-06-05 22:31:31 +02001175struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1176 const uint8_t *src,
1177 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001179 struct batadv_tt_local_entry *tt_local_entry = NULL;
1180 struct batadv_tt_global_entry *tt_global_entry = NULL;
1181 struct batadv_orig_node *orig_node = NULL;
1182 struct batadv_neigh_node *router = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001183 struct hlist_head *head;
1184 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001185 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001186 int best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001187
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001188 if (src && atomic_read(&bat_priv->ap_isolation)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001189 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001190 if (!tt_local_entry)
1191 goto out;
1192 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001193
Sven Eckelmanna5130882012-05-16 20:23:16 +02001194 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001195 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001196 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001197
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001198 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001199 * isolation
1200 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001201 if (tt_local_entry &&
1202 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001203 goto out;
1204
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001205 best_tq = 0;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001206
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001207 rcu_read_lock();
1208 head = &tt_global_entry->orig_list;
1209 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001210 router = batadv_orig_node_get_router(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001211 if (!router)
1212 continue;
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001213
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001214 if (router->tq_avg > best_tq) {
1215 orig_node = orig_entry->orig_node;
1216 best_tq = router->tq_avg;
1217 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001218 batadv_neigh_node_free_ref(router);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001219 }
1220 /* found anything? */
1221 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1222 orig_node = NULL;
1223 rcu_read_unlock();
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001224out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001225 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001226 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001227 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001228 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001229
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001230 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001231}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001232
1233/* Calculates the checksum of the local table of a given orig_node */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001234static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1235 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001236{
1237 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001238 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001239 struct batadv_tt_common_entry *tt_common;
1240 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001241 struct hlist_node *node;
1242 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001243 uint32_t i;
1244 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001245
1246 for (i = 0; i < hash->size; i++) {
1247 head = &hash->table[i];
1248
1249 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001250 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001251 tt_global = container_of(tt_common,
1252 struct batadv_tt_global_entry,
1253 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001254 /* Roaming clients are in the global table for
1255 * consistency only. They don't have to be
1256 * taken into account while computing the
1257 * global crc
1258 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001259 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001260 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001261 /* Temporary clients have not been announced yet, so
1262 * they have to be skipped while computing the global
1263 * crc
1264 */
1265 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1266 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267
1268 /* find out if this global entry is announced by this
1269 * originator
1270 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001271 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001272 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001273 continue;
1274
1275 total_one = 0;
1276 for (j = 0; j < ETH_ALEN; j++)
1277 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001278 tt_common->addr[j]);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001279 total ^= total_one;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001280 }
1281 rcu_read_unlock();
1282 }
1283
1284 return total;
1285}
1286
1287/* Calculates the checksum of the local table */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001288static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001289{
1290 uint16_t total = 0, total_one;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001291 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001292 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001293 struct hlist_node *node;
1294 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001295 uint32_t i;
1296 int j;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001297
1298 for (i = 0; i < hash->size; i++) {
1299 head = &hash->table[i];
1300
1301 rcu_read_lock();
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001302 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001303 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001304 * account while computing the CRC
1305 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001306 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001307 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001308 total_one = 0;
1309 for (j = 0; j < ETH_ALEN; j++)
1310 total_one = crc16_byte(total_one,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001311 tt_common->addr[j]);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001312 total ^= total_one;
1313 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001314 rcu_read_unlock();
1315 }
1316
1317 return total;
1318}
1319
Sven Eckelmann56303d32012-06-05 22:31:31 +02001320static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001321{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001322 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001323
Sven Eckelmann807736f2012-07-15 22:26:51 +02001324 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001325
Sven Eckelmann807736f2012-07-15 22:26:51 +02001326 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001327 list_del(&node->list);
1328 kfree(node);
1329 }
1330
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001332}
1333
Sven Eckelmann56303d32012-06-05 22:31:31 +02001334static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1335 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001336 const unsigned char *tt_buff,
1337 uint8_t tt_num_changes)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001338{
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001339 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340
1341 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001342 * last OGM (the OGM could carry no changes)
1343 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001344 spin_lock_bh(&orig_node->tt_buff_lock);
1345 if (tt_buff_len > 0) {
1346 kfree(orig_node->tt_buff);
1347 orig_node->tt_buff_len = 0;
1348 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1349 if (orig_node->tt_buff) {
1350 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1351 orig_node->tt_buff_len = tt_buff_len;
1352 }
1353 }
1354 spin_unlock_bh(&orig_node->tt_buff_lock);
1355}
1356
Sven Eckelmann56303d32012-06-05 22:31:31 +02001357static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001358{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001359 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001360
Sven Eckelmann807736f2012-07-15 22:26:51 +02001361 spin_lock_bh(&bat_priv->tt.req_list_lock);
1362 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001363 if (batadv_has_timed_out(node->issued_at,
1364 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001365 list_del(&node->list);
1366 kfree(node);
1367 }
1368 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001369 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001370}
1371
1372/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001373 * has already been issued for this orig_node, NULL otherwise
1374 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001375static struct batadv_tt_req_node *
1376batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1377 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001378{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
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(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001383 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1384 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001385 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001386 goto unlock;
1387 }
1388
1389 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1390 if (!tt_req_node)
1391 goto unlock;
1392
1393 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1394 tt_req_node->issued_at = jiffies;
1395
Sven Eckelmann807736f2012-07-15 22:26:51 +02001396 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001397unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001398 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001399 return tt_req_node;
1400}
1401
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001402/* data_ptr is useless here, but has to be kept to respect the prototype */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001403static int batadv_tt_local_valid_entry(const void *entry_ptr,
1404 const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001405{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001406 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001407
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001408 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001409 return 0;
1410 return 1;
1411}
1412
Sven Eckelmanna5130882012-05-16 20:23:16 +02001413static int batadv_tt_global_valid(const void *entry_ptr,
1414 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001416 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1417 const struct batadv_tt_global_entry *tt_global_entry;
1418 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001419
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001420 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1421 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001422 return 0;
1423
Sven Eckelmann56303d32012-06-05 22:31:31 +02001424 tt_global_entry = container_of(tt_common_entry,
1425 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001426 common);
1427
Sven Eckelmanna5130882012-05-16 20:23:16 +02001428 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001429}
1430
Sven Eckelmanna5130882012-05-16 20:23:16 +02001431static struct sk_buff *
1432batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001433 struct batadv_hashtable *hash,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001434 struct batadv_hard_iface *primary_if,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001435 int (*valid_cb)(const void *, const void *),
1436 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001437{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001438 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann96412692012-06-05 22:31:30 +02001439 struct batadv_tt_query_packet *tt_response;
1440 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001441 struct hlist_node *node;
1442 struct hlist_head *head;
1443 struct sk_buff *skb = NULL;
1444 uint16_t tt_tot, tt_count;
Sven Eckelmann96412692012-06-05 22:31:30 +02001445 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
Antonio Quartullic90681b2011-10-05 17:05:25 +02001446 uint32_t i;
Sven Eckelmann96412692012-06-05 22:31:30 +02001447 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001448
1449 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1450 tt_len = primary_if->soft_iface->mtu - tt_query_size;
Sven Eckelmann96412692012-06-05 22:31:30 +02001451 tt_len -= tt_len % sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001452 }
Sven Eckelmann96412692012-06-05 22:31:30 +02001453 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001454
Sven Eckelmann96412692012-06-05 22:31:30 +02001455 len = tt_query_size + tt_len;
1456 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001457 if (!skb)
1458 goto out;
1459
1460 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +02001461 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001462 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001463
Sven Eckelmann96412692012-06-05 22:31:30 +02001464 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001465 tt_count = 0;
1466
1467 rcu_read_lock();
1468 for (i = 0; i < hash->size; i++) {
1469 head = &hash->table[i];
1470
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001471 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001472 head, hash_entry) {
1473 if (tt_count == tt_tot)
1474 break;
1475
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001476 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001477 continue;
1478
Antonio Quartulli48100ba2011-10-30 12:17:33 +01001479 memcpy(tt_change->addr, tt_common_entry->addr,
1480 ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001481 tt_change->flags = BATADV_NO_FLAGS;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482
1483 tt_count++;
1484 tt_change++;
1485 }
1486 }
1487 rcu_read_unlock();
1488
Antonio Quartulli9d852392011-10-17 14:25:13 +02001489 /* store in the message the number of entries we have successfully
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001490 * copied
1491 */
Antonio Quartulli9d852392011-10-17 14:25:13 +02001492 tt_response->tt_data = htons(tt_count);
1493
Antonio Quartullia73105b2011-04-27 14:27:44 +02001494out:
1495 return skb;
1496}
1497
Sven Eckelmann56303d32012-06-05 22:31:31 +02001498static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1499 struct batadv_orig_node *dst_orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001500 uint8_t ttvn, uint16_t tt_crc,
1501 bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001502{
1503 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001504 struct batadv_tt_query_packet *tt_request;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001505 struct batadv_neigh_node *neigh_node = NULL;
1506 struct batadv_hard_iface *primary_if;
1507 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001508 int ret = 1;
Sven Eckelmann96412692012-06-05 22:31:30 +02001509 size_t tt_req_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001510
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001511 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001512 if (!primary_if)
1513 goto out;
1514
1515 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001516 * reply from the same orig_node yet
1517 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001518 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001519 if (!tt_req_node)
1520 goto out;
1521
Sven Eckelmann96412692012-06-05 22:31:30 +02001522 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001523 if (!skb)
1524 goto out;
1525
1526 skb_reserve(skb, ETH_HLEN);
1527
Sven Eckelmann96412692012-06-05 22:31:30 +02001528 tt_req_len = sizeof(*tt_request);
1529 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001530
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001531 tt_request->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001532 tt_request->header.version = BATADV_COMPAT_VERSION;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001533 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1534 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001535 tt_request->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001536 tt_request->ttvn = ttvn;
Antonio Quartulli6d2003f2012-04-14 13:15:27 +02001537 tt_request->tt_data = htons(tt_crc);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001538 tt_request->flags = BATADV_TT_REQUEST;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001539
1540 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001541 tt_request->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001542
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001543 neigh_node = batadv_orig_node_get_router(dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001544 if (!neigh_node)
1545 goto out;
1546
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001547 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001548 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1549 dst_orig_node->orig, neigh_node->addr,
1550 (full_table ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001551
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001552 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001553
Sven Eckelmann9455e342012-05-12 02:09:37 +02001554 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555 ret = 0;
1556
1557out:
1558 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001559 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001560 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001561 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001562 if (ret)
1563 kfree_skb(skb);
1564 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001565 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001566 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001567 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001568 kfree(tt_req_node);
1569 }
1570 return ret;
1571}
1572
Sven Eckelmann96412692012-06-05 22:31:30 +02001573static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001574batadv_send_other_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001575 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001577 struct batadv_orig_node *req_dst_orig_node = NULL;
1578 struct batadv_orig_node *res_dst_orig_node = NULL;
1579 struct batadv_neigh_node *neigh_node = NULL;
1580 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001581 uint8_t orig_ttvn, req_ttvn, ttvn;
1582 int ret = false;
1583 unsigned char *tt_buff;
1584 bool full_table;
1585 uint16_t tt_len, tt_tot;
1586 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001587 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001588 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001589 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001590
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001591 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001592 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1593 tt_request->src, tt_request->ttvn, tt_request->dst,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001594 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001595
1596 /* Let's get the orig node of the REAL destination */
Sven Eckelmannda641192012-05-12 13:48:56 +02001597 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001598 if (!req_dst_orig_node)
1599 goto out;
1600
Sven Eckelmannda641192012-05-12 13:48:56 +02001601 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001602 if (!res_dst_orig_node)
1603 goto out;
1604
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001605 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001606 if (!neigh_node)
1607 goto out;
1608
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001609 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001610 if (!primary_if)
1611 goto out;
1612
1613 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1614 req_ttvn = tt_request->ttvn;
1615
Antonio Quartulli015758d2011-07-09 17:52:13 +02001616 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001617 if (orig_ttvn != req_ttvn ||
Al Virof25bd582012-04-22 07:44:27 +01001618 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001619 goto out;
1620
Antonio Quartulli015758d2011-07-09 17:52:13 +02001621 /* If the full table has been explicitly requested */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001622 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02001623 !req_dst_orig_node->tt_buff)
1624 full_table = true;
1625 else
1626 full_table = false;
1627
1628 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001629 * I'll send only one packet with as much TT entries as I can
1630 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001631 if (!full_table) {
1632 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1633 tt_len = req_dst_orig_node->tt_buff_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001634 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635
Sven Eckelmann96412692012-06-05 22:31:30 +02001636 len = sizeof(*tt_response) + tt_len;
1637 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001638 if (!skb)
1639 goto unlock;
1640
1641 skb_reserve(skb, ETH_HLEN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001642 packet_pos = skb_put(skb, len);
1643 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001644 tt_response->ttvn = req_ttvn;
1645 tt_response->tt_data = htons(tt_tot);
1646
Sven Eckelmann96412692012-06-05 22:31:30 +02001647 tt_buff = skb->data + sizeof(*tt_response);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001648 /* Copy the last orig_node's OGM buffer */
1649 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1650 req_dst_orig_node->tt_buff_len);
1651
1652 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1653 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +02001654 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1655 tt_len *= sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001656 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1657
Sven Eckelmanna5130882012-05-16 20:23:16 +02001658 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001659 bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001660 primary_if,
1661 batadv_tt_global_valid,
1662 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001663 if (!skb)
1664 goto out;
1665
Sven Eckelmann96412692012-06-05 22:31:30 +02001666 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001667 }
1668
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001669 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001670 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001671 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001672 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1673 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001674 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001675
1676 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001677 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001678
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001679 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001680 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1681 res_dst_orig_node->orig, neigh_node->addr,
1682 req_dst_orig_node->orig, req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001683
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001684 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001685
Sven Eckelmann9455e342012-05-12 02:09:37 +02001686 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001687 ret = true;
1688 goto out;
1689
1690unlock:
1691 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1692
1693out:
1694 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001695 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001696 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001697 batadv_orig_node_free_ref(req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001698 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001699 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001700 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001701 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001702 if (!ret)
1703 kfree_skb(skb);
1704 return ret;
1705
1706}
Sven Eckelmann96412692012-06-05 22:31:30 +02001707
1708static bool
Sven Eckelmann56303d32012-06-05 22:31:31 +02001709batadv_send_my_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001710 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001711{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001712 struct batadv_orig_node *orig_node = NULL;
1713 struct batadv_neigh_node *neigh_node = NULL;
1714 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001715 uint8_t my_ttvn, req_ttvn, ttvn;
1716 int ret = false;
1717 unsigned char *tt_buff;
1718 bool full_table;
1719 uint16_t tt_len, tt_tot;
1720 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001721 struct batadv_tt_query_packet *tt_response;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001722 uint8_t *packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +02001723 size_t len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001724
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001725 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001726 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1727 tt_request->src, tt_request->ttvn,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001728 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001729
1730
Sven Eckelmann807736f2012-07-15 22:26:51 +02001731 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001732 req_ttvn = tt_request->ttvn;
1733
Sven Eckelmannda641192012-05-12 13:48:56 +02001734 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001735 if (!orig_node)
1736 goto out;
1737
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001738 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001739 if (!neigh_node)
1740 goto out;
1741
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001742 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743 if (!primary_if)
1744 goto out;
1745
1746 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001747 * is too big send the whole local translation table
1748 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001749 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02001750 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001751 full_table = true;
1752 else
1753 full_table = false;
1754
1755 /* In this version, fragmentation is not implemented, then
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001756 * I'll send only one packet with as much TT entries as I can
1757 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001758 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001759 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1760 tt_len = bat_priv->tt.last_changeset_len;
Sven Eckelmann96412692012-06-05 22:31:30 +02001761 tt_tot = tt_len / sizeof(struct batadv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001762
Sven Eckelmann96412692012-06-05 22:31:30 +02001763 len = sizeof(*tt_response) + tt_len;
1764 skb = dev_alloc_skb(len + ETH_HLEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001765 if (!skb)
1766 goto unlock;
1767
1768 skb_reserve(skb, ETH_HLEN);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001769 packet_pos = skb_put(skb, len);
1770 tt_response = (struct batadv_tt_query_packet *)packet_pos;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001771 tt_response->ttvn = req_ttvn;
1772 tt_response->tt_data = htons(tt_tot);
1773
Sven Eckelmann96412692012-06-05 22:31:30 +02001774 tt_buff = skb->data + sizeof(*tt_response);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001775 memcpy(tt_buff, bat_priv->tt.last_changeset,
1776 bat_priv->tt.last_changeset_len);
1777 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001778 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001779 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
Sven Eckelmann96412692012-06-05 22:31:30 +02001780 tt_len *= sizeof(struct batadv_tt_change);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001781 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001782
Sven Eckelmanna5130882012-05-16 20:23:16 +02001783 skb = batadv_tt_response_fill_table(tt_len, ttvn,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001784 bat_priv->tt.local_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001785 primary_if,
1786 batadv_tt_local_valid_entry,
1787 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788 if (!skb)
1789 goto out;
1790
Sven Eckelmann96412692012-06-05 22:31:30 +02001791 tt_response = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001792 }
1793
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001794 tt_response->header.packet_type = BATADV_TT_QUERY;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001795 tt_response->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001796 tt_response->header.ttl = BATADV_TTL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001797 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1798 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001799 tt_response->flags = BATADV_TT_RESPONSE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001800
1801 if (full_table)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001802 tt_response->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001803
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001804 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001805 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1806 orig_node->orig, neigh_node->addr,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001807 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001808
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001809 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02001810
Sven Eckelmann9455e342012-05-12 02:09:37 +02001811 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812 ret = true;
1813 goto out;
1814
1815unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02001816 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001817out:
1818 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001819 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001820 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001821 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001822 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001823 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001824 if (!ret)
1825 kfree_skb(skb);
1826 /* This packet was for me, so it doesn't need to be re-routed */
1827 return true;
1828}
1829
Sven Eckelmann56303d32012-06-05 22:31:31 +02001830bool batadv_send_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001831 struct batadv_tt_query_packet *tt_request)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001832{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001833 if (batadv_is_my_mac(tt_request->dst)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001834 /* don't answer backbone gws! */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001835 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001836 return true;
1837
Sven Eckelmanna5130882012-05-16 20:23:16 +02001838 return batadv_send_my_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001839 } else {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001840 return batadv_send_other_tt_response(bat_priv, tt_request);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001841 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001842}
1843
Sven Eckelmann56303d32012-06-05 22:31:31 +02001844static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1845 struct batadv_orig_node *orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001846 struct batadv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001847 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001848{
1849 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001850 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001851
1852 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001853 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1854 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02001855 batadv_tt_global_del(bat_priv, orig_node,
1856 (tt_change + i)->addr,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001857 "tt removed by changes",
1858 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001859 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001860 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02001861 (tt_change + i)->addr,
1862 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001863 /* In case of problem while storing a
1864 * global_entry, we stop the updating
1865 * procedure without committing the
1866 * ttvn change. This will avoid to send
1867 * corrupted data on tt_request
1868 */
1869 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001870 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001871 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001872 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001873}
1874
Sven Eckelmann56303d32012-06-05 22:31:31 +02001875static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001876 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001877{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001878 struct batadv_orig_node *orig_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001879
Sven Eckelmannda641192012-05-12 13:48:56 +02001880 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001881 if (!orig_node)
1882 goto out;
1883
1884 /* Purge the old table first.. */
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001885 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02001886
Sven Eckelmanna5130882012-05-16 20:23:16 +02001887 _batadv_tt_update_changes(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001888 (struct batadv_tt_change *)(tt_response + 1),
Sven Eckelmanna5130882012-05-16 20:23:16 +02001889 ntohs(tt_response->tt_data),
1890 tt_response->ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001891
1892 spin_lock_bh(&orig_node->tt_buff_lock);
1893 kfree(orig_node->tt_buff);
1894 orig_node->tt_buff_len = 0;
1895 orig_node->tt_buff = NULL;
1896 spin_unlock_bh(&orig_node->tt_buff_lock);
1897
1898 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1899
1900out:
1901 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001902 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001903}
1904
Sven Eckelmann56303d32012-06-05 22:31:31 +02001905static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1906 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001907 uint16_t tt_num_changes, uint8_t ttvn,
Sven Eckelmann96412692012-06-05 22:31:30 +02001908 struct batadv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909{
Sven Eckelmanna5130882012-05-16 20:23:16 +02001910 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1911 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001912
Sven Eckelmanna5130882012-05-16 20:23:16 +02001913 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1914 (unsigned char *)tt_change, tt_num_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001915 atomic_set(&orig_node->last_ttvn, ttvn);
1916}
1917
Sven Eckelmann56303d32012-06-05 22:31:31 +02001918bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001919{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001920 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001921 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001922
Sven Eckelmanna5130882012-05-16 20:23:16 +02001923 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001924 if (!tt_local_entry)
1925 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001926 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001927 * consistency purpose)
1928 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001929 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001930 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001931 ret = true;
1932out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001933 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001934 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001935 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001936}
1937
Sven Eckelmann56303d32012-06-05 22:31:31 +02001938void batadv_handle_tt_response(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001939 struct batadv_tt_query_packet *tt_response)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001940{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001941 struct batadv_tt_req_node *node, *safe;
1942 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001943 struct batadv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001944
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001945 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001946 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1947 tt_response->src, tt_response->ttvn,
1948 ntohs(tt_response->tt_data),
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001949 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001950
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001951 /* we should have never asked a backbone gw */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001952 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001953 goto out;
1954
Sven Eckelmannda641192012-05-12 13:48:56 +02001955 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956 if (!orig_node)
1957 goto out;
1958
Sven Eckelmann96412692012-06-05 22:31:30 +02001959 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001960 batadv_tt_fill_gtable(bat_priv, tt_response);
Sven Eckelmann96412692012-06-05 22:31:30 +02001961 } else {
1962 tt_change = (struct batadv_tt_change *)(tt_response + 1);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001963 batadv_tt_update_changes(bat_priv, orig_node,
1964 ntohs(tt_response->tt_data),
Sven Eckelmann96412692012-06-05 22:31:30 +02001965 tt_response->ttvn, tt_change);
1966 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001967
1968 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001969 spin_lock_bh(&bat_priv->tt.req_list_lock);
1970 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001971 if (!batadv_compare_eth(node->addr, tt_response->src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001972 continue;
1973 list_del(&node->list);
1974 kfree(node);
1975 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001976 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001977
1978 /* Recalculate the CRC for this orig_node and store it */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001979 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001980 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001981 * unset the flag
1982 */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001983 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001984out:
1985 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001986 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001987}
1988
Sven Eckelmann56303d32012-06-05 22:31:31 +02001989int batadv_tt_init(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001990{
Sven Eckelmann5346c352012-05-05 13:27:28 +02001991 int ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001992
Sven Eckelmanna5130882012-05-16 20:23:16 +02001993 ret = batadv_tt_local_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001994 if (ret < 0)
1995 return ret;
1996
Sven Eckelmanna5130882012-05-16 20:23:16 +02001997 ret = batadv_tt_global_init(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001998 if (ret < 0)
1999 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002000
Sven Eckelmanna5130882012-05-16 20:23:16 +02002001 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002002
2003 return 1;
2004}
2005
Sven Eckelmann56303d32012-06-05 22:31:31 +02002006static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002007{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002008 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002009
Sven Eckelmann807736f2012-07-15 22:26:51 +02002010 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002011
Sven Eckelmann807736f2012-07-15 22:26:51 +02002012 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002013 list_del(&node->list);
2014 kfree(node);
2015 }
2016
Sven Eckelmann807736f2012-07-15 22:26:51 +02002017 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002018}
2019
Sven Eckelmann56303d32012-06-05 22:31:31 +02002020static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002021{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002022 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002023
Sven Eckelmann807736f2012-07-15 22:26:51 +02002024 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2025 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002026 if (!batadv_has_timed_out(node->first_time,
2027 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002028 continue;
2029
2030 list_del(&node->list);
2031 kfree(node);
2032 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002033 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002034}
2035
2036/* This function checks whether the client already reached the
2037 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2038 * will not be sent.
2039 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002040 * returns true if the ROAMING_ADV can be sent, false otherwise
2041 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002042static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002043 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002044{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002045 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002046 bool ret = false;
2047
Sven Eckelmann807736f2012-07-15 22:26:51 +02002048 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002049 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002050 * reply from the same orig_node yet
2051 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002052 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002053 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002054 continue;
2055
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002056 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002057 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002058 continue;
2059
Sven Eckelmann3e348192012-05-16 20:23:22 +02002060 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002061 /* Sorry, you roamed too many times! */
2062 goto unlock;
2063 ret = true;
2064 break;
2065 }
2066
2067 if (!ret) {
2068 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2069 if (!tt_roam_node)
2070 goto unlock;
2071
2072 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002073 atomic_set(&tt_roam_node->counter,
2074 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002075 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2076
Sven Eckelmann807736f2012-07-15 22:26:51 +02002077 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002078 ret = true;
2079 }
2080
2081unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002082 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002083 return ret;
2084}
2085
Sven Eckelmann56303d32012-06-05 22:31:31 +02002086static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2087 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002088{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002089 struct batadv_neigh_node *neigh_node = NULL;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002090 struct sk_buff *skb = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02002091 struct batadv_roam_adv_packet *roam_adv_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002092 int ret = 1;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002093 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +02002094 size_t len = sizeof(*roam_adv_packet);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002095
2096 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002097 * already roamed to us too many times
2098 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002099 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002100 goto out;
2101
Sven Eckelmann96412692012-06-05 22:31:30 +02002102 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002103 if (!skb)
2104 goto out;
2105
2106 skb_reserve(skb, ETH_HLEN);
2107
Sven Eckelmann96412692012-06-05 22:31:30 +02002108 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002109
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002110 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
Sven Eckelmann7e071c72012-06-03 22:19:13 +02002111 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002112 roam_adv_packet->header.ttl = BATADV_TTL;
Sven Eckelmann162d5492012-06-28 11:56:52 +02002113 roam_adv_packet->reserved = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002114 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002115 if (!primary_if)
2116 goto out;
2117 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002118 batadv_hardif_free_ref(primary_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002119 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2120 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2121
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002122 neigh_node = batadv_orig_node_get_router(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002123 if (!neigh_node)
2124 goto out;
2125
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002126 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002127 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2128 orig_node->orig, client, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002129
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002130 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002131
Sven Eckelmann9455e342012-05-12 02:09:37 +02002132 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002133 ret = 0;
2134
2135out:
2136 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002137 batadv_neigh_node_free_ref(neigh_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002138 if (ret)
2139 kfree_skb(skb);
2140 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002141}
2142
Sven Eckelmanna5130882012-05-16 20:23:16 +02002143static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002145 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002146 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002147 struct batadv_priv *bat_priv;
2148
2149 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002150 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2151 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002152
Sven Eckelmanna5130882012-05-16 20:23:16 +02002153 batadv_tt_local_purge(bat_priv);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002154 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002155 batadv_tt_req_purge(bat_priv);
2156 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002157
Sven Eckelmanna5130882012-05-16 20:23:16 +02002158 batadv_tt_start_timer(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002160
Sven Eckelmann56303d32012-06-05 22:31:31 +02002161void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002162{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002163 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002164
Sven Eckelmanna5130882012-05-16 20:23:16 +02002165 batadv_tt_local_table_free(bat_priv);
2166 batadv_tt_global_table_free(bat_priv);
2167 batadv_tt_req_list_free(bat_priv);
2168 batadv_tt_changes_list_free(bat_priv);
2169 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002170
Sven Eckelmann807736f2012-07-15 22:26:51 +02002171 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002172}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002173
Antonio Quartulli697f2532011-11-07 16:47:01 +01002174/* This function will enable or disable the specified flags for all the entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002175 * in the given hash table and returns the number of modified entries
2176 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002177static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2178 uint16_t flags, bool enable)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002179{
Antonio Quartullic90681b2011-10-05 17:05:25 +02002180 uint32_t i;
Antonio Quartulli697f2532011-11-07 16:47:01 +01002181 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002182 struct hlist_head *head;
2183 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002184 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002185
2186 if (!hash)
Antonio Quartulli697f2532011-11-07 16:47:01 +01002187 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002188
2189 for (i = 0; i < hash->size; i++) {
2190 head = &hash->table[i];
2191
2192 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002193 hlist_for_each_entry_rcu(tt_common_entry, node,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002194 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01002195 if (enable) {
2196 if ((tt_common_entry->flags & flags) == flags)
2197 continue;
2198 tt_common_entry->flags |= flags;
2199 } else {
2200 if (!(tt_common_entry->flags & flags))
2201 continue;
2202 tt_common_entry->flags &= ~flags;
2203 }
2204 changed_num++;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002205 }
2206 rcu_read_unlock();
2207 }
Antonio Quartulli697f2532011-11-07 16:47:01 +01002208out:
2209 return changed_num;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002210}
2211
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002212/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002213static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002214{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002215 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002216 struct batadv_tt_common_entry *tt_common;
2217 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002218 struct hlist_node *node, *node_tmp;
2219 struct hlist_head *head;
2220 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02002221 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002222
2223 if (!hash)
2224 return;
2225
2226 for (i = 0; i < hash->size; i++) {
2227 head = &hash->table[i];
2228 list_lock = &hash->list_locks[i];
2229
2230 spin_lock_bh(list_lock);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002231 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2232 hash_entry) {
2233 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002234 continue;
2235
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002236 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002237 "Deleting local tt entry (%pM): pending\n",
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002238 tt_common->addr);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002239
Sven Eckelmann807736f2012-07-15 22:26:51 +02002240 atomic_dec(&bat_priv->tt.local_entry_num);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002241 hlist_del_rcu(node);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002242 tt_local = container_of(tt_common,
2243 struct batadv_tt_local_entry,
2244 common);
2245 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002246 }
2247 spin_unlock_bh(list_lock);
2248 }
2249
2250}
2251
Sven Eckelmann56303d32012-06-05 22:31:31 +02002252static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002253 unsigned char **packet_buff,
2254 int *packet_buff_len, int packet_min_len)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002255{
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002256 uint16_t changed_num = 0;
2257
Sven Eckelmann807736f2012-07-15 22:26:51 +02002258 if (atomic_read(&bat_priv->tt.local_changes) < 1)
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002259 return -ENOENT;
2260
Sven Eckelmann807736f2012-07-15 22:26:51 +02002261 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002262 BATADV_TT_CLIENT_NEW, false);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002263
2264 /* all reset entries have to be counted as local entries */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002265 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002266 batadv_tt_local_purge_pending_clients(bat_priv);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002267 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002268
2269 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002270 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002271 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002272 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02002273 (uint8_t)atomic_read(&bat_priv->tt.vn));
2274 bat_priv->tt.poss_change = false;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002275
2276 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002277 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002278
Sven Eckelmanna5130882012-05-16 20:23:16 +02002279 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2280 packet_buff_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002281}
2282
2283/* when calling this function (hard_iface == primary_if) has to be true */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002284int batadv_tt_append_diff(struct batadv_priv *bat_priv,
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002285 unsigned char **packet_buff, int *packet_buff_len,
2286 int packet_min_len)
2287{
2288 int tt_num_changes;
2289
2290 /* if at least one change happened */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002291 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2292 packet_buff_len,
2293 packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002294
2295 /* if the changes have been sent often enough */
2296 if ((tt_num_changes < 0) &&
Sven Eckelmann807736f2012-07-15 22:26:51 +02002297 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02002298 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2299 packet_min_len, packet_min_len);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08002300 tt_num_changes = 0;
2301 }
2302
2303 return tt_num_changes;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002304}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002305
Sven Eckelmann56303d32012-06-05 22:31:31 +02002306bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002307 uint8_t *dst)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002308{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002309 struct batadv_tt_local_entry *tt_local_entry = NULL;
2310 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner5870adc2012-06-20 17:16:05 +02002311 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002312
2313 if (!atomic_read(&bat_priv->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02002314 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002315
Sven Eckelmanna5130882012-05-16 20:23:16 +02002316 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002317 if (!tt_local_entry)
2318 goto out;
2319
Sven Eckelmanna5130882012-05-16 20:23:16 +02002320 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002321 if (!tt_global_entry)
2322 goto out;
2323
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00002324 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002325 goto out;
2326
Marek Lindner5870adc2012-06-20 17:16:05 +02002327 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002328
2329out:
2330 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002331 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002332 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002333 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002334 return ret;
2335}
Marek Lindnera943cac2011-07-30 13:10:18 +02002336
Sven Eckelmann56303d32012-06-05 22:31:31 +02002337void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2338 struct batadv_orig_node *orig_node,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002339 const unsigned char *tt_buff, uint8_t tt_num_changes,
2340 uint8_t ttvn, uint16_t tt_crc)
Marek Lindnera943cac2011-07-30 13:10:18 +02002341{
2342 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2343 bool full_table = true;
Sven Eckelmann96412692012-06-05 22:31:30 +02002344 struct batadv_tt_change *tt_change;
Marek Lindnera943cac2011-07-30 13:10:18 +02002345
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002346 /* don't care about a backbone gateways updates. */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002347 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01002348 return;
2349
Antonio Quartulli17071572011-11-07 16:36:40 +01002350 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002351 * increased by one -> we can apply the attached changes
2352 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002353 if ((!orig_node->tt_initialised && ttvn == 1) ||
2354 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002355 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002356 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2357 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002358 * In this case send a tt request
2359 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002360 if (!tt_num_changes) {
2361 full_table = false;
2362 goto request_table;
2363 }
2364
Sven Eckelmann96412692012-06-05 22:31:30 +02002365 tt_change = (struct batadv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002366 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02002367 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02002368
2369 /* Even if we received the precomputed crc with the OGM, we
2370 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002371 * in the global table
2372 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002373 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02002374
2375 /* The ttvn alone is not enough to guarantee consistency
2376 * because a single value could represent different states
2377 * (due to the wrap around). Thus a node has to check whether
2378 * the resulting table (after applying the changes) is still
2379 * consistent or not. E.g. a node could disconnect while its
2380 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2381 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002382 * inconsistency
2383 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002384 if (orig_node->tt_crc != tt_crc)
2385 goto request_table;
2386
2387 /* Roaming phase is over: tables are in sync again. I can
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002388 * unset the flag
2389 */
Marek Lindnera943cac2011-07-30 13:10:18 +02002390 orig_node->tt_poss_change = false;
2391 } else {
2392 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002393 * in sync anymore -> request fresh tt data
2394 */
Antonio Quartulli17071572011-11-07 16:36:40 +01002395 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2396 orig_node->tt_crc != tt_crc) {
Marek Lindnera943cac2011-07-30 13:10:18 +02002397request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002398 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002399 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2400 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2401 orig_node->tt_crc, tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002402 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2403 tt_crc, full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02002404 return;
2405 }
2406 }
2407}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002408
2409/* returns true whether we know that the client has moved from its old
2410 * originator to another one. This entry is kept is still kept for consistency
2411 * purposes
2412 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002413bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002414 uint8_t *addr)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002415{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002416 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002417 bool ret = false;
2418
Sven Eckelmanna5130882012-05-16 20:23:16 +02002419 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002420 if (!tt_global_entry)
2421 goto out;
2422
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002423 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002424 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01002425out:
2426 return ret;
2427}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002428
2429bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2430 struct batadv_orig_node *orig_node,
2431 const unsigned char *addr)
2432{
2433 bool ret = false;
2434
2435 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2436 BATADV_TT_CLIENT_TEMP,
2437 atomic_read(&orig_node->last_ttvn)))
2438 goto out;
2439
2440 batadv_dbg(BATADV_DBG_TT, bat_priv,
2441 "Added temporary global client (addr: %pM orig: %pM)\n",
2442 addr, orig_node->orig);
2443 ret = true;
2444out:
2445 return ret;
2446}