blob: 07b263a437d1b2488d0e882696669593df23eb62 [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2007-2014 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"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010027#include "multicast.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028
Antonio Quartulliced72932013-04-24 16:37:51 +020029#include <linux/crc32c.h>
Antonio Quartullia73105b2011-04-27 14:27:44 +020030
Antonio Quartullidec05072012-11-10 11:00:32 +010031/* hash class keys */
32static struct lock_class_key batadv_tt_local_hash_lock_class_key;
33static struct lock_class_key batadv_tt_global_hash_lock_class_key;
34
Sven Eckelmann56303d32012-06-05 22:31:31 +020035static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020036 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020037 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020038static void batadv_tt_purge(struct work_struct *work);
39static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020040batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020041static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42 struct batadv_orig_node *orig_node,
43 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020044 unsigned short vid, const char *message,
45 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Marek Lindner7aadf882011-02-18 12:28:09 +000047/* returns 1 if they are the same mac addr */
Sven Eckelmanna5130882012-05-16 20:23:16 +020048static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000049{
Sven Eckelmann56303d32012-06-05 22:31:31 +020050 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020051 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000052
dingtianhong323813e2013-12-26 19:40:39 +080053 return batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000054}
55
Antonio Quartullic018ad32013-06-04 12:11:39 +020056/**
57 * batadv_choose_tt - return the index of the tt entry in the hash table
58 * @data: pointer to the tt_common_entry object to map
59 * @size: the size of the hash table
60 *
61 * Returns the hash index where the object represented by 'data' should be
62 * stored at.
63 */
64static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
65{
66 struct batadv_tt_common_entry *tt;
67 uint32_t hash = 0;
68
69 tt = (struct batadv_tt_common_entry *)data;
70 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
71 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
72
73 hash += (hash << 3);
74 hash ^= (hash >> 11);
75 hash += (hash << 15);
76
77 return hash % size;
78}
79
80/**
81 * batadv_tt_hash_find - look for a client in the given hash table
82 * @hash: the hash table to search
83 * @addr: the mac address of the client to look for
84 * @vid: VLAN identifier
85 *
86 * Returns a pointer to the tt_common struct belonging to the searched client if
87 * found, NULL otherwise.
88 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_tt_common_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +020090batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
91 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +000092{
Marek Lindner7aadf882011-02-18 12:28:09 +000093 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +020094 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020095 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000096
97 if (!hash)
98 return NULL;
99
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100100 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200101 to_search.vid = vid;
102
103 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000104 head = &hash->table[index];
105
106 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200107 hlist_for_each_entry_rcu(tt, head, hash_entry) {
108 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000109 continue;
110
Antonio Quartullic018ad32013-06-04 12:11:39 +0200111 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200112 continue;
113
Antonio Quartullic018ad32013-06-04 12:11:39 +0200114 if (!atomic_inc_not_zero(&tt->refcount))
115 continue;
116
117 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000118 break;
119 }
120 rcu_read_unlock();
121
Antonio Quartullic018ad32013-06-04 12:11:39 +0200122 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100123}
124
Antonio Quartullic018ad32013-06-04 12:11:39 +0200125/**
126 * batadv_tt_local_hash_find - search the local table for a given client
127 * @bat_priv: the bat priv with all the soft interface information
128 * @addr: the mac address of the client to look for
129 * @vid: VLAN identifier
130 *
131 * Returns a pointer to the corresponding tt_local_entry struct if the client is
132 * found, NULL otherwise.
133 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200134static struct batadv_tt_local_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200135batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
136 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100137{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138 struct batadv_tt_common_entry *tt_common_entry;
139 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100140
Antonio Quartullic018ad32013-06-04 12:11:39 +0200141 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
142 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100143 if (tt_common_entry)
144 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200145 struct batadv_tt_local_entry,
146 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100147 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000148}
149
Antonio Quartullic018ad32013-06-04 12:11:39 +0200150/**
151 * batadv_tt_global_hash_find - search the global table for a given client
152 * @bat_priv: the bat priv with all the soft interface information
153 * @addr: the mac address of the client to look for
154 * @vid: VLAN identifier
155 *
156 * Returns a pointer to the corresponding tt_global_entry struct if the client
157 * is found, NULL otherwise.
158 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200159static struct batadv_tt_global_entry *
Antonio Quartullic018ad32013-06-04 12:11:39 +0200160batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
161 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000162{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200163 struct batadv_tt_common_entry *tt_common_entry;
164 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000165
Antonio Quartullic018ad32013-06-04 12:11:39 +0200166 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
167 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100168 if (tt_common_entry)
169 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200170 struct batadv_tt_global_entry,
171 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100172 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000173}
174
Sven Eckelmanna5130882012-05-16 20:23:16 +0200175static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200176batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200177{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100178 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
179 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200180}
181
Antonio Quartulli21026052013-05-07 00:29:22 +0200182/**
183 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
184 * tt_global_entry and possibly free it
185 * @tt_global_entry: the object to free
186 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200187static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200188batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200189{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200190 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200191 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200192 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200193 }
194}
195
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100196/**
197 * batadv_tt_global_hash_count - count the number of orig entries
198 * @hash: hash table containing the tt entries
199 * @addr: the mac address of the client to count entries for
200 * @vid: VLAN identifier
201 *
202 * Return the number of originators advertising the given address/data
203 * (excluding ourself).
204 */
205int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
206 const uint8_t *addr, unsigned short vid)
207{
208 struct batadv_tt_global_entry *tt_global_entry;
209 int count;
210
211 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
212 if (!tt_global_entry)
213 return 0;
214
215 count = atomic_read(&tt_global_entry->orig_list_count);
216 batadv_tt_global_entry_free_ref(tt_global_entry);
217
218 return count;
219}
220
Sven Eckelmanna5130882012-05-16 20:23:16 +0200221static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200222{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200223 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200224
Sven Eckelmann56303d32012-06-05 22:31:31 +0200225 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800226
227 /* We are in an rcu callback here, therefore we cannot use
228 * batadv_orig_node_free_ref() and its call_rcu():
229 * An rcu_barrier() wouldn't wait for that to finish
230 */
231 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200232 kfree(orig_entry);
233}
234
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200235/**
236 * batadv_tt_local_size_mod - change the size by v of the local table identified
237 * by vid
238 * @bat_priv: the bat priv with all the soft interface information
239 * @vid: the VLAN identifier of the sub-table to change
240 * @v: the amount to sum to the local table size
241 */
242static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
243 unsigned short vid, int v)
244{
245 struct batadv_softif_vlan *vlan;
246
247 vlan = batadv_softif_vlan_get(bat_priv, vid);
248 if (!vlan)
249 return;
250
251 atomic_add(v, &vlan->tt.num_entries);
252
253 batadv_softif_vlan_free_ref(vlan);
254}
255
256/**
257 * batadv_tt_local_size_inc - increase by one the local table size for the given
258 * vid
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 */
262static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
263 unsigned short vid)
264{
265 batadv_tt_local_size_mod(bat_priv, vid, 1);
266}
267
268/**
269 * batadv_tt_local_size_dec - decrease by one the local table size for the given
270 * vid
271 * @bat_priv: the bat priv with all the soft interface information
272 * @vid: the VLAN identifier
273 */
274static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
275 unsigned short vid)
276{
277 batadv_tt_local_size_mod(bat_priv, vid, -1);
278}
279
280/**
281 * batadv_tt_global_size_mod - change the size by v of the local table
282 * identified by vid
283 * @bat_priv: the bat priv with all the soft interface information
284 * @vid: the VLAN identifier
285 * @v: the amount to sum to the global table size
286 */
287static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
288 unsigned short vid, int v)
289{
290 struct batadv_orig_node_vlan *vlan;
291
292 vlan = batadv_orig_node_vlan_new(orig_node, vid);
293 if (!vlan)
294 return;
295
296 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
297 spin_lock_bh(&orig_node->vlan_list_lock);
298 list_del_rcu(&vlan->list);
299 spin_unlock_bh(&orig_node->vlan_list_lock);
300 batadv_orig_node_vlan_free_ref(vlan);
301 }
302
303 batadv_orig_node_vlan_free_ref(vlan);
304}
305
306/**
307 * batadv_tt_global_size_inc - increase by one the global table size for the
308 * given vid
309 * @orig_node: the originator which global table size has to be decreased
310 * @vid: the vlan identifier
311 */
312static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
313 unsigned short vid)
314{
315 batadv_tt_global_size_mod(orig_node, vid, 1);
316}
317
318/**
319 * batadv_tt_global_size_dec - decrease by one the global table size for the
320 * given vid
321 * @orig_node: the originator which global table size has to be decreased
322 * @vid: the vlan identifier
323 */
324static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
325 unsigned short vid)
326{
327 batadv_tt_global_size_mod(orig_node, vid, -1);
328}
329
Sven Eckelmanna5130882012-05-16 20:23:16 +0200330static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200332{
Antonio Quartullid657e622012-07-01 14:09:12 +0200333 if (!atomic_dec_and_test(&orig_entry->refcount))
334 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200335
Sven Eckelmanna5130882012-05-16 20:23:16 +0200336 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200337}
338
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200339/**
340 * batadv_tt_local_event - store a local TT event (ADD/DEL)
341 * @bat_priv: the bat priv with all the soft interface information
342 * @tt_local_entry: the TT entry involved in the event
343 * @event_flags: flags to store in the event structure
344 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200345static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200346 struct batadv_tt_local_entry *tt_local_entry,
347 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200348{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200350 struct batadv_tt_common_entry *common = &tt_local_entry->common;
351 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200352 bool event_removed = false;
353 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200354
355 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200356 if (!tt_change_node)
357 return;
358
Antonio Quartulliff66c972011-06-30 01:14:00 +0200359 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100360 memset(tt_change_node->change.reserved, 0,
361 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100362 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200363 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200364
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200365 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200366
367 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200368 spin_lock_bh(&bat_priv->tt.changes_list_lock);
369 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200370 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200371 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200372 continue;
373
374 /* DEL+ADD in the same orig interval have no effect and can be
375 * removed to avoid silly behaviour on the receiver side. The
376 * other way around (ADD+DEL) can happen in case of roaming of
377 * a client still in the NEW state. Roaming of NEW clients is
378 * now possible due to automatically recognition of "temporary"
379 * clients
380 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200381 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200382 if (!del_op_requested && del_op_entry)
383 goto del;
384 if (del_op_requested && !del_op_entry)
385 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200386
387 /* this is a second add in the same originator interval. It
388 * means that flags have been changed: update them!
389 */
390 if (!del_op_requested && !del_op_entry)
391 entry->change.flags = flags;
392
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200393 continue;
394del:
395 list_del(&entry->list);
396 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000397 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200398 event_removed = true;
399 goto unlock;
400 }
401
Antonio Quartullia73105b2011-04-27 14:27:44 +0200402 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200403 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200404
405unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200406 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200407
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200408 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200409 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200410 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200411 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200412}
413
Marek Lindner335fbe02013-04-23 21:40:02 +0800414/**
415 * batadv_tt_len - compute length in bytes of given number of tt changes
416 * @changes_num: number of tt changes
417 *
418 * Returns computed length in bytes.
419 */
420static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200421{
Marek Lindner335fbe02013-04-23 21:40:02 +0800422 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200423}
424
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200425/**
426 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
427 * @tt_len: available space
428 *
429 * Returns the number of entries.
430 */
431static uint16_t batadv_tt_entries(uint16_t tt_len)
432{
433 return tt_len / batadv_tt_len(1);
434}
435
Marek Lindnera19d3d82013-05-27 15:33:25 +0800436/**
437 * batadv_tt_local_table_transmit_size - calculates the local translation table
438 * size when transmitted over the air
439 * @bat_priv: the bat priv with all the soft interface information
440 *
441 * Returns local translation table size in bytes.
442 */
443static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
444{
445 uint16_t num_vlan = 0, tt_local_entries = 0;
446 struct batadv_softif_vlan *vlan;
447 int hdr_size;
448
449 rcu_read_lock();
450 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
451 num_vlan++;
452 tt_local_entries += atomic_read(&vlan->tt.num_entries);
453 }
454 rcu_read_unlock();
455
456 /* header size of tvlv encapsulated tt response payload */
457 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
458 hdr_size += sizeof(struct batadv_tvlv_hdr);
459 hdr_size += sizeof(struct batadv_tvlv_tt_data);
460 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
461
462 return hdr_size + batadv_tt_len(tt_local_entries);
463}
464
Sven Eckelmann56303d32012-06-05 22:31:31 +0200465static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200467 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200468 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469
Sven Eckelmann807736f2012-07-15 22:26:51 +0200470 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471
Sven Eckelmann807736f2012-07-15 22:26:51 +0200472 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200473 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474
Antonio Quartullidec05072012-11-10 11:00:32 +0100475 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
476 &batadv_tt_local_hash_lock_class_key);
477
Sven Eckelmann5346c352012-05-05 13:27:28 +0200478 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479}
480
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200481static void batadv_tt_global_free(struct batadv_priv *bat_priv,
482 struct batadv_tt_global_entry *tt_global,
483 const char *message)
484{
485 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200486 "Deleting global tt entry %pM (vid: %d): %s\n",
487 tt_global->common.addr,
488 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200489
490 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200491 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200492 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200493}
494
Antonio Quartullic018ad32013-06-04 12:11:39 +0200495/**
496 * batadv_tt_local_add - add a new client to the local table or update an
497 * existing client
498 * @soft_iface: netdev struct of the mesh interface
499 * @addr: the mac address of the client to add
500 * @vid: VLAN identifier
501 * @ifindex: index of the interface where the client is connected to (useful to
502 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100503 * @mark: the value contained in the skb->mark field of the received packet (if
504 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800505 *
506 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200507 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800508bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartulli9464d072013-11-16 12:03:48 +0100509 unsigned short vid, int ifindex, uint32_t mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200511 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200512 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100513 struct batadv_tt_global_entry *tt_global = NULL;
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200514 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200515 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200516 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200517 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800518 int hash_added, table_size, packet_size_max;
519 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200520 uint8_t remote_flags;
Antonio Quartulli9464d072013-11-16 12:03:48 +0100521 uint32_t match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200523 if (ifindex != BATADV_NULL_IFINDEX)
524 in_dev = dev_get_by_index(&init_net, ifindex);
525
Antonio Quartullic018ad32013-06-04 12:11:39 +0200526 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100527
528 if (!is_multicast_ether_addr(addr))
529 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530
Antonio Quartulli47c94652012-09-23 22:38:35 +0200531 if (tt_local) {
532 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200533 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
534 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200535 "Re-adding pending client %pM (vid: %d)\n",
536 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200537 /* whatever the reason why the PENDING flag was set,
538 * this is a client which was enqueued to be removed in
539 * this orig_interval. Since it popped up again, the
540 * flag can be reset like it was never enqueued
541 */
542 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
543 goto add_event;
544 }
545
546 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
547 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200548 "Roaming client %pM (vid: %d) came back to its original location\n",
549 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200550 /* the ROAM flag is set because this client roamed away
551 * and the node got a roaming_advertisement message. Now
552 * that the client popped up again at its original
553 * location such flag can be unset
554 */
555 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
556 roamed_back = true;
557 }
558 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 }
560
Marek Lindnera19d3d82013-05-27 15:33:25 +0800561 /* Ignore the client if we cannot send it in a full table response. */
562 table_size = batadv_tt_local_table_transmit_size(bat_priv);
563 table_size += batadv_tt_len(1);
564 packet_size_max = atomic_read(&bat_priv->packet_size_max);
565 if (table_size > packet_size_max) {
566 net_ratelimited_function(batadv_info, soft_iface,
567 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
568 table_size, packet_size_max, addr);
569 goto out;
570 }
571
Antonio Quartulli47c94652012-09-23 22:38:35 +0200572 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
573 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200574 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200575
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200576 /* increase the refcounter of the related vlan */
577 vlan = batadv_softif_vlan_get(bat_priv, vid);
578
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200579 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200580 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
581 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200582 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100584 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100585 /* The local entry has to be marked as NEW to avoid to send it in
586 * a full table response going out before the next ttvn increment
587 * (consistency check)
588 */
589 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200590 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200591 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200592 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
593 atomic_set(&tt_local->common.refcount, 2);
594 tt_local->last_seen = jiffies;
595 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100597 /* the batman interface mac and multicast addresses should never be
598 * purged
599 */
600 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
601 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200602 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Sven Eckelmann807736f2012-07-15 22:26:51 +0200604 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200605 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200606 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100607
608 if (unlikely(hash_added != 0)) {
609 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200610 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200611 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100612 goto out;
613 }
614
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200615add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200616 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200617
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200618check_roaming:
619 /* Check whether it is a roaming, but don't do anything if the roaming
620 * process has already been handled
621 */
622 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200623 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200624 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200625 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800626 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200627 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200628 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200629 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200630 }
631 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200632 if (roamed_back) {
633 batadv_tt_global_free(bat_priv, tt_global,
634 "Roaming canceled");
635 tt_global = NULL;
636 } else {
637 /* The global entry has to be marked as ROAMING and
638 * has to be kept for consistency purpose
639 */
640 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
641 tt_global->roam_at = jiffies;
642 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200643 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200644
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200645 /* store the current remote flags before altering them. This helps
646 * understanding is flags are changing or not
647 */
648 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800649
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200650 if (batadv_is_wifi_netdev(in_dev))
651 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
652 else
653 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
654
Antonio Quartulli9464d072013-11-16 12:03:48 +0100655 /* check the mark in the skb: if it's equal to the configured
656 * isolation_mark, it means the packet is coming from an isolated
657 * non-mesh client
658 */
659 match_mark = (mark & bat_priv->isolation_mark_mask);
660 if (bat_priv->isolation_mark_mask &&
661 match_mark == bat_priv->isolation_mark)
662 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
663 else
664 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
665
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200666 /* if any "dynamic" flag has been modified, resend an ADD event for this
667 * entry so that all the nodes can get the new flags
668 */
669 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
670 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
671
672 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200673out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200674 if (in_dev)
675 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200676 if (tt_local)
677 batadv_tt_local_entry_free_ref(tt_local);
678 if (tt_global)
679 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800680 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681}
682
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800683/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200684 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
685 * within a TT Response directed to another node
686 * @orig_node: originator for which the TT data has to be prepared
687 * @tt_data: uninitialised pointer to the address of the TVLV buffer
688 * @tt_change: uninitialised pointer to the address of the area where the TT
689 * changed can be stored
690 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
691 * function reserves the amount of space needed to send the entire global TT
692 * table. In case of success the value is updated with the real amount of
693 * reserved bytes
694
695 * Allocate the needed amount of memory for the entire TT TVLV and write its
696 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
697 * objects, one per active VLAN served by the originator node.
698 *
699 * Return the size of the allocated buffer or 0 in case of failure.
700 */
701static uint16_t
702batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
703 struct batadv_tvlv_tt_data **tt_data,
704 struct batadv_tvlv_tt_change **tt_change,
705 int32_t *tt_len)
706{
707 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
708 struct batadv_tvlv_tt_vlan_data *tt_vlan;
709 struct batadv_orig_node_vlan *vlan;
710 uint8_t *tt_change_ptr;
711
712 rcu_read_lock();
713 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
714 num_vlan++;
715 num_entries += atomic_read(&vlan->tt.num_entries);
716 }
717
718 change_offset = sizeof(**tt_data);
719 change_offset += num_vlan * sizeof(*tt_vlan);
720
721 /* if tt_len is negative, allocate the space needed by the full table */
722 if (*tt_len < 0)
723 *tt_len = batadv_tt_len(num_entries);
724
725 tvlv_len = *tt_len;
726 tvlv_len += change_offset;
727
728 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
729 if (!*tt_data) {
730 *tt_len = 0;
731 goto out;
732 }
733
734 (*tt_data)->flags = BATADV_NO_FLAGS;
735 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
736 (*tt_data)->num_vlan = htons(num_vlan);
737
738 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
739 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
740 tt_vlan->vid = htons(vlan->vid);
741 tt_vlan->crc = htonl(vlan->tt.crc);
742
743 tt_vlan++;
744 }
745
746 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
747 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
748
749out:
750 rcu_read_unlock();
751 return tvlv_len;
752}
753
754/**
755 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
756 * node
757 * @bat_priv: the bat priv with all the soft interface information
758 * @tt_data: uninitialised pointer to the address of the TVLV buffer
759 * @tt_change: uninitialised pointer to the address of the area where the TT
760 * changes can be stored
761 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
762 * function reserves the amount of space needed to send the entire local TT
763 * table. In case of success the value is updated with the real amount of
764 * reserved bytes
765 *
766 * Allocate the needed amount of memory for the entire TT TVLV and write its
767 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
768 * objects, one per active VLAN.
769 *
770 * Return the size of the allocated buffer or 0 in case of failure.
771 */
772static uint16_t
773batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
774 struct batadv_tvlv_tt_data **tt_data,
775 struct batadv_tvlv_tt_change **tt_change,
776 int32_t *tt_len)
777{
778 struct batadv_tvlv_tt_vlan_data *tt_vlan;
779 struct batadv_softif_vlan *vlan;
780 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
781 uint8_t *tt_change_ptr;
782 int change_offset;
783
784 rcu_read_lock();
785 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
786 num_vlan++;
787 num_entries += atomic_read(&vlan->tt.num_entries);
788 }
789
790 change_offset = sizeof(**tt_data);
791 change_offset += num_vlan * sizeof(*tt_vlan);
792
793 /* if tt_len is negative, allocate the space needed by the full table */
794 if (*tt_len < 0)
795 *tt_len = batadv_tt_len(num_entries);
796
797 tvlv_len = *tt_len;
798 tvlv_len += change_offset;
799
800 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
801 if (!*tt_data) {
802 tvlv_len = 0;
803 goto out;
804 }
805
806 (*tt_data)->flags = BATADV_NO_FLAGS;
807 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
808 (*tt_data)->num_vlan = htons(num_vlan);
809
810 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
811 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
812 tt_vlan->vid = htons(vlan->vid);
813 tt_vlan->crc = htonl(vlan->tt.crc);
814
815 tt_vlan++;
816 }
817
818 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
819 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
820
821out:
822 rcu_read_unlock();
823 return tvlv_len;
824}
825
826/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800827 * batadv_tt_tvlv_container_update - update the translation table tvlv container
828 * after local tt changes have been committed
829 * @bat_priv: the bat priv with all the soft interface information
830 */
831static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800833 struct batadv_tt_change_node *entry, *safe;
834 struct batadv_tvlv_tt_data *tt_data;
835 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200836 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800837 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200838 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200840 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
841 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800842
843 /* if we have too many changes for one packet don't send any
844 * and wait for the tt table request which will be fragmented
845 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800846 if (tt_diff_len > bat_priv->soft_iface->mtu)
847 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800848
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200849 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
850 &tt_change, &tt_diff_len);
851 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800852 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800853
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800854 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800855
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800856 if (tt_diff_len == 0)
857 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800858
Sven Eckelmann807736f2012-07-15 22:26:51 +0200859 spin_lock_bh(&bat_priv->tt.changes_list_lock);
860 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
Sven Eckelmann807736f2012-07-15 22:26:51 +0200862 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100863 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800864 if (tt_diff_entries_count < tt_diff_entries_num) {
865 memcpy(tt_change + tt_diff_entries_count,
866 &entry->change,
867 sizeof(struct batadv_tvlv_tt_change));
868 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200870 list_del(&entry->list);
871 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200873 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
Antonio Quartullia73105b2011-04-27 14:27:44 +0200875 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200876 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
877 kfree(bat_priv->tt.last_changeset);
878 bat_priv->tt.last_changeset_len = 0;
879 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800880 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800881 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800882 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800883 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200884 * instead of providing the diff
885 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800886 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200887 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800888 memcpy(bat_priv->tt.last_changeset,
889 tt_change, tt_change_len);
890 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200891 }
892 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200893 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800895container_register:
896 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200897 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800898 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899}
900
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200901int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902{
903 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200904 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200905 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200906 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100907 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200908 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200909 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200911 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200912 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100913 int last_seen_secs;
914 int last_seen_msecs;
915 unsigned long last_seen_jiffies;
916 bool no_purge;
917 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918
Marek Lindner30da63a2012-08-03 17:15:46 +0200919 primary_if = batadv_seq_print_text_primary_if_get(seq);
920 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200921 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100923 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200924 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
925 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100926 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200927 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929 for (i = 0; i < hash->size; i++) {
930 head = &hash->table[i];
931
Marek Lindner7aadf882011-02-18 12:28:09 +0000932 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800933 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000934 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100935 tt_local = container_of(tt_common_entry,
936 struct batadv_tt_local_entry,
937 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200938 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100939 last_seen_jiffies = jiffies - tt_local->last_seen;
940 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
941 last_seen_secs = last_seen_msecs / 1000;
942 last_seen_msecs = last_seen_msecs % 1000;
943
944 no_purge = tt_common_entry->flags & np_flag;
945
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200946 vlan = batadv_softif_vlan_get(bat_priv, vid);
947 if (!vlan) {
948 seq_printf(seq, "Cannot retrieve VLAN %d\n",
949 BATADV_PRINT_VID(vid));
950 continue;
951 }
952
953 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100954 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100955 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200956 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100957 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200958 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100959 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100960 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200961 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100962 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200963 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100964 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100965 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100966 (tt_common_entry->flags &
967 BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100968 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200969 no_purge ? 0 : last_seen_msecs,
970 vlan->tt.crc);
971
972 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000973 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000974 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200976out:
977 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200978 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200979 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000980}
981
Sven Eckelmann56303d32012-06-05 22:31:31 +0200982static void
983batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
984 struct batadv_tt_local_entry *tt_local_entry,
985 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000986{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200987 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000988
Antonio Quartulli015758d2011-07-09 17:52:13 +0200989 /* The local client has to be marked as "pending to be removed" but has
990 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200991 * response issued before the net ttvn increment (consistency check)
992 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200993 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100994
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200995 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200996 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
997 tt_local_entry->common.addr,
998 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999}
1000
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001001/**
1002 * batadv_tt_local_remove - logically remove an entry from the local table
1003 * @bat_priv: the bat priv with all the soft interface information
1004 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001005 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001006 * @message: message to append to the log on deletion
1007 * @roaming: true if the deletion is due to a roaming event
1008 *
1009 * Returns the flags assigned to the local entry before being deleted
1010 */
1011uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001012 const uint8_t *addr, unsigned short vid,
1013 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001015 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001016 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001017 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018
Antonio Quartullic018ad32013-06-04 12:11:39 +02001019 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001020 if (!tt_local_entry)
1021 goto out;
1022
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001023 curr_flags = tt_local_entry->common.flags;
1024
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001025 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001026 /* if this global entry addition is due to a roaming, the node has to
1027 * mark the local entry as "roamed" in order to correctly reroute
1028 * packets later
1029 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001030 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001031 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001032 /* mark the local client as ROAMed */
1033 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1034 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001035
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001036 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1037 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1038 message);
1039 goto out;
1040 }
1041 /* if this client has been added right now, it is possible to
1042 * immediately purge it
1043 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001044 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001045 hlist_del_rcu(&tt_local_entry->common.hash_entry);
1046 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001047
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001048 /* decrease the reference held for this vlan */
1049 vlan = batadv_softif_vlan_get(bat_priv, vid);
1050 batadv_softif_vlan_free_ref(vlan);
1051 batadv_softif_vlan_free_ref(vlan);
1052
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001053out:
1054 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001055 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001056
1057 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058}
1059
Marek Lindnera19d3d82013-05-27 15:33:25 +08001060/**
1061 * batadv_tt_local_purge_list - purge inactive tt local entries
1062 * @bat_priv: the bat priv with all the soft interface information
1063 * @head: pointer to the list containing the local tt entries
1064 * @timeout: parameter deciding whether a given tt local entry is considered
1065 * inactive or not
1066 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001067static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001068 struct hlist_head *head,
1069 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001070{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001071 struct batadv_tt_local_entry *tt_local_entry;
1072 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001074
Sasha Levinb67bfe02013-02-27 17:06:00 -08001075 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001076 hash_entry) {
1077 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001078 struct batadv_tt_local_entry,
1079 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001080 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1081 continue;
1082
1083 /* entry already marked for deletion */
1084 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1085 continue;
1086
Marek Lindnera19d3d82013-05-27 15:33:25 +08001087 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001088 continue;
1089
1090 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1091 BATADV_TT_CLIENT_DEL, "timed out");
1092 }
1093}
1094
Marek Lindnera19d3d82013-05-27 15:33:25 +08001095/**
1096 * batadv_tt_local_purge - purge inactive tt local entries
1097 * @bat_priv: the bat priv with all the soft interface information
1098 * @timeout: parameter deciding whether a given tt local entry is considered
1099 * inactive or not
1100 */
1101static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1102 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001103{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001104 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001105 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001106 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001107 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001109 for (i = 0; i < hash->size; i++) {
1110 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001111 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001112
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001113 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001114 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001115 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001117}
1118
Sven Eckelmann56303d32012-06-05 22:31:31 +02001119static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001120{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001121 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001122 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001123 struct batadv_tt_common_entry *tt_common_entry;
1124 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001125 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001126 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001127 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001128 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001129
Sven Eckelmann807736f2012-07-15 22:26:51 +02001130 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001131 return;
1132
Sven Eckelmann807736f2012-07-15 22:26:51 +02001133 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001134
1135 for (i = 0; i < hash->size; i++) {
1136 head = &hash->table[i];
1137 list_lock = &hash->list_locks[i];
1138
1139 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001140 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001141 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001142 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001143 tt_local = container_of(tt_common_entry,
1144 struct batadv_tt_local_entry,
1145 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001146
1147 /* decrease the reference held for this vlan */
1148 vlan = batadv_softif_vlan_get(bat_priv,
1149 tt_common_entry->vid);
1150 batadv_softif_vlan_free_ref(vlan);
1151 batadv_softif_vlan_free_ref(vlan);
1152
Sven Eckelmann56303d32012-06-05 22:31:31 +02001153 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001154 }
1155 spin_unlock_bh(list_lock);
1156 }
1157
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001158 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001159
Sven Eckelmann807736f2012-07-15 22:26:51 +02001160 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001161}
1162
Sven Eckelmann56303d32012-06-05 22:31:31 +02001163static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001165 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001166 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001167
Sven Eckelmann807736f2012-07-15 22:26:51 +02001168 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169
Sven Eckelmann807736f2012-07-15 22:26:51 +02001170 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001171 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001172
Antonio Quartullidec05072012-11-10 11:00:32 +01001173 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1174 &batadv_tt_global_hash_lock_class_key);
1175
Sven Eckelmann5346c352012-05-05 13:27:28 +02001176 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001177}
1178
Sven Eckelmann56303d32012-06-05 22:31:31 +02001179static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001180{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001182
Sven Eckelmann807736f2012-07-15 22:26:51 +02001183 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001184
Sven Eckelmann807736f2012-07-15 22:26:51 +02001185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001186 list) {
1187 list_del(&entry->list);
1188 kfree(entry);
1189 }
1190
Sven Eckelmann807736f2012-07-15 22:26:51 +02001191 atomic_set(&bat_priv->tt.local_changes, 0);
1192 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001193}
1194
Antonio Quartullid657e622012-07-01 14:09:12 +02001195/* retrieves the orig_tt_list_entry belonging to orig_node from the
1196 * batadv_tt_global_entry list
1197 *
1198 * returns it with an increased refcounter, NULL if not found
1199 */
1200static struct batadv_tt_orig_list_entry *
1201batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1202 const struct batadv_orig_node *orig_node)
1203{
1204 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1205 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001206
1207 rcu_read_lock();
1208 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001210 if (tmp_orig_entry->orig_node != orig_node)
1211 continue;
1212 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1213 continue;
1214
1215 orig_entry = tmp_orig_entry;
1216 break;
1217 }
1218 rcu_read_unlock();
1219
1220 return orig_entry;
1221}
1222
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001223/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001224 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001226static bool
1227batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1228 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001229{
Antonio Quartullid657e622012-07-01 14:09:12 +02001230 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001231 bool found = false;
1232
Antonio Quartullid657e622012-07-01 14:09:12 +02001233 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1234 if (orig_entry) {
1235 found = true;
1236 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001237 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001238
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001239 return found;
1240}
1241
Sven Eckelmanna5130882012-05-16 20:23:16 +02001242static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001243batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001244 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001245{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001246 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001247
Antonio Quartullid657e622012-07-01 14:09:12 +02001248 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001249 if (orig_entry) {
1250 /* refresh the ttvn: the current value could be a bogus one that
1251 * was added during a "temporary client detection"
1252 */
1253 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001254 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001255 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001256
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001257 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1258 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001259 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001260
1261 INIT_HLIST_NODE(&orig_entry->list);
1262 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001263 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001264 orig_entry->orig_node = orig_node;
1265 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001266 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001267
Antonio Quartullid657e622012-07-01 14:09:12 +02001268 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001269 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001270 &tt_global->orig_list);
1271 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001272 atomic_inc(&tt_global->orig_list_count);
1273
Antonio Quartullid657e622012-07-01 14:09:12 +02001274out:
1275 if (orig_entry)
1276 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001277}
1278
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001279/**
1280 * batadv_tt_global_add - add a new TT global entry or update an existing one
1281 * @bat_priv: the bat priv with all the soft interface information
1282 * @orig_node: the originator announcing the client
1283 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001284 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001285 * @flags: TT flags that have to be set for this non-mesh client
1286 * @ttvn: the tt version number ever announcing this non-mesh client
1287 *
1288 * Add a new TT global entry for the given originator. If the entry already
1289 * exists add a new reference to the given originator (a global entry can have
1290 * references to multiple originators) and adjust the flags attribute to reflect
1291 * the function argument.
1292 * If a TT local entry exists for this non-mesh client remove it.
1293 *
1294 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001295 *
1296 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001297 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001298static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1299 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001300 const unsigned char *tt_addr,
1301 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001302 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001303{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001304 struct batadv_tt_global_entry *tt_global_entry;
1305 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001306 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001307 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001308 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001309 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001310
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001311 /* ignore global entries from backbone nodes */
1312 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1313 return true;
1314
Antonio Quartullic018ad32013-06-04 12:11:39 +02001315 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1316 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001317
1318 /* if the node already has a local client for this entry, it has to wait
1319 * for a roaming advertisement instead of manually messing up the global
1320 * table
1321 */
1322 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1323 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1324 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001325
Antonio Quartullia73105b2011-04-27 14:27:44 +02001326 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001327 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001328 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001329 goto out;
1330
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001331 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001332 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001333 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001334
Antonio Quartullid4f44692012-05-25 00:00:54 +02001335 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001336 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001337 /* node must store current time in case of roaming. This is
1338 * needed to purge this entry out on timeout (if nobody claims
1339 * it)
1340 */
1341 if (flags & BATADV_TT_CLIENT_ROAM)
1342 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001343 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001344 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001345
1346 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001347 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001348 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001349
Sven Eckelmann807736f2012-07-15 22:26:51 +02001350 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001351 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001352 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001353 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001354
1355 if (unlikely(hash_added != 0)) {
1356 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001357 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001358 goto out_remove;
1359 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001360 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001361 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001362 /* If there is already a global entry, we can use this one for
1363 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001364 * But if we are trying to add a temporary client then here are
1365 * two options at this point:
1366 * 1) the global client is not a temporary client: the global
1367 * client has to be left as it is, temporary information
1368 * should never override any already known client state
1369 * 2) the global client is a temporary client: purge the
1370 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001371 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001372 if (flags & BATADV_TT_CLIENT_TEMP) {
1373 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1374 goto out;
1375 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1376 orig_node))
1377 goto out_remove;
1378 batadv_tt_global_del_orig_list(tt_global_entry);
1379 goto add_orig_entry;
1380 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001381
1382 /* if the client was temporary added before receiving the first
1383 * OGM announcing it, we have to clear the TEMP flag
1384 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001385 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001386
Antonio Quartullie9c001362012-11-07 15:05:33 +01001387 /* the change can carry possible "attribute" flags like the
1388 * TT_CLIENT_WIFI, therefore they have to be copied in the
1389 * client entry
1390 */
1391 tt_global_entry->common.flags |= flags;
1392
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001393 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1394 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001395 * delete + roaming change for this originator.
1396 *
1397 * We should first delete the old originator before adding the
1398 * new one.
1399 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001400 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001401 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001402 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001403 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001404 }
1405 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001406add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001407 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001408 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001409
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001410 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001411 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1412 common->addr, BATADV_PRINT_VID(common->vid),
1413 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001414 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001415
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001416out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001417 /* Do not remove multicast addresses from the local hash on
1418 * global additions
1419 */
1420 if (is_multicast_ether_addr(tt_addr))
1421 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001422
Antonio Quartullia73105b2011-04-27 14:27:44 +02001423 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001424 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001425 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001426 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001427 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1428
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001429 if (!(flags & BATADV_TT_CLIENT_ROAM))
1430 /* this is a normal global add. Therefore the client is not in a
1431 * roaming state anymore.
1432 */
1433 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1434
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001435out:
1436 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001437 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001438 if (tt_local_entry)
1439 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001440 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001441}
1442
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001443/**
1444 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001445 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001446 * @tt_global_entry: global translation table entry to be analyzed
1447 *
1448 * This functon assumes the caller holds rcu_read_lock().
1449 * Returns best originator list entry or NULL on errors.
1450 */
1451static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001452batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1453 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001454{
Antonio Quartulli46274562013-09-03 11:10:24 +02001455 struct batadv_neigh_node *router, *best_router = NULL;
1456 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001457 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001458 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001459
1460 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001461 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001462 router = batadv_orig_router_get(orig_entry->orig_node,
1463 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001464 if (!router)
1465 continue;
1466
Antonio Quartulli46274562013-09-03 11:10:24 +02001467 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001468 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1469 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001470 batadv_neigh_node_free_ref(router);
1471 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001472 }
1473
Antonio Quartulli46274562013-09-03 11:10:24 +02001474 /* release the refcount for the "old" best */
1475 if (best_router)
1476 batadv_neigh_node_free_ref(best_router);
1477
1478 best_entry = orig_entry;
1479 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001480 }
1481
Antonio Quartulli46274562013-09-03 11:10:24 +02001482 if (best_router)
1483 batadv_neigh_node_free_ref(best_router);
1484
Sven Eckelmann981d8902012-10-07 13:34:15 +02001485 return best_entry;
1486}
1487
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001488/**
1489 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1490 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001491 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001492 * @tt_global_entry: global translation table entry to be printed
1493 * @seq: debugfs table seq_file struct
1494 *
1495 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001496 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001497static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001498batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1499 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001500 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001501{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001502 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001503 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001504 struct batadv_orig_node_vlan *vlan;
1505 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001506 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001507 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001508
1509 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001510 flags = tt_common_entry->flags;
1511
Antonio Quartulli46274562013-09-03 11:10:24 +02001512 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001513 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001514 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1515 tt_common_entry->vid);
1516 if (!vlan) {
1517 seq_printf(seq,
1518 " * Cannot retrieve VLAN %d for originator %pM\n",
1519 BATADV_PRINT_VID(tt_common_entry->vid),
1520 best_entry->orig_node->orig);
1521 goto print_list;
1522 }
1523
Sven Eckelmann981d8902012-10-07 13:34:15 +02001524 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001525 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001526 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001527 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001528 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001529 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001530 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001531 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1532 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001533 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001534 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001535
1536 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001537 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001538
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001539print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001540 head = &tt_global_entry->orig_list;
1541
Sasha Levinb67bfe02013-02-27 17:06:00 -08001542 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001543 if (best_entry == orig_entry)
1544 continue;
1545
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001546 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1547 tt_common_entry->vid);
1548 if (!vlan) {
1549 seq_printf(seq,
1550 " + Cannot retrieve VLAN %d for originator %pM\n",
1551 BATADV_PRINT_VID(tt_common_entry->vid),
1552 orig_entry->orig_node->orig);
1553 continue;
1554 }
1555
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001556 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001557 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001558 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001559 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001560 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001561 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001562 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001563 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001564 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001565 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001566 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001567
1568 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001569 }
1570}
1571
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001572int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001573{
1574 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001575 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001576 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001577 struct batadv_tt_common_entry *tt_common_entry;
1578 struct batadv_tt_global_entry *tt_global;
1579 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001580 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001581 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001582
Marek Lindner30da63a2012-08-03 17:15:46 +02001583 primary_if = batadv_seq_print_text_primary_if_get(seq);
1584 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001585 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001586
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001587 seq_printf(seq,
1588 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001589 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001590 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1591 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1592 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001593
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001594 for (i = 0; i < hash->size; i++) {
1595 head = &hash->table[i];
1596
Marek Lindner7aadf882011-02-18 12:28:09 +00001597 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001598 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001599 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001600 tt_global = container_of(tt_common_entry,
1601 struct batadv_tt_global_entry,
1602 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001603 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001604 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001605 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001606 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001607out:
1608 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001609 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001610 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001611}
1612
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001613/**
1614 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1615 * @tt_global_entry: the global entry to remove the orig_entry from
1616 * @orig_entry: the orig entry to remove and free
1617 *
1618 * Remove an orig_entry from its list in the given tt_global_entry and
1619 * free this orig_entry afterwards.
1620 */
1621static void
1622batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1623 struct batadv_tt_orig_list_entry *orig_entry)
1624{
1625 batadv_tt_global_size_dec(orig_entry->orig_node,
1626 tt_global_entry->common.vid);
1627 atomic_dec(&tt_global_entry->orig_list_count);
1628 hlist_del_rcu(&orig_entry->list);
1629 batadv_tt_orig_list_entry_free_ref(orig_entry);
1630}
1631
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001632/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001633static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001634batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001635{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001636 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001637 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001638 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001639
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001640 spin_lock_bh(&tt_global_entry->list_lock);
1641 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001642 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1643 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001644 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001645}
1646
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001647/**
1648 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1649 * @bat_priv: the bat priv with all the soft interface information
1650 * @tt_global_entry: the global entry to remove the orig_node from
1651 * @orig_node: the originator announcing the client
1652 * @message: message to append to the log on deletion
1653 *
1654 * Remove the given orig_node and its according orig_entry from the given
1655 * global tt entry.
1656 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001657static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001658batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1659 struct batadv_tt_global_entry *tt_global_entry,
1660 struct batadv_orig_node *orig_node,
1661 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001662{
1663 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001664 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001665 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001666 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001667
1668 spin_lock_bh(&tt_global_entry->list_lock);
1669 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001670 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001671 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001672 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001673 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001674 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001675 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001676 tt_global_entry->common.addr,
1677 BATADV_PRINT_VID(vid), message);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001678 batadv_tt_global_del_orig_entry(tt_global_entry,
1679 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001680 }
1681 }
1682 spin_unlock_bh(&tt_global_entry->list_lock);
1683}
1684
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001685/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001686 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1687 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001688 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001689static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001690batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1691 struct batadv_tt_global_entry *tt_global_entry,
1692 struct batadv_orig_node *orig_node,
1693 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001694{
1695 bool last_entry = true;
1696 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001697 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001698
1699 /* no local entry exists, case 1:
1700 * Check if this is the last one or if other entries exist.
1701 */
1702
1703 rcu_read_lock();
1704 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001705 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001706 if (orig_entry->orig_node != orig_node) {
1707 last_entry = false;
1708 break;
1709 }
1710 }
1711 rcu_read_unlock();
1712
1713 if (last_entry) {
1714 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001715 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001716 tt_global_entry->roam_at = jiffies;
1717 } else
1718 /* there is another entry, we can simply delete this
1719 * one and can still use the other one.
1720 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001721 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1722 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001723}
1724
Antonio Quartullic018ad32013-06-04 12:11:39 +02001725/**
1726 * batadv_tt_global_del - remove a client from the global table
1727 * @bat_priv: the bat priv with all the soft interface information
1728 * @orig_node: an originator serving this client
1729 * @addr: the mac address of the client
1730 * @vid: VLAN identifier
1731 * @message: a message explaining the reason for deleting the client to print
1732 * for debugging purpose
1733 * @roaming: true if the deletion has been triggered by a roaming event
1734 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001735static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1736 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001737 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001738 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001739{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001740 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001741 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001742
Antonio Quartullic018ad32013-06-04 12:11:39 +02001743 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001744 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001745 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001746
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001747 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001748 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1749 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001750
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001751 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001752 batadv_tt_global_free(bat_priv, tt_global_entry,
1753 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001754
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001755 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001756 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001757
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001758 /* if we are deleting a global entry due to a roam
1759 * event, there are two possibilities:
1760 * 1) the client roamed from node A to node B => if there
1761 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001762 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001763 * wait for node B to claim it. In case of timeout
1764 * the entry is purged.
1765 *
1766 * If there are other originators left, we directly delete
1767 * the originator.
1768 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001769 * the global entry, since it is useless now.
1770 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001771 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001772 tt_global_entry->common.addr,
1773 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001774 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001775 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001776 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001777 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001778 } else
1779 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001780 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1781 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001782
Antonio Quartullicc47f662011-04-27 14:27:57 +02001783out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001784 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001785 batadv_tt_global_entry_free_ref(tt_global_entry);
1786 if (local_entry)
1787 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001788}
1789
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001790/**
1791 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1792 * given originator matching the provided vid
1793 * @bat_priv: the bat priv with all the soft interface information
1794 * @orig_node: the originator owning the entries to remove
1795 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1796 * removed
1797 * @message: debug message to print as "reason"
1798 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001799void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1800 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001801 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001802 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001803{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001804 struct batadv_tt_global_entry *tt_global;
1805 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001806 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001807 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001808 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001809 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001810 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001811 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001812
Simon Wunderlich6e801492011-10-19 10:28:26 +02001813 if (!hash)
1814 return;
1815
Antonio Quartullia73105b2011-04-27 14:27:44 +02001816 for (i = 0; i < hash->size; i++) {
1817 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001818 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001819
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001820 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001821 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001822 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001823 /* remove only matching entries */
1824 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1825 continue;
1826
Sven Eckelmann56303d32012-06-05 22:31:31 +02001827 tt_global = container_of(tt_common_entry,
1828 struct batadv_tt_global_entry,
1829 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001830
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001831 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1832 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001833
Sven Eckelmann56303d32012-06-05 22:31:31 +02001834 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001835 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001836 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001837 "Deleting global tt entry %pM (vid: %d): %s\n",
1838 tt_global->common.addr,
1839 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001840 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001841 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001842 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001843 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001844 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001845 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01001846 orig_node->capa_initialized &= ~BATADV_ORIG_CAPA_HAS_TT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001847}
1848
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001849static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1850 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001851{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001852 bool purge = false;
1853 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1854 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001855
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001856 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1857 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1858 purge = true;
1859 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001860 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001861
1862 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1863 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1864 purge = true;
1865 *msg = "Temporary client timeout\n";
1866 }
1867
1868 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001869}
1870
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001871static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001872{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001873 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001874 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001875 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001876 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001877 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001878 char *msg = NULL;
1879 struct batadv_tt_common_entry *tt_common;
1880 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001881
Antonio Quartullicc47f662011-04-27 14:27:57 +02001882 for (i = 0; i < hash->size; i++) {
1883 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001884 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001885
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001886 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001887 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001888 hash_entry) {
1889 tt_global = container_of(tt_common,
1890 struct batadv_tt_global_entry,
1891 common);
1892
1893 if (!batadv_tt_global_to_purge(tt_global, &msg))
1894 continue;
1895
1896 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001897 "Deleting global tt entry %pM (vid: %d): %s\n",
1898 tt_global->common.addr,
1899 BATADV_PRINT_VID(tt_global->common.vid),
1900 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001901
Sasha Levinb67bfe02013-02-27 17:06:00 -08001902 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001903
1904 batadv_tt_global_entry_free_ref(tt_global);
1905 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001906 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001907 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001908}
1909
Sven Eckelmann56303d32012-06-05 22:31:31 +02001910static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001911{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001912 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001913 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001914 struct batadv_tt_common_entry *tt_common_entry;
1915 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001916 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001917 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001918 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001919
Sven Eckelmann807736f2012-07-15 22:26:51 +02001920 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001921 return;
1922
Sven Eckelmann807736f2012-07-15 22:26:51 +02001923 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001924
1925 for (i = 0; i < hash->size; i++) {
1926 head = &hash->table[i];
1927 list_lock = &hash->list_locks[i];
1928
1929 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001930 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001931 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001932 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001933 tt_global = container_of(tt_common_entry,
1934 struct batadv_tt_global_entry,
1935 common);
1936 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001937 }
1938 spin_unlock_bh(list_lock);
1939 }
1940
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001941 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001942
Sven Eckelmann807736f2012-07-15 22:26:51 +02001943 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001944}
1945
Sven Eckelmann56303d32012-06-05 22:31:31 +02001946static bool
1947_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1948 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001949{
1950 bool ret = false;
1951
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001952 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1953 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001954 ret = true;
1955
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001956 /* check if the two clients are marked as isolated */
1957 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1958 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1959 ret = true;
1960
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001961 return ret;
1962}
1963
Antonio Quartullic018ad32013-06-04 12:11:39 +02001964/**
1965 * batadv_transtable_search - get the mesh destination for a given client
1966 * @bat_priv: the bat priv with all the soft interface information
1967 * @src: mac address of the source client
1968 * @addr: mac address of the destination client
1969 * @vid: VLAN identifier
1970 *
1971 * Returns a pointer to the originator that was selected as destination in the
1972 * mesh for contacting the client 'addr', NULL otherwise.
1973 * In case of multiple originators serving the same client, the function returns
1974 * the best one (best in terms of metric towards the destination node).
1975 *
1976 * If the two clients are AP isolated the function returns NULL.
1977 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001978struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1979 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001980 const uint8_t *addr,
1981 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001982{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001983 struct batadv_tt_local_entry *tt_local_entry = NULL;
1984 struct batadv_tt_global_entry *tt_global_entry = NULL;
1985 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001986 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001987
Antonio Quartullieceb22a2013-11-16 12:03:51 +01001988 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001989 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001990 if (!tt_local_entry ||
1991 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001992 goto out;
1993 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001994
Antonio Quartullic018ad32013-06-04 12:11:39 +02001995 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001996 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001997 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001998
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001999 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002000 * isolation
2001 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002002 if (tt_local_entry &&
2003 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002004 goto out;
2005
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002006 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002007 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002008 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002009 if (best_entry)
2010 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002011 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2012 orig_node = NULL;
2013 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002014
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002015out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002016 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002017 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002018 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002019 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002020
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002021 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002022}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002023
Antonio Quartulliced72932013-04-24 16:37:51 +02002024/**
2025 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2026 * to the given orig_node
2027 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002028 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002029 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002030 *
2031 * This function computes the checksum for the global table corresponding to a
2032 * specific originator. In particular, the checksum is computed as follows: For
2033 * each client connected to the originator the CRC32C of the MAC address and the
2034 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2035 * together.
2036 *
2037 * The idea behind is that CRC32C should be used as much as possible in order to
2038 * produce a unique hash of the table, but since the order which is used to feed
2039 * the CRC32C function affects the result and since every node in the network
2040 * probably sorts the clients differently, the hash function cannot be directly
2041 * computed over the entire table. Hence the CRC32C is used only on
2042 * the single client entry, while all the results are then xor'ed together
2043 * because the XOR operation can combine them all while trying to reduce the
2044 * noise as much as possible.
2045 *
2046 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002047 */
2048static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002049 struct batadv_orig_node *orig_node,
2050 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002051{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002052 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002053 struct batadv_tt_common_entry *tt_common;
2054 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002055 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002056 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002057 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002058 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002059
2060 for (i = 0; i < hash->size; i++) {
2061 head = &hash->table[i];
2062
2063 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002064 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002065 tt_global = container_of(tt_common,
2066 struct batadv_tt_global_entry,
2067 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002068 /* compute the CRC only for entries belonging to the
2069 * VLAN identified by the vid passed as parameter
2070 */
2071 if (tt_common->vid != vid)
2072 continue;
2073
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002074 /* Roaming clients are in the global table for
2075 * consistency only. They don't have to be
2076 * taken into account while computing the
2077 * global crc
2078 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002079 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002080 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002081 /* Temporary clients have not been announced yet, so
2082 * they have to be skipped while computing the global
2083 * crc
2084 */
2085 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2086 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002087
2088 /* find out if this global entry is announced by this
2089 * originator
2090 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002091 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002092 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002093 continue;
2094
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002095 /* use network order to read the VID: this ensures that
2096 * every node reads the bytes in the same order.
2097 */
2098 tmp_vid = htons(tt_common->vid);
2099 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002100
2101 /* compute the CRC on flags that have to be kept in sync
2102 * among nodes
2103 */
2104 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2105 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2106
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002107 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002108 }
2109 rcu_read_unlock();
2110 }
2111
Antonio Quartulliced72932013-04-24 16:37:51 +02002112 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002113}
2114
Antonio Quartulliced72932013-04-24 16:37:51 +02002115/**
2116 * batadv_tt_local_crc - calculates the checksum of the local table
2117 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002118 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002119 *
2120 * For details about the computation, please refer to the documentation for
2121 * batadv_tt_global_crc().
2122 *
2123 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002124 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002125static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2126 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002127{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002128 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002129 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002130 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002131 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002132 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002133 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002134
2135 for (i = 0; i < hash->size; i++) {
2136 head = &hash->table[i];
2137
2138 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002139 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002140 /* compute the CRC only for entries belonging to the
2141 * VLAN identified by vid
2142 */
2143 if (tt_common->vid != vid)
2144 continue;
2145
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002146 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002147 * account while computing the CRC
2148 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002149 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002150 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002151
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002152 /* use network order to read the VID: this ensures that
2153 * every node reads the bytes in the same order.
2154 */
2155 tmp_vid = htons(tt_common->vid);
2156 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002157
2158 /* compute the CRC on flags that have to be kept in sync
2159 * among nodes
2160 */
2161 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2162 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2163
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002164 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002165 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166 rcu_read_unlock();
2167 }
2168
Antonio Quartulliced72932013-04-24 16:37:51 +02002169 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002170}
2171
Sven Eckelmann56303d32012-06-05 22:31:31 +02002172static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002173{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002174 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002175
Sven Eckelmann807736f2012-07-15 22:26:51 +02002176 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002177
Sven Eckelmann807736f2012-07-15 22:26:51 +02002178 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002179 list_del(&node->list);
2180 kfree(node);
2181 }
2182
Sven Eckelmann807736f2012-07-15 22:26:51 +02002183 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002184}
2185
Sven Eckelmann56303d32012-06-05 22:31:31 +02002186static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2187 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002188 const void *tt_buff,
2189 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002190{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002191 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002192 * last OGM (the OGM could carry no changes)
2193 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002194 spin_lock_bh(&orig_node->tt_buff_lock);
2195 if (tt_buff_len > 0) {
2196 kfree(orig_node->tt_buff);
2197 orig_node->tt_buff_len = 0;
2198 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2199 if (orig_node->tt_buff) {
2200 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2201 orig_node->tt_buff_len = tt_buff_len;
2202 }
2203 }
2204 spin_unlock_bh(&orig_node->tt_buff_lock);
2205}
2206
Sven Eckelmann56303d32012-06-05 22:31:31 +02002207static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002208{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002209 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002210
Sven Eckelmann807736f2012-07-15 22:26:51 +02002211 spin_lock_bh(&bat_priv->tt.req_list_lock);
2212 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002213 if (batadv_has_timed_out(node->issued_at,
2214 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002215 list_del(&node->list);
2216 kfree(node);
2217 }
2218 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002219 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002220}
2221
2222/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002223 * has already been issued for this orig_node, NULL otherwise
2224 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002225static struct batadv_tt_req_node *
2226batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2227 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002228{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002229 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002230
Sven Eckelmann807736f2012-07-15 22:26:51 +02002231 spin_lock_bh(&bat_priv->tt.req_list_lock);
2232 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002233 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2234 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002235 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002236 goto unlock;
2237 }
2238
2239 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2240 if (!tt_req_node)
2241 goto unlock;
2242
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002243 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002244 tt_req_node->issued_at = jiffies;
2245
Sven Eckelmann807736f2012-07-15 22:26:51 +02002246 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002247unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002248 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002249 return tt_req_node;
2250}
2251
Marek Lindner335fbe02013-04-23 21:40:02 +08002252/**
2253 * batadv_tt_local_valid - verify that given tt entry is a valid one
2254 * @entry_ptr: to be checked local tt entry
2255 * @data_ptr: not used but definition required to satisfy the callback prototype
2256 *
2257 * Returns 1 if the entry is a valid, 0 otherwise.
2258 */
2259static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002260{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002262
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002263 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002264 return 0;
2265 return 1;
2266}
2267
Sven Eckelmanna5130882012-05-16 20:23:16 +02002268static int batadv_tt_global_valid(const void *entry_ptr,
2269 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002270{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002271 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2272 const struct batadv_tt_global_entry *tt_global_entry;
2273 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002274
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002275 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2276 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002277 return 0;
2278
Sven Eckelmann56303d32012-06-05 22:31:31 +02002279 tt_global_entry = container_of(tt_common_entry,
2280 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002281 common);
2282
Sven Eckelmanna5130882012-05-16 20:23:16 +02002283 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002284}
2285
Marek Lindner335fbe02013-04-23 21:40:02 +08002286/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002287 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2288 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002289 * @bat_priv: the bat priv with all the soft interface information
2290 * @hash: hash table containing the tt entries
2291 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002292 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002293 * @valid_cb: function to filter tt change entries
2294 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002295 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002296static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2297 struct batadv_hashtable *hash,
2298 void *tvlv_buff, uint16_t tt_len,
2299 int (*valid_cb)(const void *, const void *),
2300 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002301{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002302 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002303 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002304 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002305 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002306 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002307
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002308 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002309 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310
2311 rcu_read_lock();
2312 for (i = 0; i < hash->size; i++) {
2313 head = &hash->table[i];
2314
Sasha Levinb67bfe02013-02-27 17:06:00 -08002315 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002316 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002317 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002318 break;
2319
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002320 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002321 continue;
2322
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002323 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002324 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002325 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002326 memset(tt_change->reserved, 0,
2327 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002328
Marek Lindner335fbe02013-04-23 21:40:02 +08002329 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002330 tt_change++;
2331 }
2332 }
2333 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002334}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002335
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002336/**
2337 * batadv_tt_global_check_crc - check if all the CRCs are correct
2338 * @orig_node: originator for which the CRCs have to be checked
2339 * @tt_vlan: pointer to the first tvlv VLAN entry
2340 * @num_vlan: number of tvlv VLAN entries
2341 * @create: if true, create VLAN objects if not found
2342 *
2343 * Return true if all the received CRCs match the locally stored ones, false
2344 * otherwise
2345 */
2346static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2347 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2348 uint16_t num_vlan)
2349{
2350 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2351 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002352 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002353 int i;
2354
2355 /* check if each received CRC matches the locally stored one */
2356 for (i = 0; i < num_vlan; i++) {
2357 tt_vlan_tmp = tt_vlan + i;
2358
2359 /* if orig_node is a backbone node for this VLAN, don't check
2360 * the CRC as we ignore all the global entries over it
2361 */
2362 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002363 orig_node->orig,
2364 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002365 continue;
2366
2367 vlan = batadv_orig_node_vlan_get(orig_node,
2368 ntohs(tt_vlan_tmp->vid));
2369 if (!vlan)
2370 return false;
2371
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002372 crc = vlan->tt.crc;
2373 batadv_orig_node_vlan_free_ref(vlan);
2374
2375 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002376 return false;
2377 }
2378
2379 return true;
2380}
2381
2382/**
2383 * batadv_tt_local_update_crc - update all the local CRCs
2384 * @bat_priv: the bat priv with all the soft interface information
2385 */
2386static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2387{
2388 struct batadv_softif_vlan *vlan;
2389
2390 /* recompute the global CRC for each VLAN */
2391 rcu_read_lock();
2392 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2393 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2394 }
2395 rcu_read_unlock();
2396}
2397
2398/**
2399 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2400 * @bat_priv: the bat priv with all the soft interface information
2401 * @orig_node: the orig_node for which the CRCs have to be updated
2402 */
2403static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2404 struct batadv_orig_node *orig_node)
2405{
2406 struct batadv_orig_node_vlan *vlan;
2407 uint32_t crc;
2408
2409 /* recompute the global CRC for each VLAN */
2410 rcu_read_lock();
2411 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2412 /* if orig_node is a backbone node for this VLAN, don't compute
2413 * the CRC as we ignore all the global entries over it
2414 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002415 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2416 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002417 continue;
2418
2419 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2420 vlan->tt.crc = crc;
2421 }
2422 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002423}
2424
Antonio Quartulliced72932013-04-24 16:37:51 +02002425/**
2426 * batadv_send_tt_request - send a TT Request message to a given node
2427 * @bat_priv: the bat priv with all the soft interface information
2428 * @dst_orig_node: the destination of the message
2429 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002430 * @tt_vlan: pointer to the first tvlv VLAN object to request
2431 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002432 * @full_table: ask for the entire translation table if true, while only for the
2433 * last TT diff otherwise
2434 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002435static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2436 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002437 uint8_t ttvn,
2438 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2439 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002440{
Marek Lindner335fbe02013-04-23 21:40:02 +08002441 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002442 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002443 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2444 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002445 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002446 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002447
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002448 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002449 if (!primary_if)
2450 goto out;
2451
2452 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002453 * reply from the same orig_node yet
2454 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002455 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002456 if (!tt_req_node)
2457 goto out;
2458
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002459 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2460 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002461 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002462 goto out;
2463
Marek Lindner335fbe02013-04-23 21:40:02 +08002464 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2465 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002466 tvlv_tt_data->num_vlan = htons(num_vlan);
2467
2468 /* send all the CRCs within the request. This is needed by intermediate
2469 * nodes to ensure they have the correct table before replying
2470 */
2471 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2472 for (i = 0; i < num_vlan; i++) {
2473 tt_vlan_req->vid = tt_vlan->vid;
2474 tt_vlan_req->crc = tt_vlan->crc;
2475
2476 tt_vlan_req++;
2477 tt_vlan++;
2478 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002479
2480 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002481 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002482
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002483 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002484 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002485
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002486 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002487 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2488 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002489 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002490 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002491
2492out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002493 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002494 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002495 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002496 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002497 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002498 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002499 kfree(tt_req_node);
2500 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002501 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002502 return ret;
2503}
2504
Marek Lindner335fbe02013-04-23 21:40:02 +08002505/**
2506 * batadv_send_other_tt_response - send reply to tt request concerning another
2507 * node's translation table
2508 * @bat_priv: the bat priv with all the soft interface information
2509 * @tt_data: tt data containing the tt request information
2510 * @req_src: mac address of tt request sender
2511 * @req_dst: mac address of tt request recipient
2512 *
2513 * Returns true if tt request reply was sent, false otherwise.
2514 */
2515static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2516 struct batadv_tvlv_tt_data *tt_data,
2517 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002518{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002519 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002520 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002521 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002522 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002523 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002524 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002525 uint8_t orig_ttvn, req_ttvn;
2526 uint16_t tvlv_len;
2527 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002528
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002529 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002530 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002531 req_src, tt_data->ttvn, req_dst,
2532 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533
2534 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002535 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002536 if (!req_dst_orig_node)
2537 goto out;
2538
Marek Lindner335fbe02013-04-23 21:40:02 +08002539 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002540 if (!res_dst_orig_node)
2541 goto out;
2542
Antonio Quartullia73105b2011-04-27 14:27:44 +02002543 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002544 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002545
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002546 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002547 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002548 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002549 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2550 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002551 goto out;
2552
Antonio Quartulli015758d2011-07-09 17:52:13 +02002553 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002554 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002555 !req_dst_orig_node->tt_buff)
2556 full_table = true;
2557 else
2558 full_table = false;
2559
Marek Lindner335fbe02013-04-23 21:40:02 +08002560 /* TT fragmentation hasn't been implemented yet, so send as many
2561 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002562 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002563 if (!full_table) {
2564 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2565 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002566
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002567 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2568 &tvlv_tt_data,
2569 &tt_change,
2570 &tt_len);
2571 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002572 goto unlock;
2573
Antonio Quartullia73105b2011-04-27 14:27:44 +02002574 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002575 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002576 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002577 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2578 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002579 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2580 * in the initial part
2581 */
2582 tt_len = -1;
2583 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2584 &tvlv_tt_data,
2585 &tt_change,
2586 &tt_len);
2587 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002588 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002589
2590 /* fill the rest of the tvlv with the real TT entries */
2591 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2592 tt_change, tt_len,
2593 batadv_tt_global_valid,
2594 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002595 }
2596
Marek Lindnera19d3d82013-05-27 15:33:25 +08002597 /* Don't send the response, if larger than fragmented packet. */
2598 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2599 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2600 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2601 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2602 res_dst_orig_node->orig);
2603 goto out;
2604 }
2605
Marek Lindner335fbe02013-04-23 21:40:02 +08002606 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2607 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002608
2609 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002610 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002611
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002612 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002613 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2614 res_dst_orig_node->orig, req_dst_orig_node->orig,
2615 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002616
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002617 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002618
Marek Lindner335fbe02013-04-23 21:40:02 +08002619 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002620 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2621 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002622
Marek Lindner335fbe02013-04-23 21:40:02 +08002623 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002624 goto out;
2625
2626unlock:
2627 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2628
2629out:
2630 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002631 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002632 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002633 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002634 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002635 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002636}
Sven Eckelmann96412692012-06-05 22:31:30 +02002637
Marek Lindner335fbe02013-04-23 21:40:02 +08002638/**
2639 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2640 * translation table
2641 * @bat_priv: the bat priv with all the soft interface information
2642 * @tt_data: tt data containing the tt request information
2643 * @req_src: mac address of tt request sender
2644 *
2645 * Returns true if tt request reply was sent, false otherwise.
2646 */
2647static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2648 struct batadv_tvlv_tt_data *tt_data,
2649 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002650{
Marek Lindner335fbe02013-04-23 21:40:02 +08002651 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002652 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002653 struct batadv_tvlv_tt_change *tt_change;
2654 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002655 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002656 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002657 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002658 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002659
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002660 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002661 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002662 req_src, tt_data->ttvn,
2663 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002664
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002665 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002666
Sven Eckelmann807736f2012-07-15 22:26:51 +02002667 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002668 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002669
Marek Lindner335fbe02013-04-23 21:40:02 +08002670 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002671 if (!orig_node)
2672 goto out;
2673
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002674 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002675 if (!primary_if)
2676 goto out;
2677
2678 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002679 * is too big send the whole local translation table
2680 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002681 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002682 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002683 full_table = true;
2684 else
2685 full_table = false;
2686
Marek Lindner335fbe02013-04-23 21:40:02 +08002687 /* TT fragmentation hasn't been implemented yet, so send as many
2688 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002689 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002691 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002692
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002693 tt_len = bat_priv->tt.last_changeset_len;
2694 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2695 &tvlv_tt_data,
2696 &tt_change,
2697 &tt_len);
2698 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002699 goto unlock;
2700
Marek Lindner335fbe02013-04-23 21:40:02 +08002701 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002702 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002703 bat_priv->tt.last_changeset_len);
2704 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002706 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002707
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002708 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2709 * in the initial part
2710 */
2711 tt_len = -1;
2712 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2713 &tvlv_tt_data,
2714 &tt_change,
2715 &tt_len);
2716 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002717 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002718
2719 /* fill the rest of the tvlv with the real TT entries */
2720 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2721 tt_change, tt_len,
2722 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002723 }
2724
Marek Lindner335fbe02013-04-23 21:40:02 +08002725 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2726 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002727
2728 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002729 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002730
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002731 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002732 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2733 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002734
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002735 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002736
Marek Lindner335fbe02013-04-23 21:40:02 +08002737 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002738 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2739 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002740
Antonio Quartullia73105b2011-04-27 14:27:44 +02002741 goto out;
2742
2743unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002744 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002745out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002746 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002747 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002748 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002749 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002750 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002751 kfree(tvlv_tt_data);
2752 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002753 return true;
2754}
2755
Marek Lindner335fbe02013-04-23 21:40:02 +08002756/**
2757 * batadv_send_tt_response - send reply to tt request
2758 * @bat_priv: the bat priv with all the soft interface information
2759 * @tt_data: tt data containing the tt request information
2760 * @req_src: mac address of tt request sender
2761 * @req_dst: mac address of tt request recipient
2762 *
2763 * Returns true if tt request reply was sent, false otherwise.
2764 */
2765static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2766 struct batadv_tvlv_tt_data *tt_data,
2767 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002768{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002769 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002770 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002771 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2772 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002773}
2774
Sven Eckelmann56303d32012-06-05 22:31:31 +02002775static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2776 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002777 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002778 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002779{
2780 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002781 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002782
2783 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002784 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2785 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002786 batadv_tt_global_del(bat_priv, orig_node,
2787 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002788 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002789 "tt removed by changes",
2790 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002791 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002792 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002793 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002794 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002795 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002796 /* In case of problem while storing a
2797 * global_entry, we stop the updating
2798 * procedure without committing the
2799 * ttvn change. This will avoid to send
2800 * corrupted data on tt_request
2801 */
2802 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002803 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002804 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01002805 orig_node->capa_initialized |= BATADV_ORIG_CAPA_HAS_TT;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002806}
2807
Sven Eckelmann56303d32012-06-05 22:31:31 +02002808static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002809 struct batadv_tvlv_tt_change *tt_change,
2810 uint8_t ttvn, uint8_t *resp_src,
2811 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002812{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002813 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002814
Marek Lindner335fbe02013-04-23 21:40:02 +08002815 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002816 if (!orig_node)
2817 goto out;
2818
2819 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002820 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2821 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002822
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002823 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2824 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002825
2826 spin_lock_bh(&orig_node->tt_buff_lock);
2827 kfree(orig_node->tt_buff);
2828 orig_node->tt_buff_len = 0;
2829 orig_node->tt_buff = NULL;
2830 spin_unlock_bh(&orig_node->tt_buff_lock);
2831
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002832 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833
2834out:
2835 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002836 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837}
2838
Sven Eckelmann56303d32012-06-05 22:31:31 +02002839static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2840 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002841 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002842 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002843{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002844 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2845 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002846
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002847 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2848 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002849 atomic_set(&orig_node->last_ttvn, ttvn);
2850}
2851
Antonio Quartullic018ad32013-06-04 12:11:39 +02002852/**
2853 * batadv_is_my_client - check if a client is served by the local node
2854 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002855 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002856 * @vid: VLAN identifier
2857 *
2858 * Returns true if the client is served by this node, false otherwise.
2859 */
2860bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2861 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002862{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002863 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002864 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002865
Antonio Quartullic018ad32013-06-04 12:11:39 +02002866 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002867 if (!tt_local_entry)
2868 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002869 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002870 * consistency purpose)
2871 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002872 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2873 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002874 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002875 ret = true;
2876out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002877 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002878 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002879 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002880}
2881
Marek Lindner335fbe02013-04-23 21:40:02 +08002882/**
2883 * batadv_handle_tt_response - process incoming tt reply
2884 * @bat_priv: the bat priv with all the soft interface information
2885 * @tt_data: tt data containing the tt request information
2886 * @resp_src: mac address of tt reply sender
2887 * @num_entries: number of tt change entries appended to the tt data
2888 */
2889static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2890 struct batadv_tvlv_tt_data *tt_data,
2891 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002892{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002893 struct batadv_tt_req_node *node, *safe;
2894 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002895 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002896 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2897 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002898
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002899 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002900 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002901 resp_src, tt_data->ttvn, num_entries,
2902 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002903
Marek Lindner335fbe02013-04-23 21:40:02 +08002904 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002905 if (!orig_node)
2906 goto out;
2907
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002908 spin_lock_bh(&orig_node->tt_lock);
2909
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002910 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2911 change_offset *= ntohs(tt_data->num_vlan);
2912 change_offset += sizeof(*tt_data);
2913 tvlv_ptr += change_offset;
2914
2915 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002916 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002917 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2918 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002919 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002920 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2921 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002922 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002923
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002924 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002925 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002926
2927 spin_unlock_bh(&orig_node->tt_lock);
2928
Antonio Quartullia73105b2011-04-27 14:27:44 +02002929 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002930 spin_lock_bh(&bat_priv->tt.req_list_lock);
2931 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002932 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002933 continue;
2934 list_del(&node->list);
2935 kfree(node);
2936 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002937
Sven Eckelmann807736f2012-07-15 22:26:51 +02002938 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002939out:
2940 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002941 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002942}
2943
Sven Eckelmann56303d32012-06-05 22:31:31 +02002944static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002945{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002946 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002947
Sven Eckelmann807736f2012-07-15 22:26:51 +02002948 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002949
Sven Eckelmann807736f2012-07-15 22:26:51 +02002950 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002951 list_del(&node->list);
2952 kfree(node);
2953 }
2954
Sven Eckelmann807736f2012-07-15 22:26:51 +02002955 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002956}
2957
Sven Eckelmann56303d32012-06-05 22:31:31 +02002958static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002959{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002960 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002961
Sven Eckelmann807736f2012-07-15 22:26:51 +02002962 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2963 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002964 if (!batadv_has_timed_out(node->first_time,
2965 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002966 continue;
2967
2968 list_del(&node->list);
2969 kfree(node);
2970 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002971 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002972}
2973
2974/* This function checks whether the client already reached the
2975 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2976 * will not be sent.
2977 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002978 * returns true if the ROAMING_ADV can be sent, false otherwise
2979 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002980static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002981 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002982{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002983 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002984 bool ret = false;
2985
Sven Eckelmann807736f2012-07-15 22:26:51 +02002986 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002987 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002988 * reply from the same orig_node yet
2989 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002990 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002991 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002992 continue;
2993
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002994 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002995 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002996 continue;
2997
Sven Eckelmann3e348192012-05-16 20:23:22 +02002998 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002999 /* Sorry, you roamed too many times! */
3000 goto unlock;
3001 ret = true;
3002 break;
3003 }
3004
3005 if (!ret) {
3006 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3007 if (!tt_roam_node)
3008 goto unlock;
3009
3010 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003011 atomic_set(&tt_roam_node->counter,
3012 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003013 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003014
Sven Eckelmann807736f2012-07-15 22:26:51 +02003015 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003016 ret = true;
3017 }
3018
3019unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003020 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003021 return ret;
3022}
3023
Antonio Quartullic018ad32013-06-04 12:11:39 +02003024/**
3025 * batadv_send_roam_adv - send a roaming advertisement message
3026 * @bat_priv: the bat priv with all the soft interface information
3027 * @client: mac address of the roaming client
3028 * @vid: VLAN identifier
3029 * @orig_node: message destination
3030 *
3031 * Send a ROAMING_ADV message to the node which was previously serving this
3032 * client. This is done to inform the node that from now on all traffic destined
3033 * for this particular roamed client has to be forwarded to the sender of the
3034 * roaming message.
3035 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003036static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003037 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003038 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003039{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003040 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003041 struct batadv_tvlv_roam_adv tvlv_roam;
3042
3043 primary_if = batadv_primary_if_get_selected(bat_priv);
3044 if (!primary_if)
3045 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003046
3047 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003048 * already roamed to us too many times
3049 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003050 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003051 goto out;
3052
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003053 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003054 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3055 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003056
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003057 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003058
Marek Lindner122edaa2013-04-23 21:40:03 +08003059 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003060 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003061
3062 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3063 orig_node->orig, BATADV_TVLV_ROAM, 1,
3064 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003065
3066out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003067 if (primary_if)
3068 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003069}
3070
Sven Eckelmanna5130882012-05-16 20:23:16 +02003071static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003072{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003073 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003074 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003075 struct batadv_priv *bat_priv;
3076
3077 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003078 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3079 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003080
Marek Lindnera19d3d82013-05-27 15:33:25 +08003081 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003082 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003083 batadv_tt_req_purge(bat_priv);
3084 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003085
Antonio Quartulli72414442012-12-25 13:14:37 +01003086 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3087 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003088}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003089
Sven Eckelmann56303d32012-06-05 22:31:31 +02003090void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003091{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003092 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3093 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3094
Sven Eckelmann807736f2012-07-15 22:26:51 +02003095 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003096
Sven Eckelmanna5130882012-05-16 20:23:16 +02003097 batadv_tt_local_table_free(bat_priv);
3098 batadv_tt_global_table_free(bat_priv);
3099 batadv_tt_req_list_free(bat_priv);
3100 batadv_tt_changes_list_free(bat_priv);
3101 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003102
Sven Eckelmann807736f2012-07-15 22:26:51 +02003103 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003104}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003105
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003106/**
3107 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3108 * table and possibly count them in the TT size
3109 * @bat_priv: the bat priv with all the soft interface information
3110 * @flags: the flag to switch
3111 * @enable: whether to set or unset the flag
3112 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003113 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003114static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3115 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003116{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003117 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3118 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003119 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003120 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003121 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003122
3123 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003124 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003125
3126 for (i = 0; i < hash->size; i++) {
3127 head = &hash->table[i];
3128
3129 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003130 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003131 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003132 if (enable) {
3133 if ((tt_common_entry->flags & flags) == flags)
3134 continue;
3135 tt_common_entry->flags |= flags;
3136 } else {
3137 if (!(tt_common_entry->flags & flags))
3138 continue;
3139 tt_common_entry->flags &= ~flags;
3140 }
3141 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003142
3143 if (!count)
3144 continue;
3145
3146 batadv_tt_local_size_inc(bat_priv,
3147 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003148 }
3149 rcu_read_unlock();
3150 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003151}
3152
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003153/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003154static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003155{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003156 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003157 struct batadv_tt_common_entry *tt_common;
3158 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003159 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003160 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003161 struct hlist_head *head;
3162 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003163 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003164
3165 if (!hash)
3166 return;
3167
3168 for (i = 0; i < hash->size; i++) {
3169 head = &hash->table[i];
3170 list_lock = &hash->list_locks[i];
3171
3172 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003173 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003174 hash_entry) {
3175 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003176 continue;
3177
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003178 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003179 "Deleting local tt entry (%pM, vid: %d): pending\n",
3180 tt_common->addr,
3181 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003182
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003183 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003184 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003185 tt_local = container_of(tt_common,
3186 struct batadv_tt_local_entry,
3187 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003188
3189 /* decrease the reference held for this vlan */
3190 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3191 batadv_softif_vlan_free_ref(vlan);
3192 batadv_softif_vlan_free_ref(vlan);
3193
Sven Eckelmann56303d32012-06-05 22:31:31 +02003194 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003195 }
3196 spin_unlock_bh(list_lock);
3197 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003198}
3199
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003200/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003201 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3202 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003203 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003204 *
3205 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003206 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003207static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003208{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003209 /* Update multicast addresses in local translation table */
3210 batadv_mcast_mla_update(bat_priv);
3211
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003212 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3213 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3214 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003215 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003216 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003217
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003218 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003219
Sven Eckelmanna5130882012-05-16 20:23:16 +02003220 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003221 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003222
3223 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003224 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003225 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003226 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003227 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003228
3229 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003230 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003231 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003232}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003233
Marek Lindnera19d3d82013-05-27 15:33:25 +08003234/**
3235 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3236 * have been queued in the time since the last commit
3237 * @bat_priv: the bat priv with all the soft interface information
3238 */
3239void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3240{
3241 spin_lock_bh(&bat_priv->tt.commit_lock);
3242 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003243 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003244}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003245
Sven Eckelmann56303d32012-06-05 22:31:31 +02003246bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003247 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003248{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003249 struct batadv_tt_local_entry *tt_local_entry = NULL;
3250 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003251 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003252 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003253
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003254 vlan = batadv_softif_vlan_get(bat_priv, vid);
3255 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003256 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003257
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003258 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003259 if (!tt_local_entry)
3260 goto out;
3261
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003262 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003263 if (!tt_global_entry)
3264 goto out;
3265
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003266 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003267 goto out;
3268
Marek Lindner5870adc2012-06-20 17:16:05 +02003269 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003270
3271out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003272 if (vlan)
3273 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003274 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003275 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003276 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003277 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003278 return ret;
3279}
Marek Lindnera943cac2011-07-30 13:10:18 +02003280
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003281/**
3282 * batadv_tt_update_orig - update global translation table with new tt
3283 * information received via ogms
3284 * @bat_priv: the bat priv with all the soft interface information
3285 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003286 * @tt_vlan: pointer to the first tvlv VLAN entry
3287 * @tt_num_vlan: number of tvlv VLAN entries
3288 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003289 * @tt_num_changes: number of tt changes inside the tt buffer
3290 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003291 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003292 */
3293static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3294 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003295 const void *tt_buff, uint16_t tt_num_vlan,
3296 struct batadv_tvlv_tt_change *tt_change,
3297 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003298{
3299 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003300 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003301 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003302 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003303
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003304 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003305 has_tt_init = orig_node->capa_initialized & BATADV_ORIG_CAPA_HAS_TT;
3306
Antonio Quartulli17071572011-11-07 16:36:40 +01003307 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003308 * increased by one -> we can apply the attached changes
3309 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003310 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003311 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003312 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3313 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003314 * In this case send a tt request
3315 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003316 if (!tt_num_changes) {
3317 full_table = false;
3318 goto request_table;
3319 }
3320
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003321 spin_lock_bh(&orig_node->tt_lock);
3322
Sven Eckelmanna5130882012-05-16 20:23:16 +02003323 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003324 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003325
3326 /* Even if we received the precomputed crc with the OGM, we
3327 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003328 * in the global table
3329 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003330 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003331
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003332 spin_unlock_bh(&orig_node->tt_lock);
3333
Marek Lindnera943cac2011-07-30 13:10:18 +02003334 /* The ttvn alone is not enough to guarantee consistency
3335 * because a single value could represent different states
3336 * (due to the wrap around). Thus a node has to check whether
3337 * the resulting table (after applying the changes) is still
3338 * consistent or not. E.g. a node could disconnect while its
3339 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3340 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003341 * inconsistency
3342 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003343 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3344 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003345 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003346 } else {
3347 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003348 * in sync anymore -> request fresh tt data
3349 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003350 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003351 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3352 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003353request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003354 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003355 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3356 orig_node->orig, ttvn, orig_ttvn,
3357 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003358 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003359 tt_vlan, tt_num_vlan,
3360 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003361 return;
3362 }
3363 }
3364}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003365
Antonio Quartullic018ad32013-06-04 12:11:39 +02003366/**
3367 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3368 * @bat_priv: the bat priv with all the soft interface information
3369 * @addr: the mac address of the client to check
3370 * @vid: VLAN identifier
3371 *
3372 * Returns true if we know that the client has moved from its old originator
3373 * to another one. This entry is still kept for consistency purposes and will be
3374 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003375 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003376bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003377 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003378{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003379 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003380 bool ret = false;
3381
Antonio Quartullic018ad32013-06-04 12:11:39 +02003382 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003383 if (!tt_global_entry)
3384 goto out;
3385
Antonio Quartullic1d07432013-01-15 22:17:19 +10003386 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003387 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003388out:
3389 return ret;
3390}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003391
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003392/**
3393 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3394 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003395 * @addr: the mac address of the local client to query
3396 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003397 *
3398 * Returns true if the local client is known to be roaming (it is not served by
3399 * this node anymore) or not. If yes, the client is still present in the table
3400 * to keep the latter consistent with the node TTVN
3401 */
3402bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003403 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003404{
3405 struct batadv_tt_local_entry *tt_local_entry;
3406 bool ret = false;
3407
Antonio Quartullic018ad32013-06-04 12:11:39 +02003408 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003409 if (!tt_local_entry)
3410 goto out;
3411
3412 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3413 batadv_tt_local_entry_free_ref(tt_local_entry);
3414out:
3415 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003416}
3417
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003418bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3419 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003420 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003421 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003422{
3423 bool ret = false;
3424
Antonio Quartulli16052782013-06-04 12:11:41 +02003425 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003426 BATADV_TT_CLIENT_TEMP,
3427 atomic_read(&orig_node->last_ttvn)))
3428 goto out;
3429
3430 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003431 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3432 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003433 ret = true;
3434out:
3435 return ret;
3436}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003437
3438/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003439 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3440 * maximum packet size that can be transported through the mesh
3441 * @soft_iface: netdev struct of the mesh interface
3442 *
3443 * Remove entries older than 'timeout' and half timeout if more entries need
3444 * to be removed.
3445 */
3446void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3447{
3448 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3449 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3450 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3451 bool reduced = false;
3452
3453 spin_lock_bh(&bat_priv->tt.commit_lock);
3454
3455 while (true) {
3456 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3457 if (packet_size_max >= table_size)
3458 break;
3459
3460 batadv_tt_local_purge(bat_priv, timeout);
3461 batadv_tt_local_purge_pending_clients(bat_priv);
3462
3463 timeout /= 2;
3464 reduced = true;
3465 net_ratelimited_function(batadv_info, soft_iface,
3466 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3467 packet_size_max);
3468 }
3469
3470 /* commit these changes immediately, to avoid synchronization problem
3471 * with the TTVN
3472 */
3473 if (reduced)
3474 batadv_tt_local_commit_changes_nolock(bat_priv);
3475
3476 spin_unlock_bh(&bat_priv->tt.commit_lock);
3477}
3478
3479/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003480 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3481 * @bat_priv: the bat priv with all the soft interface information
3482 * @orig: the orig_node of the ogm
3483 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3484 * @tvlv_value: tvlv buffer containing the gateway data
3485 * @tvlv_value_len: tvlv buffer length
3486 */
3487static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3488 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003489 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003490 uint16_t tvlv_value_len)
3491{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003492 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3493 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003494 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003495 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003496
3497 if (tvlv_value_len < sizeof(*tt_data))
3498 return;
3499
3500 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3501 tvlv_value_len -= sizeof(*tt_data);
3502
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003503 num_vlan = ntohs(tt_data->num_vlan);
3504
3505 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3506 return;
3507
3508 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3509 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3510 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3511
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003512 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003513
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003514 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3515 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003516}
3517
3518/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003519 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3520 * container
3521 * @bat_priv: the bat priv with all the soft interface information
3522 * @src: mac address of tt tvlv sender
3523 * @dst: mac address of tt tvlv recipient
3524 * @tvlv_value: tvlv buffer containing the tt data
3525 * @tvlv_value_len: tvlv buffer length
3526 *
3527 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3528 * otherwise.
3529 */
3530static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3531 uint8_t *src, uint8_t *dst,
3532 void *tvlv_value,
3533 uint16_t tvlv_value_len)
3534{
3535 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003536 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003537 char tt_flag;
3538 bool ret;
3539
3540 if (tvlv_value_len < sizeof(*tt_data))
3541 return NET_RX_SUCCESS;
3542
3543 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3544 tvlv_value_len -= sizeof(*tt_data);
3545
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003546 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3547 tt_vlan_len *= ntohs(tt_data->num_vlan);
3548
3549 if (tvlv_value_len < tt_vlan_len)
3550 return NET_RX_SUCCESS;
3551
3552 tvlv_value_len -= tt_vlan_len;
3553 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003554
3555 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3556 case BATADV_TT_REQUEST:
3557 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3558
3559 /* If this node cannot provide a TT response the tt_request is
3560 * forwarded
3561 */
3562 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3563 if (!ret) {
3564 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3565 tt_flag = 'F';
3566 else
3567 tt_flag = '.';
3568
3569 batadv_dbg(BATADV_DBG_TT, bat_priv,
3570 "Routing TT_REQUEST to %pM [%c]\n",
3571 dst, tt_flag);
3572 /* tvlv API will re-route the packet */
3573 return NET_RX_DROP;
3574 }
3575 break;
3576 case BATADV_TT_RESPONSE:
3577 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3578
3579 if (batadv_is_my_mac(bat_priv, dst)) {
3580 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003581 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003582 return NET_RX_SUCCESS;
3583 }
3584
3585 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3586 tt_flag = 'F';
3587 else
3588 tt_flag = '.';
3589
3590 batadv_dbg(BATADV_DBG_TT, bat_priv,
3591 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3592
3593 /* tvlv API will re-route the packet */
3594 return NET_RX_DROP;
3595 }
3596
3597 return NET_RX_SUCCESS;
3598}
3599
3600/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003601 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3602 * @bat_priv: the bat priv with all the soft interface information
3603 * @src: mac address of tt tvlv sender
3604 * @dst: mac address of tt tvlv recipient
3605 * @tvlv_value: tvlv buffer containing the tt data
3606 * @tvlv_value_len: tvlv buffer length
3607 *
3608 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3609 * otherwise.
3610 */
3611static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3612 uint8_t *src, uint8_t *dst,
3613 void *tvlv_value,
3614 uint16_t tvlv_value_len)
3615{
3616 struct batadv_tvlv_roam_adv *roaming_adv;
3617 struct batadv_orig_node *orig_node = NULL;
3618
3619 /* If this node is not the intended recipient of the
3620 * roaming advertisement the packet is forwarded
3621 * (the tvlv API will re-route the packet).
3622 */
3623 if (!batadv_is_my_mac(bat_priv, dst))
3624 return NET_RX_DROP;
3625
Marek Lindner122edaa2013-04-23 21:40:03 +08003626 if (tvlv_value_len < sizeof(*roaming_adv))
3627 goto out;
3628
3629 orig_node = batadv_orig_hash_find(bat_priv, src);
3630 if (!orig_node)
3631 goto out;
3632
3633 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3634 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3635
3636 batadv_dbg(BATADV_DBG_TT, bat_priv,
3637 "Received ROAMING_ADV from %pM (client %pM)\n",
3638 src, roaming_adv->client);
3639
3640 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003641 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003642 atomic_read(&orig_node->last_ttvn) + 1);
3643
3644out:
3645 if (orig_node)
3646 batadv_orig_node_free_ref(orig_node);
3647 return NET_RX_SUCCESS;
3648}
3649
3650/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003651 * batadv_tt_init - initialise the translation table internals
3652 * @bat_priv: the bat priv with all the soft interface information
3653 *
3654 * Return 0 on success or negative error number in case of failure.
3655 */
3656int batadv_tt_init(struct batadv_priv *bat_priv)
3657{
3658 int ret;
3659
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003660 /* synchronized flags must be remote */
3661 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3662
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003663 ret = batadv_tt_local_init(bat_priv);
3664 if (ret < 0)
3665 return ret;
3666
3667 ret = batadv_tt_global_init(bat_priv);
3668 if (ret < 0)
3669 return ret;
3670
3671 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003672 batadv_tt_tvlv_unicast_handler_v1,
3673 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003674
Marek Lindner122edaa2013-04-23 21:40:03 +08003675 batadv_tvlv_handler_register(bat_priv, NULL,
3676 batadv_roam_tvlv_unicast_handler_v1,
3677 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3678
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003679 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3680 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3681 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3682
3683 return 1;
3684}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003685
3686/**
3687 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3688 * @bat_priv: the bat priv with all the soft interface information
3689 * @addr: the mac address of the client
3690 * @vid: the identifier of the VLAN where this client is connected
3691 *
3692 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3693 * otherwise
3694 */
3695bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3696 const uint8_t *addr, unsigned short vid)
3697{
3698 struct batadv_tt_global_entry *tt;
3699 bool ret;
3700
3701 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3702 if (!tt)
3703 return false;
3704
3705 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3706
3707 batadv_tt_global_entry_free_ref(tt);
3708
3709 return ret;
3710}