blob: 4082d05a93a93b5a1946aea347ac9cbdeaf44992 [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
Sven Eckelmanna5130882012-05-16 20:23:16 +0200196static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200197{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200198 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200199
Sven Eckelmann56303d32012-06-05 22:31:31 +0200200 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
Linus Lüssing72822222013-04-15 21:43:29 +0800201
202 /* We are in an rcu callback here, therefore we cannot use
203 * batadv_orig_node_free_ref() and its call_rcu():
204 * An rcu_barrier() wouldn't wait for that to finish
205 */
206 batadv_orig_node_free_ref_now(orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200207 kfree(orig_entry);
208}
209
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200210/**
211 * batadv_tt_local_size_mod - change the size by v of the local table identified
212 * by vid
213 * @bat_priv: the bat priv with all the soft interface information
214 * @vid: the VLAN identifier of the sub-table to change
215 * @v: the amount to sum to the local table size
216 */
217static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
218 unsigned short vid, int v)
219{
220 struct batadv_softif_vlan *vlan;
221
222 vlan = batadv_softif_vlan_get(bat_priv, vid);
223 if (!vlan)
224 return;
225
226 atomic_add(v, &vlan->tt.num_entries);
227
228 batadv_softif_vlan_free_ref(vlan);
229}
230
231/**
232 * batadv_tt_local_size_inc - increase by one the local table size for the given
233 * vid
234 * @bat_priv: the bat priv with all the soft interface information
235 * @vid: the VLAN identifier
236 */
237static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
238 unsigned short vid)
239{
240 batadv_tt_local_size_mod(bat_priv, vid, 1);
241}
242
243/**
244 * batadv_tt_local_size_dec - decrease by one the local table size for the given
245 * vid
246 * @bat_priv: the bat priv with all the soft interface information
247 * @vid: the VLAN identifier
248 */
249static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
250 unsigned short vid)
251{
252 batadv_tt_local_size_mod(bat_priv, vid, -1);
253}
254
255/**
256 * batadv_tt_global_size_mod - change the size by v of the local table
257 * identified by vid
258 * @bat_priv: the bat priv with all the soft interface information
259 * @vid: the VLAN identifier
260 * @v: the amount to sum to the global table size
261 */
262static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
263 unsigned short vid, int v)
264{
265 struct batadv_orig_node_vlan *vlan;
266
267 vlan = batadv_orig_node_vlan_new(orig_node, vid);
268 if (!vlan)
269 return;
270
271 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
272 spin_lock_bh(&orig_node->vlan_list_lock);
273 list_del_rcu(&vlan->list);
274 spin_unlock_bh(&orig_node->vlan_list_lock);
275 batadv_orig_node_vlan_free_ref(vlan);
276 }
277
278 batadv_orig_node_vlan_free_ref(vlan);
279}
280
281/**
282 * batadv_tt_global_size_inc - increase by one the global table size for the
283 * given vid
284 * @orig_node: the originator which global table size has to be decreased
285 * @vid: the vlan identifier
286 */
287static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
288 unsigned short vid)
289{
290 batadv_tt_global_size_mod(orig_node, vid, 1);
291}
292
293/**
294 * batadv_tt_global_size_dec - decrease by one the global table size for the
295 * given vid
296 * @orig_node: the originator which global table size has to be decreased
297 * @vid: the vlan identifier
298 */
299static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
300 unsigned short vid)
301{
302 batadv_tt_global_size_mod(orig_node, vid, -1);
303}
304
Sven Eckelmanna5130882012-05-16 20:23:16 +0200305static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200306batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200307{
Antonio Quartullid657e622012-07-01 14:09:12 +0200308 if (!atomic_dec_and_test(&orig_entry->refcount))
309 return;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200310
Sven Eckelmanna5130882012-05-16 20:23:16 +0200311 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200312}
313
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200314/**
315 * batadv_tt_local_event - store a local TT event (ADD/DEL)
316 * @bat_priv: the bat priv with all the soft interface information
317 * @tt_local_entry: the TT entry involved in the event
318 * @event_flags: flags to store in the event structure
319 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200320static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200321 struct batadv_tt_local_entry *tt_local_entry,
322 uint8_t event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200323{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200324 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200325 struct batadv_tt_common_entry *common = &tt_local_entry->common;
326 uint8_t flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200327 bool event_removed = false;
328 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200329
330 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200331 if (!tt_change_node)
332 return;
333
Antonio Quartulliff66c972011-06-30 01:14:00 +0200334 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100335 memset(tt_change_node->change.reserved, 0,
336 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100337 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200338 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200339
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200341
342 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200343 spin_lock_bh(&bat_priv->tt.changes_list_lock);
344 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200345 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200346 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200347 continue;
348
349 /* DEL+ADD in the same orig interval have no effect and can be
350 * removed to avoid silly behaviour on the receiver side. The
351 * other way around (ADD+DEL) can happen in case of roaming of
352 * a client still in the NEW state. Roaming of NEW clients is
353 * now possible due to automatically recognition of "temporary"
354 * clients
355 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200356 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200357 if (!del_op_requested && del_op_entry)
358 goto del;
359 if (del_op_requested && !del_op_entry)
360 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200361
362 /* this is a second add in the same originator interval. It
363 * means that flags have been changed: update them!
364 */
365 if (!del_op_requested && !del_op_entry)
366 entry->change.flags = flags;
367
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200368 continue;
369del:
370 list_del(&entry->list);
371 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000372 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200373 event_removed = true;
374 goto unlock;
375 }
376
Antonio Quartullia73105b2011-04-27 14:27:44 +0200377 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200378 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200379
380unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200381 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200382
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200383 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200384 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200385 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200386 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200387}
388
Marek Lindner335fbe02013-04-23 21:40:02 +0800389/**
390 * batadv_tt_len - compute length in bytes of given number of tt changes
391 * @changes_num: number of tt changes
392 *
393 * Returns computed length in bytes.
394 */
395static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200396{
Marek Lindner335fbe02013-04-23 21:40:02 +0800397 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398}
399
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200400/**
401 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
402 * @tt_len: available space
403 *
404 * Returns the number of entries.
405 */
406static uint16_t batadv_tt_entries(uint16_t tt_len)
407{
408 return tt_len / batadv_tt_len(1);
409}
410
Marek Lindnera19d3d82013-05-27 15:33:25 +0800411/**
412 * batadv_tt_local_table_transmit_size - calculates the local translation table
413 * size when transmitted over the air
414 * @bat_priv: the bat priv with all the soft interface information
415 *
416 * Returns local translation table size in bytes.
417 */
418static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
419{
420 uint16_t num_vlan = 0, tt_local_entries = 0;
421 struct batadv_softif_vlan *vlan;
422 int hdr_size;
423
424 rcu_read_lock();
425 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
426 num_vlan++;
427 tt_local_entries += atomic_read(&vlan->tt.num_entries);
428 }
429 rcu_read_unlock();
430
431 /* header size of tvlv encapsulated tt response payload */
432 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
433 hdr_size += sizeof(struct batadv_tvlv_hdr);
434 hdr_size += sizeof(struct batadv_tvlv_tt_data);
435 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
436
437 return hdr_size + batadv_tt_len(tt_local_entries);
438}
439
Sven Eckelmann56303d32012-06-05 22:31:31 +0200440static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200442 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200443 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Sven Eckelmann807736f2012-07-15 22:26:51 +0200445 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Sven Eckelmann807736f2012-07-15 22:26:51 +0200447 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200448 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Antonio Quartullidec05072012-11-10 11:00:32 +0100450 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
451 &batadv_tt_local_hash_lock_class_key);
452
Sven Eckelmann5346c352012-05-05 13:27:28 +0200453 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454}
455
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200456static void batadv_tt_global_free(struct batadv_priv *bat_priv,
457 struct batadv_tt_global_entry *tt_global,
458 const char *message)
459{
460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200461 "Deleting global tt entry %pM (vid: %d): %s\n",
462 tt_global->common.addr,
463 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200464
465 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200466 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200467 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200468}
469
Antonio Quartullic018ad32013-06-04 12:11:39 +0200470/**
471 * batadv_tt_local_add - add a new client to the local table or update an
472 * existing client
473 * @soft_iface: netdev struct of the mesh interface
474 * @addr: the mac address of the client to add
475 * @vid: VLAN identifier
476 * @ifindex: index of the interface where the client is connected to (useful to
477 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100478 * @mark: the value contained in the skb->mark field of the received packet (if
479 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800480 *
481 * Returns true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200482 */
Marek Lindnera19d3d82013-05-27 15:33:25 +0800483bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
Antonio Quartulli9464d072013-11-16 12:03:48 +0100484 unsigned short vid, int ifindex, uint32_t mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200486 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200487 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100488 struct batadv_tt_global_entry *tt_global = NULL;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200489 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200490 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200491 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800492 int hash_added, table_size, packet_size_max;
493 bool ret = false, roamed_back = false;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200494 uint8_t remote_flags;
Antonio Quartulli9464d072013-11-16 12:03:48 +0100495 uint32_t match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200497 if (ifindex != BATADV_NULL_IFINDEX)
498 in_dev = dev_get_by_index(&init_net, ifindex);
499
Antonio Quartullic018ad32013-06-04 12:11:39 +0200500 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100501
502 if (!is_multicast_ether_addr(addr))
503 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Antonio Quartulli47c94652012-09-23 22:38:35 +0200505 if (tt_local) {
506 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200507 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
508 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200509 "Re-adding pending client %pM (vid: %d)\n",
510 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200511 /* whatever the reason why the PENDING flag was set,
512 * this is a client which was enqueued to be removed in
513 * this orig_interval. Since it popped up again, the
514 * flag can be reset like it was never enqueued
515 */
516 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
517 goto add_event;
518 }
519
520 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
521 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200522 "Roaming client %pM (vid: %d) came back to its original location\n",
523 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200524 /* the ROAM flag is set because this client roamed away
525 * and the node got a roaming_advertisement message. Now
526 * that the client popped up again at its original
527 * location such flag can be unset
528 */
529 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
530 roamed_back = true;
531 }
532 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 }
534
Marek Lindnera19d3d82013-05-27 15:33:25 +0800535 /* Ignore the client if we cannot send it in a full table response. */
536 table_size = batadv_tt_local_table_transmit_size(bat_priv);
537 table_size += batadv_tt_len(1);
538 packet_size_max = atomic_read(&bat_priv->packet_size_max);
539 if (table_size > packet_size_max) {
540 net_ratelimited_function(batadv_info, soft_iface,
541 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
542 table_size, packet_size_max, addr);
543 goto out;
544 }
545
Antonio Quartulli47c94652012-09-23 22:38:35 +0200546 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
547 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200548 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200549
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200550 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200551 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
552 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann807736f2012-07-15 22:26:51 +0200553 (uint8_t)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100555 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100556 /* The local entry has to be marked as NEW to avoid to send it in
557 * a full table response going out before the next ttvn increment
558 * (consistency check)
559 */
560 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200561 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200562 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200563 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
564 atomic_set(&tt_local->common.refcount, 2);
565 tt_local->last_seen = jiffies;
566 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100568 /* the batman interface mac and multicast addresses should never be
569 * purged
570 */
571 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
572 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200573 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
Sven Eckelmann807736f2012-07-15 22:26:51 +0200575 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200576 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200577 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100578
579 if (unlikely(hash_added != 0)) {
580 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200581 batadv_tt_local_entry_free_ref(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100582 goto out;
583 }
584
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200585add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200586 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200587
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200588check_roaming:
589 /* Check whether it is a roaming, but don't do anything if the roaming
590 * process has already been handled
591 */
592 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200593 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200594 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200595 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800596 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200597 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200598 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200599 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200600 }
601 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200602 if (roamed_back) {
603 batadv_tt_global_free(bat_priv, tt_global,
604 "Roaming canceled");
605 tt_global = NULL;
606 } else {
607 /* The global entry has to be marked as ROAMING and
608 * has to be kept for consistency purpose
609 */
610 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
611 tt_global->roam_at = jiffies;
612 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200613 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200614
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200615 /* store the current remote flags before altering them. This helps
616 * understanding is flags are changing or not
617 */
618 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800619
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200620 if (batadv_is_wifi_netdev(in_dev))
621 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
622 else
623 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
624
Antonio Quartulli9464d072013-11-16 12:03:48 +0100625 /* check the mark in the skb: if it's equal to the configured
626 * isolation_mark, it means the packet is coming from an isolated
627 * non-mesh client
628 */
629 match_mark = (mark & bat_priv->isolation_mark_mask);
630 if (bat_priv->isolation_mark_mask &&
631 match_mark == bat_priv->isolation_mark)
632 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
633 else
634 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
635
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200636 /* if any "dynamic" flag has been modified, resend an ADD event for this
637 * entry so that all the nodes can get the new flags
638 */
639 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
640 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
641
642 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200643out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200644 if (in_dev)
645 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200646 if (tt_local)
647 batadv_tt_local_entry_free_ref(tt_local);
648 if (tt_global)
649 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800650 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651}
652
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800653/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200654 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
655 * within a TT Response directed to another node
656 * @orig_node: originator for which the TT data has to be prepared
657 * @tt_data: uninitialised pointer to the address of the TVLV buffer
658 * @tt_change: uninitialised pointer to the address of the area where the TT
659 * changed can be stored
660 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
661 * function reserves the amount of space needed to send the entire global TT
662 * table. In case of success the value is updated with the real amount of
663 * reserved bytes
664
665 * Allocate the needed amount of memory for the entire TT TVLV and write its
666 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
667 * objects, one per active VLAN served by the originator node.
668 *
669 * Return the size of the allocated buffer or 0 in case of failure.
670 */
671static uint16_t
672batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
673 struct batadv_tvlv_tt_data **tt_data,
674 struct batadv_tvlv_tt_change **tt_change,
675 int32_t *tt_len)
676{
677 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
678 struct batadv_tvlv_tt_vlan_data *tt_vlan;
679 struct batadv_orig_node_vlan *vlan;
680 uint8_t *tt_change_ptr;
681
682 rcu_read_lock();
683 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
684 num_vlan++;
685 num_entries += atomic_read(&vlan->tt.num_entries);
686 }
687
688 change_offset = sizeof(**tt_data);
689 change_offset += num_vlan * sizeof(*tt_vlan);
690
691 /* if tt_len is negative, allocate the space needed by the full table */
692 if (*tt_len < 0)
693 *tt_len = batadv_tt_len(num_entries);
694
695 tvlv_len = *tt_len;
696 tvlv_len += change_offset;
697
698 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
699 if (!*tt_data) {
700 *tt_len = 0;
701 goto out;
702 }
703
704 (*tt_data)->flags = BATADV_NO_FLAGS;
705 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
706 (*tt_data)->num_vlan = htons(num_vlan);
707
708 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
709 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
710 tt_vlan->vid = htons(vlan->vid);
711 tt_vlan->crc = htonl(vlan->tt.crc);
712
713 tt_vlan++;
714 }
715
716 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
717 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
718
719out:
720 rcu_read_unlock();
721 return tvlv_len;
722}
723
724/**
725 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
726 * node
727 * @bat_priv: the bat priv with all the soft interface information
728 * @tt_data: uninitialised pointer to the address of the TVLV buffer
729 * @tt_change: uninitialised pointer to the address of the area where the TT
730 * changes can be stored
731 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
732 * function reserves the amount of space needed to send the entire local TT
733 * table. In case of success the value is updated with the real amount of
734 * reserved bytes
735 *
736 * Allocate the needed amount of memory for the entire TT TVLV and write its
737 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
738 * objects, one per active VLAN.
739 *
740 * Return the size of the allocated buffer or 0 in case of failure.
741 */
742static uint16_t
743batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
744 struct batadv_tvlv_tt_data **tt_data,
745 struct batadv_tvlv_tt_change **tt_change,
746 int32_t *tt_len)
747{
748 struct batadv_tvlv_tt_vlan_data *tt_vlan;
749 struct batadv_softif_vlan *vlan;
750 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
751 uint8_t *tt_change_ptr;
752 int change_offset;
753
754 rcu_read_lock();
755 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
756 num_vlan++;
757 num_entries += atomic_read(&vlan->tt.num_entries);
758 }
759
760 change_offset = sizeof(**tt_data);
761 change_offset += num_vlan * sizeof(*tt_vlan);
762
763 /* if tt_len is negative, allocate the space needed by the full table */
764 if (*tt_len < 0)
765 *tt_len = batadv_tt_len(num_entries);
766
767 tvlv_len = *tt_len;
768 tvlv_len += change_offset;
769
770 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
771 if (!*tt_data) {
772 tvlv_len = 0;
773 goto out;
774 }
775
776 (*tt_data)->flags = BATADV_NO_FLAGS;
777 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
778 (*tt_data)->num_vlan = htons(num_vlan);
779
780 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
781 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
782 tt_vlan->vid = htons(vlan->vid);
783 tt_vlan->crc = htonl(vlan->tt.crc);
784
785 tt_vlan++;
786 }
787
788 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
789 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
790
791out:
792 rcu_read_unlock();
793 return tvlv_len;
794}
795
796/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800797 * batadv_tt_tvlv_container_update - update the translation table tvlv container
798 * after local tt changes have been committed
799 * @bat_priv: the bat priv with all the soft interface information
800 */
801static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800803 struct batadv_tt_change_node *entry, *safe;
804 struct batadv_tvlv_tt_data *tt_data;
805 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200806 int tt_diff_len, tt_change_len = 0;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800807 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200808 uint16_t tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200810 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
811 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800812
813 /* if we have too many changes for one packet don't send any
814 * and wait for the tt table request which will be fragmented
815 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800816 if (tt_diff_len > bat_priv->soft_iface->mtu)
817 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800818
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200819 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
820 &tt_change, &tt_diff_len);
821 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800822 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800823
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800824 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800825
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800826 if (tt_diff_len == 0)
827 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800828
Sven Eckelmann807736f2012-07-15 22:26:51 +0200829 spin_lock_bh(&bat_priv->tt.changes_list_lock);
830 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831
Sven Eckelmann807736f2012-07-15 22:26:51 +0200832 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100833 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800834 if (tt_diff_entries_count < tt_diff_entries_num) {
835 memcpy(tt_change + tt_diff_entries_count,
836 &entry->change,
837 sizeof(struct batadv_tvlv_tt_change));
838 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200840 list_del(&entry->list);
841 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200843 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844
Antonio Quartullia73105b2011-04-27 14:27:44 +0200845 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200846 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
847 kfree(bat_priv->tt.last_changeset);
848 bat_priv->tt.last_changeset_len = 0;
849 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800850 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800851 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800852 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800853 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200854 * instead of providing the diff
855 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800856 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200857 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800858 memcpy(bat_priv->tt.last_changeset,
859 tt_change, tt_change_len);
860 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200861 }
862 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200863 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800865container_register:
866 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200867 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800868 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869}
870
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200871int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000872{
873 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200874 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200875 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200876 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100877 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200878 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200879 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200881 unsigned short vid;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200882 uint32_t i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100883 int last_seen_secs;
884 int last_seen_msecs;
885 unsigned long last_seen_jiffies;
886 bool no_purge;
887 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000888
Marek Lindner30da63a2012-08-03 17:15:46 +0200889 primary_if = batadv_seq_print_text_primary_if_get(seq);
890 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200891 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100893 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200894 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
895 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100896 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200897 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000898
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899 for (i = 0; i < hash->size; i++) {
900 head = &hash->table[i];
901
Marek Lindner7aadf882011-02-18 12:28:09 +0000902 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800903 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000904 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100905 tt_local = container_of(tt_common_entry,
906 struct batadv_tt_local_entry,
907 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200908 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100909 last_seen_jiffies = jiffies - tt_local->last_seen;
910 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
911 last_seen_secs = last_seen_msecs / 1000;
912 last_seen_msecs = last_seen_msecs % 1000;
913
914 no_purge = tt_common_entry->flags & np_flag;
915
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200916 vlan = batadv_softif_vlan_get(bat_priv, vid);
917 if (!vlan) {
918 seq_printf(seq, "Cannot retrieve VLAN %d\n",
919 BATADV_PRINT_VID(vid));
920 continue;
921 }
922
923 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100924 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100925 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +0200926 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100927 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200928 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +0100929 no_purge ? 'P' : '.',
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100930 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200931 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100932 (tt_common_entry->flags &
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200933 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100934 (tt_common_entry->flags &
Antonio Quartulli85766a82012-11-08 22:16:16 +0100935 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100936 (tt_common_entry->flags &
937 BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +0100938 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200939 no_purge ? 0 : last_seen_msecs,
940 vlan->tt.crc);
941
942 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000944 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945 }
Marek Lindner32ae9b22011-04-20 15:40:58 +0200946out:
947 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200948 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +0200949 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950}
951
Sven Eckelmann56303d32012-06-05 22:31:31 +0200952static void
953batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
954 struct batadv_tt_local_entry *tt_local_entry,
955 uint16_t flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000956{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200957 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000958
Antonio Quartulli015758d2011-07-09 17:52:13 +0200959 /* The local client has to be marked as "pending to be removed" but has
960 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200961 * response issued before the net ttvn increment (consistency check)
962 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200963 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +0100964
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200965 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200966 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
967 tt_local_entry->common.addr,
968 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000969}
970
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200971/**
972 * batadv_tt_local_remove - logically remove an entry from the local table
973 * @bat_priv: the bat priv with all the soft interface information
974 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +0200975 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200976 * @message: message to append to the log on deletion
977 * @roaming: true if the deletion is due to a roaming event
978 *
979 * Returns the flags assigned to the local entry before being deleted
980 */
981uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200982 const uint8_t *addr, unsigned short vid,
983 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000984{
Sven Eckelmann170173b2012-10-07 12:02:22 +0200985 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200986 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000987
Antonio Quartullic018ad32013-06-04 12:11:39 +0200988 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200989 if (!tt_local_entry)
990 goto out;
991
Antonio Quartulli7f91d062012-08-27 11:44:43 +0200992 curr_flags = tt_local_entry->common.flags;
993
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200994 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200995 /* if this global entry addition is due to a roaming, the node has to
996 * mark the local entry as "roamed" in order to correctly reroute
997 * packets later
998 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200999 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001000 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001001 /* mark the local client as ROAMed */
1002 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1003 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001004
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001005 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1006 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1007 message);
1008 goto out;
1009 }
1010 /* if this client has been added right now, it is possible to
1011 * immediately purge it
1012 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001013 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001014 hlist_del_rcu(&tt_local_entry->common.hash_entry);
1015 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001016
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001017out:
1018 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001019 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001020
1021 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022}
1023
Marek Lindnera19d3d82013-05-27 15:33:25 +08001024/**
1025 * batadv_tt_local_purge_list - purge inactive tt local entries
1026 * @bat_priv: the bat priv with all the soft interface information
1027 * @head: pointer to the list containing the local tt entries
1028 * @timeout: parameter deciding whether a given tt local entry is considered
1029 * inactive or not
1030 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001031static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001032 struct hlist_head *head,
1033 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001035 struct batadv_tt_local_entry *tt_local_entry;
1036 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001037 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001038
Sasha Levinb67bfe02013-02-27 17:06:00 -08001039 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001040 hash_entry) {
1041 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001042 struct batadv_tt_local_entry,
1043 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001044 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1045 continue;
1046
1047 /* entry already marked for deletion */
1048 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1049 continue;
1050
Marek Lindnera19d3d82013-05-27 15:33:25 +08001051 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001052 continue;
1053
1054 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1055 BATADV_TT_CLIENT_DEL, "timed out");
1056 }
1057}
1058
Marek Lindnera19d3d82013-05-27 15:33:25 +08001059/**
1060 * batadv_tt_local_purge - purge inactive tt local entries
1061 * @bat_priv: the bat priv with all the soft interface information
1062 * @timeout: parameter deciding whether a given tt local entry is considered
1063 * inactive or not
1064 */
1065static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1066 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001067{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001068 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001069 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001070 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001071 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073 for (i = 0; i < hash->size; i++) {
1074 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001075 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001077 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001078 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001079 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001080 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081}
1082
Sven Eckelmann56303d32012-06-05 22:31:31 +02001083static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001085 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001086 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001087 struct batadv_tt_common_entry *tt_common_entry;
1088 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001089 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001090 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001091 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001092
Sven Eckelmann807736f2012-07-15 22:26:51 +02001093 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094 return;
1095
Sven Eckelmann807736f2012-07-15 22:26:51 +02001096 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001097
1098 for (i = 0; i < hash->size; i++) {
1099 head = &hash->table[i];
1100 list_lock = &hash->list_locks[i];
1101
1102 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001103 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001104 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001105 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001106 tt_local = container_of(tt_common_entry,
1107 struct batadv_tt_local_entry,
1108 common);
1109 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001110 }
1111 spin_unlock_bh(list_lock);
1112 }
1113
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001114 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001115
Sven Eckelmann807736f2012-07-15 22:26:51 +02001116 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001117}
1118
Sven Eckelmann56303d32012-06-05 22:31:31 +02001119static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001120{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001121 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001122 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123
Sven Eckelmann807736f2012-07-15 22:26:51 +02001124 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001125
Sven Eckelmann807736f2012-07-15 22:26:51 +02001126 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001127 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128
Antonio Quartullidec05072012-11-10 11:00:32 +01001129 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1130 &batadv_tt_global_hash_lock_class_key);
1131
Sven Eckelmann5346c352012-05-05 13:27:28 +02001132 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001133}
1134
Sven Eckelmann56303d32012-06-05 22:31:31 +02001135static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001136{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001137 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001138
Sven Eckelmann807736f2012-07-15 22:26:51 +02001139 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001140
Sven Eckelmann807736f2012-07-15 22:26:51 +02001141 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001142 list) {
1143 list_del(&entry->list);
1144 kfree(entry);
1145 }
1146
Sven Eckelmann807736f2012-07-15 22:26:51 +02001147 atomic_set(&bat_priv->tt.local_changes, 0);
1148 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001149}
1150
Antonio Quartullid657e622012-07-01 14:09:12 +02001151/* retrieves the orig_tt_list_entry belonging to orig_node from the
1152 * batadv_tt_global_entry list
1153 *
1154 * returns it with an increased refcounter, NULL if not found
1155 */
1156static struct batadv_tt_orig_list_entry *
1157batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1158 const struct batadv_orig_node *orig_node)
1159{
1160 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1161 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001162
1163 rcu_read_lock();
1164 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001165 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001166 if (tmp_orig_entry->orig_node != orig_node)
1167 continue;
1168 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1169 continue;
1170
1171 orig_entry = tmp_orig_entry;
1172 break;
1173 }
1174 rcu_read_unlock();
1175
1176 return orig_entry;
1177}
1178
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001179/* find out if an orig_node is already in the list of a tt_global_entry.
Antonio Quartullid657e622012-07-01 14:09:12 +02001180 * returns true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001181 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001182static bool
1183batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1184 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001185{
Antonio Quartullid657e622012-07-01 14:09:12 +02001186 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001187 bool found = false;
1188
Antonio Quartullid657e622012-07-01 14:09:12 +02001189 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1190 if (orig_entry) {
1191 found = true;
1192 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001193 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001194
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001195 return found;
1196}
1197
Sven Eckelmanna5130882012-05-16 20:23:16 +02001198static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001199batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001200 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001201{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001202 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001203
Antonio Quartullid657e622012-07-01 14:09:12 +02001204 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001205 if (orig_entry) {
1206 /* refresh the ttvn: the current value could be a bogus one that
1207 * was added during a "temporary client detection"
1208 */
1209 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001210 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001211 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001212
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001213 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1214 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001215 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001216
1217 INIT_HLIST_NODE(&orig_entry->list);
1218 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001219 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001220 orig_entry->orig_node = orig_node;
1221 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001222 atomic_set(&orig_entry->refcount, 2);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001223
Antonio Quartullid657e622012-07-01 14:09:12 +02001224 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001225 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001226 &tt_global->orig_list);
1227 spin_unlock_bh(&tt_global->list_lock);
1228out:
1229 if (orig_entry)
1230 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001231}
1232
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001233/**
1234 * batadv_tt_global_add - add a new TT global entry or update an existing one
1235 * @bat_priv: the bat priv with all the soft interface information
1236 * @orig_node: the originator announcing the client
1237 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001238 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001239 * @flags: TT flags that have to be set for this non-mesh client
1240 * @ttvn: the tt version number ever announcing this non-mesh client
1241 *
1242 * Add a new TT global entry for the given originator. If the entry already
1243 * exists add a new reference to the given originator (a global entry can have
1244 * references to multiple originators) and adjust the flags attribute to reflect
1245 * the function argument.
1246 * If a TT local entry exists for this non-mesh client remove it.
1247 *
1248 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001249 *
1250 * Return true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001251 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001252static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1253 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001254 const unsigned char *tt_addr,
1255 unsigned short vid, uint16_t flags,
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001256 uint8_t ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001257{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001258 struct batadv_tt_global_entry *tt_global_entry;
1259 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001260 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001261 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001262 struct batadv_tt_common_entry *common;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001263 uint16_t local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001264
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001265 /* ignore global entries from backbone nodes */
1266 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1267 return true;
1268
Antonio Quartullic018ad32013-06-04 12:11:39 +02001269 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1270 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001271
1272 /* if the node already has a local client for this entry, it has to wait
1273 * for a roaming advertisement instead of manually messing up the global
1274 * table
1275 */
1276 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1277 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1278 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001279
Antonio Quartullia73105b2011-04-27 14:27:44 +02001280 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001281 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001282 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001283 goto out;
1284
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001285 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001286 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001287 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001288
Antonio Quartullid4f44692012-05-25 00:00:54 +02001289 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001290 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001291 /* node must store current time in case of roaming. This is
1292 * needed to purge this entry out on timeout (if nobody claims
1293 * it)
1294 */
1295 if (flags & BATADV_TT_CLIENT_ROAM)
1296 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001297 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001298 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001299
1300 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1301 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001302
Sven Eckelmann807736f2012-07-15 22:26:51 +02001303 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001304 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001305 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001306 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001307
1308 if (unlikely(hash_added != 0)) {
1309 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001310 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001311 goto out_remove;
1312 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001313 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001314 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001315 /* If there is already a global entry, we can use this one for
1316 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001317 * But if we are trying to add a temporary client then here are
1318 * two options at this point:
1319 * 1) the global client is not a temporary client: the global
1320 * client has to be left as it is, temporary information
1321 * should never override any already known client state
1322 * 2) the global client is a temporary client: purge the
1323 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001324 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001325 if (flags & BATADV_TT_CLIENT_TEMP) {
1326 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1327 goto out;
1328 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1329 orig_node))
1330 goto out_remove;
1331 batadv_tt_global_del_orig_list(tt_global_entry);
1332 goto add_orig_entry;
1333 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001334
1335 /* if the client was temporary added before receiving the first
1336 * OGM announcing it, we have to clear the TEMP flag
1337 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001338 common->flags &= ~BATADV_TT_CLIENT_TEMP;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001339
Antonio Quartullie9c001362012-11-07 15:05:33 +01001340 /* the change can carry possible "attribute" flags like the
1341 * TT_CLIENT_WIFI, therefore they have to be copied in the
1342 * client entry
1343 */
1344 tt_global_entry->common.flags |= flags;
1345
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001346 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1347 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001348 * delete + roaming change for this originator.
1349 *
1350 * We should first delete the old originator before adding the
1351 * new one.
1352 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001353 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001354 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001355 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001356 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001357 }
1358 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001359add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001360 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001361 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001362
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001363 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001364 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1365 common->addr, BATADV_PRINT_VID(common->vid),
1366 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001367 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001368
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001369out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001370 /* Do not remove multicast addresses from the local hash on
1371 * global additions
1372 */
1373 if (is_multicast_ether_addr(tt_addr))
1374 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001375
Antonio Quartullia73105b2011-04-27 14:27:44 +02001376 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001377 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001378 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001379 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001380 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1381
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001382 if (!(flags & BATADV_TT_CLIENT_ROAM))
1383 /* this is a normal global add. Therefore the client is not in a
1384 * roaming state anymore.
1385 */
1386 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1387
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001388out:
1389 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001390 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001391 if (tt_local_entry)
1392 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001393 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001394}
1395
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001396/**
1397 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001398 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001399 * @tt_global_entry: global translation table entry to be analyzed
1400 *
1401 * This functon assumes the caller holds rcu_read_lock().
1402 * Returns best originator list entry or NULL on errors.
1403 */
1404static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001405batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1406 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001407{
Antonio Quartulli46274562013-09-03 11:10:24 +02001408 struct batadv_neigh_node *router, *best_router = NULL;
1409 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001410 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001411 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001412
1413 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001414 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001415 router = batadv_orig_router_get(orig_entry->orig_node,
1416 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001417 if (!router)
1418 continue;
1419
Antonio Quartulli46274562013-09-03 11:10:24 +02001420 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001421 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1422 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001423 batadv_neigh_node_free_ref(router);
1424 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001425 }
1426
Antonio Quartulli46274562013-09-03 11:10:24 +02001427 /* release the refcount for the "old" best */
1428 if (best_router)
1429 batadv_neigh_node_free_ref(best_router);
1430
1431 best_entry = orig_entry;
1432 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001433 }
1434
Antonio Quartulli46274562013-09-03 11:10:24 +02001435 if (best_router)
1436 batadv_neigh_node_free_ref(best_router);
1437
Sven Eckelmann981d8902012-10-07 13:34:15 +02001438 return best_entry;
1439}
1440
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001441/**
1442 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1443 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001444 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001445 * @tt_global_entry: global translation table entry to be printed
1446 * @seq: debugfs table seq_file struct
1447 *
1448 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001449 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001450static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001451batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1452 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001453 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001454{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001455 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001456 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001457 struct batadv_orig_node_vlan *vlan;
1458 struct hlist_head *head;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001459 uint8_t last_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001460 uint16_t flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001461
1462 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001463 flags = tt_common_entry->flags;
1464
Antonio Quartulli46274562013-09-03 11:10:24 +02001465 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001466 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001467 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1468 tt_common_entry->vid);
1469 if (!vlan) {
1470 seq_printf(seq,
1471 " * Cannot retrieve VLAN %d for originator %pM\n",
1472 BATADV_PRINT_VID(tt_common_entry->vid),
1473 best_entry->orig_node->orig);
1474 goto print_list;
1475 }
1476
Sven Eckelmann981d8902012-10-07 13:34:15 +02001477 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001478 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001479 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001480 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001481 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001482 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001483 last_ttvn, vlan->tt.crc,
Sven Eckelmann981d8902012-10-07 13:34:15 +02001484 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1485 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001486 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001487 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001488
1489 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001490 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001491
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001492print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001493 head = &tt_global_entry->orig_list;
1494
Sasha Levinb67bfe02013-02-27 17:06:00 -08001495 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001496 if (best_entry == orig_entry)
1497 continue;
1498
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001499 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1500 tt_common_entry->vid);
1501 if (!vlan) {
1502 seq_printf(seq,
1503 " + Cannot retrieve VLAN %d for originator %pM\n",
1504 BATADV_PRINT_VID(tt_common_entry->vid),
1505 orig_entry->orig_node->orig);
1506 continue;
1507 }
1508
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001509 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001510 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001511 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001512 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001513 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001514 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001515 last_ttvn, vlan->tt.crc,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001516 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001517 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001518 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001519 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001520
1521 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001522 }
1523}
1524
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001525int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001526{
1527 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001528 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001529 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001530 struct batadv_tt_common_entry *tt_common_entry;
1531 struct batadv_tt_global_entry *tt_global;
1532 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001533 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001534 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001535
Marek Lindner30da63a2012-08-03 17:15:46 +02001536 primary_if = batadv_seq_print_text_primary_if_get(seq);
1537 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001538 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001539
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001540 seq_printf(seq,
1541 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001542 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001543 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1544 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1545 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001546
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001547 for (i = 0; i < hash->size; i++) {
1548 head = &hash->table[i];
1549
Marek Lindner7aadf882011-02-18 12:28:09 +00001550 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001551 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001552 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001553 tt_global = container_of(tt_common_entry,
1554 struct batadv_tt_global_entry,
1555 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001556 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001557 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001558 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001559 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001560out:
1561 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001562 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001563 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001564}
1565
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001566/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001567static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001568batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001569{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001570 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001571 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001572 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001573
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001574 spin_lock_bh(&tt_global_entry->list_lock);
1575 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001576 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1577 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001578 batadv_tt_global_size_dec(orig_entry->orig_node,
1579 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001580 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001581 }
1582 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001583}
1584
Sven Eckelmanna5130882012-05-16 20:23:16 +02001585static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001586batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1587 struct batadv_tt_global_entry *tt_global_entry,
1588 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001589 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001590{
1591 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001592 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001593 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001594 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001595
1596 spin_lock_bh(&tt_global_entry->list_lock);
1597 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001598 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001599 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001600 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001601 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001602 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001603 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001604 tt_global_entry->common.addr,
1605 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001606 hlist_del_rcu(&orig_entry->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001607 batadv_tt_global_size_dec(orig_node,
1608 tt_global_entry->common.vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001609 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001610 }
1611 }
1612 spin_unlock_bh(&tt_global_entry->list_lock);
1613}
1614
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001615/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001616 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1617 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001618 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001619static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001620batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1621 struct batadv_tt_global_entry *tt_global_entry,
1622 struct batadv_orig_node *orig_node,
1623 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001624{
1625 bool last_entry = true;
1626 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001627 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001628
1629 /* no local entry exists, case 1:
1630 * Check if this is the last one or if other entries exist.
1631 */
1632
1633 rcu_read_lock();
1634 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001635 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001636 if (orig_entry->orig_node != orig_node) {
1637 last_entry = false;
1638 break;
1639 }
1640 }
1641 rcu_read_unlock();
1642
1643 if (last_entry) {
1644 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001645 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001646 tt_global_entry->roam_at = jiffies;
1647 } else
1648 /* there is another entry, we can simply delete this
1649 * one and can still use the other one.
1650 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001651 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1652 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001653}
1654
Antonio Quartullic018ad32013-06-04 12:11:39 +02001655/**
1656 * batadv_tt_global_del - remove a client from the global table
1657 * @bat_priv: the bat priv with all the soft interface information
1658 * @orig_node: an originator serving this client
1659 * @addr: the mac address of the client
1660 * @vid: VLAN identifier
1661 * @message: a message explaining the reason for deleting the client to print
1662 * for debugging purpose
1663 * @roaming: true if the deletion has been triggered by a roaming event
1664 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001665static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1666 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001667 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001668 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001669{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001670 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001671 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001672
Antonio Quartullic018ad32013-06-04 12:11:39 +02001673 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001674 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001675 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001676
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001677 if (!roaming) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001678 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1679 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001680
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001681 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001682 batadv_tt_global_free(bat_priv, tt_global_entry,
1683 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001684
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001685 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001686 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001687
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001688 /* if we are deleting a global entry due to a roam
1689 * event, there are two possibilities:
1690 * 1) the client roamed from node A to node B => if there
1691 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001692 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001693 * wait for node B to claim it. In case of timeout
1694 * the entry is purged.
1695 *
1696 * If there are other originators left, we directly delete
1697 * the originator.
1698 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001699 * the global entry, since it is useless now.
1700 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001701 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001702 tt_global_entry->common.addr,
1703 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001704 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001705 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001706 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001707 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001708 } else
1709 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001710 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1711 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001712
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001713
Antonio Quartullicc47f662011-04-27 14:27:57 +02001714out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001715 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001716 batadv_tt_global_entry_free_ref(tt_global_entry);
1717 if (local_entry)
1718 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001719}
1720
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001721/**
1722 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1723 * given originator matching the provided vid
1724 * @bat_priv: the bat priv with all the soft interface information
1725 * @orig_node: the originator owning the entries to remove
1726 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1727 * removed
1728 * @message: debug message to print as "reason"
1729 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001730void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1731 struct batadv_orig_node *orig_node,
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001732 int32_t match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001733 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001734{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001735 struct batadv_tt_global_entry *tt_global;
1736 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001737 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001738 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001739 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001740 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001741 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001742 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001743
Simon Wunderlich6e801492011-10-19 10:28:26 +02001744 if (!hash)
1745 return;
1746
Antonio Quartullia73105b2011-04-27 14:27:44 +02001747 for (i = 0; i < hash->size; i++) {
1748 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001749 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001750
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001751 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001752 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001753 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001754 /* remove only matching entries */
1755 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1756 continue;
1757
Sven Eckelmann56303d32012-06-05 22:31:31 +02001758 tt_global = container_of(tt_common_entry,
1759 struct batadv_tt_global_entry,
1760 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001761
Sven Eckelmann56303d32012-06-05 22:31:31 +02001762 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001763 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001764
Sven Eckelmann56303d32012-06-05 22:31:31 +02001765 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001766 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001767 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001768 "Deleting global tt entry %pM (vid: %d): %s\n",
1769 tt_global->common.addr,
1770 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001771 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001772 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001773 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001774 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001775 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001776 }
Antonio Quartulli17071572011-11-07 16:36:40 +01001777 orig_node->tt_initialised = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001778}
1779
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001780static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1781 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001782{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001783 bool purge = false;
1784 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1785 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001786
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001787 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1788 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1789 purge = true;
1790 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001791 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001792
1793 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1794 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1795 purge = true;
1796 *msg = "Temporary client timeout\n";
1797 }
1798
1799 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001800}
1801
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001802static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001803{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001804 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001805 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001806 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001807 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02001808 uint32_t i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001809 char *msg = NULL;
1810 struct batadv_tt_common_entry *tt_common;
1811 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001812
Antonio Quartullicc47f662011-04-27 14:27:57 +02001813 for (i = 0; i < hash->size; i++) {
1814 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001815 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001816
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001817 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001818 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001819 hash_entry) {
1820 tt_global = container_of(tt_common,
1821 struct batadv_tt_global_entry,
1822 common);
1823
1824 if (!batadv_tt_global_to_purge(tt_global, &msg))
1825 continue;
1826
1827 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001828 "Deleting global tt entry %pM (vid: %d): %s\n",
1829 tt_global->common.addr,
1830 BATADV_PRINT_VID(tt_global->common.vid),
1831 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001832
Sasha Levinb67bfe02013-02-27 17:06:00 -08001833 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001834
1835 batadv_tt_global_entry_free_ref(tt_global);
1836 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001837 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001838 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001839}
1840
Sven Eckelmann56303d32012-06-05 22:31:31 +02001841static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001842{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001843 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001844 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001845 struct batadv_tt_common_entry *tt_common_entry;
1846 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001847 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001848 struct hlist_head *head;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001849 uint32_t i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001850
Sven Eckelmann807736f2012-07-15 22:26:51 +02001851 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001852 return;
1853
Sven Eckelmann807736f2012-07-15 22:26:51 +02001854 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001855
1856 for (i = 0; i < hash->size; i++) {
1857 head = &hash->table[i];
1858 list_lock = &hash->list_locks[i];
1859
1860 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001861 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001862 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001863 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001864 tt_global = container_of(tt_common_entry,
1865 struct batadv_tt_global_entry,
1866 common);
1867 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001868 }
1869 spin_unlock_bh(list_lock);
1870 }
1871
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001872 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001873
Sven Eckelmann807736f2012-07-15 22:26:51 +02001874 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001875}
1876
Sven Eckelmann56303d32012-06-05 22:31:31 +02001877static bool
1878_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1879 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001880{
1881 bool ret = false;
1882
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001883 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1884 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001885 ret = true;
1886
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01001887 /* check if the two clients are marked as isolated */
1888 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1889 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1890 ret = true;
1891
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001892 return ret;
1893}
1894
Antonio Quartullic018ad32013-06-04 12:11:39 +02001895/**
1896 * batadv_transtable_search - get the mesh destination for a given client
1897 * @bat_priv: the bat priv with all the soft interface information
1898 * @src: mac address of the source client
1899 * @addr: mac address of the destination client
1900 * @vid: VLAN identifier
1901 *
1902 * Returns a pointer to the originator that was selected as destination in the
1903 * mesh for contacting the client 'addr', NULL otherwise.
1904 * In case of multiple originators serving the same client, the function returns
1905 * the best one (best in terms of metric towards the destination node).
1906 *
1907 * If the two clients are AP isolated the function returns NULL.
1908 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001909struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1910 const uint8_t *src,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001911 const uint8_t *addr,
1912 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001913{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001914 struct batadv_tt_local_entry *tt_local_entry = NULL;
1915 struct batadv_tt_global_entry *tt_global_entry = NULL;
1916 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001917 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001918
Antonio Quartullieceb22a2013-11-16 12:03:51 +01001919 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02001920 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001921 if (!tt_local_entry ||
1922 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001923 goto out;
1924 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001925
Antonio Quartullic018ad32013-06-04 12:11:39 +02001926 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001927 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001928 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001929
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001930 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001931 * isolation
1932 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001933 if (tt_local_entry &&
1934 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001935 goto out;
1936
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001937 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02001938 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001939 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02001940 if (best_entry)
1941 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001942 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1943 orig_node = NULL;
1944 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02001945
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001946out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001947 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001948 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001949 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001950 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02001951
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001952 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001953}
Antonio Quartullia73105b2011-04-27 14:27:44 +02001954
Antonio Quartulliced72932013-04-24 16:37:51 +02001955/**
1956 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1957 * to the given orig_node
1958 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001959 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001960 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001961 *
1962 * This function computes the checksum for the global table corresponding to a
1963 * specific originator. In particular, the checksum is computed as follows: For
1964 * each client connected to the originator the CRC32C of the MAC address and the
1965 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1966 * together.
1967 *
1968 * The idea behind is that CRC32C should be used as much as possible in order to
1969 * produce a unique hash of the table, but since the order which is used to feed
1970 * the CRC32C function affects the result and since every node in the network
1971 * probably sorts the clients differently, the hash function cannot be directly
1972 * computed over the entire table. Hence the CRC32C is used only on
1973 * the single client entry, while all the results are then xor'ed together
1974 * because the XOR operation can combine them all while trying to reduce the
1975 * noise as much as possible.
1976 *
1977 * Returns the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02001978 */
1979static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001980 struct batadv_orig_node *orig_node,
1981 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001982{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001983 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001984 struct batadv_tt_common_entry *tt_common;
1985 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001986 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02001987 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02001988 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01001989 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001990
1991 for (i = 0; i < hash->size; i++) {
1992 head = &hash->table[i];
1993
1994 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001995 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001996 tt_global = container_of(tt_common,
1997 struct batadv_tt_global_entry,
1998 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001999 /* compute the CRC only for entries belonging to the
2000 * VLAN identified by the vid passed as parameter
2001 */
2002 if (tt_common->vid != vid)
2003 continue;
2004
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002005 /* Roaming clients are in the global table for
2006 * consistency only. They don't have to be
2007 * taken into account while computing the
2008 * global crc
2009 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002010 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002011 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002012 /* Temporary clients have not been announced yet, so
2013 * they have to be skipped while computing the global
2014 * crc
2015 */
2016 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2017 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002018
2019 /* find out if this global entry is announced by this
2020 * originator
2021 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002022 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002023 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002024 continue;
2025
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002026 /* use network order to read the VID: this ensures that
2027 * every node reads the bytes in the same order.
2028 */
2029 tmp_vid = htons(tt_common->vid);
2030 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002031
2032 /* compute the CRC on flags that have to be kept in sync
2033 * among nodes
2034 */
2035 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2036 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2037
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002038 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002039 }
2040 rcu_read_unlock();
2041 }
2042
Antonio Quartulliced72932013-04-24 16:37:51 +02002043 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002044}
2045
Antonio Quartulliced72932013-04-24 16:37:51 +02002046/**
2047 * batadv_tt_local_crc - calculates the checksum of the local table
2048 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002049 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002050 *
2051 * For details about the computation, please refer to the documentation for
2052 * batadv_tt_global_crc().
2053 *
2054 * Returns the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002055 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002056static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2057 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002058{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002059 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002060 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002061 struct hlist_head *head;
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002062 uint32_t i, crc_tmp, crc = 0;
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002063 uint8_t flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002064 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002065
2066 for (i = 0; i < hash->size; i++) {
2067 head = &hash->table[i];
2068
2069 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002070 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002071 /* compute the CRC only for entries belonging to the
2072 * VLAN identified by vid
2073 */
2074 if (tt_common->vid != vid)
2075 continue;
2076
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002077 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002078 * account while computing the CRC
2079 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002080 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002081 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002082
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002083 /* use network order to read the VID: this ensures that
2084 * every node reads the bytes in the same order.
2085 */
2086 tmp_vid = htons(tt_common->vid);
2087 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002088
2089 /* compute the CRC on flags that have to be kept in sync
2090 * among nodes
2091 */
2092 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2093 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2094
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002095 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002096 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002097 rcu_read_unlock();
2098 }
2099
Antonio Quartulliced72932013-04-24 16:37:51 +02002100 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002101}
2102
Sven Eckelmann56303d32012-06-05 22:31:31 +02002103static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002104{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002105 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002106
Sven Eckelmann807736f2012-07-15 22:26:51 +02002107 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002108
Sven Eckelmann807736f2012-07-15 22:26:51 +02002109 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002110 list_del(&node->list);
2111 kfree(node);
2112 }
2113
Sven Eckelmann807736f2012-07-15 22:26:51 +02002114 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002115}
2116
Sven Eckelmann56303d32012-06-05 22:31:31 +02002117static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2118 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002119 const void *tt_buff,
2120 uint16_t tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002121{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002122 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002123 * last OGM (the OGM could carry no changes)
2124 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002125 spin_lock_bh(&orig_node->tt_buff_lock);
2126 if (tt_buff_len > 0) {
2127 kfree(orig_node->tt_buff);
2128 orig_node->tt_buff_len = 0;
2129 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2130 if (orig_node->tt_buff) {
2131 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2132 orig_node->tt_buff_len = tt_buff_len;
2133 }
2134 }
2135 spin_unlock_bh(&orig_node->tt_buff_lock);
2136}
2137
Sven Eckelmann56303d32012-06-05 22:31:31 +02002138static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002139{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002140 struct batadv_tt_req_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002141
Sven Eckelmann807736f2012-07-15 22:26:51 +02002142 spin_lock_bh(&bat_priv->tt.req_list_lock);
2143 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002144 if (batadv_has_timed_out(node->issued_at,
2145 BATADV_TT_REQUEST_TIMEOUT)) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02002146 list_del(&node->list);
2147 kfree(node);
2148 }
2149 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002150 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002151}
2152
2153/* returns the pointer to the new tt_req_node struct if no request
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002154 * has already been issued for this orig_node, NULL otherwise
2155 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002156static struct batadv_tt_req_node *
2157batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2158 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002159{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002160 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002161
Sven Eckelmann807736f2012-07-15 22:26:51 +02002162 spin_lock_bh(&bat_priv->tt.req_list_lock);
2163 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002164 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2165 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002166 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002167 goto unlock;
2168 }
2169
2170 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2171 if (!tt_req_node)
2172 goto unlock;
2173
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002174 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002175 tt_req_node->issued_at = jiffies;
2176
Sven Eckelmann807736f2012-07-15 22:26:51 +02002177 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002178unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002179 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002180 return tt_req_node;
2181}
2182
Marek Lindner335fbe02013-04-23 21:40:02 +08002183/**
2184 * batadv_tt_local_valid - verify that given tt entry is a valid one
2185 * @entry_ptr: to be checked local tt entry
2186 * @data_ptr: not used but definition required to satisfy the callback prototype
2187 *
2188 * Returns 1 if the entry is a valid, 0 otherwise.
2189 */
2190static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002191{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002192 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002193
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002194 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002195 return 0;
2196 return 1;
2197}
2198
Sven Eckelmanna5130882012-05-16 20:23:16 +02002199static int batadv_tt_global_valid(const void *entry_ptr,
2200 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002201{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002202 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2203 const struct batadv_tt_global_entry *tt_global_entry;
2204 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002205
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002206 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2207 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002208 return 0;
2209
Sven Eckelmann56303d32012-06-05 22:31:31 +02002210 tt_global_entry = container_of(tt_common_entry,
2211 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002212 common);
2213
Sven Eckelmanna5130882012-05-16 20:23:16 +02002214 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002215}
2216
Marek Lindner335fbe02013-04-23 21:40:02 +08002217/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002218 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2219 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002220 * @bat_priv: the bat priv with all the soft interface information
2221 * @hash: hash table containing the tt entries
2222 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002223 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002224 * @valid_cb: function to filter tt change entries
2225 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002226 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002227static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2228 struct batadv_hashtable *hash,
2229 void *tvlv_buff, uint16_t tt_len,
2230 int (*valid_cb)(const void *, const void *),
2231 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002232{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002233 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002234 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002235 struct hlist_head *head;
Marek Lindner335fbe02013-04-23 21:40:02 +08002236 uint16_t tt_tot, tt_num_entries = 0;
Antonio Quartullic90681b2011-10-05 17:05:25 +02002237 uint32_t i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002238
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002239 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002240 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002241
2242 rcu_read_lock();
2243 for (i = 0; i < hash->size; i++) {
2244 head = &hash->table[i];
2245
Sasha Levinb67bfe02013-02-27 17:06:00 -08002246 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002247 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002248 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002249 break;
2250
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002251 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002252 continue;
2253
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002254 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002255 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002256 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002257 memset(tt_change->reserved, 0,
2258 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002259
Marek Lindner335fbe02013-04-23 21:40:02 +08002260 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002261 tt_change++;
2262 }
2263 }
2264 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002265}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002266
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002267/**
2268 * batadv_tt_global_check_crc - check if all the CRCs are correct
2269 * @orig_node: originator for which the CRCs have to be checked
2270 * @tt_vlan: pointer to the first tvlv VLAN entry
2271 * @num_vlan: number of tvlv VLAN entries
2272 * @create: if true, create VLAN objects if not found
2273 *
2274 * Return true if all the received CRCs match the locally stored ones, false
2275 * otherwise
2276 */
2277static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2278 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2279 uint16_t num_vlan)
2280{
2281 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2282 struct batadv_orig_node_vlan *vlan;
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002283 uint32_t crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002284 int i;
2285
2286 /* check if each received CRC matches the locally stored one */
2287 for (i = 0; i < num_vlan; i++) {
2288 tt_vlan_tmp = tt_vlan + i;
2289
2290 /* if orig_node is a backbone node for this VLAN, don't check
2291 * the CRC as we ignore all the global entries over it
2292 */
2293 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002294 orig_node->orig,
2295 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002296 continue;
2297
2298 vlan = batadv_orig_node_vlan_get(orig_node,
2299 ntohs(tt_vlan_tmp->vid));
2300 if (!vlan)
2301 return false;
2302
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002303 crc = vlan->tt.crc;
2304 batadv_orig_node_vlan_free_ref(vlan);
2305
2306 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002307 return false;
2308 }
2309
2310 return true;
2311}
2312
2313/**
2314 * batadv_tt_local_update_crc - update all the local CRCs
2315 * @bat_priv: the bat priv with all the soft interface information
2316 */
2317static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2318{
2319 struct batadv_softif_vlan *vlan;
2320
2321 /* recompute the global CRC for each VLAN */
2322 rcu_read_lock();
2323 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2324 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2325 }
2326 rcu_read_unlock();
2327}
2328
2329/**
2330 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2331 * @bat_priv: the bat priv with all the soft interface information
2332 * @orig_node: the orig_node for which the CRCs have to be updated
2333 */
2334static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2335 struct batadv_orig_node *orig_node)
2336{
2337 struct batadv_orig_node_vlan *vlan;
2338 uint32_t crc;
2339
2340 /* recompute the global CRC for each VLAN */
2341 rcu_read_lock();
2342 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2343 /* if orig_node is a backbone node for this VLAN, don't compute
2344 * the CRC as we ignore all the global entries over it
2345 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002346 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2347 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002348 continue;
2349
2350 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2351 vlan->tt.crc = crc;
2352 }
2353 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002354}
2355
Antonio Quartulliced72932013-04-24 16:37:51 +02002356/**
2357 * batadv_send_tt_request - send a TT Request message to a given node
2358 * @bat_priv: the bat priv with all the soft interface information
2359 * @dst_orig_node: the destination of the message
2360 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002361 * @tt_vlan: pointer to the first tvlv VLAN object to request
2362 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002363 * @full_table: ask for the entire translation table if true, while only for the
2364 * last TT diff otherwise
2365 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002366static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2367 struct batadv_orig_node *dst_orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002368 uint8_t ttvn,
2369 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2370 uint16_t num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002371{
Marek Lindner335fbe02013-04-23 21:40:02 +08002372 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002373 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002374 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2375 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002376 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002377 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002378
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002379 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002380 if (!primary_if)
2381 goto out;
2382
2383 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002384 * reply from the same orig_node yet
2385 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002386 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002387 if (!tt_req_node)
2388 goto out;
2389
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002390 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2391 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002392 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002393 goto out;
2394
Marek Lindner335fbe02013-04-23 21:40:02 +08002395 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2396 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002397 tvlv_tt_data->num_vlan = htons(num_vlan);
2398
2399 /* send all the CRCs within the request. This is needed by intermediate
2400 * nodes to ensure they have the correct table before replying
2401 */
2402 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2403 for (i = 0; i < num_vlan; i++) {
2404 tt_vlan_req->vid = tt_vlan->vid;
2405 tt_vlan_req->crc = tt_vlan->crc;
2406
2407 tt_vlan_req++;
2408 tt_vlan++;
2409 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002410
2411 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002412 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002413
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002414 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002415 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002416
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002417 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002418 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2419 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002420 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002421 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002422
2423out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002424 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002425 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002426 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002427 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002428 list_del(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002429 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002430 kfree(tt_req_node);
2431 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002432 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002433 return ret;
2434}
2435
Marek Lindner335fbe02013-04-23 21:40:02 +08002436/**
2437 * batadv_send_other_tt_response - send reply to tt request concerning another
2438 * node's translation table
2439 * @bat_priv: the bat priv with all the soft interface information
2440 * @tt_data: tt data containing the tt request information
2441 * @req_src: mac address of tt request sender
2442 * @req_dst: mac address of tt request recipient
2443 *
2444 * Returns true if tt request reply was sent, false otherwise.
2445 */
2446static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2447 struct batadv_tvlv_tt_data *tt_data,
2448 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002449{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002450 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002451 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002452 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002453 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002454 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002455 bool ret = false, full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002456 uint8_t orig_ttvn, req_ttvn;
2457 uint16_t tvlv_len;
2458 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002460 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002461 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002462 req_src, tt_data->ttvn, req_dst,
2463 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002464
2465 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002466 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002467 if (!req_dst_orig_node)
2468 goto out;
2469
Marek Lindner335fbe02013-04-23 21:40:02 +08002470 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002471 if (!res_dst_orig_node)
2472 goto out;
2473
Antonio Quartullia73105b2011-04-27 14:27:44 +02002474 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002475 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002476
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002477 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002478 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002479 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002480 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2481 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002482 goto out;
2483
Antonio Quartulli015758d2011-07-09 17:52:13 +02002484 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002485 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002486 !req_dst_orig_node->tt_buff)
2487 full_table = true;
2488 else
2489 full_table = false;
2490
Marek Lindner335fbe02013-04-23 21:40:02 +08002491 /* TT fragmentation hasn't been implemented yet, so send as many
2492 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002493 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002494 if (!full_table) {
2495 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2496 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002497
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002498 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2499 &tvlv_tt_data,
2500 &tt_change,
2501 &tt_len);
2502 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002503 goto unlock;
2504
Antonio Quartullia73105b2011-04-27 14:27:44 +02002505 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002506 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002507 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002508 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2509 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002510 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2511 * in the initial part
2512 */
2513 tt_len = -1;
2514 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2515 &tvlv_tt_data,
2516 &tt_change,
2517 &tt_len);
2518 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002519 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002520
2521 /* fill the rest of the tvlv with the real TT entries */
2522 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2523 tt_change, tt_len,
2524 batadv_tt_global_valid,
2525 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002526 }
2527
Marek Lindnera19d3d82013-05-27 15:33:25 +08002528 /* Don't send the response, if larger than fragmented packet. */
2529 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2530 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2531 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2532 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2533 res_dst_orig_node->orig);
2534 goto out;
2535 }
2536
Marek Lindner335fbe02013-04-23 21:40:02 +08002537 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2538 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002539
2540 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002541 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002542
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002543 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002544 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2545 res_dst_orig_node->orig, req_dst_orig_node->orig,
2546 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002547
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002548 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002549
Marek Lindner335fbe02013-04-23 21:40:02 +08002550 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002551 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2552 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002553
Marek Lindner335fbe02013-04-23 21:40:02 +08002554 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002555 goto out;
2556
2557unlock:
2558 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2559
2560out:
2561 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002562 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002563 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002564 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002565 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002566 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002567}
Sven Eckelmann96412692012-06-05 22:31:30 +02002568
Marek Lindner335fbe02013-04-23 21:40:02 +08002569/**
2570 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2571 * translation table
2572 * @bat_priv: the bat priv with all the soft interface information
2573 * @tt_data: tt data containing the tt request information
2574 * @req_src: mac address of tt request sender
2575 *
2576 * Returns true if tt request reply was sent, false otherwise.
2577 */
2578static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2579 struct batadv_tvlv_tt_data *tt_data,
2580 uint8_t *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002581{
Marek Lindner335fbe02013-04-23 21:40:02 +08002582 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002583 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002584 struct batadv_tvlv_tt_change *tt_change;
2585 struct batadv_orig_node *orig_node;
Marek Lindner335fbe02013-04-23 21:40:02 +08002586 uint8_t my_ttvn, req_ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002587 uint16_t tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002588 bool full_table;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002589 int32_t tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002590
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002591 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002592 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002593 req_src, tt_data->ttvn,
2594 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002595
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002596 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002597
Sven Eckelmann807736f2012-07-15 22:26:51 +02002598 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002599 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002600
Marek Lindner335fbe02013-04-23 21:40:02 +08002601 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002602 if (!orig_node)
2603 goto out;
2604
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002605 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002606 if (!primary_if)
2607 goto out;
2608
2609 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002610 * is too big send the whole local translation table
2611 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002612 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002613 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002614 full_table = true;
2615 else
2616 full_table = false;
2617
Marek Lindner335fbe02013-04-23 21:40:02 +08002618 /* TT fragmentation hasn't been implemented yet, so send as many
2619 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002620 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002621 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002622 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002623
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002624 tt_len = bat_priv->tt.last_changeset_len;
2625 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2626 &tvlv_tt_data,
2627 &tt_change,
2628 &tt_len);
2629 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002630 goto unlock;
2631
Marek Lindner335fbe02013-04-23 21:40:02 +08002632 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002633 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002634 bat_priv->tt.last_changeset_len);
2635 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002636 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002637 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002638
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002639 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2640 * in the initial part
2641 */
2642 tt_len = -1;
2643 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2644 &tvlv_tt_data,
2645 &tt_change,
2646 &tt_len);
2647 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002648 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002649
2650 /* fill the rest of the tvlv with the real TT entries */
2651 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2652 tt_change, tt_len,
2653 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002654 }
2655
Marek Lindner335fbe02013-04-23 21:40:02 +08002656 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2657 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002658
2659 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002660 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002662 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2664 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002666 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002667
Marek Lindner335fbe02013-04-23 21:40:02 +08002668 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002669 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2670 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002671
Antonio Quartullia73105b2011-04-27 14:27:44 +02002672 goto out;
2673
2674unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002675 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002677 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002678 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002679 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002680 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002681 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002682 kfree(tvlv_tt_data);
2683 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002684 return true;
2685}
2686
Marek Lindner335fbe02013-04-23 21:40:02 +08002687/**
2688 * batadv_send_tt_response - send reply to tt request
2689 * @bat_priv: the bat priv with all the soft interface information
2690 * @tt_data: tt data containing the tt request information
2691 * @req_src: mac address of tt request sender
2692 * @req_dst: mac address of tt request recipient
2693 *
2694 * Returns true if tt request reply was sent, false otherwise.
2695 */
2696static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2697 struct batadv_tvlv_tt_data *tt_data,
2698 uint8_t *req_src, uint8_t *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002699{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002700 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002701 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002702 else
Marek Lindner335fbe02013-04-23 21:40:02 +08002703 return batadv_send_other_tt_response(bat_priv, tt_data,
2704 req_src, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705}
2706
Sven Eckelmann56303d32012-06-05 22:31:31 +02002707static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2708 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002709 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002710 uint16_t tt_num_changes, uint8_t ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002711{
2712 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002713 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002714
2715 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002716 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2717 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002718 batadv_tt_global_del(bat_priv, orig_node,
2719 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002720 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002721 "tt removed by changes",
2722 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002723 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002724 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002725 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002726 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002727 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002728 /* In case of problem while storing a
2729 * global_entry, we stop the updating
2730 * procedure without committing the
2731 * ttvn change. This will avoid to send
2732 * corrupted data on tt_request
2733 */
2734 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002735 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002736 }
Antonio Quartulli17071572011-11-07 16:36:40 +01002737 orig_node->tt_initialised = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002738}
2739
Sven Eckelmann56303d32012-06-05 22:31:31 +02002740static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002741 struct batadv_tvlv_tt_change *tt_change,
2742 uint8_t ttvn, uint8_t *resp_src,
2743 uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002744{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002745 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002746
Marek Lindner335fbe02013-04-23 21:40:02 +08002747 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002748 if (!orig_node)
2749 goto out;
2750
2751 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002752 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2753 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002754
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002755 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2756 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002757
2758 spin_lock_bh(&orig_node->tt_buff_lock);
2759 kfree(orig_node->tt_buff);
2760 orig_node->tt_buff_len = 0;
2761 orig_node->tt_buff = NULL;
2762 spin_unlock_bh(&orig_node->tt_buff_lock);
2763
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002764 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002765
2766out:
2767 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002768 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002769}
2770
Sven Eckelmann56303d32012-06-05 22:31:31 +02002771static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2772 struct batadv_orig_node *orig_node,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002773 uint16_t tt_num_changes, uint8_t ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002774 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002775{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002776 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2777 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002778
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002779 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2780 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002781 atomic_set(&orig_node->last_ttvn, ttvn);
2782}
2783
Antonio Quartullic018ad32013-06-04 12:11:39 +02002784/**
2785 * batadv_is_my_client - check if a client is served by the local node
2786 * @bat_priv: the bat priv with all the soft interface information
2787 * @addr: the mac adress of the client to check
2788 * @vid: VLAN identifier
2789 *
2790 * Returns true if the client is served by this node, false otherwise.
2791 */
2792bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2793 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002795 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002796 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002797
Antonio Quartullic018ad32013-06-04 12:11:39 +02002798 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002799 if (!tt_local_entry)
2800 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002801 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002802 * consistency purpose)
2803 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002804 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2805 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002806 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002807 ret = true;
2808out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002809 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002810 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002811 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002812}
2813
Marek Lindner335fbe02013-04-23 21:40:02 +08002814/**
2815 * batadv_handle_tt_response - process incoming tt reply
2816 * @bat_priv: the bat priv with all the soft interface information
2817 * @tt_data: tt data containing the tt request information
2818 * @resp_src: mac address of tt reply sender
2819 * @num_entries: number of tt change entries appended to the tt data
2820 */
2821static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2822 struct batadv_tvlv_tt_data *tt_data,
2823 uint8_t *resp_src, uint16_t num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002824{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002825 struct batadv_tt_req_node *node, *safe;
2826 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08002827 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002828 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2829 uint16_t change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002830
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002831 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002832 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002833 resp_src, tt_data->ttvn, num_entries,
2834 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002835
Marek Lindner335fbe02013-04-23 21:40:02 +08002836 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837 if (!orig_node)
2838 goto out;
2839
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002840 spin_lock_bh(&orig_node->tt_lock);
2841
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002842 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2843 change_offset *= ntohs(tt_data->num_vlan);
2844 change_offset += sizeof(*tt_data);
2845 tvlv_ptr += change_offset;
2846
2847 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08002848 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002849 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2850 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02002851 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08002852 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2853 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02002854 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002855
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002856 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002857 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002858
2859 spin_unlock_bh(&orig_node->tt_lock);
2860
Antonio Quartullia73105b2011-04-27 14:27:44 +02002861 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002862 spin_lock_bh(&bat_priv->tt.req_list_lock);
2863 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002864 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002865 continue;
2866 list_del(&node->list);
2867 kfree(node);
2868 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002869
Sven Eckelmann807736f2012-07-15 22:26:51 +02002870 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002871out:
2872 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002873 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002874}
2875
Sven Eckelmann56303d32012-06-05 22:31:31 +02002876static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002877{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002878 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002879
Sven Eckelmann807736f2012-07-15 22:26:51 +02002880 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002881
Sven Eckelmann807736f2012-07-15 22:26:51 +02002882 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02002883 list_del(&node->list);
2884 kfree(node);
2885 }
2886
Sven Eckelmann807736f2012-07-15 22:26:51 +02002887 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002888}
2889
Sven Eckelmann56303d32012-06-05 22:31:31 +02002890static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002891{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002892 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002893
Sven Eckelmann807736f2012-07-15 22:26:51 +02002894 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2895 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002896 if (!batadv_has_timed_out(node->first_time,
2897 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002898 continue;
2899
2900 list_del(&node->list);
2901 kfree(node);
2902 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002903 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002904}
2905
2906/* This function checks whether the client already reached the
2907 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2908 * will not be sent.
2909 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002910 * returns true if the ROAMING_ADV can be sent, false otherwise
2911 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002912static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002913 uint8_t *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002914{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002915 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002916 bool ret = false;
2917
Sven Eckelmann807736f2012-07-15 22:26:51 +02002918 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002919 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002920 * reply from the same orig_node yet
2921 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02002922 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002923 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002924 continue;
2925
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002926 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002927 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002928 continue;
2929
Sven Eckelmann3e348192012-05-16 20:23:22 +02002930 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002931 /* Sorry, you roamed too many times! */
2932 goto unlock;
2933 ret = true;
2934 break;
2935 }
2936
2937 if (!ret) {
2938 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2939 if (!tt_roam_node)
2940 goto unlock;
2941
2942 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002943 atomic_set(&tt_roam_node->counter,
2944 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002945 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002946
Sven Eckelmann807736f2012-07-15 22:26:51 +02002947 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002948 ret = true;
2949 }
2950
2951unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002952 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002953 return ret;
2954}
2955
Antonio Quartullic018ad32013-06-04 12:11:39 +02002956/**
2957 * batadv_send_roam_adv - send a roaming advertisement message
2958 * @bat_priv: the bat priv with all the soft interface information
2959 * @client: mac address of the roaming client
2960 * @vid: VLAN identifier
2961 * @orig_node: message destination
2962 *
2963 * Send a ROAMING_ADV message to the node which was previously serving this
2964 * client. This is done to inform the node that from now on all traffic destined
2965 * for this particular roamed client has to be forwarded to the sender of the
2966 * roaming message.
2967 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002968static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002969 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002970 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002971{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002972 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08002973 struct batadv_tvlv_roam_adv tvlv_roam;
2974
2975 primary_if = batadv_primary_if_get_selected(bat_priv);
2976 if (!primary_if)
2977 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002978
2979 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002980 * already roamed to us too many times
2981 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002982 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02002983 goto out;
2984
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002985 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002986 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2987 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002988
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002989 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002990
Marek Lindner122edaa2013-04-23 21:40:03 +08002991 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02002992 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08002993
2994 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2995 orig_node->orig, BATADV_TVLV_ROAM, 1,
2996 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02002997
2998out:
Marek Lindner122edaa2013-04-23 21:40:03 +08002999 if (primary_if)
3000 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003001}
3002
Sven Eckelmanna5130882012-05-16 20:23:16 +02003003static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003004{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003005 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003006 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003007 struct batadv_priv *bat_priv;
3008
3009 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003010 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3011 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003012
Marek Lindnera19d3d82013-05-27 15:33:25 +08003013 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003014 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003015 batadv_tt_req_purge(bat_priv);
3016 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003017
Antonio Quartulli72414442012-12-25 13:14:37 +01003018 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3019 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003020}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003021
Sven Eckelmann56303d32012-06-05 22:31:31 +02003022void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003023{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003024 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3025 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3026
Sven Eckelmann807736f2012-07-15 22:26:51 +02003027 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003028
Sven Eckelmanna5130882012-05-16 20:23:16 +02003029 batadv_tt_local_table_free(bat_priv);
3030 batadv_tt_global_table_free(bat_priv);
3031 batadv_tt_req_list_free(bat_priv);
3032 batadv_tt_changes_list_free(bat_priv);
3033 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003034
Sven Eckelmann807736f2012-07-15 22:26:51 +02003035 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003036}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003037
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003038/**
3039 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3040 * table and possibly count them in the TT size
3041 * @bat_priv: the bat priv with all the soft interface information
3042 * @flags: the flag to switch
3043 * @enable: whether to set or unset the flag
3044 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003045 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003046static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3047 uint16_t flags, bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003048{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003049 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3050 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli697f2532011-11-07 16:47:01 +01003051 uint16_t changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003052 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003053 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003054
3055 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003056 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003057
3058 for (i = 0; i < hash->size; i++) {
3059 head = &hash->table[i];
3060
3061 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003062 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003063 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003064 if (enable) {
3065 if ((tt_common_entry->flags & flags) == flags)
3066 continue;
3067 tt_common_entry->flags |= flags;
3068 } else {
3069 if (!(tt_common_entry->flags & flags))
3070 continue;
3071 tt_common_entry->flags &= ~flags;
3072 }
3073 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003074
3075 if (!count)
3076 continue;
3077
3078 batadv_tt_local_size_inc(bat_priv,
3079 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003080 }
3081 rcu_read_unlock();
3082 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003083}
3084
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003085/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003086static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003087{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003088 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003089 struct batadv_tt_common_entry *tt_common;
3090 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003091 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003092 struct hlist_head *head;
3093 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullic90681b2011-10-05 17:05:25 +02003094 uint32_t i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003095
3096 if (!hash)
3097 return;
3098
3099 for (i = 0; i < hash->size; i++) {
3100 head = &hash->table[i];
3101 list_lock = &hash->list_locks[i];
3102
3103 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003104 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003105 hash_entry) {
3106 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003107 continue;
3108
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003109 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003110 "Deleting local tt entry (%pM, vid: %d): pending\n",
3111 tt_common->addr,
3112 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003113
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003114 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003115 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003116 tt_local = container_of(tt_common,
3117 struct batadv_tt_local_entry,
3118 common);
3119 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003120 }
3121 spin_unlock_bh(list_lock);
3122 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003123}
3124
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003125/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003126 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3127 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003128 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003129 *
3130 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003131 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003132static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003133{
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003134 /* Update multicast addresses in local translation table */
3135 batadv_mcast_mla_update(bat_priv);
3136
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003137 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3138 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3139 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003140 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003141 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003142
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003143 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003144
Sven Eckelmanna5130882012-05-16 20:23:16 +02003145 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003146 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003147
3148 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003149 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003150 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003151 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann807736f2012-07-15 22:26:51 +02003152 (uint8_t)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003153
3154 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003155 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003156 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003157}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003158
Marek Lindnera19d3d82013-05-27 15:33:25 +08003159/**
3160 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3161 * have been queued in the time since the last commit
3162 * @bat_priv: the bat priv with all the soft interface information
3163 */
3164void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3165{
3166 spin_lock_bh(&bat_priv->tt.commit_lock);
3167 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003168 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003169}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003170
Sven Eckelmann56303d32012-06-05 22:31:31 +02003171bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003172 uint8_t *dst, unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003173{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003174 struct batadv_tt_local_entry *tt_local_entry = NULL;
3175 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003176 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003177 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003178
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003179 vlan = batadv_softif_vlan_get(bat_priv, vid);
3180 if (!vlan || !atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003181 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003182
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003183 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003184 if (!tt_local_entry)
3185 goto out;
3186
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003187 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003188 if (!tt_global_entry)
3189 goto out;
3190
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003191 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003192 goto out;
3193
Marek Lindner5870adc2012-06-20 17:16:05 +02003194 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003195
3196out:
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003197 if (vlan)
3198 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003199 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003200 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003201 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003202 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003203 return ret;
3204}
Marek Lindnera943cac2011-07-30 13:10:18 +02003205
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003206/**
3207 * batadv_tt_update_orig - update global translation table with new tt
3208 * information received via ogms
3209 * @bat_priv: the bat priv with all the soft interface information
3210 * @orig: the orig_node of the ogm
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003211 * @tt_vlan: pointer to the first tvlv VLAN entry
3212 * @tt_num_vlan: number of tvlv VLAN entries
3213 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003214 * @tt_num_changes: number of tt changes inside the tt buffer
3215 * @ttvn: translation table version number of this changeset
Antonio Quartulliced72932013-04-24 16:37:51 +02003216 * @tt_crc: crc32 checksum of orig node's translation table
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003217 */
3218static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3219 struct batadv_orig_node *orig_node,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003220 const void *tt_buff, uint16_t tt_num_vlan,
3221 struct batadv_tvlv_tt_change *tt_change,
3222 uint16_t tt_num_changes, uint8_t ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003223{
3224 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003225 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003226 bool full_table = true;
3227
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003228 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Antonio Quartulli17071572011-11-07 16:36:40 +01003229 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003230 * increased by one -> we can apply the attached changes
3231 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003232 if ((!orig_node->tt_initialised && ttvn == 1) ||
3233 ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003234 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003235 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3236 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003237 * In this case send a tt request
3238 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003239 if (!tt_num_changes) {
3240 full_table = false;
3241 goto request_table;
3242 }
3243
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003244 spin_lock_bh(&orig_node->tt_lock);
3245
Sven Eckelmanna5130882012-05-16 20:23:16 +02003246 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003247 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003248
3249 /* Even if we received the precomputed crc with the OGM, we
3250 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003251 * in the global table
3252 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003253 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003254
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003255 spin_unlock_bh(&orig_node->tt_lock);
3256
Marek Lindnera943cac2011-07-30 13:10:18 +02003257 /* The ttvn alone is not enough to guarantee consistency
3258 * because a single value could represent different states
3259 * (due to the wrap around). Thus a node has to check whether
3260 * the resulting table (after applying the changes) is still
3261 * consistent or not. E.g. a node could disconnect while its
3262 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3263 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003264 * inconsistency
3265 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003266 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3267 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003268 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003269 } else {
3270 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003271 * in sync anymore -> request fresh tt data
3272 */
Antonio Quartulli17071572011-11-07 16:36:40 +01003273 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003274 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3275 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003276request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003277 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003278 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3279 orig_node->orig, ttvn, orig_ttvn,
3280 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003281 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003282 tt_vlan, tt_num_vlan,
3283 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003284 return;
3285 }
3286 }
3287}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003288
Antonio Quartullic018ad32013-06-04 12:11:39 +02003289/**
3290 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3291 * @bat_priv: the bat priv with all the soft interface information
3292 * @addr: the mac address of the client to check
3293 * @vid: VLAN identifier
3294 *
3295 * Returns true if we know that the client has moved from its old originator
3296 * to another one. This entry is still kept for consistency purposes and will be
3297 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003298 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003299bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003300 uint8_t *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003301{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003302 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003303 bool ret = false;
3304
Antonio Quartullic018ad32013-06-04 12:11:39 +02003305 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003306 if (!tt_global_entry)
3307 goto out;
3308
Antonio Quartullic1d07432013-01-15 22:17:19 +10003309 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003310 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003311out:
3312 return ret;
3313}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003314
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003315/**
3316 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3317 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003318 * @addr: the mac address of the local client to query
3319 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003320 *
3321 * Returns true if the local client is known to be roaming (it is not served by
3322 * this node anymore) or not. If yes, the client is still present in the table
3323 * to keep the latter consistent with the node TTVN
3324 */
3325bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003326 uint8_t *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003327{
3328 struct batadv_tt_local_entry *tt_local_entry;
3329 bool ret = false;
3330
Antonio Quartullic018ad32013-06-04 12:11:39 +02003331 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003332 if (!tt_local_entry)
3333 goto out;
3334
3335 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3336 batadv_tt_local_entry_free_ref(tt_local_entry);
3337out:
3338 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003339}
3340
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003341bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3342 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003343 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003344 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003345{
3346 bool ret = false;
3347
Antonio Quartulli16052782013-06-04 12:11:41 +02003348 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003349 BATADV_TT_CLIENT_TEMP,
3350 atomic_read(&orig_node->last_ttvn)))
3351 goto out;
3352
3353 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003354 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3355 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003356 ret = true;
3357out:
3358 return ret;
3359}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003360
3361/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003362 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3363 * maximum packet size that can be transported through the mesh
3364 * @soft_iface: netdev struct of the mesh interface
3365 *
3366 * Remove entries older than 'timeout' and half timeout if more entries need
3367 * to be removed.
3368 */
3369void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3370{
3371 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3372 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3373 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3374 bool reduced = false;
3375
3376 spin_lock_bh(&bat_priv->tt.commit_lock);
3377
3378 while (true) {
3379 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3380 if (packet_size_max >= table_size)
3381 break;
3382
3383 batadv_tt_local_purge(bat_priv, timeout);
3384 batadv_tt_local_purge_pending_clients(bat_priv);
3385
3386 timeout /= 2;
3387 reduced = true;
3388 net_ratelimited_function(batadv_info, soft_iface,
3389 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3390 packet_size_max);
3391 }
3392
3393 /* commit these changes immediately, to avoid synchronization problem
3394 * with the TTVN
3395 */
3396 if (reduced)
3397 batadv_tt_local_commit_changes_nolock(bat_priv);
3398
3399 spin_unlock_bh(&bat_priv->tt.commit_lock);
3400}
3401
3402/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003403 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3404 * @bat_priv: the bat priv with all the soft interface information
3405 * @orig: the orig_node of the ogm
3406 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3407 * @tvlv_value: tvlv buffer containing the gateway data
3408 * @tvlv_value_len: tvlv buffer length
3409 */
3410static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3411 struct batadv_orig_node *orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003412 uint8_t flags, void *tvlv_value,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003413 uint16_t tvlv_value_len)
3414{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003415 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3416 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003417 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003418 uint16_t num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003419
3420 if (tvlv_value_len < sizeof(*tt_data))
3421 return;
3422
3423 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3424 tvlv_value_len -= sizeof(*tt_data);
3425
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003426 num_vlan = ntohs(tt_data->num_vlan);
3427
3428 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3429 return;
3430
3431 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3432 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3433 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3434
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003435 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003436
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003437 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3438 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003439}
3440
3441/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003442 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3443 * container
3444 * @bat_priv: the bat priv with all the soft interface information
3445 * @src: mac address of tt tvlv sender
3446 * @dst: mac address of tt tvlv recipient
3447 * @tvlv_value: tvlv buffer containing the tt data
3448 * @tvlv_value_len: tvlv buffer length
3449 *
3450 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3451 * otherwise.
3452 */
3453static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3454 uint8_t *src, uint8_t *dst,
3455 void *tvlv_value,
3456 uint16_t tvlv_value_len)
3457{
3458 struct batadv_tvlv_tt_data *tt_data;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003459 uint16_t tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003460 char tt_flag;
3461 bool ret;
3462
3463 if (tvlv_value_len < sizeof(*tt_data))
3464 return NET_RX_SUCCESS;
3465
3466 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3467 tvlv_value_len -= sizeof(*tt_data);
3468
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003469 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3470 tt_vlan_len *= ntohs(tt_data->num_vlan);
3471
3472 if (tvlv_value_len < tt_vlan_len)
3473 return NET_RX_SUCCESS;
3474
3475 tvlv_value_len -= tt_vlan_len;
3476 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003477
3478 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3479 case BATADV_TT_REQUEST:
3480 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3481
3482 /* If this node cannot provide a TT response the tt_request is
3483 * forwarded
3484 */
3485 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3486 if (!ret) {
3487 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3488 tt_flag = 'F';
3489 else
3490 tt_flag = '.';
3491
3492 batadv_dbg(BATADV_DBG_TT, bat_priv,
3493 "Routing TT_REQUEST to %pM [%c]\n",
3494 dst, tt_flag);
3495 /* tvlv API will re-route the packet */
3496 return NET_RX_DROP;
3497 }
3498 break;
3499 case BATADV_TT_RESPONSE:
3500 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3501
3502 if (batadv_is_my_mac(bat_priv, dst)) {
3503 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003504 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003505 return NET_RX_SUCCESS;
3506 }
3507
3508 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3509 tt_flag = 'F';
3510 else
3511 tt_flag = '.';
3512
3513 batadv_dbg(BATADV_DBG_TT, bat_priv,
3514 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3515
3516 /* tvlv API will re-route the packet */
3517 return NET_RX_DROP;
3518 }
3519
3520 return NET_RX_SUCCESS;
3521}
3522
3523/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003524 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3525 * @bat_priv: the bat priv with all the soft interface information
3526 * @src: mac address of tt tvlv sender
3527 * @dst: mac address of tt tvlv recipient
3528 * @tvlv_value: tvlv buffer containing the tt data
3529 * @tvlv_value_len: tvlv buffer length
3530 *
3531 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3532 * otherwise.
3533 */
3534static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3535 uint8_t *src, uint8_t *dst,
3536 void *tvlv_value,
3537 uint16_t tvlv_value_len)
3538{
3539 struct batadv_tvlv_roam_adv *roaming_adv;
3540 struct batadv_orig_node *orig_node = NULL;
3541
3542 /* If this node is not the intended recipient of the
3543 * roaming advertisement the packet is forwarded
3544 * (the tvlv API will re-route the packet).
3545 */
3546 if (!batadv_is_my_mac(bat_priv, dst))
3547 return NET_RX_DROP;
3548
Marek Lindner122edaa2013-04-23 21:40:03 +08003549 if (tvlv_value_len < sizeof(*roaming_adv))
3550 goto out;
3551
3552 orig_node = batadv_orig_hash_find(bat_priv, src);
3553 if (!orig_node)
3554 goto out;
3555
3556 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3557 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3558
3559 batadv_dbg(BATADV_DBG_TT, bat_priv,
3560 "Received ROAMING_ADV from %pM (client %pM)\n",
3561 src, roaming_adv->client);
3562
3563 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003564 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003565 atomic_read(&orig_node->last_ttvn) + 1);
3566
3567out:
3568 if (orig_node)
3569 batadv_orig_node_free_ref(orig_node);
3570 return NET_RX_SUCCESS;
3571}
3572
3573/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003574 * batadv_tt_init - initialise the translation table internals
3575 * @bat_priv: the bat priv with all the soft interface information
3576 *
3577 * Return 0 on success or negative error number in case of failure.
3578 */
3579int batadv_tt_init(struct batadv_priv *bat_priv)
3580{
3581 int ret;
3582
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003583 /* synchronized flags must be remote */
3584 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3585
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003586 ret = batadv_tt_local_init(bat_priv);
3587 if (ret < 0)
3588 return ret;
3589
3590 ret = batadv_tt_global_init(bat_priv);
3591 if (ret < 0)
3592 return ret;
3593
3594 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003595 batadv_tt_tvlv_unicast_handler_v1,
3596 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003597
Marek Lindner122edaa2013-04-23 21:40:03 +08003598 batadv_tvlv_handler_register(bat_priv, NULL,
3599 batadv_roam_tvlv_unicast_handler_v1,
3600 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3601
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003602 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3603 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3604 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3605
3606 return 1;
3607}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003608
3609/**
3610 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3611 * @bat_priv: the bat priv with all the soft interface information
3612 * @addr: the mac address of the client
3613 * @vid: the identifier of the VLAN where this client is connected
3614 *
3615 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3616 * otherwise
3617 */
3618bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3619 const uint8_t *addr, unsigned short vid)
3620{
3621 struct batadv_tt_global_entry *tt;
3622 bool ret;
3623
3624 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3625 if (!tt)
3626 return false;
3627
3628 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3629
3630 batadv_tt_global_entry_free_ref(tt);
3631
3632 return ret;
3633}