blob: 2eaa1e9d8c99ea4e7ec6b908332222358e958bbd [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
18#include "main.h"
19#include "translation-table.h"
20#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020021#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020022#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "hash.h"
24#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020025#include "routing.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010026#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027
Antonio Quartulliced72932013-04-24 16:37:51 +020028#include <linux/crc32c.h>
Antonio Quartullia73105b2011-04-27 14:27:44 +020029
Antonio Quartullidec05072012-11-10 11:00:32 +010030/* hash class keys */
31static struct lock_class_key batadv_tt_local_hash_lock_class_key;
32static struct lock_class_key batadv_tt_global_hash_lock_class_key;
33
Sven Eckelmann56303d32012-06-05 22:31:31 +020034static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020035 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020036 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020037static void batadv_tt_purge(struct work_struct *work);
38static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020039batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020040static void batadv_tt_global_del(struct batadv_priv *bat_priv,
41 struct batadv_orig_node *orig_node,
42 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020043 unsigned short vid, const char *message,
44 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Marek Lindner7aadf882011-02-18 12:28:09 +000046/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020047static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000048{
Sven Eckelmann56303d32012-06-05 22:31:31 +020049 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000051
dingtianhong323813e2013-12-26 19:40:39 +080052 return batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000053}
54
Antonio Quartullic018ad32013-06-04 12:11:39 +020055/**
56 * batadv_choose_tt - return the index of the tt entry in the hash table
57 * @data: pointer to the tt_common_entry object to map
58 * @size: the size of the hash table
59 *
60 * Returns the hash index where the object represented by 'data' should be
61 * stored at.
62 */
63static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
64{
65 struct batadv_tt_common_entry *tt;
66 uint32_t hash = 0;
67
68 tt = (struct batadv_tt_common_entry *)data;
69 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
70 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
71
72 hash += (hash << 3);
73 hash ^= (hash >> 11);
74 hash += (hash << 15);
75
76 return hash % size;
77}
78
79/**
80 * batadv_tt_hash_find - look for a client in the given hash table
81 * @hash: the hash table to search
82 * @addr: the mac address of the client to look for
83 * @vid: VLAN identifier
84 *
85 * Returns a pointer to the tt_common struct belonging to the searched client if
86 * found, NULL otherwise.
87 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020088static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +020089batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
90 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +000091{
Marek Lindner7aadf882011-02-18 12:28:09 +000092 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +020093 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020094 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000095
96 if (!hash)
97 return NULL;
98
Antonio Quartullic018ad32013-06-04 12:11:39 +020099 memcpy(to_search.addr, addr, ETH_ALEN);
100 to_search.vid = vid;
101
102 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000103 head = &hash->table[index];
104
105 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200106 hlist_for_each_entry_rcu(tt, head, hash_entry) {
107 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000108 continue;
109
Antonio Quartullic018ad32013-06-04 12:11:39 +0200110 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200111 continue;
112
Antonio Quartullic018ad32013-06-04 12:11:39 +0200113 if (!atomic_inc_not_zero(&tt->refcount))
114 continue;
115
116 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000117 break;
118 }
119 rcu_read_unlock();
120
Antonio Quartullic018ad32013-06-04 12:11:39 +0200121 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100122}
123
Antonio Quartullic018ad32013-06-04 12:11:39 +0200124/**
125 * batadv_tt_local_hash_find - search the local table for a given client
126 * @bat_priv: the bat priv with all the soft interface information
127 * @addr: the mac address of the client to look for
128 * @vid: VLAN identifier
129 *
130 * Returns a pointer to the corresponding tt_local_entry struct if the client is
131 * found, NULL otherwise.
132 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200133static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200134batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
135 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100136{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200137 struct batadv_tt_common_entry *tt_common_entry;
138 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100139
Antonio Quartullic018ad32013-06-04 12:11:39 +0200140 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
141 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100142 if (tt_common_entry)
143 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200144 struct batadv_tt_local_entry,
145 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100146 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000147}
148
Antonio Quartullic018ad32013-06-04 12:11:39 +0200149/**
150 * batadv_tt_global_hash_find - search the global table for a given client
151 * @bat_priv: the bat priv with all the soft interface information
152 * @addr: the mac address of the client to look for
153 * @vid: VLAN identifier
154 *
155 * Returns a pointer to the corresponding tt_global_entry struct if the client
156 * is found, NULL otherwise.
157 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200158static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200159batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
160 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000161{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162 struct batadv_tt_common_entry *tt_common_entry;
163 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000164
Antonio Quartullic018ad32013-06-04 12:11:39 +0200165 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
166 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100167 if (tt_common_entry)
168 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_global_entry,
170 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100171 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000172}
173
Sven Eckelmanna5130882012-05-16 20:23:16 +0200174static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200175batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200176{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100177 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
178 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200179}
180
Antonio Quartulli21026052013-05-07 00:29:22 +0200181/**
182 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
183 * tt_global_entry and possibly free it
184 * @tt_global_entry: the object to free
185 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200186static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200187batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200188{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200189 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200190 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200191 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200192 }
193}
194
Sven Eckelmanna5130882012-05-16 20:23:16 +0200195static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200196{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200197 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200198
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800200
201 /* We are in an rcu callback here, therefore we cannot use
202 * batadv_orig_node_free_ref() and its call_rcu():
203 * An rcu_barrier() wouldn't wait for that to finish
204 */
205 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200206 kfree(orig_entry);
207}
208
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200209/**
210 * batadv_tt_local_size_mod - change the size by v of the local table identified
211 * by vid
212 * @bat_priv: the bat priv with all the soft interface information
213 * @vid: the VLAN identifier of the sub-table to change
214 * @v: the amount to sum to the local table size
215 */
216static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
217 unsigned short vid, int v)
218{
219 struct batadv_softif_vlan *vlan;
220
221 vlan = batadv_softif_vlan_get(bat_priv, vid);
222 if (!vlan)
223 return;
224
225 atomic_add(v, &vlan->tt.num_entries);
226
227 batadv_softif_vlan_free_ref(vlan);
228}
229
230/**
231 * batadv_tt_local_size_inc - increase by one the local table size for the given
232 * vid
233 * @bat_priv: the bat priv with all the soft interface information
234 * @vid: the VLAN identifier
235 */
236static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
237 unsigned short vid)
238{
239 batadv_tt_local_size_mod(bat_priv, vid, 1);
240}
241
242/**
243 * batadv_tt_local_size_dec - decrease by one the local table size for the given
244 * vid
245 * @bat_priv: the bat priv with all the soft interface information
246 * @vid: the VLAN identifier
247 */
248static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
249 unsigned short vid)
250{
251 batadv_tt_local_size_mod(bat_priv, vid, -1);
252}
253
254/**
255 * batadv_tt_global_size_mod - change the size by v of the local table
256 * identified by vid
257 * @bat_priv: the bat priv with all the soft interface information
258 * @vid: the VLAN identifier
259 * @v: the amount to sum to the global table size
260 */
261static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
262 unsigned short vid, int v)
263{
264 struct batadv_orig_node_vlan *vlan;
265
266 vlan = batadv_orig_node_vlan_new(orig_node, vid);
267 if (!vlan)
268 return;
269
270 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
271 spin_lock_bh(&orig_node->vlan_list_lock);
272 list_del_rcu(&vlan->list);
273 spin_unlock_bh(&orig_node->vlan_list_lock);
274 batadv_orig_node_vlan_free_ref(vlan);
275 }
276
277 batadv_orig_node_vlan_free_ref(vlan);
278}
279
280/**
281 * batadv_tt_global_size_inc - increase by one the global table size for the
282 * given vid
283 * @orig_node: the originator which global table size has to be decreased
284 * @vid: the vlan identifier
285 */
286static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
287 unsigned short vid)
288{
289 batadv_tt_global_size_mod(orig_node, vid, 1);
290}
291
292/**
293 * batadv_tt_global_size_dec - decrease by one the global table size for the
294 * given vid
295 * @orig_node: the originator which global table size has to be decreased
296 * @vid: the vlan identifier
297 */
298static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
299 unsigned short vid)
300{
301 batadv_tt_global_size_mod(orig_node, vid, -1);
302}
303
Sven Eckelmanna5130882012-05-16 20:23:16 +0200304static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200305batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200306{
Antonio Quartullid657e622012-07-01 14:09:12 +0200307 if (!atomic_dec_and_test(&orig_entry->refcount))
308 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200309
Sven Eckelmanna5130882012-05-16 20:23:16 +0200310 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200311}
312
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200313/**
314 * batadv_tt_local_event - store a local TT event (ADD/DEL)
315 * @bat_priv: the bat priv with all the soft interface information
316 * @tt_local_entry: the TT entry involved in the event
317 * @event_flags: flags to store in the event structure
318 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200319static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200320 struct batadv_tt_local_entry *tt_local_entry,
321 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200322{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200323 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200324 struct batadv_tt_common_entry *common = &tt_local_entry->common;
325 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200326 bool event_removed = false;
327 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200328
329 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200330 if (!tt_change_node)
331 return;
332
Antonio Quartulliff66c972011-06-30 01:14:00 +0200333 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100334 memset(tt_change_node->change.reserved, 0,
335 sizeof(tt_change_node->change.reserved));
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200336 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200337 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200338
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200339 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200340
341 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200342 spin_lock_bh(&bat_priv->tt.changes_list_lock);
343 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200344 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200345 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200346 continue;
347
348 /* DEL+ADD in the same orig interval have no effect and can be
349 * removed to avoid silly behaviour on the receiver side. The
350 * other way around (ADD+DEL) can happen in case of roaming of
351 * a client still in the NEW state. Roaming of NEW clients is
352 * now possible due to automatically recognition of "temporary"
353 * clients
354 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200355 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200356 if (!del_op_requested && del_op_entry)
357 goto del;
358 if (del_op_requested && !del_op_entry)
359 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200360
361 /* this is a second add in the same originator interval. It
362 * means that flags have been changed: update them!
363 */
364 if (!del_op_requested && !del_op_entry)
365 entry->change.flags = flags;
366
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200367 continue;
368del:
369 list_del(&entry->list);
370 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000371 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200372 event_removed = true;
373 goto unlock;
374 }
375
Antonio Quartullia73105b2011-04-27 14:27:44 +0200376 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200377 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200378
379unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200380 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200381
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200382 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200383 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200384 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200385 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200386}
387
Marek Lindner335fbe02013-04-23 21:40:02 +0800388/**
389 * batadv_tt_len - compute length in bytes of given number of tt changes
390 * @changes_num: number of tt changes
391 *
392 * Returns computed length in bytes.
393 */
394static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200395{
Marek Lindner335fbe02013-04-23 21:40:02 +0800396 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397}
398
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200399/**
400 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
401 * @tt_len: available space
402 *
403 * Returns the number of entries.
404 */
405static uint16_t batadv_tt_entries(uint16_t tt_len)
406{
407 return tt_len / batadv_tt_len(1);
408}
409
Marek Lindnera19d3d82013-05-27 15:33:25 +0800410/**
411 * batadv_tt_local_table_transmit_size - calculates the local translation table
412 * size when transmitted over the air
413 * @bat_priv: the bat priv with all the soft interface information
414 *
415 * Returns local translation table size in bytes.
416 */
417static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
418{
419 uint16_t num_vlan = 0, tt_local_entries = 0;
420 struct batadv_softif_vlan *vlan;
421 int hdr_size;
422
423 rcu_read_lock();
424 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
425 num_vlan++;
426 tt_local_entries += atomic_read(&vlan->tt.num_entries);
427 }
428 rcu_read_unlock();
429
430 /* header size of tvlv encapsulated tt response payload */
431 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
432 hdr_size += sizeof(struct batadv_tvlv_hdr);
433 hdr_size += sizeof(struct batadv_tvlv_tt_data);
434 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
435
436 return hdr_size + batadv_tt_len(tt_local_entries);
437}
438
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200441 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200442 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
Sven Eckelmann807736f2012-07-15 22:26:51 +0200444 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Sven Eckelmann807736f2012-07-15 22:26:51 +0200446 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200447 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
Antonio Quartullidec05072012-11-10 11:00:32 +0100449 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
450 &batadv_tt_local_hash_lock_class_key);
451
Sven Eckelmann5346c352012-05-05 13:27:28 +0200452 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453}
454
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200455static void batadv_tt_global_free(struct batadv_priv *bat_priv,
456 struct batadv_tt_global_entry *tt_global,
457 const char *message)
458{
459 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200460 "Deleting global tt entry %pM (vid: %d): %s\n",
461 tt_global->common.addr,
462 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200463
464 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200465 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200466 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200467}
468
Antonio Quartullic018ad32013-06-04 12:11:39 +0200469/**
470 * batadv_tt_local_add - add a new client to the local table or update an
471 * existing client
472 * @soft_iface: netdev struct of the mesh interface
473 * @addr: the mac address of the client to add
474 * @vid: VLAN identifier
475 * @ifindex: index of the interface where the client is connected to (useful to
476 * identify wireless clients)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800477 *
478 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200479 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800480bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200481 unsigned short vid, int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200483 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200484 struct batadv_tt_local_entry *tt_local;
485 struct batadv_tt_global_entry *tt_global;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200486 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200487 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200488 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800489 int hash_added, table_size, packet_size_max;
490 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200491 uint8_t remote_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200493 if (ifindex != BATADV_NULL_IFINDEX)
494 in_dev = dev_get_by_index(&init_net, ifindex);
495
Antonio Quartullic018ad32013-06-04 12:11:39 +0200496 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
497 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498
Antonio Quartulli47c94652012-09-23 22:38:35 +0200499 if (tt_local) {
500 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200501 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
502 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200503 "Re-adding pending client %pM (vid: %d)\n",
504 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200505 /* whatever the reason why the PENDING flag was set,
506 * this is a client which was enqueued to be removed in
507 * this orig_interval. Since it popped up again, the
508 * flag can be reset like it was never enqueued
509 */
510 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
511 goto add_event;
512 }
513
514 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
515 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200516 "Roaming client %pM (vid: %d) came back to its original location\n",
517 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200518 /* the ROAM flag is set because this client roamed away
519 * and the node got a roaming_advertisement message. Now
520 * that the client popped up again at its original
521 * location such flag can be unset
522 */
523 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
524 roamed_back = true;
525 }
526 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527 }
528
Marek Lindnera19d3d82013-05-27 15:33:25 +0800529 /* Ignore the client if we cannot send it in a full table response. */
530 table_size = batadv_tt_local_table_transmit_size(bat_priv);
531 table_size += batadv_tt_len(1);
532 packet_size_max = atomic_read(&bat_priv->packet_size_max);
533 if (table_size > packet_size_max) {
534 net_ratelimited_function(batadv_info, soft_iface,
535 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
536 table_size, packet_size_max, addr);
537 goto out;
538 }
539
Antonio Quartulli47c94652012-09-23 22:38:35 +0200540 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
541 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200542 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200543
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200544 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200545 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
546 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200547 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548
Antonio Quartulli47c94652012-09-23 22:38:35 +0200549 memcpy(tt_local->common.addr, addr, ETH_ALEN);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100550 /* The local entry has to be marked as NEW to avoid to send it in
551 * a full table response going out before the next ttvn increment
552 * (consistency check)
553 */
554 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200555 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200556 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200557 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
558 atomic_set(&tt_local->common.refcount, 2);
559 tt_local->last_seen = jiffies;
560 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
562 /* the batman interface mac address should never be purged */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200563 if (batadv_compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200564 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Sven Eckelmann807736f2012-07-15 22:26:51 +0200566 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200567 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200568 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100569
570 if (unlikely(hash_added != 0)) {
571 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200572 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100573 goto out;
574 }
575
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200576add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200577 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200578
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200579check_roaming:
580 /* Check whether it is a roaming, but don't do anything if the roaming
581 * process has already been handled
582 */
583 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200584 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200585 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200586 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800587 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200588 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200589 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200590 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200591 }
592 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200593 if (roamed_back) {
594 batadv_tt_global_free(bat_priv, tt_global,
595 "Roaming canceled");
596 tt_global = NULL;
597 } else {
598 /* The global entry has to be marked as ROAMING and
599 * has to be kept for consistency purpose
600 */
601 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
602 tt_global->roam_at = jiffies;
603 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200604 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200605
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200606 /* store the current remote flags before altering them. This helps
607 * understanding is flags are changing or not
608 */
609 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800610
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200611 if (batadv_is_wifi_netdev(in_dev))
612 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
613 else
614 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
615
616 /* if any "dynamic" flag has been modified, resend an ADD event for this
617 * entry so that all the nodes can get the new flags
618 */
619 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
620 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
621
622 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200623out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200624 if (in_dev)
625 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200626 if (tt_local)
627 batadv_tt_local_entry_free_ref(tt_local);
628 if (tt_global)
629 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800630 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631}
632
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800633/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200634 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
635 * within a TT Response directed to another node
636 * @orig_node: originator for which the TT data has to be prepared
637 * @tt_data: uninitialised pointer to the address of the TVLV buffer
638 * @tt_change: uninitialised pointer to the address of the area where the TT
639 * changed can be stored
640 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
641 * function reserves the amount of space needed to send the entire global TT
642 * table. In case of success the value is updated with the real amount of
643 * reserved bytes
644
645 * Allocate the needed amount of memory for the entire TT TVLV and write its
646 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
647 * objects, one per active VLAN served by the originator node.
648 *
649 * Return the size of the allocated buffer or 0 in case of failure.
650 */
651static uint16_t
652batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
653 struct batadv_tvlv_tt_data **tt_data,
654 struct batadv_tvlv_tt_change **tt_change,
655 int32_t *tt_len)
656{
657 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
658 struct batadv_tvlv_tt_vlan_data *tt_vlan;
659 struct batadv_orig_node_vlan *vlan;
660 uint8_t *tt_change_ptr;
661
662 rcu_read_lock();
663 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
664 num_vlan++;
665 num_entries += atomic_read(&vlan->tt.num_entries);
666 }
667
668 change_offset = sizeof(**tt_data);
669 change_offset += num_vlan * sizeof(*tt_vlan);
670
671 /* if tt_len is negative, allocate the space needed by the full table */
672 if (*tt_len < 0)
673 *tt_len = batadv_tt_len(num_entries);
674
675 tvlv_len = *tt_len;
676 tvlv_len += change_offset;
677
678 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
679 if (!*tt_data) {
680 *tt_len = 0;
681 goto out;
682 }
683
684 (*tt_data)->flags = BATADV_NO_FLAGS;
685 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
686 (*tt_data)->num_vlan = htons(num_vlan);
687
688 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
689 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
690 tt_vlan->vid = htons(vlan->vid);
691 tt_vlan->crc = htonl(vlan->tt.crc);
692
693 tt_vlan++;
694 }
695
696 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
697 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
698
699out:
700 rcu_read_unlock();
701 return tvlv_len;
702}
703
704/**
705 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
706 * node
707 * @bat_priv: the bat priv with all the soft interface information
708 * @tt_data: uninitialised pointer to the address of the TVLV buffer
709 * @tt_change: uninitialised pointer to the address of the area where the TT
710 * changes can be stored
711 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
712 * function reserves the amount of space needed to send the entire local TT
713 * table. In case of success the value is updated with the real amount of
714 * reserved bytes
715 *
716 * Allocate the needed amount of memory for the entire TT TVLV and write its
717 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
718 * objects, one per active VLAN.
719 *
720 * Return the size of the allocated buffer or 0 in case of failure.
721 */
722static uint16_t
723batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
724 struct batadv_tvlv_tt_data **tt_data,
725 struct batadv_tvlv_tt_change **tt_change,
726 int32_t *tt_len)
727{
728 struct batadv_tvlv_tt_vlan_data *tt_vlan;
729 struct batadv_softif_vlan *vlan;
730 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
731 uint8_t *tt_change_ptr;
732 int change_offset;
733
734 rcu_read_lock();
735 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
736 num_vlan++;
737 num_entries += atomic_read(&vlan->tt.num_entries);
738 }
739
740 change_offset = sizeof(**tt_data);
741 change_offset += num_vlan * sizeof(*tt_vlan);
742
743 /* if tt_len is negative, allocate the space needed by the full table */
744 if (*tt_len < 0)
745 *tt_len = batadv_tt_len(num_entries);
746
747 tvlv_len = *tt_len;
748 tvlv_len += change_offset;
749
750 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
751 if (!*tt_data) {
752 tvlv_len = 0;
753 goto out;
754 }
755
756 (*tt_data)->flags = BATADV_NO_FLAGS;
757 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
758 (*tt_data)->num_vlan = htons(num_vlan);
759
760 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
761 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
762 tt_vlan->vid = htons(vlan->vid);
763 tt_vlan->crc = htonl(vlan->tt.crc);
764
765 tt_vlan++;
766 }
767
768 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
769 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
770
771out:
772 rcu_read_unlock();
773 return tvlv_len;
774}
775
776/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800777 * batadv_tt_tvlv_container_update - update the translation table tvlv container
778 * after local tt changes have been committed
779 * @bat_priv: the bat priv with all the soft interface information
780 */
781static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800783 struct batadv_tt_change_node *entry, *safe;
784 struct batadv_tvlv_tt_data *tt_data;
785 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200786 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800787 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200788 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200790 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
791 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800792
793 /* if we have too many changes for one packet don't send any
794 * and wait for the tt table request which will be fragmented
795 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800796 if (tt_diff_len > bat_priv->soft_iface->mtu)
797 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800798
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200799 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
800 &tt_change, &tt_diff_len);
801 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800802 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800803
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800804 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800805
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800806 if (tt_diff_len == 0)
807 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800808
Sven Eckelmann807736f2012-07-15 22:26:51 +0200809 spin_lock_bh(&bat_priv->tt.changes_list_lock);
810 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811
Sven Eckelmann807736f2012-07-15 22:26:51 +0200812 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100813 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800814 if (tt_diff_entries_count < tt_diff_entries_num) {
815 memcpy(tt_change + tt_diff_entries_count,
816 &entry->change,
817 sizeof(struct batadv_tvlv_tt_change));
818 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200820 list_del(&entry->list);
821 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200823 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824
Antonio Quartullia73105b2011-04-27 14:27:44 +0200825 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200826 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
827 kfree(bat_priv->tt.last_changeset);
828 bat_priv->tt.last_changeset_len = 0;
829 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800830 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800831 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800832 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800833 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200834 * instead of providing the diff
835 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800836 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200837 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800838 memcpy(bat_priv->tt.last_changeset,
839 tt_change, tt_change_len);
840 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200841 }
842 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200843 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800845container_register:
846 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200847 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800848 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849}
850
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200851int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000852{
853 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200854 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200855 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200856 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100857 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200858 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200859 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000860 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200861 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200862 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100863 int last_seen_secs;
864 int last_seen_msecs;
865 unsigned long last_seen_jiffies;
866 bool no_purge;
867 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868
Marek Lindner30da63a2012-08-03 17:15:46 +0200869 primary_if = batadv_seq_print_text_primary_if_get(seq);
870 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200871 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100873 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200874 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
875 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
876 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
877 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879 for (i = 0; i < hash->size; i++) {
880 head = &hash->table[i];
881
Marek Lindner7aadf882011-02-18 12:28:09 +0000882 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000884 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100885 tt_local = container_of(tt_common_entry,
886 struct batadv_tt_local_entry,
887 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200888 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100889 last_seen_jiffies = jiffies - tt_local->last_seen;
890 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
891 last_seen_secs = last_seen_msecs / 1000;
892 last_seen_msecs = last_seen_msecs % 1000;
893
894 no_purge = tt_common_entry->flags & np_flag;
895
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200896 vlan = batadv_softif_vlan_get(bat_priv, vid);
897 if (!vlan) {
898 seq_printf(seq, "Cannot retrieve VLAN %d\n",
899 BATADV_PRINT_VID(vid));
900 continue;
901 }
902
903 seq_printf(seq,
904 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100905 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200906 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100907 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200908 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100909 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100910 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200911 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100912 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200913 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100914 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100915 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100916 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200917 no_purge ? 0 : last_seen_msecs,
918 vlan->tt.crc);
919
920 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000922 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000923 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200924out:
925 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200926 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200927 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928}
929
Sven Eckelmann56303d32012-06-05 22:31:31 +0200930static void
931batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
932 struct batadv_tt_local_entry *tt_local_entry,
933 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200935 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000936
Antonio Quartulli015758d2011-07-09 17:52:13 +0200937 /* The local client has to be marked as "pending to be removed" but has
938 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200939 * response issued before the net ttvn increment (consistency check)
940 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200941 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100942
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200943 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200944 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
945 tt_local_entry->common.addr,
946 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947}
948
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200949/**
950 * batadv_tt_local_remove - logically remove an entry from the local table
951 * @bat_priv: the bat priv with all the soft interface information
952 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200953 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200954 * @message: message to append to the log on deletion
955 * @roaming: true if the deletion is due to a roaming event
956 *
957 * Returns the flags assigned to the local entry before being deleted
958 */
959uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200960 const uint8_t *addr, unsigned short vid,
961 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000962{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200963 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200964 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000965
Antonio Quartullic018ad32013-06-04 12:11:39 +0200966 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200967 if (!tt_local_entry)
968 goto out;
969
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200970 curr_flags = tt_local_entry->common.flags;
971
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200972 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200973 /* if this global entry addition is due to a roaming, the node has to
974 * mark the local entry as "roamed" in order to correctly reroute
975 * packets later
976 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200977 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200978 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200979 /* mark the local client as ROAMed */
980 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
981 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200982
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200983 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
984 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
985 message);
986 goto out;
987 }
988 /* if this client has been added right now, it is possible to
989 * immediately purge it
990 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200991 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200992 hlist_del_rcu(&tt_local_entry->common.hash_entry);
993 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200994
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200995out:
996 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +0200997 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200998
999 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001000}
1001
Marek Lindnera19d3d82013-05-27 15:33:25 +08001002/**
1003 * batadv_tt_local_purge_list - purge inactive tt local entries
1004 * @bat_priv: the bat priv with all the soft interface information
1005 * @head: pointer to the list containing the local tt entries
1006 * @timeout: parameter deciding whether a given tt local entry is considered
1007 * inactive or not
1008 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001009static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001010 struct hlist_head *head,
1011 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001013 struct batadv_tt_local_entry *tt_local_entry;
1014 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001015 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001016
Sasha Levinb67bfe02013-02-27 17:06:00 -08001017 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001018 hash_entry) {
1019 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 struct batadv_tt_local_entry,
1021 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001022 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1023 continue;
1024
1025 /* entry already marked for deletion */
1026 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1027 continue;
1028
Marek Lindnera19d3d82013-05-27 15:33:25 +08001029 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001030 continue;
1031
1032 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1033 BATADV_TT_CLIENT_DEL, "timed out");
1034 }
1035}
1036
Marek Lindnera19d3d82013-05-27 15:33:25 +08001037/**
1038 * batadv_tt_local_purge - purge inactive tt local entries
1039 * @bat_priv: the bat priv with all the soft interface information
1040 * @timeout: parameter deciding whether a given tt local entry is considered
1041 * inactive or not
1042 */
1043static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1044 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001045{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001046 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001048 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001049 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051 for (i = 0; i < hash->size; i++) {
1052 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001053 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001055 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001056 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001057 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059}
1060
Sven Eckelmann56303d32012-06-05 22:31:31 +02001061static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001063 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001064 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001065 struct batadv_tt_common_entry *tt_common_entry;
1066 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001067 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001068 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001069 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001070
Sven Eckelmann807736f2012-07-15 22:26:51 +02001071 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072 return;
1073
Sven Eckelmann807736f2012-07-15 22:26:51 +02001074 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001075
1076 for (i = 0; i < hash->size; i++) {
1077 head = &hash->table[i];
1078 list_lock = &hash->list_locks[i];
1079
1080 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001081 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001082 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001083 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001084 tt_local = container_of(tt_common_entry,
1085 struct batadv_tt_local_entry,
1086 common);
1087 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001088 }
1089 spin_unlock_bh(list_lock);
1090 }
1091
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001092 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001093
Sven Eckelmann807736f2012-07-15 22:26:51 +02001094 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001095}
1096
Sven Eckelmann56303d32012-06-05 22:31:31 +02001097static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001098{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001099 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001100 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101
Sven Eckelmann807736f2012-07-15 22:26:51 +02001102 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001103
Sven Eckelmann807736f2012-07-15 22:26:51 +02001104 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001105 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001106
Antonio Quartullidec05072012-11-10 11:00:32 +01001107 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1108 &batadv_tt_global_hash_lock_class_key);
1109
Sven Eckelmann5346c352012-05-05 13:27:28 +02001110 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001111}
1112
Sven Eckelmann56303d32012-06-05 22:31:31 +02001113static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001114{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001115 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001116
Sven Eckelmann807736f2012-07-15 22:26:51 +02001117 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001118
Sven Eckelmann807736f2012-07-15 22:26:51 +02001119 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001120 list) {
1121 list_del(&entry->list);
1122 kfree(entry);
1123 }
1124
Sven Eckelmann807736f2012-07-15 22:26:51 +02001125 atomic_set(&bat_priv->tt.local_changes, 0);
1126 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001127}
1128
Antonio Quartullid657e622012-07-01 14:09:12 +02001129/* retrieves the orig_tt_list_entry belonging to orig_node from the
1130 * batadv_tt_global_entry list
1131 *
1132 * returns it with an increased refcounter, NULL if not found
1133 */
1134static struct batadv_tt_orig_list_entry *
1135batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1136 const struct batadv_orig_node *orig_node)
1137{
1138 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1139 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001140
1141 rcu_read_lock();
1142 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001143 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001144 if (tmp_orig_entry->orig_node != orig_node)
1145 continue;
1146 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1147 continue;
1148
1149 orig_entry = tmp_orig_entry;
1150 break;
1151 }
1152 rcu_read_unlock();
1153
1154 return orig_entry;
1155}
1156
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001157/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001158 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001159 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001160static bool
1161batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1162 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001163{
Antonio Quartullid657e622012-07-01 14:09:12 +02001164 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001165 bool found = false;
1166
Antonio Quartullid657e622012-07-01 14:09:12 +02001167 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1168 if (orig_entry) {
1169 found = true;
1170 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001171 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001172
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001173 return found;
1174}
1175
Sven Eckelmanna5130882012-05-16 20:23:16 +02001176static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001177batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001178 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001179{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001180 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181
Antonio Quartullid657e622012-07-01 14:09:12 +02001182 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001183 if (orig_entry) {
1184 /* refresh the ttvn: the current value could be a bogus one that
1185 * was added during a "temporary client detection"
1186 */
1187 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001188 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001189 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001190
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001191 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1192 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001193 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001194
1195 INIT_HLIST_NODE(&orig_entry->list);
1196 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001197 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001198 orig_entry->orig_node = orig_node;
1199 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001200 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001201
Antonio Quartullid657e622012-07-01 14:09:12 +02001202 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001203 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001204 &tt_global->orig_list);
1205 spin_unlock_bh(&tt_global->list_lock);
1206out:
1207 if (orig_entry)
1208 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001209}
1210
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001211/**
1212 * batadv_tt_global_add - add a new TT global entry or update an existing one
1213 * @bat_priv: the bat priv with all the soft interface information
1214 * @orig_node: the originator announcing the client
1215 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001216 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001217 * @flags: TT flags that have to be set for this non-mesh client
1218 * @ttvn: the tt version number ever announcing this non-mesh client
1219 *
1220 * Add a new TT global entry for the given originator. If the entry already
1221 * exists add a new reference to the given originator (a global entry can have
1222 * references to multiple originators) and adjust the flags attribute to reflect
1223 * the function argument.
1224 * If a TT local entry exists for this non-mesh client remove it.
1225 *
1226 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001227 *
1228 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001229 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001230static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1231 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001232 const unsigned char *tt_addr,
1233 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001234 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001235{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001236 struct batadv_tt_global_entry *tt_global_entry;
1237 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001238 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001239 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001240 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001241 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001243 /* ignore global entries from backbone nodes */
1244 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1245 return true;
1246
Antonio Quartullic018ad32013-06-04 12:11:39 +02001247 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1248 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001249
1250 /* if the node already has a local client for this entry, it has to wait
1251 * for a roaming advertisement instead of manually messing up the global
1252 * table
1253 */
1254 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1255 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1256 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001257
Antonio Quartullia73105b2011-04-27 14:27:44 +02001258 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001259 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001261 goto out;
1262
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001263 common = &tt_global_entry->common;
1264 memcpy(common->addr, tt_addr, ETH_ALEN);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001265 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001266
Antonio Quartullid4f44692012-05-25 00:00:54 +02001267 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001268 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001269 /* node must store current time in case of roaming. This is
1270 * needed to purge this entry out on timeout (if nobody claims
1271 * it)
1272 */
1273 if (flags & BATADV_TT_CLIENT_ROAM)
1274 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001275 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001276 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277
1278 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1279 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001280
Sven Eckelmann807736f2012-07-15 22:26:51 +02001281 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001282 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001283 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001284 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001285
1286 if (unlikely(hash_added != 0)) {
1287 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001288 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001289 goto out_remove;
1290 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001291 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001292 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001293 /* If there is already a global entry, we can use this one for
1294 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001295 * But if we are trying to add a temporary client then here are
1296 * two options at this point:
1297 * 1) the global client is not a temporary client: the global
1298 * client has to be left as it is, temporary information
1299 * should never override any already known client state
1300 * 2) the global client is a temporary client: purge the
1301 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001302 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001303 if (flags & BATADV_TT_CLIENT_TEMP) {
1304 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1305 goto out;
1306 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1307 orig_node))
1308 goto out_remove;
1309 batadv_tt_global_del_orig_list(tt_global_entry);
1310 goto add_orig_entry;
1311 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001312
1313 /* if the client was temporary added before receiving the first
1314 * OGM announcing it, we have to clear the TEMP flag
1315 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001316 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001317
Antonio Quartullie9c001362012-11-07 15:05:33 +01001318 /* the change can carry possible "attribute" flags like the
1319 * TT_CLIENT_WIFI, therefore they have to be copied in the
1320 * client entry
1321 */
1322 tt_global_entry->common.flags |= flags;
1323
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001324 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1325 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001326 * delete + roaming change for this originator.
1327 *
1328 * We should first delete the old originator before adding the
1329 * new one.
1330 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001331 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001332 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001333 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001334 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001335 }
1336 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001337add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001338 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001339 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001340
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001341 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001342 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1343 common->addr, BATADV_PRINT_VID(common->vid),
1344 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001345 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001346
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001347out_remove:
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001348
Antonio Quartullia73105b2011-04-27 14:27:44 +02001349 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001350 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001351 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001352 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001353 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1354
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001355 if (!(flags & BATADV_TT_CLIENT_ROAM))
1356 /* this is a normal global add. Therefore the client is not in a
1357 * roaming state anymore.
1358 */
1359 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1360
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001361out:
1362 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001363 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001364 if (tt_local_entry)
1365 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001366 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001367}
1368
Sven Eckelmann981d8902012-10-07 13:34:15 +02001369/* batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001370 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001371 * @tt_global_entry: global translation table entry to be analyzed
1372 *
1373 * This functon assumes the caller holds rcu_read_lock().
1374 * Returns best originator list entry or NULL on errors.
1375 */
1376static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001377batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1378 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001379{
Antonio Quartulli46274562013-09-03 11:10:24 +02001380 struct batadv_neigh_node *router, *best_router = NULL;
1381 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001382 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001383 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001384
1385 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001386 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001387 router = batadv_orig_node_get_router(orig_entry->orig_node);
1388 if (!router)
1389 continue;
1390
Antonio Quartulli46274562013-09-03 11:10:24 +02001391 if (best_router &&
1392 bao->bat_neigh_cmp(router, best_router) <= 0) {
1393 batadv_neigh_node_free_ref(router);
1394 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001395 }
1396
Antonio Quartulli46274562013-09-03 11:10:24 +02001397 /* release the refcount for the "old" best */
1398 if (best_router)
1399 batadv_neigh_node_free_ref(best_router);
1400
1401 best_entry = orig_entry;
1402 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001403 }
1404
Antonio Quartulli46274562013-09-03 11:10:24 +02001405 if (best_router)
1406 batadv_neigh_node_free_ref(best_router);
1407
Sven Eckelmann981d8902012-10-07 13:34:15 +02001408 return best_entry;
1409}
1410
1411/* batadv_tt_global_print_entry - print all orig nodes who announce the address
1412 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001413 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001414 * @tt_global_entry: global translation table entry to be printed
1415 * @seq: debugfs table seq_file struct
1416 *
1417 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001418 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001419static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001420batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1421 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001422 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001423{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001424 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001425 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001426 struct batadv_orig_node_vlan *vlan;
1427 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001428 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001429 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001430
1431 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001432 flags = tt_common_entry->flags;
1433
Antonio Quartulli46274562013-09-03 11:10:24 +02001434 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001435 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001436 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1437 tt_common_entry->vid);
1438 if (!vlan) {
1439 seq_printf(seq,
1440 " * Cannot retrieve VLAN %d for originator %pM\n",
1441 BATADV_PRINT_VID(tt_common_entry->vid),
1442 best_entry->orig_node->orig);
1443 goto print_list;
1444 }
1445
Sven Eckelmann981d8902012-10-07 13:34:15 +02001446 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001447 seq_printf(seq,
Antonio Quartulli16052782013-06-04 12:11:41 +02001448 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001449 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001450 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001451 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001452 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001453 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1454 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1455 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001456
1457 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001458 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001459
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001460print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001461 head = &tt_global_entry->orig_list;
1462
Sasha Levinb67bfe02013-02-27 17:06:00 -08001463 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001464 if (best_entry == orig_entry)
1465 continue;
1466
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001467 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1468 tt_common_entry->vid);
1469 if (!vlan) {
1470 seq_printf(seq,
1471 " + Cannot retrieve VLAN %d for originator %pM\n",
1472 BATADV_PRINT_VID(tt_common_entry->vid),
1473 orig_entry->orig_node->orig);
1474 continue;
1475 }
1476
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001477 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001478 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001479 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001480 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001481 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001482 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001483 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001484 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001485 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1486 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001487
1488 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001489 }
1490}
1491
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001492int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001493{
1494 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001495 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001496 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001497 struct batadv_tt_common_entry *tt_common_entry;
1498 struct batadv_tt_global_entry *tt_global;
1499 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001500 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001501 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001502
Marek Lindner30da63a2012-08-03 17:15:46 +02001503 primary_if = batadv_seq_print_text_primary_if_get(seq);
1504 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001505 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001506
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001507 seq_printf(seq,
1508 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001509 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001510 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1511 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1512 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001513
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001514 for (i = 0; i < hash->size; i++) {
1515 head = &hash->table[i];
1516
Marek Lindner7aadf882011-02-18 12:28:09 +00001517 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001518 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001519 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001520 tt_global = container_of(tt_common_entry,
1521 struct batadv_tt_global_entry,
1522 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001523 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001524 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001525 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001526 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001527out:
1528 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001529 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001530 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001531}
1532
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001533/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001534static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001535batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001536{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001537 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001538 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001539 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001540
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001541 spin_lock_bh(&tt_global_entry->list_lock);
1542 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001543 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1544 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001545 batadv_tt_global_size_dec(orig_entry->orig_node,
1546 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001547 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001548 }
1549 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001550}
1551
Sven Eckelmanna5130882012-05-16 20:23:16 +02001552static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001553batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1554 struct batadv_tt_global_entry *tt_global_entry,
1555 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001556 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001557{
1558 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001559 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001560 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001561 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001562
1563 spin_lock_bh(&tt_global_entry->list_lock);
1564 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001565 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001566 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001567 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001568 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001569 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001570 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001571 tt_global_entry->common.addr,
1572 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001573 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001574 batadv_tt_global_size_dec(orig_node,
1575 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001576 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001577 }
1578 }
1579 spin_unlock_bh(&tt_global_entry->list_lock);
1580}
1581
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001582/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001583 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1584 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001585 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001586static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001587batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1588 struct batadv_tt_global_entry *tt_global_entry,
1589 struct batadv_orig_node *orig_node,
1590 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001591{
1592 bool last_entry = true;
1593 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001594 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001595
1596 /* no local entry exists, case 1:
1597 * Check if this is the last one or if other entries exist.
1598 */
1599
1600 rcu_read_lock();
1601 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001602 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001603 if (orig_entry->orig_node != orig_node) {
1604 last_entry = false;
1605 break;
1606 }
1607 }
1608 rcu_read_unlock();
1609
1610 if (last_entry) {
1611 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001612 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001613 tt_global_entry->roam_at = jiffies;
1614 } else
1615 /* there is another entry, we can simply delete this
1616 * one and can still use the other one.
1617 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001618 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1619 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001620}
1621
Antonio Quartullic018ad32013-06-04 12:11:39 +02001622/**
1623 * batadv_tt_global_del - remove a client from the global table
1624 * @bat_priv: the bat priv with all the soft interface information
1625 * @orig_node: an originator serving this client
1626 * @addr: the mac address of the client
1627 * @vid: VLAN identifier
1628 * @message: a message explaining the reason for deleting the client to print
1629 * for debugging purpose
1630 * @roaming: true if the deletion has been triggered by a roaming event
1631 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001632static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1633 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001634 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001635 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001636{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001637 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001638 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001639
Antonio Quartullic018ad32013-06-04 12:11:39 +02001640 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001641 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001642 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001643
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001644 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001645 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1646 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001647
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001648 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001649 batadv_tt_global_free(bat_priv, tt_global_entry,
1650 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001651
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001652 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001653 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001654
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001655 /* if we are deleting a global entry due to a roam
1656 * event, there are two possibilities:
1657 * 1) the client roamed from node A to node B => if there
1658 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001659 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001660 * wait for node B to claim it. In case of timeout
1661 * the entry is purged.
1662 *
1663 * If there are other originators left, we directly delete
1664 * the originator.
1665 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001666 * the global entry, since it is useless now.
1667 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001668 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001669 tt_global_entry->common.addr,
1670 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001671 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001672 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001673 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001674 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001675 } else
1676 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001677 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1678 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001679
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001680
Antonio Quartullicc47f662011-04-27 14:27:57 +02001681out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001682 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001683 batadv_tt_global_entry_free_ref(tt_global_entry);
1684 if (local_entry)
1685 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686}
1687
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001688/**
1689 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1690 * given originator matching the provided vid
1691 * @bat_priv: the bat priv with all the soft interface information
1692 * @orig_node: the originator owning the entries to remove
1693 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1694 * removed
1695 * @message: debug message to print as "reason"
1696 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001697void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1698 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001699 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001700 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001701{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001702 struct batadv_tt_global_entry *tt_global;
1703 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001704 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001705 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001706 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001708 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001709 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001710
Simon Wunderlich6e801492011-10-19 10:28:26 +02001711 if (!hash)
1712 return;
1713
Antonio Quartullia73105b2011-04-27 14:27:44 +02001714 for (i = 0; i < hash->size; i++) {
1715 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001716 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001717
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001718 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001719 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001720 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001721 /* remove only matching entries */
1722 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1723 continue;
1724
Sven Eckelmann56303d32012-06-05 22:31:31 +02001725 tt_global = container_of(tt_common_entry,
1726 struct batadv_tt_global_entry,
1727 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001728
Sven Eckelmann56303d32012-06-05 22:31:31 +02001729 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001730 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001731
Sven Eckelmann56303d32012-06-05 22:31:31 +02001732 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001733 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001734 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001735 "Deleting global tt entry %pM (vid: %d): %s\n",
1736 tt_global->common.addr,
1737 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001738 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001739 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001740 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001741 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001742 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001743 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001744 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001745}
1746
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001747static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1748 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001749{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001750 bool purge = false;
1751 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1752 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001753
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001754 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1755 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1756 purge = true;
1757 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001758 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001759
1760 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1761 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1762 purge = true;
1763 *msg = "Temporary client timeout\n";
1764 }
1765
1766 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001767}
1768
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001769static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001770{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001771 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001772 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001773 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001774 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001775 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001776 char *msg = NULL;
1777 struct batadv_tt_common_entry *tt_common;
1778 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001779
Antonio Quartullicc47f662011-04-27 14:27:57 +02001780 for (i = 0; i < hash->size; i++) {
1781 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001782 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001783
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001784 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001785 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001786 hash_entry) {
1787 tt_global = container_of(tt_common,
1788 struct batadv_tt_global_entry,
1789 common);
1790
1791 if (!batadv_tt_global_to_purge(tt_global, &msg))
1792 continue;
1793
1794 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001795 "Deleting global tt entry %pM (vid: %d): %s\n",
1796 tt_global->common.addr,
1797 BATADV_PRINT_VID(tt_global->common.vid),
1798 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001799
Sasha Levinb67bfe02013-02-27 17:06:00 -08001800 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001801
1802 batadv_tt_global_entry_free_ref(tt_global);
1803 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001804 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001805 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001806}
1807
Sven Eckelmann56303d32012-06-05 22:31:31 +02001808static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001809{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001810 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001811 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001812 struct batadv_tt_common_entry *tt_common_entry;
1813 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001814 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001815 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001816 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001817
Sven Eckelmann807736f2012-07-15 22:26:51 +02001818 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001819 return;
1820
Sven Eckelmann807736f2012-07-15 22:26:51 +02001821 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001822
1823 for (i = 0; i < hash->size; i++) {
1824 head = &hash->table[i];
1825 list_lock = &hash->list_locks[i];
1826
1827 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001828 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001829 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001830 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001831 tt_global = container_of(tt_common_entry,
1832 struct batadv_tt_global_entry,
1833 common);
1834 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001835 }
1836 spin_unlock_bh(list_lock);
1837 }
1838
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001839 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001840
Sven Eckelmann807736f2012-07-15 22:26:51 +02001841 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001842}
1843
Sven Eckelmann56303d32012-06-05 22:31:31 +02001844static bool
1845_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1846 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001847{
1848 bool ret = false;
1849
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001850 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1851 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001852 ret = true;
1853
1854 return ret;
1855}
1856
Antonio Quartullic018ad32013-06-04 12:11:39 +02001857/**
1858 * batadv_transtable_search - get the mesh destination for a given client
1859 * @bat_priv: the bat priv with all the soft interface information
1860 * @src: mac address of the source client
1861 * @addr: mac address of the destination client
1862 * @vid: VLAN identifier
1863 *
1864 * Returns a pointer to the originator that was selected as destination in the
1865 * mesh for contacting the client 'addr', NULL otherwise.
1866 * In case of multiple originators serving the same client, the function returns
1867 * the best one (best in terms of metric towards the destination node).
1868 *
1869 * If the two clients are AP isolated the function returns NULL.
1870 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001871struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1872 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001873 const uint8_t *addr,
1874 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001875{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001876 struct batadv_tt_local_entry *tt_local_entry = NULL;
1877 struct batadv_tt_global_entry *tt_global_entry = NULL;
1878 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001879 struct batadv_tt_orig_list_entry *best_entry;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001880 bool ap_isolation_enabled = false;
1881 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001882
Antonio Quartullib8cbd812013-07-02 11:04:36 +02001883 /* if the AP isolation is requested on a VLAN, then check for its
1884 * setting in the proper VLAN private data structure
1885 */
1886 vlan = batadv_softif_vlan_get(bat_priv, vid);
1887 if (vlan) {
1888 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1889 batadv_softif_vlan_free_ref(vlan);
1890 }
1891
1892 if (src && ap_isolation_enabled) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001893 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001894 if (!tt_local_entry ||
1895 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001896 goto out;
1897 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001898
Antonio Quartullic018ad32013-06-04 12:11:39 +02001899 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001900 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001901 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001902
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001903 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001904 * isolation
1905 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001906 if (tt_local_entry &&
1907 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001908 goto out;
1909
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001910 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001911 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001912 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001913 if (best_entry)
1914 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001915 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1916 orig_node = NULL;
1917 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001918
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001919out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001920 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001921 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001922 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001923 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001924
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001925 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001926}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001927
Antonio Quartulliced72932013-04-24 16:37:51 +02001928/**
1929 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1930 * to the given orig_node
1931 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001932 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001933 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001934 *
1935 * This function computes the checksum for the global table corresponding to a
1936 * specific originator. In particular, the checksum is computed as follows: For
1937 * each client connected to the originator the CRC32C of the MAC address and the
1938 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1939 * together.
1940 *
1941 * The idea behind is that CRC32C should be used as much as possible in order to
1942 * produce a unique hash of the table, but since the order which is used to feed
1943 * the CRC32C function affects the result and since every node in the network
1944 * probably sorts the clients differently, the hash function cannot be directly
1945 * computed over the entire table. Hence the CRC32C is used only on
1946 * the single client entry, while all the results are then xor'ed together
1947 * because the XOR operation can combine them all while trying to reduce the
1948 * noise as much as possible.
1949 *
1950 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001951 */
1952static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001953 struct batadv_orig_node *orig_node,
1954 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001955{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001956 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001957 struct batadv_tt_common_entry *tt_common;
1958 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001959 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001960 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02001961 uint8_t flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962
1963 for (i = 0; i < hash->size; i++) {
1964 head = &hash->table[i];
1965
1966 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001967 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001968 tt_global = container_of(tt_common,
1969 struct batadv_tt_global_entry,
1970 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001971 /* compute the CRC only for entries belonging to the
1972 * VLAN identified by the vid passed as parameter
1973 */
1974 if (tt_common->vid != vid)
1975 continue;
1976
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001977 /* Roaming clients are in the global table for
1978 * consistency only. They don't have to be
1979 * taken into account while computing the
1980 * global crc
1981 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001982 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001983 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001984 /* Temporary clients have not been announced yet, so
1985 * they have to be skipped while computing the global
1986 * crc
1987 */
1988 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1989 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001990
1991 /* find out if this global entry is announced by this
1992 * originator
1993 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001994 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001995 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001996 continue;
1997
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001998 crc_tmp = crc32c(0, &tt_common->vid,
1999 sizeof(tt_common->vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002000
2001 /* compute the CRC on flags that have to be kept in sync
2002 * among nodes
2003 */
2004 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2005 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2006
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002007 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002008 }
2009 rcu_read_unlock();
2010 }
2011
Antonio Quartulliced72932013-04-24 16:37:51 +02002012 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002013}
2014
Antonio Quartulliced72932013-04-24 16:37:51 +02002015/**
2016 * batadv_tt_local_crc - calculates the checksum of the local table
2017 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002018 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002019 *
2020 * For details about the computation, please refer to the documentation for
2021 * batadv_tt_global_crc().
2022 *
2023 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002024 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002025static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2026 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002027{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002028 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002029 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002030 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002031 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002032 uint8_t flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002033
2034 for (i = 0; i < hash->size; i++) {
2035 head = &hash->table[i];
2036
2037 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002038 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002039 /* compute the CRC only for entries belonging to the
2040 * VLAN identified by vid
2041 */
2042 if (tt_common->vid != vid)
2043 continue;
2044
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002045 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002046 * account while computing the CRC
2047 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002048 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002049 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002050
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002051 crc_tmp = crc32c(0, &tt_common->vid,
2052 sizeof(tt_common->vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002053
2054 /* compute the CRC on flags that have to be kept in sync
2055 * among nodes
2056 */
2057 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2058 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2059
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002060 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002062 rcu_read_unlock();
2063 }
2064
Antonio Quartulliced72932013-04-24 16:37:51 +02002065 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002066}
2067
Sven Eckelmann56303d32012-06-05 22:31:31 +02002068static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002069{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002070 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002071
Sven Eckelmann807736f2012-07-15 22:26:51 +02002072 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002073
Sven Eckelmann807736f2012-07-15 22:26:51 +02002074 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002075 list_del(&node->list);
2076 kfree(node);
2077 }
2078
Sven Eckelmann807736f2012-07-15 22:26:51 +02002079 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002080}
2081
Sven Eckelmann56303d32012-06-05 22:31:31 +02002082static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2083 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002084 const void *tt_buff,
2085 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002086{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002087 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002088 * last OGM (the OGM could carry no changes)
2089 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002090 spin_lock_bh(&orig_node->tt_buff_lock);
2091 if (tt_buff_len > 0) {
2092 kfree(orig_node->tt_buff);
2093 orig_node->tt_buff_len = 0;
2094 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2095 if (orig_node->tt_buff) {
2096 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2097 orig_node->tt_buff_len = tt_buff_len;
2098 }
2099 }
2100 spin_unlock_bh(&orig_node->tt_buff_lock);
2101}
2102
Sven Eckelmann56303d32012-06-05 22:31:31 +02002103static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002105 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106
Sven Eckelmann807736f2012-07-15 22:26:51 +02002107 spin_lock_bh(&bat_priv->tt.req_list_lock);
2108 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002109 if (batadv_has_timed_out(node->issued_at,
2110 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002111 list_del(&node->list);
2112 kfree(node);
2113 }
2114 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002115 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116}
2117
2118/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002119 * has already been issued for this orig_node, NULL otherwise
2120 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002121static struct batadv_tt_req_node *
2122batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2123 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002124{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002125 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002126
Sven Eckelmann807736f2012-07-15 22:26:51 +02002127 spin_lock_bh(&bat_priv->tt.req_list_lock);
2128 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002129 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2130 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002131 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002132 goto unlock;
2133 }
2134
2135 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2136 if (!tt_req_node)
2137 goto unlock;
2138
2139 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2140 tt_req_node->issued_at = jiffies;
2141
Sven Eckelmann807736f2012-07-15 22:26:51 +02002142 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002143unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002144 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002145 return tt_req_node;
2146}
2147
Marek Lindner335fbe02013-04-23 21:40:02 +08002148/**
2149 * batadv_tt_local_valid - verify that given tt entry is a valid one
2150 * @entry_ptr: to be checked local tt entry
2151 * @data_ptr: not used but definition required to satisfy the callback prototype
2152 *
2153 * Returns 1 if the entry is a valid, 0 otherwise.
2154 */
2155static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002156{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002157 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002158
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002159 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002160 return 0;
2161 return 1;
2162}
2163
Sven Eckelmanna5130882012-05-16 20:23:16 +02002164static int batadv_tt_global_valid(const void *entry_ptr,
2165 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002167 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2168 const struct batadv_tt_global_entry *tt_global_entry;
2169 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002171 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2172 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002173 return 0;
2174
Sven Eckelmann56303d32012-06-05 22:31:31 +02002175 tt_global_entry = container_of(tt_common_entry,
2176 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002177 common);
2178
Sven Eckelmanna5130882012-05-16 20:23:16 +02002179 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002180}
2181
Marek Lindner335fbe02013-04-23 21:40:02 +08002182/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002183 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2184 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002185 * @bat_priv: the bat priv with all the soft interface information
2186 * @hash: hash table containing the tt entries
2187 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002188 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002189 * @valid_cb: function to filter tt change entries
2190 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002191 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002192static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2193 struct batadv_hashtable *hash,
2194 void *tvlv_buff, uint16_t tt_len,
2195 int (*valid_cb)(const void *, const void *),
2196 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002197{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002198 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002199 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002200 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002201 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002202 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002203
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002204 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002205 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002206
2207 rcu_read_lock();
2208 for (i = 0; i < hash->size; i++) {
2209 head = &hash->table[i];
2210
Sasha Levinb67bfe02013-02-27 17:06:00 -08002211 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002212 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002213 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002214 break;
2215
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002216 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002217 continue;
2218
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002219 memcpy(tt_change->addr, tt_common_entry->addr,
2220 ETH_ALEN);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002221 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002222 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002223 memset(tt_change->reserved, 0,
2224 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002225
Marek Lindner335fbe02013-04-23 21:40:02 +08002226 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002227 tt_change++;
2228 }
2229 }
2230 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002231}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002233/**
2234 * batadv_tt_global_check_crc - check if all the CRCs are correct
2235 * @orig_node: originator for which the CRCs have to be checked
2236 * @tt_vlan: pointer to the first tvlv VLAN entry
2237 * @num_vlan: number of tvlv VLAN entries
2238 * @create: if true, create VLAN objects if not found
2239 *
2240 * Return true if all the received CRCs match the locally stored ones, false
2241 * otherwise
2242 */
2243static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2244 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2245 uint16_t num_vlan)
2246{
2247 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2248 struct batadv_orig_node_vlan *vlan;
2249 int i;
2250
2251 /* check if each received CRC matches the locally stored one */
2252 for (i = 0; i < num_vlan; i++) {
2253 tt_vlan_tmp = tt_vlan + i;
2254
2255 /* if orig_node is a backbone node for this VLAN, don't check
2256 * the CRC as we ignore all the global entries over it
2257 */
2258 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002259 orig_node->orig,
2260 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002261 continue;
2262
2263 vlan = batadv_orig_node_vlan_get(orig_node,
2264 ntohs(tt_vlan_tmp->vid));
2265 if (!vlan)
2266 return false;
2267
2268 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2269 return false;
2270 }
2271
2272 return true;
2273}
2274
2275/**
2276 * batadv_tt_local_update_crc - update all the local CRCs
2277 * @bat_priv: the bat priv with all the soft interface information
2278 */
2279static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2280{
2281 struct batadv_softif_vlan *vlan;
2282
2283 /* recompute the global CRC for each VLAN */
2284 rcu_read_lock();
2285 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2286 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2287 }
2288 rcu_read_unlock();
2289}
2290
2291/**
2292 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2293 * @bat_priv: the bat priv with all the soft interface information
2294 * @orig_node: the orig_node for which the CRCs have to be updated
2295 */
2296static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2297 struct batadv_orig_node *orig_node)
2298{
2299 struct batadv_orig_node_vlan *vlan;
2300 uint32_t crc;
2301
2302 /* recompute the global CRC for each VLAN */
2303 rcu_read_lock();
2304 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2305 /* if orig_node is a backbone node for this VLAN, don't compute
2306 * the CRC as we ignore all the global entries over it
2307 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002308 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2309 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002310 continue;
2311
2312 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2313 vlan->tt.crc = crc;
2314 }
2315 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002316}
2317
Antonio Quartulliced72932013-04-24 16:37:51 +02002318/**
2319 * batadv_send_tt_request - send a TT Request message to a given node
2320 * @bat_priv: the bat priv with all the soft interface information
2321 * @dst_orig_node: the destination of the message
2322 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002323 * @tt_vlan: pointer to the first tvlv VLAN object to request
2324 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002325 * @full_table: ask for the entire translation table if true, while only for the
2326 * last TT diff otherwise
2327 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002328static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2329 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002330 uint8_t ttvn,
2331 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2332 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002333{
Marek Lindner335fbe02013-04-23 21:40:02 +08002334 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002335 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002336 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2337 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002338 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002339 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002341 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002342 if (!primary_if)
2343 goto out;
2344
2345 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002346 * reply from the same orig_node yet
2347 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002348 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002349 if (!tt_req_node)
2350 goto out;
2351
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002352 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2353 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002354 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002355 goto out;
2356
Marek Lindner335fbe02013-04-23 21:40:02 +08002357 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2358 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002359 tvlv_tt_data->num_vlan = htons(num_vlan);
2360
2361 /* send all the CRCs within the request. This is needed by intermediate
2362 * nodes to ensure they have the correct table before replying
2363 */
2364 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2365 for (i = 0; i < num_vlan; i++) {
2366 tt_vlan_req->vid = tt_vlan->vid;
2367 tt_vlan_req->crc = tt_vlan->crc;
2368
2369 tt_vlan_req++;
2370 tt_vlan++;
2371 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002372
2373 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002374 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002375
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002376 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002377 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002378
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002379 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002380 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2381 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002382 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002383 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002384
2385out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002386 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002387 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002388 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002389 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002390 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002391 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002392 kfree(tt_req_node);
2393 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002394 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002395 return ret;
2396}
2397
Marek Lindner335fbe02013-04-23 21:40:02 +08002398/**
2399 * batadv_send_other_tt_response - send reply to tt request concerning another
2400 * node's translation table
2401 * @bat_priv: the bat priv with all the soft interface information
2402 * @tt_data: tt data containing the tt request information
2403 * @req_src: mac address of tt request sender
2404 * @req_dst: mac address of tt request recipient
2405 *
2406 * Returns true if tt request reply was sent, false otherwise.
2407 */
2408static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2409 struct batadv_tvlv_tt_data *tt_data,
2410 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002411{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002412 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002413 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002414 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002415 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002416 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002417 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002418 uint8_t orig_ttvn, req_ttvn;
2419 uint16_t tvlv_len;
2420 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002421
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002422 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002423 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002424 req_src, tt_data->ttvn, req_dst,
2425 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002426
2427 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002428 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002429 if (!req_dst_orig_node)
2430 goto out;
2431
Marek Lindner335fbe02013-04-23 21:40:02 +08002432 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002433 if (!res_dst_orig_node)
2434 goto out;
2435
Antonio Quartullia73105b2011-04-27 14:27:44 +02002436 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002437 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002438
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002439 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002440 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002441 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002442 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2443 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002444 goto out;
2445
Antonio Quartulli015758d2011-07-09 17:52:13 +02002446 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002447 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002448 !req_dst_orig_node->tt_buff)
2449 full_table = true;
2450 else
2451 full_table = false;
2452
Marek Lindner335fbe02013-04-23 21:40:02 +08002453 /* TT fragmentation hasn't been implemented yet, so send as many
2454 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002455 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002456 if (!full_table) {
2457 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2458 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002460 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2461 &tvlv_tt_data,
2462 &tt_change,
2463 &tt_len);
2464 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002465 goto unlock;
2466
Antonio Quartullia73105b2011-04-27 14:27:44 +02002467 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002468 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002469 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002470 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2471 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002472 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2473 * in the initial part
2474 */
2475 tt_len = -1;
2476 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2477 &tvlv_tt_data,
2478 &tt_change,
2479 &tt_len);
2480 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002481 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002482
2483 /* fill the rest of the tvlv with the real TT entries */
2484 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2485 tt_change, tt_len,
2486 batadv_tt_global_valid,
2487 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002488 }
2489
Marek Lindnera19d3d82013-05-27 15:33:25 +08002490 /* Don't send the response, if larger than fragmented packet. */
2491 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2492 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2493 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2494 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2495 res_dst_orig_node->orig);
2496 goto out;
2497 }
2498
Marek Lindner335fbe02013-04-23 21:40:02 +08002499 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2500 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002501
2502 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002503 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002504
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002505 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002506 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2507 res_dst_orig_node->orig, req_dst_orig_node->orig,
2508 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002509
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002510 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002511
Marek Lindner335fbe02013-04-23 21:40:02 +08002512 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002513 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2514 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002515
Marek Lindner335fbe02013-04-23 21:40:02 +08002516 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002517 goto out;
2518
2519unlock:
2520 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2521
2522out:
2523 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002524 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002525 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002526 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002527 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002528 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002529}
Sven Eckelmann96412692012-06-05 22:31:30 +02002530
Marek Lindner335fbe02013-04-23 21:40:02 +08002531/**
2532 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2533 * translation table
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @tt_data: tt data containing the tt request information
2536 * @req_src: mac address of tt request sender
2537 *
2538 * Returns true if tt request reply was sent, false otherwise.
2539 */
2540static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2541 struct batadv_tvlv_tt_data *tt_data,
2542 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002543{
Marek Lindner335fbe02013-04-23 21:40:02 +08002544 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002545 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002546 struct batadv_tvlv_tt_change *tt_change;
2547 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002548 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002549 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002550 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002551 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002552
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002553 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002554 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002555 req_src, tt_data->ttvn,
2556 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002557
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002558 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002559
Sven Eckelmann807736f2012-07-15 22:26:51 +02002560 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002561 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002562
Marek Lindner335fbe02013-04-23 21:40:02 +08002563 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002564 if (!orig_node)
2565 goto out;
2566
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002567 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002568 if (!primary_if)
2569 goto out;
2570
2571 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002572 * is too big send the whole local translation table
2573 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002574 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002575 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002576 full_table = true;
2577 else
2578 full_table = false;
2579
Marek Lindner335fbe02013-04-23 21:40:02 +08002580 /* TT fragmentation hasn't been implemented yet, so send as many
2581 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002582 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002583 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002584 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002585
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002586 tt_len = bat_priv->tt.last_changeset_len;
2587 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2588 &tvlv_tt_data,
2589 &tt_change,
2590 &tt_len);
2591 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002592 goto unlock;
2593
Marek Lindner335fbe02013-04-23 21:40:02 +08002594 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002595 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002596 bat_priv->tt.last_changeset_len);
2597 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002598 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002599 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002600
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002601 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2602 * in the initial part
2603 */
2604 tt_len = -1;
2605 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2606 &tvlv_tt_data,
2607 &tt_change,
2608 &tt_len);
2609 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002610 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002611
2612 /* fill the rest of the tvlv with the real TT entries */
2613 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2614 tt_change, tt_len,
2615 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616 }
2617
Marek Lindner335fbe02013-04-23 21:40:02 +08002618 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2619 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002620
2621 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002622 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002623
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002624 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002625 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2626 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002627
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002629
Marek Lindner335fbe02013-04-23 21:40:02 +08002630 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002631 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2632 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002633
Antonio Quartullia73105b2011-04-27 14:27:44 +02002634 goto out;
2635
2636unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002637 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002638out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002639 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002640 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002641 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002642 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002643 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002644 kfree(tvlv_tt_data);
2645 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646 return true;
2647}
2648
Marek Lindner335fbe02013-04-23 21:40:02 +08002649/**
2650 * batadv_send_tt_response - send reply to tt request
2651 * @bat_priv: the bat priv with all the soft interface information
2652 * @tt_data: tt data containing the tt request information
2653 * @req_src: mac address of tt request sender
2654 * @req_dst: mac address of tt request recipient
2655 *
2656 * Returns true if tt request reply was sent, false otherwise.
2657 */
2658static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2659 struct batadv_tvlv_tt_data *tt_data,
2660 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002662 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002664 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002665 return batadv_send_other_tt_response(bat_priv, tt_data,
2666 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002667}
2668
Sven Eckelmann56303d32012-06-05 22:31:31 +02002669static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2670 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002671 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002672 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673{
2674 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002675 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676
2677 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002678 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2679 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002680 batadv_tt_global_del(bat_priv, orig_node,
2681 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002682 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002683 "tt removed by changes",
2684 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002685 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002686 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002687 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002688 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002689 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690 /* In case of problem while storing a
2691 * global_entry, we stop the updating
2692 * procedure without committing the
2693 * ttvn change. This will avoid to send
2694 * corrupted data on tt_request
2695 */
2696 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002697 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002698 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002699 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002700}
2701
Sven Eckelmann56303d32012-06-05 22:31:31 +02002702static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002703 struct batadv_tvlv_tt_change *tt_change,
2704 uint8_t ttvn, uint8_t *resp_src,
2705 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002706{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002707 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002708
Marek Lindner335fbe02013-04-23 21:40:02 +08002709 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002710 if (!orig_node)
2711 goto out;
2712
2713 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002714 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2715 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002717 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2718 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002719
2720 spin_lock_bh(&orig_node->tt_buff_lock);
2721 kfree(orig_node->tt_buff);
2722 orig_node->tt_buff_len = 0;
2723 orig_node->tt_buff = NULL;
2724 spin_unlock_bh(&orig_node->tt_buff_lock);
2725
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002726 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002727
2728out:
2729 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002730 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002731}
2732
Sven Eckelmann56303d32012-06-05 22:31:31 +02002733static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2734 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002735 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002736 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002737{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002738 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2739 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002740
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002741 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2742 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002743 atomic_set(&orig_node->last_ttvn, ttvn);
2744}
2745
Antonio Quartullic018ad32013-06-04 12:11:39 +02002746/**
2747 * batadv_is_my_client - check if a client is served by the local node
2748 * @bat_priv: the bat priv with all the soft interface information
2749 * @addr: the mac adress of the client to check
2750 * @vid: VLAN identifier
2751 *
2752 * Returns true if the client is served by this node, false otherwise.
2753 */
2754bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2755 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002756{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002757 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002758 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002759
Antonio Quartullic018ad32013-06-04 12:11:39 +02002760 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002761 if (!tt_local_entry)
2762 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002763 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002764 * consistency purpose)
2765 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002766 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2767 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002768 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002769 ret = true;
2770out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002771 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002772 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002773 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002774}
2775
Marek Lindner335fbe02013-04-23 21:40:02 +08002776/**
2777 * batadv_handle_tt_response - process incoming tt reply
2778 * @bat_priv: the bat priv with all the soft interface information
2779 * @tt_data: tt data containing the tt request information
2780 * @resp_src: mac address of tt reply sender
2781 * @num_entries: number of tt change entries appended to the tt data
2782 */
2783static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2784 struct batadv_tvlv_tt_data *tt_data,
2785 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002786{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002787 struct batadv_tt_req_node *node, *safe;
2788 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002789 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002790 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2791 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002792
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002793 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002794 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002795 resp_src, tt_data->ttvn, num_entries,
2796 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797
Marek Lindner335fbe02013-04-23 21:40:02 +08002798 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002799 if (!orig_node)
2800 goto out;
2801
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002802 spin_lock_bh(&orig_node->tt_lock);
2803
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002804 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2805 change_offset *= ntohs(tt_data->num_vlan);
2806 change_offset += sizeof(*tt_data);
2807 tvlv_ptr += change_offset;
2808
2809 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002810 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002811 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2812 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002813 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002814 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2815 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002816 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002818 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002819 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002820
2821 spin_unlock_bh(&orig_node->tt_lock);
2822
Antonio Quartullia73105b2011-04-27 14:27:44 +02002823 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002824 spin_lock_bh(&bat_priv->tt.req_list_lock);
2825 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002826 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827 continue;
2828 list_del(&node->list);
2829 kfree(node);
2830 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002831
Sven Eckelmann807736f2012-07-15 22:26:51 +02002832 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833out:
2834 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002835 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002836}
2837
Sven Eckelmann56303d32012-06-05 22:31:31 +02002838static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002839{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002840 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002841
Sven Eckelmann807736f2012-07-15 22:26:51 +02002842 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843
Sven Eckelmann807736f2012-07-15 22:26:51 +02002844 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002845 list_del(&node->list);
2846 kfree(node);
2847 }
2848
Sven Eckelmann807736f2012-07-15 22:26:51 +02002849 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002850}
2851
Sven Eckelmann56303d32012-06-05 22:31:31 +02002852static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002853{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002854 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002855
Sven Eckelmann807736f2012-07-15 22:26:51 +02002856 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2857 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002858 if (!batadv_has_timed_out(node->first_time,
2859 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002860 continue;
2861
2862 list_del(&node->list);
2863 kfree(node);
2864 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002865 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002866}
2867
2868/* This function checks whether the client already reached the
2869 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2870 * will not be sent.
2871 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002872 * returns true if the ROAMING_ADV can be sent, false otherwise
2873 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002874static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002875 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002876{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002877 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002878 bool ret = false;
2879
Sven Eckelmann807736f2012-07-15 22:26:51 +02002880 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002881 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002882 * reply from the same orig_node yet
2883 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002884 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002885 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002886 continue;
2887
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002888 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002889 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002890 continue;
2891
Sven Eckelmann3e348192012-05-16 20:23:22 +02002892 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002893 /* Sorry, you roamed too many times! */
2894 goto unlock;
2895 ret = true;
2896 break;
2897 }
2898
2899 if (!ret) {
2900 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2901 if (!tt_roam_node)
2902 goto unlock;
2903
2904 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002905 atomic_set(&tt_roam_node->counter,
2906 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002907 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2908
Sven Eckelmann807736f2012-07-15 22:26:51 +02002909 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002910 ret = true;
2911 }
2912
2913unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002914 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002915 return ret;
2916}
2917
Antonio Quartullic018ad32013-06-04 12:11:39 +02002918/**
2919 * batadv_send_roam_adv - send a roaming advertisement message
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @client: mac address of the roaming client
2922 * @vid: VLAN identifier
2923 * @orig_node: message destination
2924 *
2925 * Send a ROAMING_ADV message to the node which was previously serving this
2926 * client. This is done to inform the node that from now on all traffic destined
2927 * for this particular roamed client has to be forwarded to the sender of the
2928 * roaming message.
2929 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002930static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002931 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002932 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002933{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002934 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002935 struct batadv_tvlv_roam_adv tvlv_roam;
2936
2937 primary_if = batadv_primary_if_get_selected(bat_priv);
2938 if (!primary_if)
2939 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002940
2941 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002942 * already roamed to us too many times
2943 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002944 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002945 goto out;
2946
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002947 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002948 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2949 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002950
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002951 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002952
Marek Lindner122edaa2013-04-23 21:40:03 +08002953 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002954 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002955
2956 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2957 orig_node->orig, BATADV_TVLV_ROAM, 1,
2958 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002959
2960out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002961 if (primary_if)
2962 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002963}
2964
Sven Eckelmanna5130882012-05-16 20:23:16 +02002965static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002966{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002967 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02002968 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002969 struct batadv_priv *bat_priv;
2970
2971 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002972 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2973 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002974
Marek Lindnera19d3d82013-05-27 15:33:25 +08002975 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002976 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02002977 batadv_tt_req_purge(bat_priv);
2978 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002979
Antonio Quartulli72414442012-12-25 13:14:37 +01002980 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2981 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002982}
Antonio Quartullicc47f662011-04-27 14:27:57 +02002983
Sven Eckelmann56303d32012-06-05 22:31:31 +02002984void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002985{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08002986 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2987 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2988
Sven Eckelmann807736f2012-07-15 22:26:51 +02002989 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002990
Sven Eckelmanna5130882012-05-16 20:23:16 +02002991 batadv_tt_local_table_free(bat_priv);
2992 batadv_tt_global_table_free(bat_priv);
2993 batadv_tt_req_list_free(bat_priv);
2994 batadv_tt_changes_list_free(bat_priv);
2995 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002996
Sven Eckelmann807736f2012-07-15 22:26:51 +02002997 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002998}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002999
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003000/**
3001 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3002 * table and possibly count them in the TT size
3003 * @bat_priv: the bat priv with all the soft interface information
3004 * @flags: the flag to switch
3005 * @enable: whether to set or unset the flag
3006 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003007 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003008static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3009 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003010{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003011 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3012 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003013 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003014 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003015 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003016
3017 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003018 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003019
3020 for (i = 0; i < hash->size; i++) {
3021 head = &hash->table[i];
3022
3023 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003024 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003025 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003026 if (enable) {
3027 if ((tt_common_entry->flags & flags) == flags)
3028 continue;
3029 tt_common_entry->flags |= flags;
3030 } else {
3031 if (!(tt_common_entry->flags & flags))
3032 continue;
3033 tt_common_entry->flags &= ~flags;
3034 }
3035 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003036
3037 if (!count)
3038 continue;
3039
3040 batadv_tt_local_size_inc(bat_priv,
3041 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003042 }
3043 rcu_read_unlock();
3044 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003045}
3046
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003047/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003048static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003049{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003050 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003051 struct batadv_tt_common_entry *tt_common;
3052 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003053 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003054 struct hlist_head *head;
3055 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003056 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003057
3058 if (!hash)
3059 return;
3060
3061 for (i = 0; i < hash->size; i++) {
3062 head = &hash->table[i];
3063 list_lock = &hash->list_locks[i];
3064
3065 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003066 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003067 hash_entry) {
3068 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003069 continue;
3070
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003071 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003072 "Deleting local tt entry (%pM, vid: %d): pending\n",
3073 tt_common->addr,
3074 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003075
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003076 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003077 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003078 tt_local = container_of(tt_common,
3079 struct batadv_tt_local_entry,
3080 common);
3081 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003082 }
3083 spin_unlock_bh(list_lock);
3084 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003085}
3086
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003087/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003088 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3089 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003090 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003091 *
3092 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003093 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003094static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003095{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003096 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3097 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3098 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003099 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003100 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003101
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003102 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003103
Sven Eckelmanna5130882012-05-16 20:23:16 +02003104 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003105 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003106
3107 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003108 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003109 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003110 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003111 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003112
3113 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003114 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003115 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003116}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003117
Marek Lindnera19d3d82013-05-27 15:33:25 +08003118/**
3119 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3120 * have been queued in the time since the last commit
3121 * @bat_priv: the bat priv with all the soft interface information
3122 */
3123void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3124{
3125 spin_lock_bh(&bat_priv->tt.commit_lock);
3126 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003127 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003128}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003129
Sven Eckelmann56303d32012-06-05 22:31:31 +02003130bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003131 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003132{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003133 struct batadv_tt_local_entry *tt_local_entry = NULL;
3134 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003135 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003136 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003137
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003138 vlan = batadv_softif_vlan_get(bat_priv, vid);
3139 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003140 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003141
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003143 if (!tt_local_entry)
3144 goto out;
3145
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003146 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003147 if (!tt_global_entry)
3148 goto out;
3149
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003150 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003151 goto out;
3152
Marek Lindner5870adc2012-06-20 17:16:05 +02003153 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003154
3155out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003156 if (vlan)
3157 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003158 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003159 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003160 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003161 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003162 return ret;
3163}
Marek Lindnera943cac2011-07-30 13:10:18 +02003164
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003165/**
3166 * batadv_tt_update_orig - update global translation table with new tt
3167 * information received via ogms
3168 * @bat_priv: the bat priv with all the soft interface information
3169 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003170 * @tt_vlan: pointer to the first tvlv VLAN entry
3171 * @tt_num_vlan: number of tvlv VLAN entries
3172 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003173 * @tt_num_changes: number of tt changes inside the tt buffer
3174 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003175 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003176 */
3177static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3178 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003179 const void *tt_buff, uint16_t tt_num_vlan,
3180 struct batadv_tvlv_tt_change *tt_change,
3181 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003182{
3183 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003184 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003185 bool full_table = true;
3186
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003187 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003188 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003189 * increased by one -> we can apply the attached changes
3190 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003191 if ((!orig_node->tt_initialised && ttvn == 1) ||
3192 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003193 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003194 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3195 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003196 * In this case send a tt request
3197 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003198 if (!tt_num_changes) {
3199 full_table = false;
3200 goto request_table;
3201 }
3202
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003203 spin_lock_bh(&orig_node->tt_lock);
3204
Marek Lindner335fbe02013-04-23 21:40:02 +08003205 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003206 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003207 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003208
3209 /* Even if we received the precomputed crc with the OGM, we
3210 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003211 * in the global table
3212 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003213 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003214
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003215 spin_unlock_bh(&orig_node->tt_lock);
3216
Marek Lindnera943cac2011-07-30 13:10:18 +02003217 /* The ttvn alone is not enough to guarantee consistency
3218 * because a single value could represent different states
3219 * (due to the wrap around). Thus a node has to check whether
3220 * the resulting table (after applying the changes) is still
3221 * consistent or not. E.g. a node could disconnect while its
3222 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3223 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003224 * inconsistency
3225 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003226 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3227 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003228 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003229 } else {
3230 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003231 * in sync anymore -> request fresh tt data
3232 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003233 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003234 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3235 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003236request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003237 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003238 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3239 orig_node->orig, ttvn, orig_ttvn,
3240 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003241 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003242 tt_vlan, tt_num_vlan,
3243 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003244 return;
3245 }
3246 }
3247}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003248
Antonio Quartullic018ad32013-06-04 12:11:39 +02003249/**
3250 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3251 * @bat_priv: the bat priv with all the soft interface information
3252 * @addr: the mac address of the client to check
3253 * @vid: VLAN identifier
3254 *
3255 * Returns true if we know that the client has moved from its old originator
3256 * to another one. This entry is still kept for consistency purposes and will be
3257 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003258 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003259bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003260 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003261{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003262 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003263 bool ret = false;
3264
Antonio Quartullic018ad32013-06-04 12:11:39 +02003265 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003266 if (!tt_global_entry)
3267 goto out;
3268
Antonio Quartullic1d07432013-01-15 22:17:19 +10003269 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003270 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003271out:
3272 return ret;
3273}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003274
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003275/**
3276 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3277 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003278 * @addr: the mac address of the local client to query
3279 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003280 *
3281 * Returns true if the local client is known to be roaming (it is not served by
3282 * this node anymore) or not. If yes, the client is still present in the table
3283 * to keep the latter consistent with the node TTVN
3284 */
3285bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003286 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003287{
3288 struct batadv_tt_local_entry *tt_local_entry;
3289 bool ret = false;
3290
Antonio Quartullic018ad32013-06-04 12:11:39 +02003291 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003292 if (!tt_local_entry)
3293 goto out;
3294
3295 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3296 batadv_tt_local_entry_free_ref(tt_local_entry);
3297out:
3298 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003299}
3300
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003301bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3302 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003303 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003304 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003305{
3306 bool ret = false;
3307
Antonio Quartulli16052782013-06-04 12:11:41 +02003308 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003309 BATADV_TT_CLIENT_TEMP,
3310 atomic_read(&orig_node->last_ttvn)))
3311 goto out;
3312
3313 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003314 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3315 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003316 ret = true;
3317out:
3318 return ret;
3319}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003320
3321/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003322 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3323 * maximum packet size that can be transported through the mesh
3324 * @soft_iface: netdev struct of the mesh interface
3325 *
3326 * Remove entries older than 'timeout' and half timeout if more entries need
3327 * to be removed.
3328 */
3329void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3330{
3331 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3332 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3333 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3334 bool reduced = false;
3335
3336 spin_lock_bh(&bat_priv->tt.commit_lock);
3337
3338 while (true) {
3339 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3340 if (packet_size_max >= table_size)
3341 break;
3342
3343 batadv_tt_local_purge(bat_priv, timeout);
3344 batadv_tt_local_purge_pending_clients(bat_priv);
3345
3346 timeout /= 2;
3347 reduced = true;
3348 net_ratelimited_function(batadv_info, soft_iface,
3349 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3350 packet_size_max);
3351 }
3352
3353 /* commit these changes immediately, to avoid synchronization problem
3354 * with the TTVN
3355 */
3356 if (reduced)
3357 batadv_tt_local_commit_changes_nolock(bat_priv);
3358
3359 spin_unlock_bh(&bat_priv->tt.commit_lock);
3360}
3361
3362/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003363 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3364 * @bat_priv: the bat priv with all the soft interface information
3365 * @orig: the orig_node of the ogm
3366 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3367 * @tvlv_value: tvlv buffer containing the gateway data
3368 * @tvlv_value_len: tvlv buffer length
3369 */
3370static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3371 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003372 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003373 uint16_t tvlv_value_len)
3374{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003375 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3376 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003377 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003378 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003379
3380 if (tvlv_value_len < sizeof(*tt_data))
3381 return;
3382
3383 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3384 tvlv_value_len -= sizeof(*tt_data);
3385
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003386 num_vlan = ntohs(tt_data->num_vlan);
3387
3388 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3389 return;
3390
3391 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3392 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3393 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3394
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003395 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003396
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003397 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3398 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003399}
3400
3401/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003402 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3403 * container
3404 * @bat_priv: the bat priv with all the soft interface information
3405 * @src: mac address of tt tvlv sender
3406 * @dst: mac address of tt tvlv recipient
3407 * @tvlv_value: tvlv buffer containing the tt data
3408 * @tvlv_value_len: tvlv buffer length
3409 *
3410 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3411 * otherwise.
3412 */
3413static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3414 uint8_t *src, uint8_t *dst,
3415 void *tvlv_value,
3416 uint16_t tvlv_value_len)
3417{
3418 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003419 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003420 char tt_flag;
3421 bool ret;
3422
3423 if (tvlv_value_len < sizeof(*tt_data))
3424 return NET_RX_SUCCESS;
3425
3426 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3427 tvlv_value_len -= sizeof(*tt_data);
3428
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003429 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3430 tt_vlan_len *= ntohs(tt_data->num_vlan);
3431
3432 if (tvlv_value_len < tt_vlan_len)
3433 return NET_RX_SUCCESS;
3434
3435 tvlv_value_len -= tt_vlan_len;
3436 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003437
3438 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3439 case BATADV_TT_REQUEST:
3440 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3441
3442 /* If this node cannot provide a TT response the tt_request is
3443 * forwarded
3444 */
3445 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3446 if (!ret) {
3447 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3448 tt_flag = 'F';
3449 else
3450 tt_flag = '.';
3451
3452 batadv_dbg(BATADV_DBG_TT, bat_priv,
3453 "Routing TT_REQUEST to %pM [%c]\n",
3454 dst, tt_flag);
3455 /* tvlv API will re-route the packet */
3456 return NET_RX_DROP;
3457 }
3458 break;
3459 case BATADV_TT_RESPONSE:
3460 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3461
3462 if (batadv_is_my_mac(bat_priv, dst)) {
3463 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003464 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003465 return NET_RX_SUCCESS;
3466 }
3467
3468 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3469 tt_flag = 'F';
3470 else
3471 tt_flag = '.';
3472
3473 batadv_dbg(BATADV_DBG_TT, bat_priv,
3474 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3475
3476 /* tvlv API will re-route the packet */
3477 return NET_RX_DROP;
3478 }
3479
3480 return NET_RX_SUCCESS;
3481}
3482
3483/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003484 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3485 * @bat_priv: the bat priv with all the soft interface information
3486 * @src: mac address of tt tvlv sender
3487 * @dst: mac address of tt tvlv recipient
3488 * @tvlv_value: tvlv buffer containing the tt data
3489 * @tvlv_value_len: tvlv buffer length
3490 *
3491 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3492 * otherwise.
3493 */
3494static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3495 uint8_t *src, uint8_t *dst,
3496 void *tvlv_value,
3497 uint16_t tvlv_value_len)
3498{
3499 struct batadv_tvlv_roam_adv *roaming_adv;
3500 struct batadv_orig_node *orig_node = NULL;
3501
3502 /* If this node is not the intended recipient of the
3503 * roaming advertisement the packet is forwarded
3504 * (the tvlv API will re-route the packet).
3505 */
3506 if (!batadv_is_my_mac(bat_priv, dst))
3507 return NET_RX_DROP;
3508
Marek Lindner122edaa2013-04-23 21:40:03 +08003509 if (tvlv_value_len < sizeof(*roaming_adv))
3510 goto out;
3511
3512 orig_node = batadv_orig_hash_find(bat_priv, src);
3513 if (!orig_node)
3514 goto out;
3515
3516 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3517 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3518
3519 batadv_dbg(BATADV_DBG_TT, bat_priv,
3520 "Received ROAMING_ADV from %pM (client %pM)\n",
3521 src, roaming_adv->client);
3522
3523 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003524 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003525 atomic_read(&orig_node->last_ttvn) + 1);
3526
3527out:
3528 if (orig_node)
3529 batadv_orig_node_free_ref(orig_node);
3530 return NET_RX_SUCCESS;
3531}
3532
3533/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003534 * batadv_tt_init - initialise the translation table internals
3535 * @bat_priv: the bat priv with all the soft interface information
3536 *
3537 * Return 0 on success or negative error number in case of failure.
3538 */
3539int batadv_tt_init(struct batadv_priv *bat_priv)
3540{
3541 int ret;
3542
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003543 /* synchronized flags must be remote */
3544 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3545
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003546 ret = batadv_tt_local_init(bat_priv);
3547 if (ret < 0)
3548 return ret;
3549
3550 ret = batadv_tt_global_init(bat_priv);
3551 if (ret < 0)
3552 return ret;
3553
3554 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003555 batadv_tt_tvlv_unicast_handler_v1,
3556 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003557
Marek Lindner122edaa2013-04-23 21:40:03 +08003558 batadv_tvlv_handler_register(bat_priv, NULL,
3559 batadv_roam_tvlv_unicast_handler_v1,
3560 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3561
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003562 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3563 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3564 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3565
3566 return 1;
3567}