blob: d636bde72c9ace9cfbcead01353c955f17923155 [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 Quartulli0c69aec2013-10-13 02:50:18 +0200514 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200515 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800517 int hash_added, table_size, packet_size_max;
518 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200519 uint8_t remote_flags;
Antonio Quartulli9464d072013-11-16 12:03:48 +0100520 uint32_t match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200522 if (ifindex != BATADV_NULL_IFINDEX)
523 in_dev = dev_get_by_index(&init_net, ifindex);
524
Antonio Quartullic018ad32013-06-04 12:11:39 +0200525 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100526
527 if (!is_multicast_ether_addr(addr))
528 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529
Antonio Quartulli47c94652012-09-23 22:38:35 +0200530 if (tt_local) {
531 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200532 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
533 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200534 "Re-adding pending client %pM (vid: %d)\n",
535 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200536 /* whatever the reason why the PENDING flag was set,
537 * this is a client which was enqueued to be removed in
538 * this orig_interval. Since it popped up again, the
539 * flag can be reset like it was never enqueued
540 */
541 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
542 goto add_event;
543 }
544
545 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
546 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200547 "Roaming client %pM (vid: %d) came back to its original location\n",
548 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200549 /* the ROAM flag is set because this client roamed away
550 * and the node got a roaming_advertisement message. Now
551 * that the client popped up again at its original
552 * location such flag can be unset
553 */
554 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
555 roamed_back = true;
556 }
557 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 }
559
Marek Lindnera19d3d82013-05-27 15:33:25 +0800560 /* Ignore the client if we cannot send it in a full table response. */
561 table_size = batadv_tt_local_table_transmit_size(bat_priv);
562 table_size += batadv_tt_len(1);
563 packet_size_max = atomic_read(&bat_priv->packet_size_max);
564 if (table_size > packet_size_max) {
565 net_ratelimited_function(batadv_info, soft_iface,
566 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
567 table_size, packet_size_max, addr);
568 goto out;
569 }
570
Antonio Quartulli47c94652012-09-23 22:38:35 +0200571 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
572 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200573 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200574
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200575 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200576 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
577 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200578 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100580 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100581 /* The local entry has to be marked as NEW to avoid to send it in
582 * a full table response going out before the next ttvn increment
583 * (consistency check)
584 */
585 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200586 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200587 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200588 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
589 atomic_set(&tt_local->common.refcount, 2);
590 tt_local->last_seen = jiffies;
591 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100593 /* the batman interface mac and multicast addresses should never be
594 * purged
595 */
596 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
597 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200598 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599
Sven Eckelmann807736f2012-07-15 22:26:51 +0200600 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200601 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200602 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100603
604 if (unlikely(hash_added != 0)) {
605 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200606 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100607 goto out;
608 }
609
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200610add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200611 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200612
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200613check_roaming:
614 /* Check whether it is a roaming, but don't do anything if the roaming
615 * process has already been handled
616 */
617 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200618 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200619 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200620 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800621 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200622 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200623 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200624 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200625 }
626 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200627 if (roamed_back) {
628 batadv_tt_global_free(bat_priv, tt_global,
629 "Roaming canceled");
630 tt_global = NULL;
631 } else {
632 /* The global entry has to be marked as ROAMING and
633 * has to be kept for consistency purpose
634 */
635 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
636 tt_global->roam_at = jiffies;
637 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200639
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200640 /* store the current remote flags before altering them. This helps
641 * understanding is flags are changing or not
642 */
643 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800644
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200645 if (batadv_is_wifi_netdev(in_dev))
646 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
647 else
648 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
649
Antonio Quartulli9464d072013-11-16 12:03:48 +0100650 /* check the mark in the skb: if it's equal to the configured
651 * isolation_mark, it means the packet is coming from an isolated
652 * non-mesh client
653 */
654 match_mark = (mark & bat_priv->isolation_mark_mask);
655 if (bat_priv->isolation_mark_mask &&
656 match_mark == bat_priv->isolation_mark)
657 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
658 else
659 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
660
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200661 /* if any "dynamic" flag has been modified, resend an ADD event for this
662 * entry so that all the nodes can get the new flags
663 */
664 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
665 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
666
667 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200668out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200669 if (in_dev)
670 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200671 if (tt_local)
672 batadv_tt_local_entry_free_ref(tt_local);
673 if (tt_global)
674 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800675 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676}
677
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800678/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200679 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
680 * within a TT Response directed to another node
681 * @orig_node: originator for which the TT data has to be prepared
682 * @tt_data: uninitialised pointer to the address of the TVLV buffer
683 * @tt_change: uninitialised pointer to the address of the area where the TT
684 * changed can be stored
685 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
686 * function reserves the amount of space needed to send the entire global TT
687 * table. In case of success the value is updated with the real amount of
688 * reserved bytes
689
690 * Allocate the needed amount of memory for the entire TT TVLV and write its
691 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
692 * objects, one per active VLAN served by the originator node.
693 *
694 * Return the size of the allocated buffer or 0 in case of failure.
695 */
696static uint16_t
697batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
698 struct batadv_tvlv_tt_data **tt_data,
699 struct batadv_tvlv_tt_change **tt_change,
700 int32_t *tt_len)
701{
702 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
703 struct batadv_tvlv_tt_vlan_data *tt_vlan;
704 struct batadv_orig_node_vlan *vlan;
705 uint8_t *tt_change_ptr;
706
707 rcu_read_lock();
708 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
709 num_vlan++;
710 num_entries += atomic_read(&vlan->tt.num_entries);
711 }
712
713 change_offset = sizeof(**tt_data);
714 change_offset += num_vlan * sizeof(*tt_vlan);
715
716 /* if tt_len is negative, allocate the space needed by the full table */
717 if (*tt_len < 0)
718 *tt_len = batadv_tt_len(num_entries);
719
720 tvlv_len = *tt_len;
721 tvlv_len += change_offset;
722
723 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
724 if (!*tt_data) {
725 *tt_len = 0;
726 goto out;
727 }
728
729 (*tt_data)->flags = BATADV_NO_FLAGS;
730 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
731 (*tt_data)->num_vlan = htons(num_vlan);
732
733 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
734 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
735 tt_vlan->vid = htons(vlan->vid);
736 tt_vlan->crc = htonl(vlan->tt.crc);
737
738 tt_vlan++;
739 }
740
741 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
742 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
743
744out:
745 rcu_read_unlock();
746 return tvlv_len;
747}
748
749/**
750 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
751 * node
752 * @bat_priv: the bat priv with all the soft interface information
753 * @tt_data: uninitialised pointer to the address of the TVLV buffer
754 * @tt_change: uninitialised pointer to the address of the area where the TT
755 * changes can be stored
756 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
757 * function reserves the amount of space needed to send the entire local TT
758 * table. In case of success the value is updated with the real amount of
759 * reserved bytes
760 *
761 * Allocate the needed amount of memory for the entire TT TVLV and write its
762 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
763 * objects, one per active VLAN.
764 *
765 * Return the size of the allocated buffer or 0 in case of failure.
766 */
767static uint16_t
768batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
769 struct batadv_tvlv_tt_data **tt_data,
770 struct batadv_tvlv_tt_change **tt_change,
771 int32_t *tt_len)
772{
773 struct batadv_tvlv_tt_vlan_data *tt_vlan;
774 struct batadv_softif_vlan *vlan;
775 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
776 uint8_t *tt_change_ptr;
777 int change_offset;
778
779 rcu_read_lock();
780 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
781 num_vlan++;
782 num_entries += atomic_read(&vlan->tt.num_entries);
783 }
784
785 change_offset = sizeof(**tt_data);
786 change_offset += num_vlan * sizeof(*tt_vlan);
787
788 /* if tt_len is negative, allocate the space needed by the full table */
789 if (*tt_len < 0)
790 *tt_len = batadv_tt_len(num_entries);
791
792 tvlv_len = *tt_len;
793 tvlv_len += change_offset;
794
795 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
796 if (!*tt_data) {
797 tvlv_len = 0;
798 goto out;
799 }
800
801 (*tt_data)->flags = BATADV_NO_FLAGS;
802 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
803 (*tt_data)->num_vlan = htons(num_vlan);
804
805 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
806 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
807 tt_vlan->vid = htons(vlan->vid);
808 tt_vlan->crc = htonl(vlan->tt.crc);
809
810 tt_vlan++;
811 }
812
813 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
814 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
815
816out:
817 rcu_read_unlock();
818 return tvlv_len;
819}
820
821/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800822 * batadv_tt_tvlv_container_update - update the translation table tvlv container
823 * after local tt changes have been committed
824 * @bat_priv: the bat priv with all the soft interface information
825 */
826static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800828 struct batadv_tt_change_node *entry, *safe;
829 struct batadv_tvlv_tt_data *tt_data;
830 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200831 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800832 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200833 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200835 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
836 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800837
838 /* if we have too many changes for one packet don't send any
839 * and wait for the tt table request which will be fragmented
840 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800841 if (tt_diff_len > bat_priv->soft_iface->mtu)
842 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800843
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200844 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
845 &tt_change, &tt_diff_len);
846 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800847 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800848
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800849 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800850
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800851 if (tt_diff_len == 0)
852 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800853
Sven Eckelmann807736f2012-07-15 22:26:51 +0200854 spin_lock_bh(&bat_priv->tt.changes_list_lock);
855 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856
Sven Eckelmann807736f2012-07-15 22:26:51 +0200857 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100858 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800859 if (tt_diff_entries_count < tt_diff_entries_num) {
860 memcpy(tt_change + tt_diff_entries_count,
861 &entry->change,
862 sizeof(struct batadv_tvlv_tt_change));
863 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200865 list_del(&entry->list);
866 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000867 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200868 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
Antonio Quartullia73105b2011-04-27 14:27:44 +0200870 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200871 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
872 kfree(bat_priv->tt.last_changeset);
873 bat_priv->tt.last_changeset_len = 0;
874 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800875 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800876 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800877 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800878 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200879 * instead of providing the diff
880 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800881 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200882 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800883 memcpy(bat_priv->tt.last_changeset,
884 tt_change, tt_change_len);
885 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200886 }
887 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200888 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800890container_register:
891 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200892 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800893 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894}
895
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200896int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000897{
898 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200899 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200900 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200901 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100902 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200903 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200904 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200906 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200907 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100908 int last_seen_secs;
909 int last_seen_msecs;
910 unsigned long last_seen_jiffies;
911 bool no_purge;
912 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913
Marek Lindner30da63a2012-08-03 17:15:46 +0200914 primary_if = batadv_seq_print_text_primary_if_get(seq);
915 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200916 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100918 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200919 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
920 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100921 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200922 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000923
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924 for (i = 0; i < hash->size; i++) {
925 head = &hash->table[i];
926
Marek Lindner7aadf882011-02-18 12:28:09 +0000927 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800928 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000929 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100930 tt_local = container_of(tt_common_entry,
931 struct batadv_tt_local_entry,
932 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200933 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100934 last_seen_jiffies = jiffies - tt_local->last_seen;
935 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
936 last_seen_secs = last_seen_msecs / 1000;
937 last_seen_msecs = last_seen_msecs % 1000;
938
939 no_purge = tt_common_entry->flags & np_flag;
940
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200941 vlan = batadv_softif_vlan_get(bat_priv, vid);
942 if (!vlan) {
943 seq_printf(seq, "Cannot retrieve VLAN %d\n",
944 BATADV_PRINT_VID(vid));
945 continue;
946 }
947
948 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100949 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100950 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200951 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100952 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200953 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100954 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100955 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200956 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100957 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200958 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100959 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100960 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100961 (tt_common_entry->flags &
962 BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100963 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200964 no_purge ? 0 : last_seen_msecs,
965 vlan->tt.crc);
966
967 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000968 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000969 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200971out:
972 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200973 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200974 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975}
976
Sven Eckelmann56303d32012-06-05 22:31:31 +0200977static void
978batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
979 struct batadv_tt_local_entry *tt_local_entry,
980 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000981{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200982 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000983
Antonio Quartulli015758d2011-07-09 17:52:13 +0200984 /* The local client has to be marked as "pending to be removed" but has
985 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200986 * response issued before the net ttvn increment (consistency check)
987 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200988 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100989
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200990 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200991 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
992 tt_local_entry->common.addr,
993 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994}
995
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200996/**
997 * batadv_tt_local_remove - logically remove an entry from the local table
998 * @bat_priv: the bat priv with all the soft interface information
999 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001000 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001001 * @message: message to append to the log on deletion
1002 * @roaming: true if the deletion is due to a roaming event
1003 *
1004 * Returns the flags assigned to the local entry before being deleted
1005 */
1006uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001007 const uint8_t *addr, unsigned short vid,
1008 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001010 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001011 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001012
Antonio Quartullic018ad32013-06-04 12:11:39 +02001013 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001014 if (!tt_local_entry)
1015 goto out;
1016
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001017 curr_flags = tt_local_entry->common.flags;
1018
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001019 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001020 /* if this global entry addition is due to a roaming, the node has to
1021 * mark the local entry as "roamed" in order to correctly reroute
1022 * packets later
1023 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001024 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001025 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001026 /* mark the local client as ROAMed */
1027 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1028 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001029
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001030 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1031 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1032 message);
1033 goto out;
1034 }
1035 /* if this client has been added right now, it is possible to
1036 * immediately purge it
1037 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001038 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001039 hlist_del_rcu(&tt_local_entry->common.hash_entry);
1040 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001041
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001042out:
1043 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001044 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001045
1046 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047}
1048
Marek Lindnera19d3d82013-05-27 15:33:25 +08001049/**
1050 * batadv_tt_local_purge_list - purge inactive tt local entries
1051 * @bat_priv: the bat priv with all the soft interface information
1052 * @head: pointer to the list containing the local tt entries
1053 * @timeout: parameter deciding whether a given tt local entry is considered
1054 * inactive or not
1055 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001056static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001057 struct hlist_head *head,
1058 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001060 struct batadv_tt_local_entry *tt_local_entry;
1061 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001062 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001063
Sasha Levinb67bfe02013-02-27 17:06:00 -08001064 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001065 hash_entry) {
1066 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001067 struct batadv_tt_local_entry,
1068 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001069 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1070 continue;
1071
1072 /* entry already marked for deletion */
1073 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1074 continue;
1075
Marek Lindnera19d3d82013-05-27 15:33:25 +08001076 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001077 continue;
1078
1079 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1080 BATADV_TT_CLIENT_DEL, "timed out");
1081 }
1082}
1083
Marek Lindnera19d3d82013-05-27 15:33:25 +08001084/**
1085 * batadv_tt_local_purge - purge inactive tt local entries
1086 * @bat_priv: the bat priv with all the soft interface information
1087 * @timeout: parameter deciding whether a given tt local entry is considered
1088 * inactive or not
1089 */
1090static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1091 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001092{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001093 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001095 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001096 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001097
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001098 for (i = 0; i < hash->size; i++) {
1099 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001100 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001102 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001103 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001104 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001105 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001106}
1107
Sven Eckelmann56303d32012-06-05 22:31:31 +02001108static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001109{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001110 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001111 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001112 struct batadv_tt_common_entry *tt_common_entry;
1113 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001114 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001115 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001116 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001117
Sven Eckelmann807736f2012-07-15 22:26:51 +02001118 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001119 return;
1120
Sven Eckelmann807736f2012-07-15 22:26:51 +02001121 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001122
1123 for (i = 0; i < hash->size; i++) {
1124 head = &hash->table[i];
1125 list_lock = &hash->list_locks[i];
1126
1127 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001128 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001129 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001130 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001131 tt_local = container_of(tt_common_entry,
1132 struct batadv_tt_local_entry,
1133 common);
1134 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001135 }
1136 spin_unlock_bh(list_lock);
1137 }
1138
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001139 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001140
Sven Eckelmann807736f2012-07-15 22:26:51 +02001141 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001142}
1143
Sven Eckelmann56303d32012-06-05 22:31:31 +02001144static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001145{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001146 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001147 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001148
Sven Eckelmann807736f2012-07-15 22:26:51 +02001149 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001150
Sven Eckelmann807736f2012-07-15 22:26:51 +02001151 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001152 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001153
Antonio Quartullidec05072012-11-10 11:00:32 +01001154 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1155 &batadv_tt_global_hash_lock_class_key);
1156
Sven Eckelmann5346c352012-05-05 13:27:28 +02001157 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001158}
1159
Sven Eckelmann56303d32012-06-05 22:31:31 +02001160static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001161{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001162 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001163
Sven Eckelmann807736f2012-07-15 22:26:51 +02001164 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001165
Sven Eckelmann807736f2012-07-15 22:26:51 +02001166 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001167 list) {
1168 list_del(&entry->list);
1169 kfree(entry);
1170 }
1171
Sven Eckelmann807736f2012-07-15 22:26:51 +02001172 atomic_set(&bat_priv->tt.local_changes, 0);
1173 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001174}
1175
Antonio Quartullid657e622012-07-01 14:09:12 +02001176/* retrieves the orig_tt_list_entry belonging to orig_node from the
1177 * batadv_tt_global_entry list
1178 *
1179 * returns it with an increased refcounter, NULL if not found
1180 */
1181static struct batadv_tt_orig_list_entry *
1182batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1183 const struct batadv_orig_node *orig_node)
1184{
1185 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1186 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001187
1188 rcu_read_lock();
1189 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001190 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001191 if (tmp_orig_entry->orig_node != orig_node)
1192 continue;
1193 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1194 continue;
1195
1196 orig_entry = tmp_orig_entry;
1197 break;
1198 }
1199 rcu_read_unlock();
1200
1201 return orig_entry;
1202}
1203
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001204/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001205 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001206 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001207static bool
1208batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1209 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001210{
Antonio Quartullid657e622012-07-01 14:09:12 +02001211 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001212 bool found = false;
1213
Antonio Quartullid657e622012-07-01 14:09:12 +02001214 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1215 if (orig_entry) {
1216 found = true;
1217 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001218 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001219
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001220 return found;
1221}
1222
Sven Eckelmanna5130882012-05-16 20:23:16 +02001223static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001224batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001225 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001226{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001227 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001228
Antonio Quartullid657e622012-07-01 14:09:12 +02001229 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001230 if (orig_entry) {
1231 /* refresh the ttvn: the current value could be a bogus one that
1232 * was added during a "temporary client detection"
1233 */
1234 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001235 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001236 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001237
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001238 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1239 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001240 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001241
1242 INIT_HLIST_NODE(&orig_entry->list);
1243 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001244 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001245 orig_entry->orig_node = orig_node;
1246 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001247 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001248
Antonio Quartullid657e622012-07-01 14:09:12 +02001249 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001250 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001251 &tt_global->orig_list);
1252 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001253 atomic_inc(&tt_global->orig_list_count);
1254
Antonio Quartullid657e622012-07-01 14:09:12 +02001255out:
1256 if (orig_entry)
1257 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001258}
1259
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001260/**
1261 * batadv_tt_global_add - add a new TT global entry or update an existing one
1262 * @bat_priv: the bat priv with all the soft interface information
1263 * @orig_node: the originator announcing the client
1264 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001265 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001266 * @flags: TT flags that have to be set for this non-mesh client
1267 * @ttvn: the tt version number ever announcing this non-mesh client
1268 *
1269 * Add a new TT global entry for the given originator. If the entry already
1270 * exists add a new reference to the given originator (a global entry can have
1271 * references to multiple originators) and adjust the flags attribute to reflect
1272 * the function argument.
1273 * If a TT local entry exists for this non-mesh client remove it.
1274 *
1275 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001276 *
1277 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001278 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001279static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1280 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001281 const unsigned char *tt_addr,
1282 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001283 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001284{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001285 struct batadv_tt_global_entry *tt_global_entry;
1286 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001287 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001288 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001289 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001290 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001291
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001292 /* ignore global entries from backbone nodes */
1293 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1294 return true;
1295
Antonio Quartullic018ad32013-06-04 12:11:39 +02001296 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1297 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001298
1299 /* if the node already has a local client for this entry, it has to wait
1300 * for a roaming advertisement instead of manually messing up the global
1301 * table
1302 */
1303 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1304 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1305 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001306
Antonio Quartullia73105b2011-04-27 14:27:44 +02001307 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001308 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001309 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001310 goto out;
1311
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001312 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001313 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001314 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001315
Antonio Quartullid4f44692012-05-25 00:00:54 +02001316 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001317 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001318 /* node must store current time in case of roaming. This is
1319 * needed to purge this entry out on timeout (if nobody claims
1320 * it)
1321 */
1322 if (flags & BATADV_TT_CLIENT_ROAM)
1323 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001324 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001325 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001326
1327 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001328 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001329 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001330
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001332 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001333 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001334 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001335
1336 if (unlikely(hash_added != 0)) {
1337 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001338 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001339 goto out_remove;
1340 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001341 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001342 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001343 /* If there is already a global entry, we can use this one for
1344 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001345 * But if we are trying to add a temporary client then here are
1346 * two options at this point:
1347 * 1) the global client is not a temporary client: the global
1348 * client has to be left as it is, temporary information
1349 * should never override any already known client state
1350 * 2) the global client is a temporary client: purge the
1351 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001352 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001353 if (flags & BATADV_TT_CLIENT_TEMP) {
1354 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1355 goto out;
1356 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1357 orig_node))
1358 goto out_remove;
1359 batadv_tt_global_del_orig_list(tt_global_entry);
1360 goto add_orig_entry;
1361 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001362
1363 /* if the client was temporary added before receiving the first
1364 * OGM announcing it, we have to clear the TEMP flag
1365 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001366 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001367
Antonio Quartullie9c001362012-11-07 15:05:33 +01001368 /* the change can carry possible "attribute" flags like the
1369 * TT_CLIENT_WIFI, therefore they have to be copied in the
1370 * client entry
1371 */
1372 tt_global_entry->common.flags |= flags;
1373
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001374 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1375 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001376 * delete + roaming change for this originator.
1377 *
1378 * We should first delete the old originator before adding the
1379 * new one.
1380 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001381 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001382 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001383 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001384 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001385 }
1386 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001387add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001388 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001389 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001390
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001391 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001392 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1393 common->addr, BATADV_PRINT_VID(common->vid),
1394 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001395 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001396
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001397out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001398 /* Do not remove multicast addresses from the local hash on
1399 * global additions
1400 */
1401 if (is_multicast_ether_addr(tt_addr))
1402 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001403
Antonio Quartullia73105b2011-04-27 14:27:44 +02001404 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001405 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001406 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001407 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001408 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1409
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001410 if (!(flags & BATADV_TT_CLIENT_ROAM))
1411 /* this is a normal global add. Therefore the client is not in a
1412 * roaming state anymore.
1413 */
1414 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1415
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001416out:
1417 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001418 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001419 if (tt_local_entry)
1420 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001421 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001422}
1423
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001424/**
1425 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001426 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001427 * @tt_global_entry: global translation table entry to be analyzed
1428 *
1429 * This functon assumes the caller holds rcu_read_lock().
1430 * Returns best originator list entry or NULL on errors.
1431 */
1432static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001433batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1434 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001435{
Antonio Quartulli46274562013-09-03 11:10:24 +02001436 struct batadv_neigh_node *router, *best_router = NULL;
1437 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001438 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001439 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001440
1441 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001442 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001443 router = batadv_orig_router_get(orig_entry->orig_node,
1444 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001445 if (!router)
1446 continue;
1447
Antonio Quartulli46274562013-09-03 11:10:24 +02001448 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001449 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1450 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001451 batadv_neigh_node_free_ref(router);
1452 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001453 }
1454
Antonio Quartulli46274562013-09-03 11:10:24 +02001455 /* release the refcount for the "old" best */
1456 if (best_router)
1457 batadv_neigh_node_free_ref(best_router);
1458
1459 best_entry = orig_entry;
1460 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001461 }
1462
Antonio Quartulli46274562013-09-03 11:10:24 +02001463 if (best_router)
1464 batadv_neigh_node_free_ref(best_router);
1465
Sven Eckelmann981d8902012-10-07 13:34:15 +02001466 return best_entry;
1467}
1468
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001469/**
1470 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1471 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001472 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001473 * @tt_global_entry: global translation table entry to be printed
1474 * @seq: debugfs table seq_file struct
1475 *
1476 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001477 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001478static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001479batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1480 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001481 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001482{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001483 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001484 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001485 struct batadv_orig_node_vlan *vlan;
1486 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001487 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001488 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001489
1490 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001491 flags = tt_common_entry->flags;
1492
Antonio Quartulli46274562013-09-03 11:10:24 +02001493 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001494 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001495 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1496 tt_common_entry->vid);
1497 if (!vlan) {
1498 seq_printf(seq,
1499 " * Cannot retrieve VLAN %d for originator %pM\n",
1500 BATADV_PRINT_VID(tt_common_entry->vid),
1501 best_entry->orig_node->orig);
1502 goto print_list;
1503 }
1504
Sven Eckelmann981d8902012-10-07 13:34:15 +02001505 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001506 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001507 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001508 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001509 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001510 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001511 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001512 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1513 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001514 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001515 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001516
1517 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001518 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001519
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001520print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001521 head = &tt_global_entry->orig_list;
1522
Sasha Levinb67bfe02013-02-27 17:06:00 -08001523 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001524 if (best_entry == orig_entry)
1525 continue;
1526
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001527 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1528 tt_common_entry->vid);
1529 if (!vlan) {
1530 seq_printf(seq,
1531 " + Cannot retrieve VLAN %d for originator %pM\n",
1532 BATADV_PRINT_VID(tt_common_entry->vid),
1533 orig_entry->orig_node->orig);
1534 continue;
1535 }
1536
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001537 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001538 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001539 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001540 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001541 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001542 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001543 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001544 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001545 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001546 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001547 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001548
1549 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001550 }
1551}
1552
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001553int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001554{
1555 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001556 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001557 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001558 struct batadv_tt_common_entry *tt_common_entry;
1559 struct batadv_tt_global_entry *tt_global;
1560 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001561 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001562 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001563
Marek Lindner30da63a2012-08-03 17:15:46 +02001564 primary_if = batadv_seq_print_text_primary_if_get(seq);
1565 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001566 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001567
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001568 seq_printf(seq,
1569 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001570 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001571 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1572 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1573 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001574
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001575 for (i = 0; i < hash->size; i++) {
1576 head = &hash->table[i];
1577
Marek Lindner7aadf882011-02-18 12:28:09 +00001578 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001579 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001580 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001581 tt_global = container_of(tt_common_entry,
1582 struct batadv_tt_global_entry,
1583 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001584 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001585 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001586 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001587 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001588out:
1589 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001590 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001591 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001592}
1593
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001594/**
1595 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1596 * @tt_global_entry: the global entry to remove the orig_entry from
1597 * @orig_entry: the orig entry to remove and free
1598 *
1599 * Remove an orig_entry from its list in the given tt_global_entry and
1600 * free this orig_entry afterwards.
1601 */
1602static void
1603batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1604 struct batadv_tt_orig_list_entry *orig_entry)
1605{
1606 batadv_tt_global_size_dec(orig_entry->orig_node,
1607 tt_global_entry->common.vid);
1608 atomic_dec(&tt_global_entry->orig_list_count);
1609 hlist_del_rcu(&orig_entry->list);
1610 batadv_tt_orig_list_entry_free_ref(orig_entry);
1611}
1612
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001613/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001614static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001615batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001616{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001617 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001618 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001619 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001620
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001621 spin_lock_bh(&tt_global_entry->list_lock);
1622 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001623 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1624 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001625 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001626}
1627
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001628/**
1629 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1630 * @bat_priv: the bat priv with all the soft interface information
1631 * @tt_global_entry: the global entry to remove the orig_node from
1632 * @orig_node: the originator announcing the client
1633 * @message: message to append to the log on deletion
1634 *
1635 * Remove the given orig_node and its according orig_entry from the given
1636 * global tt entry.
1637 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001638static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001639batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1640 struct batadv_tt_global_entry *tt_global_entry,
1641 struct batadv_orig_node *orig_node,
1642 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001643{
1644 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001645 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001646 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001647 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001648
1649 spin_lock_bh(&tt_global_entry->list_lock);
1650 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001651 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001652 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001653 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001654 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001655 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001656 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001657 tt_global_entry->common.addr,
1658 BATADV_PRINT_VID(vid), message);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001659 batadv_tt_global_del_orig_entry(tt_global_entry,
1660 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001661 }
1662 }
1663 spin_unlock_bh(&tt_global_entry->list_lock);
1664}
1665
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001666/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001667 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1668 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001669 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001670static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001671batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1672 struct batadv_tt_global_entry *tt_global_entry,
1673 struct batadv_orig_node *orig_node,
1674 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001675{
1676 bool last_entry = true;
1677 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001678 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001679
1680 /* no local entry exists, case 1:
1681 * Check if this is the last one or if other entries exist.
1682 */
1683
1684 rcu_read_lock();
1685 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001686 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001687 if (orig_entry->orig_node != orig_node) {
1688 last_entry = false;
1689 break;
1690 }
1691 }
1692 rcu_read_unlock();
1693
1694 if (last_entry) {
1695 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001696 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001697 tt_global_entry->roam_at = jiffies;
1698 } else
1699 /* there is another entry, we can simply delete this
1700 * one and can still use the other one.
1701 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001702 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1703 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001704}
1705
Antonio Quartullic018ad32013-06-04 12:11:39 +02001706/**
1707 * batadv_tt_global_del - remove a client from the global table
1708 * @bat_priv: the bat priv with all the soft interface information
1709 * @orig_node: an originator serving this client
1710 * @addr: the mac address of the client
1711 * @vid: VLAN identifier
1712 * @message: a message explaining the reason for deleting the client to print
1713 * for debugging purpose
1714 * @roaming: true if the deletion has been triggered by a roaming event
1715 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001716static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1717 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001718 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001719 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001720{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001721 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001722 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001723
Antonio Quartullic018ad32013-06-04 12:11:39 +02001724 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001725 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001726 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001727
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001728 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001729 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1730 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001731
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001732 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001733 batadv_tt_global_free(bat_priv, tt_global_entry,
1734 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001735
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001736 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001737 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001738
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001739 /* if we are deleting a global entry due to a roam
1740 * event, there are two possibilities:
1741 * 1) the client roamed from node A to node B => if there
1742 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001743 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001744 * wait for node B to claim it. In case of timeout
1745 * the entry is purged.
1746 *
1747 * If there are other originators left, we directly delete
1748 * the originator.
1749 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001750 * the global entry, since it is useless now.
1751 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001752 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001753 tt_global_entry->common.addr,
1754 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001755 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001756 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001757 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001758 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001759 } else
1760 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001761 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1762 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001763
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001764
Antonio Quartullicc47f662011-04-27 14:27:57 +02001765out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001766 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001767 batadv_tt_global_entry_free_ref(tt_global_entry);
1768 if (local_entry)
1769 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001770}
1771
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001772/**
1773 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1774 * given originator matching the provided vid
1775 * @bat_priv: the bat priv with all the soft interface information
1776 * @orig_node: the originator owning the entries to remove
1777 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1778 * removed
1779 * @message: debug message to print as "reason"
1780 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001781void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1782 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001783 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001784 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001785{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001786 struct batadv_tt_global_entry *tt_global;
1787 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001788 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001789 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001790 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001791 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001792 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001793 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001794
Simon Wunderlich6e801492011-10-19 10:28:26 +02001795 if (!hash)
1796 return;
1797
Antonio Quartullia73105b2011-04-27 14:27:44 +02001798 for (i = 0; i < hash->size; i++) {
1799 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001800 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001801
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001802 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001803 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001804 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001805 /* remove only matching entries */
1806 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1807 continue;
1808
Sven Eckelmann56303d32012-06-05 22:31:31 +02001809 tt_global = container_of(tt_common_entry,
1810 struct batadv_tt_global_entry,
1811 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001812
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001813 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1814 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001815
Sven Eckelmann56303d32012-06-05 22:31:31 +02001816 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001817 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001818 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001819 "Deleting global tt entry %pM (vid: %d): %s\n",
1820 tt_global->common.addr,
1821 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001822 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001823 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001824 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001825 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001826 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001827 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01001828 orig_node->capa_initialized &= ~BATADV_ORIG_CAPA_HAS_TT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001829}
1830
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001831static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1832 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001833{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001834 bool purge = false;
1835 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1836 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001837
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001838 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1839 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1840 purge = true;
1841 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001842 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001843
1844 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1845 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1846 purge = true;
1847 *msg = "Temporary client timeout\n";
1848 }
1849
1850 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001851}
1852
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001853static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001854{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001855 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001856 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001857 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001858 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001859 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001860 char *msg = NULL;
1861 struct batadv_tt_common_entry *tt_common;
1862 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001863
Antonio Quartullicc47f662011-04-27 14:27:57 +02001864 for (i = 0; i < hash->size; i++) {
1865 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001866 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001867
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001868 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001869 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001870 hash_entry) {
1871 tt_global = container_of(tt_common,
1872 struct batadv_tt_global_entry,
1873 common);
1874
1875 if (!batadv_tt_global_to_purge(tt_global, &msg))
1876 continue;
1877
1878 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001879 "Deleting global tt entry %pM (vid: %d): %s\n",
1880 tt_global->common.addr,
1881 BATADV_PRINT_VID(tt_global->common.vid),
1882 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001883
Sasha Levinb67bfe02013-02-27 17:06:00 -08001884 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001885
1886 batadv_tt_global_entry_free_ref(tt_global);
1887 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001888 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001889 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001890}
1891
Sven Eckelmann56303d32012-06-05 22:31:31 +02001892static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001893{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001894 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001895 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001896 struct batadv_tt_common_entry *tt_common_entry;
1897 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001898 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001899 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001900 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001901
Sven Eckelmann807736f2012-07-15 22:26:51 +02001902 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001903 return;
1904
Sven Eckelmann807736f2012-07-15 22:26:51 +02001905 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001906
1907 for (i = 0; i < hash->size; i++) {
1908 head = &hash->table[i];
1909 list_lock = &hash->list_locks[i];
1910
1911 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001912 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001913 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001914 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001915 tt_global = container_of(tt_common_entry,
1916 struct batadv_tt_global_entry,
1917 common);
1918 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001919 }
1920 spin_unlock_bh(list_lock);
1921 }
1922
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001923 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001924
Sven Eckelmann807736f2012-07-15 22:26:51 +02001925 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001926}
1927
Sven Eckelmann56303d32012-06-05 22:31:31 +02001928static bool
1929_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1930 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001931{
1932 bool ret = false;
1933
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001934 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1935 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001936 ret = true;
1937
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001938 /* check if the two clients are marked as isolated */
1939 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1940 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1941 ret = true;
1942
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001943 return ret;
1944}
1945
Antonio Quartullic018ad32013-06-04 12:11:39 +02001946/**
1947 * batadv_transtable_search - get the mesh destination for a given client
1948 * @bat_priv: the bat priv with all the soft interface information
1949 * @src: mac address of the source client
1950 * @addr: mac address of the destination client
1951 * @vid: VLAN identifier
1952 *
1953 * Returns a pointer to the originator that was selected as destination in the
1954 * mesh for contacting the client 'addr', NULL otherwise.
1955 * In case of multiple originators serving the same client, the function returns
1956 * the best one (best in terms of metric towards the destination node).
1957 *
1958 * If the two clients are AP isolated the function returns NULL.
1959 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001960struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1961 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001962 const uint8_t *addr,
1963 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001964{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001965 struct batadv_tt_local_entry *tt_local_entry = NULL;
1966 struct batadv_tt_global_entry *tt_global_entry = NULL;
1967 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001968 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001969
Antonio Quartullieceb22a2013-11-16 12:03:51 +01001970 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001971 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001972 if (!tt_local_entry ||
1973 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001974 goto out;
1975 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001976
Antonio Quartullic018ad32013-06-04 12:11:39 +02001977 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001978 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001979 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001980
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001981 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001982 * isolation
1983 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001984 if (tt_local_entry &&
1985 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001986 goto out;
1987
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001988 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001989 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001990 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001991 if (best_entry)
1992 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001993 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1994 orig_node = NULL;
1995 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001996
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001997out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001998 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001999 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002000 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002001 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002002
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002003 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002004}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002005
Antonio Quartulliced72932013-04-24 16:37:51 +02002006/**
2007 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2008 * to the given orig_node
2009 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002010 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002011 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002012 *
2013 * This function computes the checksum for the global table corresponding to a
2014 * specific originator. In particular, the checksum is computed as follows: For
2015 * each client connected to the originator the CRC32C of the MAC address and the
2016 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2017 * together.
2018 *
2019 * The idea behind is that CRC32C should be used as much as possible in order to
2020 * produce a unique hash of the table, but since the order which is used to feed
2021 * the CRC32C function affects the result and since every node in the network
2022 * probably sorts the clients differently, the hash function cannot be directly
2023 * computed over the entire table. Hence the CRC32C is used only on
2024 * the single client entry, while all the results are then xor'ed together
2025 * because the XOR operation can combine them all while trying to reduce the
2026 * noise as much as possible.
2027 *
2028 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002029 */
2030static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002031 struct batadv_orig_node *orig_node,
2032 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002033{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002034 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002035 struct batadv_tt_common_entry *tt_common;
2036 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002037 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002038 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002039 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002040 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002041
2042 for (i = 0; i < hash->size; i++) {
2043 head = &hash->table[i];
2044
2045 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002046 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002047 tt_global = container_of(tt_common,
2048 struct batadv_tt_global_entry,
2049 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002050 /* compute the CRC only for entries belonging to the
2051 * VLAN identified by the vid passed as parameter
2052 */
2053 if (tt_common->vid != vid)
2054 continue;
2055
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002056 /* Roaming clients are in the global table for
2057 * consistency only. They don't have to be
2058 * taken into account while computing the
2059 * global crc
2060 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002061 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002062 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002063 /* Temporary clients have not been announced yet, so
2064 * they have to be skipped while computing the global
2065 * crc
2066 */
2067 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2068 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002069
2070 /* find out if this global entry is announced by this
2071 * originator
2072 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002073 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002074 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002075 continue;
2076
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002077 /* use network order to read the VID: this ensures that
2078 * every node reads the bytes in the same order.
2079 */
2080 tmp_vid = htons(tt_common->vid);
2081 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002082
2083 /* compute the CRC on flags that have to be kept in sync
2084 * among nodes
2085 */
2086 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2087 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2088
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002089 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002090 }
2091 rcu_read_unlock();
2092 }
2093
Antonio Quartulliced72932013-04-24 16:37:51 +02002094 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002095}
2096
Antonio Quartulliced72932013-04-24 16:37:51 +02002097/**
2098 * batadv_tt_local_crc - calculates the checksum of the local table
2099 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002100 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002101 *
2102 * For details about the computation, please refer to the documentation for
2103 * batadv_tt_global_crc().
2104 *
2105 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002106 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002107static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2108 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002109{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002110 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002111 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002113 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002114 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002115 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002116
2117 for (i = 0; i < hash->size; i++) {
2118 head = &hash->table[i];
2119
2120 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002121 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002122 /* compute the CRC only for entries belonging to the
2123 * VLAN identified by vid
2124 */
2125 if (tt_common->vid != vid)
2126 continue;
2127
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002128 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002129 * account while computing the CRC
2130 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002131 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002132 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002133
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002134 /* use network order to read the VID: this ensures that
2135 * every node reads the bytes in the same order.
2136 */
2137 tmp_vid = htons(tt_common->vid);
2138 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002139
2140 /* compute the CRC on flags that have to be kept in sync
2141 * among nodes
2142 */
2143 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2144 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2145
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002146 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002147 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002148 rcu_read_unlock();
2149 }
2150
Antonio Quartulliced72932013-04-24 16:37:51 +02002151 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002152}
2153
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002155{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002156 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002157
Sven Eckelmann807736f2012-07-15 22:26:51 +02002158 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159
Sven Eckelmann807736f2012-07-15 22:26:51 +02002160 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002161 list_del(&node->list);
2162 kfree(node);
2163 }
2164
Sven Eckelmann807736f2012-07-15 22:26:51 +02002165 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002166}
2167
Sven Eckelmann56303d32012-06-05 22:31:31 +02002168static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2169 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002170 const void *tt_buff,
2171 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002172{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002173 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002174 * last OGM (the OGM could carry no changes)
2175 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002176 spin_lock_bh(&orig_node->tt_buff_lock);
2177 if (tt_buff_len > 0) {
2178 kfree(orig_node->tt_buff);
2179 orig_node->tt_buff_len = 0;
2180 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2181 if (orig_node->tt_buff) {
2182 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2183 orig_node->tt_buff_len = tt_buff_len;
2184 }
2185 }
2186 spin_unlock_bh(&orig_node->tt_buff_lock);
2187}
2188
Sven Eckelmann56303d32012-06-05 22:31:31 +02002189static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002190{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002191 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002192
Sven Eckelmann807736f2012-07-15 22:26:51 +02002193 spin_lock_bh(&bat_priv->tt.req_list_lock);
2194 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002195 if (batadv_has_timed_out(node->issued_at,
2196 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002197 list_del(&node->list);
2198 kfree(node);
2199 }
2200 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002201 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002202}
2203
2204/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002205 * has already been issued for this orig_node, NULL otherwise
2206 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002207static struct batadv_tt_req_node *
2208batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2209 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002210{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002211 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002212
Sven Eckelmann807736f2012-07-15 22:26:51 +02002213 spin_lock_bh(&bat_priv->tt.req_list_lock);
2214 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002215 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2216 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002217 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002218 goto unlock;
2219 }
2220
2221 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2222 if (!tt_req_node)
2223 goto unlock;
2224
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002225 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002226 tt_req_node->issued_at = jiffies;
2227
Sven Eckelmann807736f2012-07-15 22:26:51 +02002228 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002229unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002230 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002231 return tt_req_node;
2232}
2233
Marek Lindner335fbe02013-04-23 21:40:02 +08002234/**
2235 * batadv_tt_local_valid - verify that given tt entry is a valid one
2236 * @entry_ptr: to be checked local tt entry
2237 * @data_ptr: not used but definition required to satisfy the callback prototype
2238 *
2239 * Returns 1 if the entry is a valid, 0 otherwise.
2240 */
2241static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002242{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002243 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002244
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002245 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002246 return 0;
2247 return 1;
2248}
2249
Sven Eckelmanna5130882012-05-16 20:23:16 +02002250static int batadv_tt_global_valid(const void *entry_ptr,
2251 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002252{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002253 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2254 const struct batadv_tt_global_entry *tt_global_entry;
2255 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002256
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002257 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2258 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002259 return 0;
2260
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261 tt_global_entry = container_of(tt_common_entry,
2262 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002263 common);
2264
Sven Eckelmanna5130882012-05-16 20:23:16 +02002265 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002266}
2267
Marek Lindner335fbe02013-04-23 21:40:02 +08002268/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002269 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2270 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002271 * @bat_priv: the bat priv with all the soft interface information
2272 * @hash: hash table containing the tt entries
2273 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002274 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002275 * @valid_cb: function to filter tt change entries
2276 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002277 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002278static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2279 struct batadv_hashtable *hash,
2280 void *tvlv_buff, uint16_t tt_len,
2281 int (*valid_cb)(const void *, const void *),
2282 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002283{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002284 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002285 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002286 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002287 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002288 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002289
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002290 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002291 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002292
2293 rcu_read_lock();
2294 for (i = 0; i < hash->size; i++) {
2295 head = &hash->table[i];
2296
Sasha Levinb67bfe02013-02-27 17:06:00 -08002297 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002298 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002299 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002300 break;
2301
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002302 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002303 continue;
2304
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002305 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002306 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002307 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002308 memset(tt_change->reserved, 0,
2309 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002310
Marek Lindner335fbe02013-04-23 21:40:02 +08002311 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002312 tt_change++;
2313 }
2314 }
2315 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002316}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002317
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002318/**
2319 * batadv_tt_global_check_crc - check if all the CRCs are correct
2320 * @orig_node: originator for which the CRCs have to be checked
2321 * @tt_vlan: pointer to the first tvlv VLAN entry
2322 * @num_vlan: number of tvlv VLAN entries
2323 * @create: if true, create VLAN objects if not found
2324 *
2325 * Return true if all the received CRCs match the locally stored ones, false
2326 * otherwise
2327 */
2328static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2329 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2330 uint16_t num_vlan)
2331{
2332 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2333 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002334 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002335 int i;
2336
2337 /* check if each received CRC matches the locally stored one */
2338 for (i = 0; i < num_vlan; i++) {
2339 tt_vlan_tmp = tt_vlan + i;
2340
2341 /* if orig_node is a backbone node for this VLAN, don't check
2342 * the CRC as we ignore all the global entries over it
2343 */
2344 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002345 orig_node->orig,
2346 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002347 continue;
2348
2349 vlan = batadv_orig_node_vlan_get(orig_node,
2350 ntohs(tt_vlan_tmp->vid));
2351 if (!vlan)
2352 return false;
2353
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002354 crc = vlan->tt.crc;
2355 batadv_orig_node_vlan_free_ref(vlan);
2356
2357 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002358 return false;
2359 }
2360
2361 return true;
2362}
2363
2364/**
2365 * batadv_tt_local_update_crc - update all the local CRCs
2366 * @bat_priv: the bat priv with all the soft interface information
2367 */
2368static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2369{
2370 struct batadv_softif_vlan *vlan;
2371
2372 /* recompute the global CRC for each VLAN */
2373 rcu_read_lock();
2374 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2375 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2376 }
2377 rcu_read_unlock();
2378}
2379
2380/**
2381 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2382 * @bat_priv: the bat priv with all the soft interface information
2383 * @orig_node: the orig_node for which the CRCs have to be updated
2384 */
2385static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2386 struct batadv_orig_node *orig_node)
2387{
2388 struct batadv_orig_node_vlan *vlan;
2389 uint32_t crc;
2390
2391 /* recompute the global CRC for each VLAN */
2392 rcu_read_lock();
2393 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2394 /* if orig_node is a backbone node for this VLAN, don't compute
2395 * the CRC as we ignore all the global entries over it
2396 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002397 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2398 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002399 continue;
2400
2401 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2402 vlan->tt.crc = crc;
2403 }
2404 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002405}
2406
Antonio Quartulliced72932013-04-24 16:37:51 +02002407/**
2408 * batadv_send_tt_request - send a TT Request message to a given node
2409 * @bat_priv: the bat priv with all the soft interface information
2410 * @dst_orig_node: the destination of the message
2411 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002412 * @tt_vlan: pointer to the first tvlv VLAN object to request
2413 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002414 * @full_table: ask for the entire translation table if true, while only for the
2415 * last TT diff otherwise
2416 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002417static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2418 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002419 uint8_t ttvn,
2420 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2421 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002422{
Marek Lindner335fbe02013-04-23 21:40:02 +08002423 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002424 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002425 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2426 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002427 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002428 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002429
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002430 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002431 if (!primary_if)
2432 goto out;
2433
2434 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002435 * reply from the same orig_node yet
2436 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002437 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002438 if (!tt_req_node)
2439 goto out;
2440
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002441 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2442 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002443 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002444 goto out;
2445
Marek Lindner335fbe02013-04-23 21:40:02 +08002446 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2447 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002448 tvlv_tt_data->num_vlan = htons(num_vlan);
2449
2450 /* send all the CRCs within the request. This is needed by intermediate
2451 * nodes to ensure they have the correct table before replying
2452 */
2453 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2454 for (i = 0; i < num_vlan; i++) {
2455 tt_vlan_req->vid = tt_vlan->vid;
2456 tt_vlan_req->crc = tt_vlan->crc;
2457
2458 tt_vlan_req++;
2459 tt_vlan++;
2460 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002461
2462 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002463 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002464
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002465 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002466 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002467
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002468 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002469 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2470 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002471 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002472 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002473
2474out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002475 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002476 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002477 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002478 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002479 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002480 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002481 kfree(tt_req_node);
2482 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002483 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002484 return ret;
2485}
2486
Marek Lindner335fbe02013-04-23 21:40:02 +08002487/**
2488 * batadv_send_other_tt_response - send reply to tt request concerning another
2489 * node's translation table
2490 * @bat_priv: the bat priv with all the soft interface information
2491 * @tt_data: tt data containing the tt request information
2492 * @req_src: mac address of tt request sender
2493 * @req_dst: mac address of tt request recipient
2494 *
2495 * Returns true if tt request reply was sent, false otherwise.
2496 */
2497static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2498 struct batadv_tvlv_tt_data *tt_data,
2499 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002500{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002501 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002502 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002503 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002504 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002505 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002506 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002507 uint8_t orig_ttvn, req_ttvn;
2508 uint16_t tvlv_len;
2509 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002510
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002511 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002512 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002513 req_src, tt_data->ttvn, req_dst,
2514 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002515
2516 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002517 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002518 if (!req_dst_orig_node)
2519 goto out;
2520
Marek Lindner335fbe02013-04-23 21:40:02 +08002521 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002522 if (!res_dst_orig_node)
2523 goto out;
2524
Antonio Quartullia73105b2011-04-27 14:27:44 +02002525 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002526 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002527
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002528 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002529 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002530 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002531 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2532 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002533 goto out;
2534
Antonio Quartulli015758d2011-07-09 17:52:13 +02002535 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002536 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002537 !req_dst_orig_node->tt_buff)
2538 full_table = true;
2539 else
2540 full_table = false;
2541
Marek Lindner335fbe02013-04-23 21:40:02 +08002542 /* TT fragmentation hasn't been implemented yet, so send as many
2543 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002544 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002545 if (!full_table) {
2546 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2547 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002548
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002549 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2550 &tvlv_tt_data,
2551 &tt_change,
2552 &tt_len);
2553 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002554 goto unlock;
2555
Antonio Quartullia73105b2011-04-27 14:27:44 +02002556 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002557 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002558 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002559 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2560 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002561 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2562 * in the initial part
2563 */
2564 tt_len = -1;
2565 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2566 &tvlv_tt_data,
2567 &tt_change,
2568 &tt_len);
2569 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002570 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002571
2572 /* fill the rest of the tvlv with the real TT entries */
2573 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2574 tt_change, tt_len,
2575 batadv_tt_global_valid,
2576 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002577 }
2578
Marek Lindnera19d3d82013-05-27 15:33:25 +08002579 /* Don't send the response, if larger than fragmented packet. */
2580 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2581 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2582 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2583 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2584 res_dst_orig_node->orig);
2585 goto out;
2586 }
2587
Marek Lindner335fbe02013-04-23 21:40:02 +08002588 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2589 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002590
2591 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002592 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002593
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002594 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002595 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2596 res_dst_orig_node->orig, req_dst_orig_node->orig,
2597 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002598
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002599 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002600
Marek Lindner335fbe02013-04-23 21:40:02 +08002601 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002602 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2603 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002604
Marek Lindner335fbe02013-04-23 21:40:02 +08002605 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002606 goto out;
2607
2608unlock:
2609 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2610
2611out:
2612 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002613 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002614 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002615 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002616 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002617 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002618}
Sven Eckelmann96412692012-06-05 22:31:30 +02002619
Marek Lindner335fbe02013-04-23 21:40:02 +08002620/**
2621 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2622 * translation table
2623 * @bat_priv: the bat priv with all the soft interface information
2624 * @tt_data: tt data containing the tt request information
2625 * @req_src: mac address of tt request sender
2626 *
2627 * Returns true if tt request reply was sent, false otherwise.
2628 */
2629static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2630 struct batadv_tvlv_tt_data *tt_data,
2631 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002632{
Marek Lindner335fbe02013-04-23 21:40:02 +08002633 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002634 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002635 struct batadv_tvlv_tt_change *tt_change;
2636 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002637 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002638 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002639 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002640 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002641
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002642 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002643 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002644 req_src, tt_data->ttvn,
2645 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002647 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002648
Sven Eckelmann807736f2012-07-15 22:26:51 +02002649 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002650 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002651
Marek Lindner335fbe02013-04-23 21:40:02 +08002652 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002653 if (!orig_node)
2654 goto out;
2655
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002656 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002657 if (!primary_if)
2658 goto out;
2659
2660 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002661 * is too big send the whole local translation table
2662 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002664 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665 full_table = true;
2666 else
2667 full_table = false;
2668
Marek Lindner335fbe02013-04-23 21:40:02 +08002669 /* TT fragmentation hasn't been implemented yet, so send as many
2670 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002671 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002672 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002673 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002674
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002675 tt_len = bat_priv->tt.last_changeset_len;
2676 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2677 &tvlv_tt_data,
2678 &tt_change,
2679 &tt_len);
2680 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002681 goto unlock;
2682
Marek Lindner335fbe02013-04-23 21:40:02 +08002683 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002684 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002685 bat_priv->tt.last_changeset_len);
2686 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002687 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002688 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002689
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002690 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2691 * in the initial part
2692 */
2693 tt_len = -1;
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 out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002700
2701 /* fill the rest of the tvlv with the real TT entries */
2702 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2703 tt_change, tt_len,
2704 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705 }
2706
Marek Lindner335fbe02013-04-23 21:40:02 +08002707 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2708 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002709
2710 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002711 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002712
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002713 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002714 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2715 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002716
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002717 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002718
Marek Lindner335fbe02013-04-23 21:40:02 +08002719 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002720 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2721 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002722
Antonio Quartullia73105b2011-04-27 14:27:44 +02002723 goto out;
2724
2725unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002726 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002727out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002728 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002729 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002730 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002731 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002732 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002733 kfree(tvlv_tt_data);
2734 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002735 return true;
2736}
2737
Marek Lindner335fbe02013-04-23 21:40:02 +08002738/**
2739 * batadv_send_tt_response - send reply to tt request
2740 * @bat_priv: the bat priv with all the soft interface information
2741 * @tt_data: tt data containing the tt request information
2742 * @req_src: mac address of tt request sender
2743 * @req_dst: mac address of tt request recipient
2744 *
2745 * Returns true if tt request reply was sent, false otherwise.
2746 */
2747static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2748 struct batadv_tvlv_tt_data *tt_data,
2749 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002750{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002751 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002752 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002753 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002754 return batadv_send_other_tt_response(bat_priv, tt_data,
2755 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002756}
2757
Sven Eckelmann56303d32012-06-05 22:31:31 +02002758static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2759 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002760 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002761 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002762{
2763 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002764 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002765
2766 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002767 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2768 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002769 batadv_tt_global_del(bat_priv, orig_node,
2770 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002771 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002772 "tt removed by changes",
2773 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002774 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002775 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002776 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002777 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002778 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002779 /* In case of problem while storing a
2780 * global_entry, we stop the updating
2781 * procedure without committing the
2782 * ttvn change. This will avoid to send
2783 * corrupted data on tt_request
2784 */
2785 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002786 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002787 }
Linus Lüssinge17931d2014-02-15 17:47:50 +01002788 orig_node->capa_initialized |= BATADV_ORIG_CAPA_HAS_TT;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002789}
2790
Sven Eckelmann56303d32012-06-05 22:31:31 +02002791static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002792 struct batadv_tvlv_tt_change *tt_change,
2793 uint8_t ttvn, uint8_t *resp_src,
2794 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002795{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002796 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797
Marek Lindner335fbe02013-04-23 21:40:02 +08002798 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002799 if (!orig_node)
2800 goto out;
2801
2802 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002803 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2804 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002805
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002806 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2807 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002808
2809 spin_lock_bh(&orig_node->tt_buff_lock);
2810 kfree(orig_node->tt_buff);
2811 orig_node->tt_buff_len = 0;
2812 orig_node->tt_buff = NULL;
2813 spin_unlock_bh(&orig_node->tt_buff_lock);
2814
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002815 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002816
2817out:
2818 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002819 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002820}
2821
Sven Eckelmann56303d32012-06-05 22:31:31 +02002822static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2823 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002824 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002825 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002826{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002827 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2828 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002829
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002830 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2831 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002832 atomic_set(&orig_node->last_ttvn, ttvn);
2833}
2834
Antonio Quartullic018ad32013-06-04 12:11:39 +02002835/**
2836 * batadv_is_my_client - check if a client is served by the local node
2837 * @bat_priv: the bat priv with all the soft interface information
2838 * @addr: the mac adress of the client to check
2839 * @vid: VLAN identifier
2840 *
2841 * Returns true if the client is served by this node, false otherwise.
2842 */
2843bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2844 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002845{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002846 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002847 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002848
Antonio Quartullic018ad32013-06-04 12:11:39 +02002849 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002850 if (!tt_local_entry)
2851 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002852 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002853 * consistency purpose)
2854 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002855 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2856 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002857 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002858 ret = true;
2859out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002860 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002861 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002862 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002863}
2864
Marek Lindner335fbe02013-04-23 21:40:02 +08002865/**
2866 * batadv_handle_tt_response - process incoming tt reply
2867 * @bat_priv: the bat priv with all the soft interface information
2868 * @tt_data: tt data containing the tt request information
2869 * @resp_src: mac address of tt reply sender
2870 * @num_entries: number of tt change entries appended to the tt data
2871 */
2872static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2873 struct batadv_tvlv_tt_data *tt_data,
2874 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002875{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002876 struct batadv_tt_req_node *node, *safe;
2877 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002878 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002879 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2880 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002881
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002882 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002883 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002884 resp_src, tt_data->ttvn, num_entries,
2885 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002886
Marek Lindner335fbe02013-04-23 21:40:02 +08002887 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002888 if (!orig_node)
2889 goto out;
2890
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002891 spin_lock_bh(&orig_node->tt_lock);
2892
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002893 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2894 change_offset *= ntohs(tt_data->num_vlan);
2895 change_offset += sizeof(*tt_data);
2896 tvlv_ptr += change_offset;
2897
2898 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002899 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002900 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2901 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002902 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002903 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2904 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002905 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002906
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002907 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002908 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002909
2910 spin_unlock_bh(&orig_node->tt_lock);
2911
Antonio Quartullia73105b2011-04-27 14:27:44 +02002912 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002913 spin_lock_bh(&bat_priv->tt.req_list_lock);
2914 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002915 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002916 continue;
2917 list_del(&node->list);
2918 kfree(node);
2919 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002920
Sven Eckelmann807736f2012-07-15 22:26:51 +02002921 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002922out:
2923 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002924 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002925}
2926
Sven Eckelmann56303d32012-06-05 22:31:31 +02002927static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002928{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002929 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002930
Sven Eckelmann807736f2012-07-15 22:26:51 +02002931 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002932
Sven Eckelmann807736f2012-07-15 22:26:51 +02002933 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002934 list_del(&node->list);
2935 kfree(node);
2936 }
2937
Sven Eckelmann807736f2012-07-15 22:26:51 +02002938 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002939}
2940
Sven Eckelmann56303d32012-06-05 22:31:31 +02002941static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002942{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002943 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002944
Sven Eckelmann807736f2012-07-15 22:26:51 +02002945 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2946 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002947 if (!batadv_has_timed_out(node->first_time,
2948 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002949 continue;
2950
2951 list_del(&node->list);
2952 kfree(node);
2953 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002954 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002955}
2956
2957/* This function checks whether the client already reached the
2958 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2959 * will not be sent.
2960 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002961 * returns true if the ROAMING_ADV can be sent, false otherwise
2962 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002963static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002964 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002965{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002966 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002967 bool ret = false;
2968
Sven Eckelmann807736f2012-07-15 22:26:51 +02002969 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002970 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002971 * reply from the same orig_node yet
2972 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002973 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002974 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002975 continue;
2976
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002977 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002978 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002979 continue;
2980
Sven Eckelmann3e348192012-05-16 20:23:22 +02002981 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002982 /* Sorry, you roamed too many times! */
2983 goto unlock;
2984 ret = true;
2985 break;
2986 }
2987
2988 if (!ret) {
2989 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2990 if (!tt_roam_node)
2991 goto unlock;
2992
2993 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002994 atomic_set(&tt_roam_node->counter,
2995 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002996 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002997
Sven Eckelmann807736f2012-07-15 22:26:51 +02002998 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002999 ret = true;
3000 }
3001
3002unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003003 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003004 return ret;
3005}
3006
Antonio Quartullic018ad32013-06-04 12:11:39 +02003007/**
3008 * batadv_send_roam_adv - send a roaming advertisement message
3009 * @bat_priv: the bat priv with all the soft interface information
3010 * @client: mac address of the roaming client
3011 * @vid: VLAN identifier
3012 * @orig_node: message destination
3013 *
3014 * Send a ROAMING_ADV message to the node which was previously serving this
3015 * client. This is done to inform the node that from now on all traffic destined
3016 * for this particular roamed client has to be forwarded to the sender of the
3017 * roaming message.
3018 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003019static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003020 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003021 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003022{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003023 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003024 struct batadv_tvlv_roam_adv tvlv_roam;
3025
3026 primary_if = batadv_primary_if_get_selected(bat_priv);
3027 if (!primary_if)
3028 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003029
3030 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003031 * already roamed to us too many times
3032 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003033 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003034 goto out;
3035
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003036 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003037 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3038 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003039
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003040 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003041
Marek Lindner122edaa2013-04-23 21:40:03 +08003042 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003043 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003044
3045 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3046 orig_node->orig, BATADV_TVLV_ROAM, 1,
3047 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003048
3049out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003050 if (primary_if)
3051 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003052}
3053
Sven Eckelmanna5130882012-05-16 20:23:16 +02003054static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003055{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003056 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003057 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003058 struct batadv_priv *bat_priv;
3059
3060 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003061 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3062 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003063
Marek Lindnera19d3d82013-05-27 15:33:25 +08003064 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003065 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003066 batadv_tt_req_purge(bat_priv);
3067 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003068
Antonio Quartulli72414442012-12-25 13:14:37 +01003069 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3070 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003071}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003072
Sven Eckelmann56303d32012-06-05 22:31:31 +02003073void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003074{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003075 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3076 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3077
Sven Eckelmann807736f2012-07-15 22:26:51 +02003078 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003079
Sven Eckelmanna5130882012-05-16 20:23:16 +02003080 batadv_tt_local_table_free(bat_priv);
3081 batadv_tt_global_table_free(bat_priv);
3082 batadv_tt_req_list_free(bat_priv);
3083 batadv_tt_changes_list_free(bat_priv);
3084 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003085
Sven Eckelmann807736f2012-07-15 22:26:51 +02003086 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003087}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003088
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003089/**
3090 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3091 * table and possibly count them in the TT size
3092 * @bat_priv: the bat priv with all the soft interface information
3093 * @flags: the flag to switch
3094 * @enable: whether to set or unset the flag
3095 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003096 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003097static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3098 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003099{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003100 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3101 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003102 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003103 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003104 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003105
3106 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003107 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003108
3109 for (i = 0; i < hash->size; i++) {
3110 head = &hash->table[i];
3111
3112 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003113 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003114 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003115 if (enable) {
3116 if ((tt_common_entry->flags & flags) == flags)
3117 continue;
3118 tt_common_entry->flags |= flags;
3119 } else {
3120 if (!(tt_common_entry->flags & flags))
3121 continue;
3122 tt_common_entry->flags &= ~flags;
3123 }
3124 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003125
3126 if (!count)
3127 continue;
3128
3129 batadv_tt_local_size_inc(bat_priv,
3130 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003131 }
3132 rcu_read_unlock();
3133 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003134}
3135
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003136/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003137static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003138{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003139 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003140 struct batadv_tt_common_entry *tt_common;
3141 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003142 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003143 struct hlist_head *head;
3144 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003145 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003146
3147 if (!hash)
3148 return;
3149
3150 for (i = 0; i < hash->size; i++) {
3151 head = &hash->table[i];
3152 list_lock = &hash->list_locks[i];
3153
3154 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003155 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003156 hash_entry) {
3157 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003158 continue;
3159
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003160 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003161 "Deleting local tt entry (%pM, vid: %d): pending\n",
3162 tt_common->addr,
3163 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003164
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003165 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003166 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003167 tt_local = container_of(tt_common,
3168 struct batadv_tt_local_entry,
3169 common);
3170 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003171 }
3172 spin_unlock_bh(list_lock);
3173 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003174}
3175
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003176/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003177 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3178 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003179 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003180 *
3181 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003182 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003183static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003184{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003185 /* Update multicast addresses in local translation table */
3186 batadv_mcast_mla_update(bat_priv);
3187
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003188 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3189 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3190 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003191 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003192 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003193
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003194 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003195
Sven Eckelmanna5130882012-05-16 20:23:16 +02003196 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003197 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003198
3199 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003200 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003201 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003202 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003203 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003204
3205 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003206 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003207 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003208}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003209
Marek Lindnera19d3d82013-05-27 15:33:25 +08003210/**
3211 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3212 * have been queued in the time since the last commit
3213 * @bat_priv: the bat priv with all the soft interface information
3214 */
3215void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3216{
3217 spin_lock_bh(&bat_priv->tt.commit_lock);
3218 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003219 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003220}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003221
Sven Eckelmann56303d32012-06-05 22:31:31 +02003222bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003223 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003224{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003225 struct batadv_tt_local_entry *tt_local_entry = NULL;
3226 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003227 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003228 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003229
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003230 vlan = batadv_softif_vlan_get(bat_priv, vid);
3231 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003232 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003233
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003234 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003235 if (!tt_local_entry)
3236 goto out;
3237
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003238 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003239 if (!tt_global_entry)
3240 goto out;
3241
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003242 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003243 goto out;
3244
Marek Lindner5870adc2012-06-20 17:16:05 +02003245 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003246
3247out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003248 if (vlan)
3249 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003250 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003251 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003252 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003253 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003254 return ret;
3255}
Marek Lindnera943cac2011-07-30 13:10:18 +02003256
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003257/**
3258 * batadv_tt_update_orig - update global translation table with new tt
3259 * information received via ogms
3260 * @bat_priv: the bat priv with all the soft interface information
3261 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003262 * @tt_vlan: pointer to the first tvlv VLAN entry
3263 * @tt_num_vlan: number of tvlv VLAN entries
3264 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003265 * @tt_num_changes: number of tt changes inside the tt buffer
3266 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003267 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003268 */
3269static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3270 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003271 const void *tt_buff, uint16_t tt_num_vlan,
3272 struct batadv_tvlv_tt_change *tt_change,
3273 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003274{
3275 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003276 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003277 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003278 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003279
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003280 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003281 has_tt_init = orig_node->capa_initialized & BATADV_ORIG_CAPA_HAS_TT;
3282
Antonio Quartulli17071572011-11-07 16:36:40 +01003283 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003284 * increased by one -> we can apply the attached changes
3285 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003286 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003287 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003288 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3289 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003290 * In this case send a tt request
3291 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003292 if (!tt_num_changes) {
3293 full_table = false;
3294 goto request_table;
3295 }
3296
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003297 spin_lock_bh(&orig_node->tt_lock);
3298
Sven Eckelmanna5130882012-05-16 20:23:16 +02003299 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003300 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003301
3302 /* Even if we received the precomputed crc with the OGM, we
3303 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003304 * in the global table
3305 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003306 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003307
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003308 spin_unlock_bh(&orig_node->tt_lock);
3309
Marek Lindnera943cac2011-07-30 13:10:18 +02003310 /* The ttvn alone is not enough to guarantee consistency
3311 * because a single value could represent different states
3312 * (due to the wrap around). Thus a node has to check whether
3313 * the resulting table (after applying the changes) is still
3314 * consistent or not. E.g. a node could disconnect while its
3315 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3316 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003317 * inconsistency
3318 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003319 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3320 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003321 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003322 } else {
3323 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003324 * in sync anymore -> request fresh tt data
3325 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003326 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003327 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3328 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003329request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003330 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003331 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3332 orig_node->orig, ttvn, orig_ttvn,
3333 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003334 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003335 tt_vlan, tt_num_vlan,
3336 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003337 return;
3338 }
3339 }
3340}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003341
Antonio Quartullic018ad32013-06-04 12:11:39 +02003342/**
3343 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3344 * @bat_priv: the bat priv with all the soft interface information
3345 * @addr: the mac address of the client to check
3346 * @vid: VLAN identifier
3347 *
3348 * Returns true if we know that the client has moved from its old originator
3349 * to another one. This entry is still kept for consistency purposes and will be
3350 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003351 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003352bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003353 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003354{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003355 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003356 bool ret = false;
3357
Antonio Quartullic018ad32013-06-04 12:11:39 +02003358 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003359 if (!tt_global_entry)
3360 goto out;
3361
Antonio Quartullic1d07432013-01-15 22:17:19 +10003362 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003363 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003364out:
3365 return ret;
3366}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003367
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003368/**
3369 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3370 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003371 * @addr: the mac address of the local client to query
3372 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003373 *
3374 * Returns true if the local client is known to be roaming (it is not served by
3375 * this node anymore) or not. If yes, the client is still present in the table
3376 * to keep the latter consistent with the node TTVN
3377 */
3378bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003379 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003380{
3381 struct batadv_tt_local_entry *tt_local_entry;
3382 bool ret = false;
3383
Antonio Quartullic018ad32013-06-04 12:11:39 +02003384 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003385 if (!tt_local_entry)
3386 goto out;
3387
3388 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3389 batadv_tt_local_entry_free_ref(tt_local_entry);
3390out:
3391 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003392}
3393
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003394bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3395 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003396 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003397 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003398{
3399 bool ret = false;
3400
Antonio Quartulli16052782013-06-04 12:11:41 +02003401 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003402 BATADV_TT_CLIENT_TEMP,
3403 atomic_read(&orig_node->last_ttvn)))
3404 goto out;
3405
3406 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003407 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3408 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003409 ret = true;
3410out:
3411 return ret;
3412}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003413
3414/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003415 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3416 * maximum packet size that can be transported through the mesh
3417 * @soft_iface: netdev struct of the mesh interface
3418 *
3419 * Remove entries older than 'timeout' and half timeout if more entries need
3420 * to be removed.
3421 */
3422void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3423{
3424 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3425 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3426 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3427 bool reduced = false;
3428
3429 spin_lock_bh(&bat_priv->tt.commit_lock);
3430
3431 while (true) {
3432 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3433 if (packet_size_max >= table_size)
3434 break;
3435
3436 batadv_tt_local_purge(bat_priv, timeout);
3437 batadv_tt_local_purge_pending_clients(bat_priv);
3438
3439 timeout /= 2;
3440 reduced = true;
3441 net_ratelimited_function(batadv_info, soft_iface,
3442 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3443 packet_size_max);
3444 }
3445
3446 /* commit these changes immediately, to avoid synchronization problem
3447 * with the TTVN
3448 */
3449 if (reduced)
3450 batadv_tt_local_commit_changes_nolock(bat_priv);
3451
3452 spin_unlock_bh(&bat_priv->tt.commit_lock);
3453}
3454
3455/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003456 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3457 * @bat_priv: the bat priv with all the soft interface information
3458 * @orig: the orig_node of the ogm
3459 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3460 * @tvlv_value: tvlv buffer containing the gateway data
3461 * @tvlv_value_len: tvlv buffer length
3462 */
3463static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3464 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003465 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003466 uint16_t tvlv_value_len)
3467{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003468 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3469 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003470 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003471 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003472
3473 if (tvlv_value_len < sizeof(*tt_data))
3474 return;
3475
3476 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3477 tvlv_value_len -= sizeof(*tt_data);
3478
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003479 num_vlan = ntohs(tt_data->num_vlan);
3480
3481 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3482 return;
3483
3484 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3485 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3486 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3487
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003488 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003489
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003490 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3491 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003492}
3493
3494/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003495 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3496 * container
3497 * @bat_priv: the bat priv with all the soft interface information
3498 * @src: mac address of tt tvlv sender
3499 * @dst: mac address of tt tvlv recipient
3500 * @tvlv_value: tvlv buffer containing the tt data
3501 * @tvlv_value_len: tvlv buffer length
3502 *
3503 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3504 * otherwise.
3505 */
3506static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3507 uint8_t *src, uint8_t *dst,
3508 void *tvlv_value,
3509 uint16_t tvlv_value_len)
3510{
3511 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003512 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003513 char tt_flag;
3514 bool ret;
3515
3516 if (tvlv_value_len < sizeof(*tt_data))
3517 return NET_RX_SUCCESS;
3518
3519 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3520 tvlv_value_len -= sizeof(*tt_data);
3521
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003522 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3523 tt_vlan_len *= ntohs(tt_data->num_vlan);
3524
3525 if (tvlv_value_len < tt_vlan_len)
3526 return NET_RX_SUCCESS;
3527
3528 tvlv_value_len -= tt_vlan_len;
3529 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003530
3531 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3532 case BATADV_TT_REQUEST:
3533 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3534
3535 /* If this node cannot provide a TT response the tt_request is
3536 * forwarded
3537 */
3538 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3539 if (!ret) {
3540 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3541 tt_flag = 'F';
3542 else
3543 tt_flag = '.';
3544
3545 batadv_dbg(BATADV_DBG_TT, bat_priv,
3546 "Routing TT_REQUEST to %pM [%c]\n",
3547 dst, tt_flag);
3548 /* tvlv API will re-route the packet */
3549 return NET_RX_DROP;
3550 }
3551 break;
3552 case BATADV_TT_RESPONSE:
3553 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3554
3555 if (batadv_is_my_mac(bat_priv, dst)) {
3556 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003557 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003558 return NET_RX_SUCCESS;
3559 }
3560
3561 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3562 tt_flag = 'F';
3563 else
3564 tt_flag = '.';
3565
3566 batadv_dbg(BATADV_DBG_TT, bat_priv,
3567 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3568
3569 /* tvlv API will re-route the packet */
3570 return NET_RX_DROP;
3571 }
3572
3573 return NET_RX_SUCCESS;
3574}
3575
3576/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003577 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3578 * @bat_priv: the bat priv with all the soft interface information
3579 * @src: mac address of tt tvlv sender
3580 * @dst: mac address of tt tvlv recipient
3581 * @tvlv_value: tvlv buffer containing the tt data
3582 * @tvlv_value_len: tvlv buffer length
3583 *
3584 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3585 * otherwise.
3586 */
3587static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3588 uint8_t *src, uint8_t *dst,
3589 void *tvlv_value,
3590 uint16_t tvlv_value_len)
3591{
3592 struct batadv_tvlv_roam_adv *roaming_adv;
3593 struct batadv_orig_node *orig_node = NULL;
3594
3595 /* If this node is not the intended recipient of the
3596 * roaming advertisement the packet is forwarded
3597 * (the tvlv API will re-route the packet).
3598 */
3599 if (!batadv_is_my_mac(bat_priv, dst))
3600 return NET_RX_DROP;
3601
Marek Lindner122edaa2013-04-23 21:40:03 +08003602 if (tvlv_value_len < sizeof(*roaming_adv))
3603 goto out;
3604
3605 orig_node = batadv_orig_hash_find(bat_priv, src);
3606 if (!orig_node)
3607 goto out;
3608
3609 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3610 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3611
3612 batadv_dbg(BATADV_DBG_TT, bat_priv,
3613 "Received ROAMING_ADV from %pM (client %pM)\n",
3614 src, roaming_adv->client);
3615
3616 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003617 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003618 atomic_read(&orig_node->last_ttvn) + 1);
3619
3620out:
3621 if (orig_node)
3622 batadv_orig_node_free_ref(orig_node);
3623 return NET_RX_SUCCESS;
3624}
3625
3626/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003627 * batadv_tt_init - initialise the translation table internals
3628 * @bat_priv: the bat priv with all the soft interface information
3629 *
3630 * Return 0 on success or negative error number in case of failure.
3631 */
3632int batadv_tt_init(struct batadv_priv *bat_priv)
3633{
3634 int ret;
3635
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003636 /* synchronized flags must be remote */
3637 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3638
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003639 ret = batadv_tt_local_init(bat_priv);
3640 if (ret < 0)
3641 return ret;
3642
3643 ret = batadv_tt_global_init(bat_priv);
3644 if (ret < 0)
3645 return ret;
3646
3647 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003648 batadv_tt_tvlv_unicast_handler_v1,
3649 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003650
Marek Lindner122edaa2013-04-23 21:40:03 +08003651 batadv_tvlv_handler_register(bat_priv, NULL,
3652 batadv_roam_tvlv_unicast_handler_v1,
3653 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3654
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003655 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3656 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3657 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3658
3659 return 1;
3660}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003661
3662/**
3663 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3664 * @bat_priv: the bat priv with all the soft interface information
3665 * @addr: the mac address of the client
3666 * @vid: the identifier of the VLAN where this client is connected
3667 *
3668 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3669 * otherwise
3670 */
3671bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3672 const uint8_t *addr, unsigned short vid)
3673{
3674 struct batadv_tt_global_entry *tt;
3675 bool ret;
3676
3677 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3678 if (!tt)
3679 return false;
3680
3681 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3682
3683 batadv_tt_global_entry_free_ref(tt);
3684
3685 return ret;
3686}