87bb2030186de5122b688cef78450db35ff8bcda
[deliverable/linux.git] / net / batman-adv / translation-table.c
1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "translation-table.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/compiler.h>
26 #include <linux/crc32c.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
29 #include <linux/fs.h>
30 #include <linux/if_ether.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/rculist.h>
39 #include <linux/rcupdate.h>
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/workqueue.h>
46
47 #include "bridge_loop_avoidance.h"
48 #include "hard-interface.h"
49 #include "hash.h"
50 #include "multicast.h"
51 #include "originator.h"
52 #include "packet.h"
53 #include "soft-interface.h"
54
55 /* hash class keys */
56 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
58
59 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
60 unsigned short vid,
61 struct batadv_orig_node *orig_node);
62 static void batadv_tt_purge(struct work_struct *work);
63 static void
64 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
65 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
68 unsigned short vid, const char *message,
69 bool roaming);
70
71 /**
72 * batadv_compare_tt - check if two TT entries are the same
73 * @node: the list element pointer of the first TT entry
74 * @data2: pointer to the tt_common_entry of the second TT entry
75 *
76 * Compare the MAC address and the VLAN ID of the two TT entries and check if
77 * they are the same TT client.
78 * Return: true if the two TT clients are the same, false otherwise
79 */
80 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
81 {
82 const void *data1 = container_of(node, struct batadv_tt_common_entry,
83 hash_entry);
84 const struct batadv_tt_common_entry *tt1 = data1;
85 const struct batadv_tt_common_entry *tt2 = data2;
86
87 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
88 }
89
90 /**
91 * batadv_choose_tt - return the index of the tt entry in the hash table
92 * @data: pointer to the tt_common_entry object to map
93 * @size: the size of the hash table
94 *
95 * Return: the hash index where the object represented by 'data' should be
96 * stored at.
97 */
98 static inline u32 batadv_choose_tt(const void *data, u32 size)
99 {
100 struct batadv_tt_common_entry *tt;
101 u32 hash = 0;
102
103 tt = (struct batadv_tt_common_entry *)data;
104 hash = jhash(&tt->addr, ETH_ALEN, hash);
105 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
106
107 return hash % size;
108 }
109
110 /**
111 * batadv_tt_hash_find - look for a client in the given hash table
112 * @hash: the hash table to search
113 * @addr: the mac address of the client to look for
114 * @vid: VLAN identifier
115 *
116 * Return: a pointer to the tt_common struct belonging to the searched client if
117 * found, NULL otherwise.
118 */
119 static struct batadv_tt_common_entry *
120 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
121 unsigned short vid)
122 {
123 struct hlist_head *head;
124 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
125 u32 index;
126
127 if (!hash)
128 return NULL;
129
130 ether_addr_copy(to_search.addr, addr);
131 to_search.vid = vid;
132
133 index = batadv_choose_tt(&to_search, hash->size);
134 head = &hash->table[index];
135
136 rcu_read_lock();
137 hlist_for_each_entry_rcu(tt, head, hash_entry) {
138 if (!batadv_compare_eth(tt, addr))
139 continue;
140
141 if (tt->vid != vid)
142 continue;
143
144 if (!kref_get_unless_zero(&tt->refcount))
145 continue;
146
147 tt_tmp = tt;
148 break;
149 }
150 rcu_read_unlock();
151
152 return tt_tmp;
153 }
154
155 /**
156 * batadv_tt_local_hash_find - search the local table for a given client
157 * @bat_priv: the bat priv with all the soft interface information
158 * @addr: the mac address of the client to look for
159 * @vid: VLAN identifier
160 *
161 * Return: a pointer to the corresponding tt_local_entry struct if the client is
162 * found, NULL otherwise.
163 */
164 static struct batadv_tt_local_entry *
165 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
166 unsigned short vid)
167 {
168 struct batadv_tt_common_entry *tt_common_entry;
169 struct batadv_tt_local_entry *tt_local_entry = NULL;
170
171 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
172 vid);
173 if (tt_common_entry)
174 tt_local_entry = container_of(tt_common_entry,
175 struct batadv_tt_local_entry,
176 common);
177 return tt_local_entry;
178 }
179
180 /**
181 * batadv_tt_global_hash_find - search the global table for a given client
182 * @bat_priv: the bat priv with all the soft interface information
183 * @addr: the mac address of the client to look for
184 * @vid: VLAN identifier
185 *
186 * Return: a pointer to the corresponding tt_global_entry struct if the client
187 * is found, NULL otherwise.
188 */
189 static struct batadv_tt_global_entry *
190 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
191 unsigned short vid)
192 {
193 struct batadv_tt_common_entry *tt_common_entry;
194 struct batadv_tt_global_entry *tt_global_entry = NULL;
195
196 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
197 vid);
198 if (tt_common_entry)
199 tt_global_entry = container_of(tt_common_entry,
200 struct batadv_tt_global_entry,
201 common);
202 return tt_global_entry;
203 }
204
205 /**
206 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
207 * for free after rcu grace period
208 * @ref: kref pointer of the nc_node
209 */
210 static void batadv_tt_local_entry_release(struct kref *ref)
211 {
212 struct batadv_tt_local_entry *tt_local_entry;
213
214 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
215 common.refcount);
216
217 batadv_softif_vlan_put(tt_local_entry->vlan);
218
219 kfree_rcu(tt_local_entry, common.rcu);
220 }
221
222 /**
223 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
224 * possibly release it
225 * @tt_local_entry: tt_local_entry to be free'd
226 */
227 static void
228 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
229 {
230 kref_put(&tt_local_entry->common.refcount,
231 batadv_tt_local_entry_release);
232 }
233
234 /**
235 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
236 * for free after rcu grace period
237 * @ref: kref pointer of the nc_node
238 */
239 static void batadv_tt_global_entry_release(struct kref *ref)
240 {
241 struct batadv_tt_global_entry *tt_global_entry;
242
243 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
244 common.refcount);
245
246 batadv_tt_global_del_orig_list(tt_global_entry);
247 kfree_rcu(tt_global_entry, common.rcu);
248 }
249
250 /**
251 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
252 * possibly release it
253 * @tt_global_entry: tt_global_entry to be free'd
254 */
255 static void
256 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
257 {
258 kref_put(&tt_global_entry->common.refcount,
259 batadv_tt_global_entry_release);
260 }
261
262 /**
263 * batadv_tt_global_hash_count - count the number of orig entries
264 * @bat_priv: the bat priv with all the soft interface information
265 * @addr: the mac address of the client to count entries for
266 * @vid: VLAN identifier
267 *
268 * Return: the number of originators advertising the given address/data
269 * (excluding ourself).
270 */
271 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
272 const u8 *addr, unsigned short vid)
273 {
274 struct batadv_tt_global_entry *tt_global_entry;
275 int count;
276
277 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
278 if (!tt_global_entry)
279 return 0;
280
281 count = atomic_read(&tt_global_entry->orig_list_count);
282 batadv_tt_global_entry_put(tt_global_entry);
283
284 return count;
285 }
286
287 /**
288 * batadv_tt_local_size_mod - change the size by v of the local table identified
289 * by vid
290 * @bat_priv: the bat priv with all the soft interface information
291 * @vid: the VLAN identifier of the sub-table to change
292 * @v: the amount to sum to the local table size
293 */
294 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
295 unsigned short vid, int v)
296 {
297 struct batadv_softif_vlan *vlan;
298
299 vlan = batadv_softif_vlan_get(bat_priv, vid);
300 if (!vlan)
301 return;
302
303 atomic_add(v, &vlan->tt.num_entries);
304
305 batadv_softif_vlan_put(vlan);
306 }
307
308 /**
309 * batadv_tt_local_size_inc - increase by one the local table size for the given
310 * vid
311 * @bat_priv: the bat priv with all the soft interface information
312 * @vid: the VLAN identifier
313 */
314 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
315 unsigned short vid)
316 {
317 batadv_tt_local_size_mod(bat_priv, vid, 1);
318 }
319
320 /**
321 * batadv_tt_local_size_dec - decrease by one the local table size for the given
322 * vid
323 * @bat_priv: the bat priv with all the soft interface information
324 * @vid: the VLAN identifier
325 */
326 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
327 unsigned short vid)
328 {
329 batadv_tt_local_size_mod(bat_priv, vid, -1);
330 }
331
332 /**
333 * batadv_tt_global_size_mod - change the size by v of the global table
334 * for orig_node identified by vid
335 * @orig_node: the originator for which the table has to be modified
336 * @vid: the VLAN identifier
337 * @v: the amount to sum to the global table size
338 */
339 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
340 unsigned short vid, int v)
341 {
342 struct batadv_orig_node_vlan *vlan;
343
344 vlan = batadv_orig_node_vlan_new(orig_node, vid);
345 if (!vlan)
346 return;
347
348 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
349 spin_lock_bh(&orig_node->vlan_list_lock);
350 if (!hlist_unhashed(&vlan->list)) {
351 hlist_del_init_rcu(&vlan->list);
352 batadv_orig_node_vlan_put(vlan);
353 }
354 spin_unlock_bh(&orig_node->vlan_list_lock);
355 }
356
357 batadv_orig_node_vlan_put(vlan);
358 }
359
360 /**
361 * batadv_tt_global_size_inc - increase by one the global table size for the
362 * given vid
363 * @orig_node: the originator which global table size has to be decreased
364 * @vid: the vlan identifier
365 */
366 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
367 unsigned short vid)
368 {
369 batadv_tt_global_size_mod(orig_node, vid, 1);
370 }
371
372 /**
373 * batadv_tt_global_size_dec - decrease by one the global table size for the
374 * given vid
375 * @orig_node: the originator which global table size has to be decreased
376 * @vid: the vlan identifier
377 */
378 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
379 unsigned short vid)
380 {
381 batadv_tt_global_size_mod(orig_node, vid, -1);
382 }
383
384 /**
385 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
386 * queue for free after rcu grace period
387 * @ref: kref pointer of the tt orig entry
388 */
389 static void batadv_tt_orig_list_entry_release(struct kref *ref)
390 {
391 struct batadv_tt_orig_list_entry *orig_entry;
392
393 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
394 refcount);
395
396 batadv_orig_node_put(orig_entry->orig_node);
397 kfree_rcu(orig_entry, rcu);
398 }
399
400 /**
401 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
402 * possibly release it
403 * @orig_entry: tt orig entry to be free'd
404 */
405 static void
406 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
407 {
408 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
409 }
410
411 /**
412 * batadv_tt_local_event - store a local TT event (ADD/DEL)
413 * @bat_priv: the bat priv with all the soft interface information
414 * @tt_local_entry: the TT entry involved in the event
415 * @event_flags: flags to store in the event structure
416 */
417 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
418 struct batadv_tt_local_entry *tt_local_entry,
419 u8 event_flags)
420 {
421 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
422 struct batadv_tt_common_entry *common = &tt_local_entry->common;
423 u8 flags = common->flags | event_flags;
424 bool event_removed = false;
425 bool del_op_requested, del_op_entry;
426
427 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
428 if (!tt_change_node)
429 return;
430
431 tt_change_node->change.flags = flags;
432 memset(tt_change_node->change.reserved, 0,
433 sizeof(tt_change_node->change.reserved));
434 ether_addr_copy(tt_change_node->change.addr, common->addr);
435 tt_change_node->change.vid = htons(common->vid);
436
437 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
438
439 /* check for ADD+DEL or DEL+ADD events */
440 spin_lock_bh(&bat_priv->tt.changes_list_lock);
441 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
442 list) {
443 if (!batadv_compare_eth(entry->change.addr, common->addr))
444 continue;
445
446 /* DEL+ADD in the same orig interval have no effect and can be
447 * removed to avoid silly behaviour on the receiver side. The
448 * other way around (ADD+DEL) can happen in case of roaming of
449 * a client still in the NEW state. Roaming of NEW clients is
450 * now possible due to automatically recognition of "temporary"
451 * clients
452 */
453 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
454 if (!del_op_requested && del_op_entry)
455 goto del;
456 if (del_op_requested && !del_op_entry)
457 goto del;
458
459 /* this is a second add in the same originator interval. It
460 * means that flags have been changed: update them!
461 */
462 if (!del_op_requested && !del_op_entry)
463 entry->change.flags = flags;
464
465 continue;
466 del:
467 list_del(&entry->list);
468 kfree(entry);
469 kfree(tt_change_node);
470 event_removed = true;
471 goto unlock;
472 }
473
474 /* track the change in the OGMinterval list */
475 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
476
477 unlock:
478 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
479
480 if (event_removed)
481 atomic_dec(&bat_priv->tt.local_changes);
482 else
483 atomic_inc(&bat_priv->tt.local_changes);
484 }
485
486 /**
487 * batadv_tt_len - compute length in bytes of given number of tt changes
488 * @changes_num: number of tt changes
489 *
490 * Return: computed length in bytes.
491 */
492 static int batadv_tt_len(int changes_num)
493 {
494 return changes_num * sizeof(struct batadv_tvlv_tt_change);
495 }
496
497 /**
498 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
499 * @tt_len: available space
500 *
501 * Return: the number of entries.
502 */
503 static u16 batadv_tt_entries(u16 tt_len)
504 {
505 return tt_len / batadv_tt_len(1);
506 }
507
508 /**
509 * batadv_tt_local_table_transmit_size - calculates the local translation table
510 * size when transmitted over the air
511 * @bat_priv: the bat priv with all the soft interface information
512 *
513 * Return: local translation table size in bytes.
514 */
515 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
516 {
517 u16 num_vlan = 0;
518 u16 tt_local_entries = 0;
519 struct batadv_softif_vlan *vlan;
520 int hdr_size;
521
522 rcu_read_lock();
523 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
524 num_vlan++;
525 tt_local_entries += atomic_read(&vlan->tt.num_entries);
526 }
527 rcu_read_unlock();
528
529 /* header size of tvlv encapsulated tt response payload */
530 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
531 hdr_size += sizeof(struct batadv_tvlv_hdr);
532 hdr_size += sizeof(struct batadv_tvlv_tt_data);
533 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
534
535 return hdr_size + batadv_tt_len(tt_local_entries);
536 }
537
538 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
539 {
540 if (bat_priv->tt.local_hash)
541 return 0;
542
543 bat_priv->tt.local_hash = batadv_hash_new(1024);
544
545 if (!bat_priv->tt.local_hash)
546 return -ENOMEM;
547
548 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
549 &batadv_tt_local_hash_lock_class_key);
550
551 return 0;
552 }
553
554 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
555 struct batadv_tt_global_entry *tt_global,
556 const char *message)
557 {
558 batadv_dbg(BATADV_DBG_TT, bat_priv,
559 "Deleting global tt entry %pM (vid: %d): %s\n",
560 tt_global->common.addr,
561 BATADV_PRINT_VID(tt_global->common.vid), message);
562
563 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
564 batadv_choose_tt, &tt_global->common);
565 batadv_tt_global_entry_put(tt_global);
566 }
567
568 /**
569 * batadv_tt_local_add - add a new client to the local table or update an
570 * existing client
571 * @soft_iface: netdev struct of the mesh interface
572 * @addr: the mac address of the client to add
573 * @vid: VLAN identifier
574 * @ifindex: index of the interface where the client is connected to (useful to
575 * identify wireless clients)
576 * @mark: the value contained in the skb->mark field of the received packet (if
577 * any)
578 *
579 * Return: true if the client was successfully added, false otherwise.
580 */
581 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
582 unsigned short vid, int ifindex, u32 mark)
583 {
584 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
585 struct batadv_tt_local_entry *tt_local;
586 struct batadv_tt_global_entry *tt_global = NULL;
587 struct net *net = dev_net(soft_iface);
588 struct batadv_softif_vlan *vlan;
589 struct net_device *in_dev = NULL;
590 struct hlist_head *head;
591 struct batadv_tt_orig_list_entry *orig_entry;
592 int hash_added, table_size, packet_size_max;
593 bool ret = false;
594 bool roamed_back = false;
595 u8 remote_flags;
596 u32 match_mark;
597
598 if (ifindex != BATADV_NULL_IFINDEX)
599 in_dev = dev_get_by_index(net, ifindex);
600
601 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
602
603 if (!is_multicast_ether_addr(addr))
604 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
605
606 if (tt_local) {
607 tt_local->last_seen = jiffies;
608 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
609 batadv_dbg(BATADV_DBG_TT, bat_priv,
610 "Re-adding pending client %pM (vid: %d)\n",
611 addr, BATADV_PRINT_VID(vid));
612 /* whatever the reason why the PENDING flag was set,
613 * this is a client which was enqueued to be removed in
614 * this orig_interval. Since it popped up again, the
615 * flag can be reset like it was never enqueued
616 */
617 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
618 goto add_event;
619 }
620
621 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
622 batadv_dbg(BATADV_DBG_TT, bat_priv,
623 "Roaming client %pM (vid: %d) came back to its original location\n",
624 addr, BATADV_PRINT_VID(vid));
625 /* the ROAM flag is set because this client roamed away
626 * and the node got a roaming_advertisement message. Now
627 * that the client popped up again at its original
628 * location such flag can be unset
629 */
630 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
631 roamed_back = true;
632 }
633 goto check_roaming;
634 }
635
636 /* Ignore the client if we cannot send it in a full table response. */
637 table_size = batadv_tt_local_table_transmit_size(bat_priv);
638 table_size += batadv_tt_len(1);
639 packet_size_max = atomic_read(&bat_priv->packet_size_max);
640 if (table_size > packet_size_max) {
641 net_ratelimited_function(batadv_info, soft_iface,
642 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
643 table_size, packet_size_max, addr);
644 goto out;
645 }
646
647 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
648 if (!tt_local)
649 goto out;
650
651 /* increase the refcounter of the related vlan */
652 vlan = batadv_softif_vlan_get(bat_priv, vid);
653 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
654 addr, BATADV_PRINT_VID(vid))) {
655 kfree(tt_local);
656 tt_local = NULL;
657 goto out;
658 }
659
660 batadv_dbg(BATADV_DBG_TT, bat_priv,
661 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
662 addr, BATADV_PRINT_VID(vid),
663 (u8)atomic_read(&bat_priv->tt.vn));
664
665 ether_addr_copy(tt_local->common.addr, addr);
666 /* The local entry has to be marked as NEW to avoid to send it in
667 * a full table response going out before the next ttvn increment
668 * (consistency check)
669 */
670 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
671 tt_local->common.vid = vid;
672 if (batadv_is_wifi_netdev(in_dev))
673 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
674 kref_init(&tt_local->common.refcount);
675 kref_get(&tt_local->common.refcount);
676 tt_local->last_seen = jiffies;
677 tt_local->common.added_at = tt_local->last_seen;
678 tt_local->vlan = vlan;
679
680 /* the batman interface mac and multicast addresses should never be
681 * purged
682 */
683 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
684 is_multicast_ether_addr(addr))
685 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
686
687 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
688 batadv_choose_tt, &tt_local->common,
689 &tt_local->common.hash_entry);
690
691 if (unlikely(hash_added != 0)) {
692 /* remove the reference for the hash */
693 batadv_tt_local_entry_put(tt_local);
694 batadv_softif_vlan_put(vlan);
695 goto out;
696 }
697
698 add_event:
699 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
700
701 check_roaming:
702 /* Check whether it is a roaming, but don't do anything if the roaming
703 * process has already been handled
704 */
705 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
706 /* These node are probably going to update their tt table */
707 head = &tt_global->orig_list;
708 rcu_read_lock();
709 hlist_for_each_entry_rcu(orig_entry, head, list) {
710 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
711 tt_global->common.vid,
712 orig_entry->orig_node);
713 }
714 rcu_read_unlock();
715 if (roamed_back) {
716 batadv_tt_global_free(bat_priv, tt_global,
717 "Roaming canceled");
718 tt_global = NULL;
719 } else {
720 /* The global entry has to be marked as ROAMING and
721 * has to be kept for consistency purpose
722 */
723 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
724 tt_global->roam_at = jiffies;
725 }
726 }
727
728 /* store the current remote flags before altering them. This helps
729 * understanding is flags are changing or not
730 */
731 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
732
733 if (batadv_is_wifi_netdev(in_dev))
734 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
735 else
736 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
737
738 /* check the mark in the skb: if it's equal to the configured
739 * isolation_mark, it means the packet is coming from an isolated
740 * non-mesh client
741 */
742 match_mark = (mark & bat_priv->isolation_mark_mask);
743 if (bat_priv->isolation_mark_mask &&
744 match_mark == bat_priv->isolation_mark)
745 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
746 else
747 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
748
749 /* if any "dynamic" flag has been modified, resend an ADD event for this
750 * entry so that all the nodes can get the new flags
751 */
752 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
753 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
754
755 ret = true;
756 out:
757 if (in_dev)
758 dev_put(in_dev);
759 if (tt_local)
760 batadv_tt_local_entry_put(tt_local);
761 if (tt_global)
762 batadv_tt_global_entry_put(tt_global);
763 return ret;
764 }
765
766 /**
767 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
768 * within a TT Response directed to another node
769 * @orig_node: originator for which the TT data has to be prepared
770 * @tt_data: uninitialised pointer to the address of the TVLV buffer
771 * @tt_change: uninitialised pointer to the address of the area where the TT
772 * changed can be stored
773 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
774 * function reserves the amount of space needed to send the entire global TT
775 * table. In case of success the value is updated with the real amount of
776 * reserved bytes
777 * Allocate the needed amount of memory for the entire TT TVLV and write its
778 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
779 * objects, one per active VLAN served by the originator node.
780 *
781 * Return: the size of the allocated buffer or 0 in case of failure.
782 */
783 static u16
784 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
785 struct batadv_tvlv_tt_data **tt_data,
786 struct batadv_tvlv_tt_change **tt_change,
787 s32 *tt_len)
788 {
789 u16 num_vlan = 0;
790 u16 num_entries = 0;
791 u16 change_offset;
792 u16 tvlv_len;
793 struct batadv_tvlv_tt_vlan_data *tt_vlan;
794 struct batadv_orig_node_vlan *vlan;
795 u8 *tt_change_ptr;
796
797 rcu_read_lock();
798 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
799 num_vlan++;
800 num_entries += atomic_read(&vlan->tt.num_entries);
801 }
802
803 change_offset = sizeof(**tt_data);
804 change_offset += num_vlan * sizeof(*tt_vlan);
805
806 /* if tt_len is negative, allocate the space needed by the full table */
807 if (*tt_len < 0)
808 *tt_len = batadv_tt_len(num_entries);
809
810 tvlv_len = *tt_len;
811 tvlv_len += change_offset;
812
813 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
814 if (!*tt_data) {
815 *tt_len = 0;
816 goto out;
817 }
818
819 (*tt_data)->flags = BATADV_NO_FLAGS;
820 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
821 (*tt_data)->num_vlan = htons(num_vlan);
822
823 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
824 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
825 tt_vlan->vid = htons(vlan->vid);
826 tt_vlan->crc = htonl(vlan->tt.crc);
827
828 tt_vlan++;
829 }
830
831 tt_change_ptr = (u8 *)*tt_data + change_offset;
832 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
833
834 out:
835 rcu_read_unlock();
836 return tvlv_len;
837 }
838
839 /**
840 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
841 * node
842 * @bat_priv: the bat priv with all the soft interface information
843 * @tt_data: uninitialised pointer to the address of the TVLV buffer
844 * @tt_change: uninitialised pointer to the address of the area where the TT
845 * changes can be stored
846 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
847 * function reserves the amount of space needed to send the entire local TT
848 * table. In case of success the value is updated with the real amount of
849 * reserved bytes
850 *
851 * Allocate the needed amount of memory for the entire TT TVLV and write its
852 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
853 * objects, one per active VLAN.
854 *
855 * Return: the size of the allocated buffer or 0 in case of failure.
856 */
857 static u16
858 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
859 struct batadv_tvlv_tt_data **tt_data,
860 struct batadv_tvlv_tt_change **tt_change,
861 s32 *tt_len)
862 {
863 struct batadv_tvlv_tt_vlan_data *tt_vlan;
864 struct batadv_softif_vlan *vlan;
865 u16 num_vlan = 0;
866 u16 num_entries = 0;
867 u16 tvlv_len;
868 u8 *tt_change_ptr;
869 int change_offset;
870
871 rcu_read_lock();
872 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
873 num_vlan++;
874 num_entries += atomic_read(&vlan->tt.num_entries);
875 }
876
877 change_offset = sizeof(**tt_data);
878 change_offset += num_vlan * sizeof(*tt_vlan);
879
880 /* if tt_len is negative, allocate the space needed by the full table */
881 if (*tt_len < 0)
882 *tt_len = batadv_tt_len(num_entries);
883
884 tvlv_len = *tt_len;
885 tvlv_len += change_offset;
886
887 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
888 if (!*tt_data) {
889 tvlv_len = 0;
890 goto out;
891 }
892
893 (*tt_data)->flags = BATADV_NO_FLAGS;
894 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
895 (*tt_data)->num_vlan = htons(num_vlan);
896
897 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
898 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
899 tt_vlan->vid = htons(vlan->vid);
900 tt_vlan->crc = htonl(vlan->tt.crc);
901
902 tt_vlan++;
903 }
904
905 tt_change_ptr = (u8 *)*tt_data + change_offset;
906 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
907
908 out:
909 rcu_read_unlock();
910 return tvlv_len;
911 }
912
913 /**
914 * batadv_tt_tvlv_container_update - update the translation table tvlv container
915 * after local tt changes have been committed
916 * @bat_priv: the bat priv with all the soft interface information
917 */
918 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
919 {
920 struct batadv_tt_change_node *entry, *safe;
921 struct batadv_tvlv_tt_data *tt_data;
922 struct batadv_tvlv_tt_change *tt_change;
923 int tt_diff_len, tt_change_len = 0;
924 int tt_diff_entries_num = 0;
925 int tt_diff_entries_count = 0;
926 u16 tvlv_len;
927
928 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
929 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
930
931 /* if we have too many changes for one packet don't send any
932 * and wait for the tt table request which will be fragmented
933 */
934 if (tt_diff_len > bat_priv->soft_iface->mtu)
935 tt_diff_len = 0;
936
937 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
938 &tt_change, &tt_diff_len);
939 if (!tvlv_len)
940 return;
941
942 tt_data->flags = BATADV_TT_OGM_DIFF;
943
944 if (tt_diff_len == 0)
945 goto container_register;
946
947 spin_lock_bh(&bat_priv->tt.changes_list_lock);
948 atomic_set(&bat_priv->tt.local_changes, 0);
949
950 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
951 list) {
952 if (tt_diff_entries_count < tt_diff_entries_num) {
953 memcpy(tt_change + tt_diff_entries_count,
954 &entry->change,
955 sizeof(struct batadv_tvlv_tt_change));
956 tt_diff_entries_count++;
957 }
958 list_del(&entry->list);
959 kfree(entry);
960 }
961 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
962
963 /* Keep the buffer for possible tt_request */
964 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
965 kfree(bat_priv->tt.last_changeset);
966 bat_priv->tt.last_changeset_len = 0;
967 bat_priv->tt.last_changeset = NULL;
968 tt_change_len = batadv_tt_len(tt_diff_entries_count);
969 /* check whether this new OGM has no changes due to size problems */
970 if (tt_diff_entries_count > 0) {
971 /* if kmalloc() fails we will reply with the full table
972 * instead of providing the diff
973 */
974 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
975 if (bat_priv->tt.last_changeset) {
976 memcpy(bat_priv->tt.last_changeset,
977 tt_change, tt_change_len);
978 bat_priv->tt.last_changeset_len = tt_diff_len;
979 }
980 }
981 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
982
983 container_register:
984 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
985 tvlv_len);
986 kfree(tt_data);
987 }
988
989 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
990 {
991 struct net_device *net_dev = (struct net_device *)seq->private;
992 struct batadv_priv *bat_priv = netdev_priv(net_dev);
993 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
994 struct batadv_tt_common_entry *tt_common_entry;
995 struct batadv_tt_local_entry *tt_local;
996 struct batadv_hard_iface *primary_if;
997 struct hlist_head *head;
998 u32 i;
999 int last_seen_secs;
1000 int last_seen_msecs;
1001 unsigned long last_seen_jiffies;
1002 bool no_purge;
1003 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1004
1005 primary_if = batadv_seq_print_text_primary_if_get(seq);
1006 if (!primary_if)
1007 goto out;
1008
1009 seq_printf(seq,
1010 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1011 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1012 seq_puts(seq,
1013 " Client VID Flags Last seen (CRC )\n");
1014
1015 for (i = 0; i < hash->size; i++) {
1016 head = &hash->table[i];
1017
1018 rcu_read_lock();
1019 hlist_for_each_entry_rcu(tt_common_entry,
1020 head, hash_entry) {
1021 tt_local = container_of(tt_common_entry,
1022 struct batadv_tt_local_entry,
1023 common);
1024 last_seen_jiffies = jiffies - tt_local->last_seen;
1025 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1026 last_seen_secs = last_seen_msecs / 1000;
1027 last_seen_msecs = last_seen_msecs % 1000;
1028
1029 no_purge = tt_common_entry->flags & np_flag;
1030 seq_printf(seq,
1031 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1032 tt_common_entry->addr,
1033 BATADV_PRINT_VID(tt_common_entry->vid),
1034 ((tt_common_entry->flags &
1035 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1036 no_purge ? 'P' : '.',
1037 ((tt_common_entry->flags &
1038 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1039 ((tt_common_entry->flags &
1040 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1041 ((tt_common_entry->flags &
1042 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1043 ((tt_common_entry->flags &
1044 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1045 no_purge ? 0 : last_seen_secs,
1046 no_purge ? 0 : last_seen_msecs,
1047 tt_local->vlan->tt.crc);
1048 }
1049 rcu_read_unlock();
1050 }
1051 out:
1052 if (primary_if)
1053 batadv_hardif_put(primary_if);
1054 return 0;
1055 }
1056
1057 static void
1058 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1059 struct batadv_tt_local_entry *tt_local_entry,
1060 u16 flags, const char *message)
1061 {
1062 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1063
1064 /* The local client has to be marked as "pending to be removed" but has
1065 * to be kept in the table in order to send it in a full table
1066 * response issued before the net ttvn increment (consistency check)
1067 */
1068 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1069
1070 batadv_dbg(BATADV_DBG_TT, bat_priv,
1071 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1072 tt_local_entry->common.addr,
1073 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1074 }
1075
1076 /**
1077 * batadv_tt_local_remove - logically remove an entry from the local table
1078 * @bat_priv: the bat priv with all the soft interface information
1079 * @addr: the MAC address of the client to remove
1080 * @vid: VLAN identifier
1081 * @message: message to append to the log on deletion
1082 * @roaming: true if the deletion is due to a roaming event
1083 *
1084 * Return: the flags assigned to the local entry before being deleted
1085 */
1086 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1087 unsigned short vid, const char *message,
1088 bool roaming)
1089 {
1090 struct batadv_tt_local_entry *tt_local_entry;
1091 u16 flags, curr_flags = BATADV_NO_FLAGS;
1092 void *tt_entry_exists;
1093
1094 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1095 if (!tt_local_entry)
1096 goto out;
1097
1098 curr_flags = tt_local_entry->common.flags;
1099
1100 flags = BATADV_TT_CLIENT_DEL;
1101 /* if this global entry addition is due to a roaming, the node has to
1102 * mark the local entry as "roamed" in order to correctly reroute
1103 * packets later
1104 */
1105 if (roaming) {
1106 flags |= BATADV_TT_CLIENT_ROAM;
1107 /* mark the local client as ROAMed */
1108 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1109 }
1110
1111 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1112 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1113 message);
1114 goto out;
1115 }
1116 /* if this client has been added right now, it is possible to
1117 * immediately purge it
1118 */
1119 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1120
1121 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1122 batadv_compare_tt,
1123 batadv_choose_tt,
1124 &tt_local_entry->common);
1125 if (!tt_entry_exists)
1126 goto out;
1127
1128 /* extra call to free the local tt entry */
1129 batadv_tt_local_entry_put(tt_local_entry);
1130
1131 out:
1132 if (tt_local_entry)
1133 batadv_tt_local_entry_put(tt_local_entry);
1134
1135 return curr_flags;
1136 }
1137
1138 /**
1139 * batadv_tt_local_purge_list - purge inactive tt local entries
1140 * @bat_priv: the bat priv with all the soft interface information
1141 * @head: pointer to the list containing the local tt entries
1142 * @timeout: parameter deciding whether a given tt local entry is considered
1143 * inactive or not
1144 */
1145 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1146 struct hlist_head *head,
1147 int timeout)
1148 {
1149 struct batadv_tt_local_entry *tt_local_entry;
1150 struct batadv_tt_common_entry *tt_common_entry;
1151 struct hlist_node *node_tmp;
1152
1153 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1154 hash_entry) {
1155 tt_local_entry = container_of(tt_common_entry,
1156 struct batadv_tt_local_entry,
1157 common);
1158 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1159 continue;
1160
1161 /* entry already marked for deletion */
1162 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1163 continue;
1164
1165 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1166 continue;
1167
1168 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1169 BATADV_TT_CLIENT_DEL, "timed out");
1170 }
1171 }
1172
1173 /**
1174 * batadv_tt_local_purge - purge inactive tt local entries
1175 * @bat_priv: the bat priv with all the soft interface information
1176 * @timeout: parameter deciding whether a given tt local entry is considered
1177 * inactive or not
1178 */
1179 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1180 int timeout)
1181 {
1182 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1183 struct hlist_head *head;
1184 spinlock_t *list_lock; /* protects write access to the hash lists */
1185 u32 i;
1186
1187 for (i = 0; i < hash->size; i++) {
1188 head = &hash->table[i];
1189 list_lock = &hash->list_locks[i];
1190
1191 spin_lock_bh(list_lock);
1192 batadv_tt_local_purge_list(bat_priv, head, timeout);
1193 spin_unlock_bh(list_lock);
1194 }
1195 }
1196
1197 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1198 {
1199 struct batadv_hashtable *hash;
1200 spinlock_t *list_lock; /* protects write access to the hash lists */
1201 struct batadv_tt_common_entry *tt_common_entry;
1202 struct batadv_tt_local_entry *tt_local;
1203 struct hlist_node *node_tmp;
1204 struct hlist_head *head;
1205 u32 i;
1206
1207 if (!bat_priv->tt.local_hash)
1208 return;
1209
1210 hash = bat_priv->tt.local_hash;
1211
1212 for (i = 0; i < hash->size; i++) {
1213 head = &hash->table[i];
1214 list_lock = &hash->list_locks[i];
1215
1216 spin_lock_bh(list_lock);
1217 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1218 head, hash_entry) {
1219 hlist_del_rcu(&tt_common_entry->hash_entry);
1220 tt_local = container_of(tt_common_entry,
1221 struct batadv_tt_local_entry,
1222 common);
1223
1224 batadv_tt_local_entry_put(tt_local);
1225 }
1226 spin_unlock_bh(list_lock);
1227 }
1228
1229 batadv_hash_destroy(hash);
1230
1231 bat_priv->tt.local_hash = NULL;
1232 }
1233
1234 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1235 {
1236 if (bat_priv->tt.global_hash)
1237 return 0;
1238
1239 bat_priv->tt.global_hash = batadv_hash_new(1024);
1240
1241 if (!bat_priv->tt.global_hash)
1242 return -ENOMEM;
1243
1244 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1245 &batadv_tt_global_hash_lock_class_key);
1246
1247 return 0;
1248 }
1249
1250 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1251 {
1252 struct batadv_tt_change_node *entry, *safe;
1253
1254 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1255
1256 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1257 list) {
1258 list_del(&entry->list);
1259 kfree(entry);
1260 }
1261
1262 atomic_set(&bat_priv->tt.local_changes, 0);
1263 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1264 }
1265
1266 /**
1267 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1268 * @entry: the TT global entry where the orig_list_entry has to be
1269 * extracted from
1270 * @orig_node: the originator for which the orig_list_entry has to be found
1271 *
1272 * retrieve the orig_tt_list_entry belonging to orig_node from the
1273 * batadv_tt_global_entry list
1274 *
1275 * Return: it with an increased refcounter, NULL if not found
1276 */
1277 static struct batadv_tt_orig_list_entry *
1278 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1279 const struct batadv_orig_node *orig_node)
1280 {
1281 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1282 const struct hlist_head *head;
1283
1284 rcu_read_lock();
1285 head = &entry->orig_list;
1286 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1287 if (tmp_orig_entry->orig_node != orig_node)
1288 continue;
1289 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1290 continue;
1291
1292 orig_entry = tmp_orig_entry;
1293 break;
1294 }
1295 rcu_read_unlock();
1296
1297 return orig_entry;
1298 }
1299
1300 /**
1301 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1302 * by a given originator
1303 * @entry: the TT global entry to check
1304 * @orig_node: the originator to search in the list
1305 *
1306 * find out if an orig_node is already in the list of a tt_global_entry.
1307 *
1308 * Return: true if found, false otherwise
1309 */
1310 static bool
1311 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1312 const struct batadv_orig_node *orig_node)
1313 {
1314 struct batadv_tt_orig_list_entry *orig_entry;
1315 bool found = false;
1316
1317 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1318 if (orig_entry) {
1319 found = true;
1320 batadv_tt_orig_list_entry_put(orig_entry);
1321 }
1322
1323 return found;
1324 }
1325
1326 static void
1327 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1328 struct batadv_orig_node *orig_node, int ttvn)
1329 {
1330 struct batadv_tt_orig_list_entry *orig_entry;
1331
1332 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1333 if (orig_entry) {
1334 /* refresh the ttvn: the current value could be a bogus one that
1335 * was added during a "temporary client detection"
1336 */
1337 orig_entry->ttvn = ttvn;
1338 goto out;
1339 }
1340
1341 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1342 if (!orig_entry)
1343 goto out;
1344
1345 INIT_HLIST_NODE(&orig_entry->list);
1346 kref_get(&orig_node->refcount);
1347 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1348 orig_entry->orig_node = orig_node;
1349 orig_entry->ttvn = ttvn;
1350 kref_init(&orig_entry->refcount);
1351 kref_get(&orig_entry->refcount);
1352
1353 spin_lock_bh(&tt_global->list_lock);
1354 hlist_add_head_rcu(&orig_entry->list,
1355 &tt_global->orig_list);
1356 spin_unlock_bh(&tt_global->list_lock);
1357 atomic_inc(&tt_global->orig_list_count);
1358
1359 out:
1360 if (orig_entry)
1361 batadv_tt_orig_list_entry_put(orig_entry);
1362 }
1363
1364 /**
1365 * batadv_tt_global_add - add a new TT global entry or update an existing one
1366 * @bat_priv: the bat priv with all the soft interface information
1367 * @orig_node: the originator announcing the client
1368 * @tt_addr: the mac address of the non-mesh client
1369 * @vid: VLAN identifier
1370 * @flags: TT flags that have to be set for this non-mesh client
1371 * @ttvn: the tt version number ever announcing this non-mesh client
1372 *
1373 * Add a new TT global entry for the given originator. If the entry already
1374 * exists add a new reference to the given originator (a global entry can have
1375 * references to multiple originators) and adjust the flags attribute to reflect
1376 * the function argument.
1377 * If a TT local entry exists for this non-mesh client remove it.
1378 *
1379 * The caller must hold orig_node refcount.
1380 *
1381 * Return: true if the new entry has been added, false otherwise
1382 */
1383 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1384 struct batadv_orig_node *orig_node,
1385 const unsigned char *tt_addr,
1386 unsigned short vid, u16 flags, u8 ttvn)
1387 {
1388 struct batadv_tt_global_entry *tt_global_entry;
1389 struct batadv_tt_local_entry *tt_local_entry;
1390 bool ret = false;
1391 int hash_added;
1392 struct batadv_tt_common_entry *common;
1393 u16 local_flags;
1394
1395 /* ignore global entries from backbone nodes */
1396 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1397 return true;
1398
1399 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1400 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1401
1402 /* if the node already has a local client for this entry, it has to wait
1403 * for a roaming advertisement instead of manually messing up the global
1404 * table
1405 */
1406 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1407 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1408 goto out;
1409
1410 if (!tt_global_entry) {
1411 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1412 if (!tt_global_entry)
1413 goto out;
1414
1415 common = &tt_global_entry->common;
1416 ether_addr_copy(common->addr, tt_addr);
1417 common->vid = vid;
1418
1419 common->flags = flags;
1420 tt_global_entry->roam_at = 0;
1421 /* node must store current time in case of roaming. This is
1422 * needed to purge this entry out on timeout (if nobody claims
1423 * it)
1424 */
1425 if (flags & BATADV_TT_CLIENT_ROAM)
1426 tt_global_entry->roam_at = jiffies;
1427 kref_init(&common->refcount);
1428 kref_get(&common->refcount);
1429 common->added_at = jiffies;
1430
1431 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1432 atomic_set(&tt_global_entry->orig_list_count, 0);
1433 spin_lock_init(&tt_global_entry->list_lock);
1434
1435 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1436 batadv_compare_tt,
1437 batadv_choose_tt, common,
1438 &common->hash_entry);
1439
1440 if (unlikely(hash_added != 0)) {
1441 /* remove the reference for the hash */
1442 batadv_tt_global_entry_put(tt_global_entry);
1443 goto out_remove;
1444 }
1445 } else {
1446 common = &tt_global_entry->common;
1447 /* If there is already a global entry, we can use this one for
1448 * our processing.
1449 * But if we are trying to add a temporary client then here are
1450 * two options at this point:
1451 * 1) the global client is not a temporary client: the global
1452 * client has to be left as it is, temporary information
1453 * should never override any already known client state
1454 * 2) the global client is a temporary client: purge the
1455 * originator list and add the new one orig_entry
1456 */
1457 if (flags & BATADV_TT_CLIENT_TEMP) {
1458 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1459 goto out;
1460 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1461 orig_node))
1462 goto out_remove;
1463 batadv_tt_global_del_orig_list(tt_global_entry);
1464 goto add_orig_entry;
1465 }
1466
1467 /* if the client was temporary added before receiving the first
1468 * OGM announcing it, we have to clear the TEMP flag. Also,
1469 * remove the previous temporary orig node and re-add it
1470 * if required. If the orig entry changed, the new one which
1471 * is a non-temporary entry is preferred.
1472 */
1473 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1474 batadv_tt_global_del_orig_list(tt_global_entry);
1475 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1476 }
1477
1478 /* the change can carry possible "attribute" flags like the
1479 * TT_CLIENT_WIFI, therefore they have to be copied in the
1480 * client entry
1481 */
1482 common->flags |= flags;
1483
1484 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1485 * one originator left in the list and we previously received a
1486 * delete + roaming change for this originator.
1487 *
1488 * We should first delete the old originator before adding the
1489 * new one.
1490 */
1491 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1492 batadv_tt_global_del_orig_list(tt_global_entry);
1493 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1494 tt_global_entry->roam_at = 0;
1495 }
1496 }
1497 add_orig_entry:
1498 /* add the new orig_entry (if needed) or update it */
1499 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1500
1501 batadv_dbg(BATADV_DBG_TT, bat_priv,
1502 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1503 common->addr, BATADV_PRINT_VID(common->vid),
1504 orig_node->orig);
1505 ret = true;
1506
1507 out_remove:
1508 /* Do not remove multicast addresses from the local hash on
1509 * global additions
1510 */
1511 if (is_multicast_ether_addr(tt_addr))
1512 goto out;
1513
1514 /* remove address from local hash if present */
1515 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1516 "global tt received",
1517 flags & BATADV_TT_CLIENT_ROAM);
1518 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1519
1520 if (!(flags & BATADV_TT_CLIENT_ROAM))
1521 /* this is a normal global add. Therefore the client is not in a
1522 * roaming state anymore.
1523 */
1524 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1525
1526 out:
1527 if (tt_global_entry)
1528 batadv_tt_global_entry_put(tt_global_entry);
1529 if (tt_local_entry)
1530 batadv_tt_local_entry_put(tt_local_entry);
1531 return ret;
1532 }
1533
1534 /**
1535 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1536 * @bat_priv: the bat priv with all the soft interface information
1537 * @tt_global_entry: global translation table entry to be analyzed
1538 *
1539 * This functon assumes the caller holds rcu_read_lock().
1540 * Return: best originator list entry or NULL on errors.
1541 */
1542 static struct batadv_tt_orig_list_entry *
1543 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1544 struct batadv_tt_global_entry *tt_global_entry)
1545 {
1546 struct batadv_neigh_node *router, *best_router = NULL;
1547 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1548 struct hlist_head *head;
1549 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1550
1551 head = &tt_global_entry->orig_list;
1552 hlist_for_each_entry_rcu(orig_entry, head, list) {
1553 router = batadv_orig_router_get(orig_entry->orig_node,
1554 BATADV_IF_DEFAULT);
1555 if (!router)
1556 continue;
1557
1558 if (best_router &&
1559 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1560 best_router, BATADV_IF_DEFAULT) <= 0) {
1561 batadv_neigh_node_put(router);
1562 continue;
1563 }
1564
1565 /* release the refcount for the "old" best */
1566 if (best_router)
1567 batadv_neigh_node_put(best_router);
1568
1569 best_entry = orig_entry;
1570 best_router = router;
1571 }
1572
1573 if (best_router)
1574 batadv_neigh_node_put(best_router);
1575
1576 return best_entry;
1577 }
1578
1579 /**
1580 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1581 * for this global entry
1582 * @bat_priv: the bat priv with all the soft interface information
1583 * @tt_global_entry: global translation table entry to be printed
1584 * @seq: debugfs table seq_file struct
1585 *
1586 * This functon assumes the caller holds rcu_read_lock().
1587 */
1588 static void
1589 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1590 struct batadv_tt_global_entry *tt_global_entry,
1591 struct seq_file *seq)
1592 {
1593 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1594 struct batadv_tt_common_entry *tt_common_entry;
1595 struct batadv_orig_node_vlan *vlan;
1596 struct hlist_head *head;
1597 u8 last_ttvn;
1598 u16 flags;
1599
1600 tt_common_entry = &tt_global_entry->common;
1601 flags = tt_common_entry->flags;
1602
1603 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1604 if (best_entry) {
1605 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1606 tt_common_entry->vid);
1607 if (!vlan) {
1608 seq_printf(seq,
1609 " * Cannot retrieve VLAN %d for originator %pM\n",
1610 BATADV_PRINT_VID(tt_common_entry->vid),
1611 best_entry->orig_node->orig);
1612 goto print_list;
1613 }
1614
1615 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1616 seq_printf(seq,
1617 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1618 '*', tt_global_entry->common.addr,
1619 BATADV_PRINT_VID(tt_global_entry->common.vid),
1620 best_entry->ttvn, best_entry->orig_node->orig,
1621 last_ttvn, vlan->tt.crc,
1622 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1623 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1624 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1625 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1626
1627 batadv_orig_node_vlan_put(vlan);
1628 }
1629
1630 print_list:
1631 head = &tt_global_entry->orig_list;
1632
1633 hlist_for_each_entry_rcu(orig_entry, head, list) {
1634 if (best_entry == orig_entry)
1635 continue;
1636
1637 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1638 tt_common_entry->vid);
1639 if (!vlan) {
1640 seq_printf(seq,
1641 " + Cannot retrieve VLAN %d for originator %pM\n",
1642 BATADV_PRINT_VID(tt_common_entry->vid),
1643 orig_entry->orig_node->orig);
1644 continue;
1645 }
1646
1647 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1648 seq_printf(seq,
1649 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1650 '+', tt_global_entry->common.addr,
1651 BATADV_PRINT_VID(tt_global_entry->common.vid),
1652 orig_entry->ttvn, orig_entry->orig_node->orig,
1653 last_ttvn, vlan->tt.crc,
1654 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1655 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1656 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1657 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1658
1659 batadv_orig_node_vlan_put(vlan);
1660 }
1661 }
1662
1663 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1664 {
1665 struct net_device *net_dev = (struct net_device *)seq->private;
1666 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1667 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1668 struct batadv_tt_common_entry *tt_common_entry;
1669 struct batadv_tt_global_entry *tt_global;
1670 struct batadv_hard_iface *primary_if;
1671 struct hlist_head *head;
1672 u32 i;
1673
1674 primary_if = batadv_seq_print_text_primary_if_get(seq);
1675 if (!primary_if)
1676 goto out;
1677
1678 seq_printf(seq,
1679 "Globally announced TT entries received via the mesh %s\n",
1680 net_dev->name);
1681 seq_puts(seq,
1682 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1683
1684 for (i = 0; i < hash->size; i++) {
1685 head = &hash->table[i];
1686
1687 rcu_read_lock();
1688 hlist_for_each_entry_rcu(tt_common_entry,
1689 head, hash_entry) {
1690 tt_global = container_of(tt_common_entry,
1691 struct batadv_tt_global_entry,
1692 common);
1693 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1694 }
1695 rcu_read_unlock();
1696 }
1697 out:
1698 if (primary_if)
1699 batadv_hardif_put(primary_if);
1700 return 0;
1701 }
1702
1703 /**
1704 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1705 * @tt_global_entry: the global entry to remove the orig_entry from
1706 * @orig_entry: the orig entry to remove and free
1707 *
1708 * Remove an orig_entry from its list in the given tt_global_entry and
1709 * free this orig_entry afterwards.
1710 *
1711 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1712 * part of a list.
1713 */
1714 static void
1715 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1716 struct batadv_tt_orig_list_entry *orig_entry)
1717 {
1718 lockdep_assert_held(&tt_global_entry->list_lock);
1719
1720 batadv_tt_global_size_dec(orig_entry->orig_node,
1721 tt_global_entry->common.vid);
1722 atomic_dec(&tt_global_entry->orig_list_count);
1723 /* requires holding tt_global_entry->list_lock and orig_entry->list
1724 * being part of a list
1725 */
1726 hlist_del_rcu(&orig_entry->list);
1727 batadv_tt_orig_list_entry_put(orig_entry);
1728 }
1729
1730 /* deletes the orig list of a tt_global_entry */
1731 static void
1732 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1733 {
1734 struct hlist_head *head;
1735 struct hlist_node *safe;
1736 struct batadv_tt_orig_list_entry *orig_entry;
1737
1738 spin_lock_bh(&tt_global_entry->list_lock);
1739 head = &tt_global_entry->orig_list;
1740 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1741 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1742 spin_unlock_bh(&tt_global_entry->list_lock);
1743 }
1744
1745 /**
1746 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1747 * @bat_priv: the bat priv with all the soft interface information
1748 * @tt_global_entry: the global entry to remove the orig_node from
1749 * @orig_node: the originator announcing the client
1750 * @message: message to append to the log on deletion
1751 *
1752 * Remove the given orig_node and its according orig_entry from the given
1753 * global tt entry.
1754 */
1755 static void
1756 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1757 struct batadv_tt_global_entry *tt_global_entry,
1758 struct batadv_orig_node *orig_node,
1759 const char *message)
1760 {
1761 struct hlist_head *head;
1762 struct hlist_node *safe;
1763 struct batadv_tt_orig_list_entry *orig_entry;
1764 unsigned short vid;
1765
1766 spin_lock_bh(&tt_global_entry->list_lock);
1767 head = &tt_global_entry->orig_list;
1768 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1769 if (orig_entry->orig_node == orig_node) {
1770 vid = tt_global_entry->common.vid;
1771 batadv_dbg(BATADV_DBG_TT, bat_priv,
1772 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1773 orig_node->orig,
1774 tt_global_entry->common.addr,
1775 BATADV_PRINT_VID(vid), message);
1776 _batadv_tt_global_del_orig_entry(tt_global_entry,
1777 orig_entry);
1778 }
1779 }
1780 spin_unlock_bh(&tt_global_entry->list_lock);
1781 }
1782
1783 /* If the client is to be deleted, we check if it is the last origantor entry
1784 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1785 * timer, otherwise we simply remove the originator scheduled for deletion.
1786 */
1787 static void
1788 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1789 struct batadv_tt_global_entry *tt_global_entry,
1790 struct batadv_orig_node *orig_node,
1791 const char *message)
1792 {
1793 bool last_entry = true;
1794 struct hlist_head *head;
1795 struct batadv_tt_orig_list_entry *orig_entry;
1796
1797 /* no local entry exists, case 1:
1798 * Check if this is the last one or if other entries exist.
1799 */
1800
1801 rcu_read_lock();
1802 head = &tt_global_entry->orig_list;
1803 hlist_for_each_entry_rcu(orig_entry, head, list) {
1804 if (orig_entry->orig_node != orig_node) {
1805 last_entry = false;
1806 break;
1807 }
1808 }
1809 rcu_read_unlock();
1810
1811 if (last_entry) {
1812 /* its the last one, mark for roaming. */
1813 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1814 tt_global_entry->roam_at = jiffies;
1815 } else
1816 /* there is another entry, we can simply delete this
1817 * one and can still use the other one.
1818 */
1819 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1820 orig_node, message);
1821 }
1822
1823 /**
1824 * batadv_tt_global_del - remove a client from the global table
1825 * @bat_priv: the bat priv with all the soft interface information
1826 * @orig_node: an originator serving this client
1827 * @addr: the mac address of the client
1828 * @vid: VLAN identifier
1829 * @message: a message explaining the reason for deleting the client to print
1830 * for debugging purpose
1831 * @roaming: true if the deletion has been triggered by a roaming event
1832 */
1833 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1834 struct batadv_orig_node *orig_node,
1835 const unsigned char *addr, unsigned short vid,
1836 const char *message, bool roaming)
1837 {
1838 struct batadv_tt_global_entry *tt_global_entry;
1839 struct batadv_tt_local_entry *local_entry = NULL;
1840
1841 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1842 if (!tt_global_entry)
1843 goto out;
1844
1845 if (!roaming) {
1846 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1847 orig_node, message);
1848
1849 if (hlist_empty(&tt_global_entry->orig_list))
1850 batadv_tt_global_free(bat_priv, tt_global_entry,
1851 message);
1852
1853 goto out;
1854 }
1855
1856 /* if we are deleting a global entry due to a roam
1857 * event, there are two possibilities:
1858 * 1) the client roamed from node A to node B => if there
1859 * is only one originator left for this client, we mark
1860 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1861 * wait for node B to claim it. In case of timeout
1862 * the entry is purged.
1863 *
1864 * If there are other originators left, we directly delete
1865 * the originator.
1866 * 2) the client roamed to us => we can directly delete
1867 * the global entry, since it is useless now.
1868 */
1869 local_entry = batadv_tt_local_hash_find(bat_priv,
1870 tt_global_entry->common.addr,
1871 vid);
1872 if (local_entry) {
1873 /* local entry exists, case 2: client roamed to us. */
1874 batadv_tt_global_del_orig_list(tt_global_entry);
1875 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1876 } else
1877 /* no local entry exists, case 1: check for roaming */
1878 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1879 orig_node, message);
1880
1881 out:
1882 if (tt_global_entry)
1883 batadv_tt_global_entry_put(tt_global_entry);
1884 if (local_entry)
1885 batadv_tt_local_entry_put(local_entry);
1886 }
1887
1888 /**
1889 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1890 * given originator matching the provided vid
1891 * @bat_priv: the bat priv with all the soft interface information
1892 * @orig_node: the originator owning the entries to remove
1893 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1894 * removed
1895 * @message: debug message to print as "reason"
1896 */
1897 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1898 struct batadv_orig_node *orig_node,
1899 s32 match_vid,
1900 const char *message)
1901 {
1902 struct batadv_tt_global_entry *tt_global;
1903 struct batadv_tt_common_entry *tt_common_entry;
1904 u32 i;
1905 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1906 struct hlist_node *safe;
1907 struct hlist_head *head;
1908 spinlock_t *list_lock; /* protects write access to the hash lists */
1909 unsigned short vid;
1910
1911 if (!hash)
1912 return;
1913
1914 for (i = 0; i < hash->size; i++) {
1915 head = &hash->table[i];
1916 list_lock = &hash->list_locks[i];
1917
1918 spin_lock_bh(list_lock);
1919 hlist_for_each_entry_safe(tt_common_entry, safe,
1920 head, hash_entry) {
1921 /* remove only matching entries */
1922 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1923 continue;
1924
1925 tt_global = container_of(tt_common_entry,
1926 struct batadv_tt_global_entry,
1927 common);
1928
1929 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1930 orig_node, message);
1931
1932 if (hlist_empty(&tt_global->orig_list)) {
1933 vid = tt_global->common.vid;
1934 batadv_dbg(BATADV_DBG_TT, bat_priv,
1935 "Deleting global tt entry %pM (vid: %d): %s\n",
1936 tt_global->common.addr,
1937 BATADV_PRINT_VID(vid), message);
1938 hlist_del_rcu(&tt_common_entry->hash_entry);
1939 batadv_tt_global_entry_put(tt_global);
1940 }
1941 }
1942 spin_unlock_bh(list_lock);
1943 }
1944 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1945 }
1946
1947 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1948 char **msg)
1949 {
1950 bool purge = false;
1951 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1952 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1953
1954 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1955 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1956 purge = true;
1957 *msg = "Roaming timeout\n";
1958 }
1959
1960 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1961 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1962 purge = true;
1963 *msg = "Temporary client timeout\n";
1964 }
1965
1966 return purge;
1967 }
1968
1969 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1970 {
1971 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1972 struct hlist_head *head;
1973 struct hlist_node *node_tmp;
1974 spinlock_t *list_lock; /* protects write access to the hash lists */
1975 u32 i;
1976 char *msg = NULL;
1977 struct batadv_tt_common_entry *tt_common;
1978 struct batadv_tt_global_entry *tt_global;
1979
1980 for (i = 0; i < hash->size; i++) {
1981 head = &hash->table[i];
1982 list_lock = &hash->list_locks[i];
1983
1984 spin_lock_bh(list_lock);
1985 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1986 hash_entry) {
1987 tt_global = container_of(tt_common,
1988 struct batadv_tt_global_entry,
1989 common);
1990
1991 if (!batadv_tt_global_to_purge(tt_global, &msg))
1992 continue;
1993
1994 batadv_dbg(BATADV_DBG_TT, bat_priv,
1995 "Deleting global tt entry %pM (vid: %d): %s\n",
1996 tt_global->common.addr,
1997 BATADV_PRINT_VID(tt_global->common.vid),
1998 msg);
1999
2000 hlist_del_rcu(&tt_common->hash_entry);
2001
2002 batadv_tt_global_entry_put(tt_global);
2003 }
2004 spin_unlock_bh(list_lock);
2005 }
2006 }
2007
2008 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2009 {
2010 struct batadv_hashtable *hash;
2011 spinlock_t *list_lock; /* protects write access to the hash lists */
2012 struct batadv_tt_common_entry *tt_common_entry;
2013 struct batadv_tt_global_entry *tt_global;
2014 struct hlist_node *node_tmp;
2015 struct hlist_head *head;
2016 u32 i;
2017
2018 if (!bat_priv->tt.global_hash)
2019 return;
2020
2021 hash = bat_priv->tt.global_hash;
2022
2023 for (i = 0; i < hash->size; i++) {
2024 head = &hash->table[i];
2025 list_lock = &hash->list_locks[i];
2026
2027 spin_lock_bh(list_lock);
2028 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2029 head, hash_entry) {
2030 hlist_del_rcu(&tt_common_entry->hash_entry);
2031 tt_global = container_of(tt_common_entry,
2032 struct batadv_tt_global_entry,
2033 common);
2034 batadv_tt_global_entry_put(tt_global);
2035 }
2036 spin_unlock_bh(list_lock);
2037 }
2038
2039 batadv_hash_destroy(hash);
2040
2041 bat_priv->tt.global_hash = NULL;
2042 }
2043
2044 static bool
2045 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2046 struct batadv_tt_global_entry *tt_global_entry)
2047 {
2048 bool ret = false;
2049
2050 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2051 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2052 ret = true;
2053
2054 /* check if the two clients are marked as isolated */
2055 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2056 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2057 ret = true;
2058
2059 return ret;
2060 }
2061
2062 /**
2063 * batadv_transtable_search - get the mesh destination for a given client
2064 * @bat_priv: the bat priv with all the soft interface information
2065 * @src: mac address of the source client
2066 * @addr: mac address of the destination client
2067 * @vid: VLAN identifier
2068 *
2069 * Return: a pointer to the originator that was selected as destination in the
2070 * mesh for contacting the client 'addr', NULL otherwise.
2071 * In case of multiple originators serving the same client, the function returns
2072 * the best one (best in terms of metric towards the destination node).
2073 *
2074 * If the two clients are AP isolated the function returns NULL.
2075 */
2076 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2077 const u8 *src,
2078 const u8 *addr,
2079 unsigned short vid)
2080 {
2081 struct batadv_tt_local_entry *tt_local_entry = NULL;
2082 struct batadv_tt_global_entry *tt_global_entry = NULL;
2083 struct batadv_orig_node *orig_node = NULL;
2084 struct batadv_tt_orig_list_entry *best_entry;
2085
2086 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2087 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2088 if (!tt_local_entry ||
2089 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2090 goto out;
2091 }
2092
2093 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2094 if (!tt_global_entry)
2095 goto out;
2096
2097 /* check whether the clients should not communicate due to AP
2098 * isolation
2099 */
2100 if (tt_local_entry &&
2101 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2102 goto out;
2103
2104 rcu_read_lock();
2105 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2106 /* found anything? */
2107 if (best_entry)
2108 orig_node = best_entry->orig_node;
2109 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2110 orig_node = NULL;
2111 rcu_read_unlock();
2112
2113 out:
2114 if (tt_global_entry)
2115 batadv_tt_global_entry_put(tt_global_entry);
2116 if (tt_local_entry)
2117 batadv_tt_local_entry_put(tt_local_entry);
2118
2119 return orig_node;
2120 }
2121
2122 /**
2123 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2124 * to the given orig_node
2125 * @bat_priv: the bat priv with all the soft interface information
2126 * @orig_node: originator for which the CRC should be computed
2127 * @vid: VLAN identifier for which the CRC32 has to be computed
2128 *
2129 * This function computes the checksum for the global table corresponding to a
2130 * specific originator. In particular, the checksum is computed as follows: For
2131 * each client connected to the originator the CRC32C of the MAC address and the
2132 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2133 * together.
2134 *
2135 * The idea behind is that CRC32C should be used as much as possible in order to
2136 * produce a unique hash of the table, but since the order which is used to feed
2137 * the CRC32C function affects the result and since every node in the network
2138 * probably sorts the clients differently, the hash function cannot be directly
2139 * computed over the entire table. Hence the CRC32C is used only on
2140 * the single client entry, while all the results are then xor'ed together
2141 * because the XOR operation can combine them all while trying to reduce the
2142 * noise as much as possible.
2143 *
2144 * Return: the checksum of the global table of a given originator.
2145 */
2146 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2147 struct batadv_orig_node *orig_node,
2148 unsigned short vid)
2149 {
2150 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2151 struct batadv_tt_common_entry *tt_common;
2152 struct batadv_tt_global_entry *tt_global;
2153 struct hlist_head *head;
2154 u32 i, crc_tmp, crc = 0;
2155 u8 flags;
2156 __be16 tmp_vid;
2157
2158 for (i = 0; i < hash->size; i++) {
2159 head = &hash->table[i];
2160
2161 rcu_read_lock();
2162 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2163 tt_global = container_of(tt_common,
2164 struct batadv_tt_global_entry,
2165 common);
2166 /* compute the CRC only for entries belonging to the
2167 * VLAN identified by the vid passed as parameter
2168 */
2169 if (tt_common->vid != vid)
2170 continue;
2171
2172 /* Roaming clients are in the global table for
2173 * consistency only. They don't have to be
2174 * taken into account while computing the
2175 * global crc
2176 */
2177 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2178 continue;
2179 /* Temporary clients have not been announced yet, so
2180 * they have to be skipped while computing the global
2181 * crc
2182 */
2183 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2184 continue;
2185
2186 /* find out if this global entry is announced by this
2187 * originator
2188 */
2189 if (!batadv_tt_global_entry_has_orig(tt_global,
2190 orig_node))
2191 continue;
2192
2193 /* use network order to read the VID: this ensures that
2194 * every node reads the bytes in the same order.
2195 */
2196 tmp_vid = htons(tt_common->vid);
2197 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2198
2199 /* compute the CRC on flags that have to be kept in sync
2200 * among nodes
2201 */
2202 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2203 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2204
2205 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2206 }
2207 rcu_read_unlock();
2208 }
2209
2210 return crc;
2211 }
2212
2213 /**
2214 * batadv_tt_local_crc - calculates the checksum of the local table
2215 * @bat_priv: the bat priv with all the soft interface information
2216 * @vid: VLAN identifier for which the CRC32 has to be computed
2217 *
2218 * For details about the computation, please refer to the documentation for
2219 * batadv_tt_global_crc().
2220 *
2221 * Return: the checksum of the local table
2222 */
2223 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2224 unsigned short vid)
2225 {
2226 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2227 struct batadv_tt_common_entry *tt_common;
2228 struct hlist_head *head;
2229 u32 i, crc_tmp, crc = 0;
2230 u8 flags;
2231 __be16 tmp_vid;
2232
2233 for (i = 0; i < hash->size; i++) {
2234 head = &hash->table[i];
2235
2236 rcu_read_lock();
2237 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2238 /* compute the CRC only for entries belonging to the
2239 * VLAN identified by vid
2240 */
2241 if (tt_common->vid != vid)
2242 continue;
2243
2244 /* not yet committed clients have not to be taken into
2245 * account while computing the CRC
2246 */
2247 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2248 continue;
2249
2250 /* use network order to read the VID: this ensures that
2251 * every node reads the bytes in the same order.
2252 */
2253 tmp_vid = htons(tt_common->vid);
2254 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2255
2256 /* compute the CRC on flags that have to be kept in sync
2257 * among nodes
2258 */
2259 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2260 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2261
2262 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2263 }
2264 rcu_read_unlock();
2265 }
2266
2267 return crc;
2268 }
2269
2270 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2271 {
2272 struct batadv_tt_req_node *node;
2273 struct hlist_node *safe;
2274
2275 spin_lock_bh(&bat_priv->tt.req_list_lock);
2276
2277 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2278 hlist_del_init(&node->list);
2279 kfree(node);
2280 }
2281
2282 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2283 }
2284
2285 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2286 struct batadv_orig_node *orig_node,
2287 const void *tt_buff,
2288 u16 tt_buff_len)
2289 {
2290 /* Replace the old buffer only if I received something in the
2291 * last OGM (the OGM could carry no changes)
2292 */
2293 spin_lock_bh(&orig_node->tt_buff_lock);
2294 if (tt_buff_len > 0) {
2295 kfree(orig_node->tt_buff);
2296 orig_node->tt_buff_len = 0;
2297 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2298 if (orig_node->tt_buff) {
2299 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2300 orig_node->tt_buff_len = tt_buff_len;
2301 }
2302 }
2303 spin_unlock_bh(&orig_node->tt_buff_lock);
2304 }
2305
2306 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2307 {
2308 struct batadv_tt_req_node *node;
2309 struct hlist_node *safe;
2310
2311 spin_lock_bh(&bat_priv->tt.req_list_lock);
2312 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2313 if (batadv_has_timed_out(node->issued_at,
2314 BATADV_TT_REQUEST_TIMEOUT)) {
2315 hlist_del_init(&node->list);
2316 kfree(node);
2317 }
2318 }
2319 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2320 }
2321
2322 /**
2323 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2324 * @bat_priv: the bat priv with all the soft interface information
2325 * @orig_node: orig node this request is being issued for
2326 *
2327 * Return: the pointer to the new tt_req_node struct if no request
2328 * has already been issued for this orig_node, NULL otherwise.
2329 */
2330 static struct batadv_tt_req_node *
2331 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2332 struct batadv_orig_node *orig_node)
2333 {
2334 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2335
2336 spin_lock_bh(&bat_priv->tt.req_list_lock);
2337 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2338 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2339 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2340 BATADV_TT_REQUEST_TIMEOUT))
2341 goto unlock;
2342 }
2343
2344 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2345 if (!tt_req_node)
2346 goto unlock;
2347
2348 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2349 tt_req_node->issued_at = jiffies;
2350
2351 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2352 unlock:
2353 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2354 return tt_req_node;
2355 }
2356
2357 /**
2358 * batadv_tt_local_valid - verify that given tt entry is a valid one
2359 * @entry_ptr: to be checked local tt entry
2360 * @data_ptr: not used but definition required to satisfy the callback prototype
2361 *
2362 * Return: true if the entry is a valid, false otherwise.
2363 */
2364 static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2365 {
2366 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2367
2368 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2369 return false;
2370 return true;
2371 }
2372
2373 static bool batadv_tt_global_valid(const void *entry_ptr,
2374 const void *data_ptr)
2375 {
2376 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2377 const struct batadv_tt_global_entry *tt_global_entry;
2378 const struct batadv_orig_node *orig_node = data_ptr;
2379
2380 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2381 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2382 return false;
2383
2384 tt_global_entry = container_of(tt_common_entry,
2385 struct batadv_tt_global_entry,
2386 common);
2387
2388 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2389 }
2390
2391 /**
2392 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2393 * specified tt hash
2394 * @bat_priv: the bat priv with all the soft interface information
2395 * @hash: hash table containing the tt entries
2396 * @tt_len: expected tvlv tt data buffer length in number of bytes
2397 * @tvlv_buff: pointer to the buffer to fill with the TT data
2398 * @valid_cb: function to filter tt change entries
2399 * @cb_data: data passed to the filter function as argument
2400 */
2401 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2402 struct batadv_hashtable *hash,
2403 void *tvlv_buff, u16 tt_len,
2404 bool (*valid_cb)(const void *,
2405 const void *),
2406 void *cb_data)
2407 {
2408 struct batadv_tt_common_entry *tt_common_entry;
2409 struct batadv_tvlv_tt_change *tt_change;
2410 struct hlist_head *head;
2411 u16 tt_tot, tt_num_entries = 0;
2412 u32 i;
2413
2414 tt_tot = batadv_tt_entries(tt_len);
2415 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2416
2417 rcu_read_lock();
2418 for (i = 0; i < hash->size; i++) {
2419 head = &hash->table[i];
2420
2421 hlist_for_each_entry_rcu(tt_common_entry,
2422 head, hash_entry) {
2423 if (tt_tot == tt_num_entries)
2424 break;
2425
2426 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2427 continue;
2428
2429 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2430 tt_change->flags = tt_common_entry->flags;
2431 tt_change->vid = htons(tt_common_entry->vid);
2432 memset(tt_change->reserved, 0,
2433 sizeof(tt_change->reserved));
2434
2435 tt_num_entries++;
2436 tt_change++;
2437 }
2438 }
2439 rcu_read_unlock();
2440 }
2441
2442 /**
2443 * batadv_tt_global_check_crc - check if all the CRCs are correct
2444 * @orig_node: originator for which the CRCs have to be checked
2445 * @tt_vlan: pointer to the first tvlv VLAN entry
2446 * @num_vlan: number of tvlv VLAN entries
2447 *
2448 * Return: true if all the received CRCs match the locally stored ones, false
2449 * otherwise
2450 */
2451 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2452 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2453 u16 num_vlan)
2454 {
2455 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2456 struct batadv_orig_node_vlan *vlan;
2457 int i, orig_num_vlan;
2458 u32 crc;
2459
2460 /* check if each received CRC matches the locally stored one */
2461 for (i = 0; i < num_vlan; i++) {
2462 tt_vlan_tmp = tt_vlan + i;
2463
2464 /* if orig_node is a backbone node for this VLAN, don't check
2465 * the CRC as we ignore all the global entries over it
2466 */
2467 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2468 orig_node->orig,
2469 ntohs(tt_vlan_tmp->vid)))
2470 continue;
2471
2472 vlan = batadv_orig_node_vlan_get(orig_node,
2473 ntohs(tt_vlan_tmp->vid));
2474 if (!vlan)
2475 return false;
2476
2477 crc = vlan->tt.crc;
2478 batadv_orig_node_vlan_put(vlan);
2479
2480 if (crc != ntohl(tt_vlan_tmp->crc))
2481 return false;
2482 }
2483
2484 /* check if any excess VLANs exist locally for the originator
2485 * which are not mentioned in the TVLV from the originator.
2486 */
2487 rcu_read_lock();
2488 orig_num_vlan = 0;
2489 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2490 orig_num_vlan++;
2491 rcu_read_unlock();
2492
2493 if (orig_num_vlan > num_vlan)
2494 return false;
2495
2496 return true;
2497 }
2498
2499 /**
2500 * batadv_tt_local_update_crc - update all the local CRCs
2501 * @bat_priv: the bat priv with all the soft interface information
2502 */
2503 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2504 {
2505 struct batadv_softif_vlan *vlan;
2506
2507 /* recompute the global CRC for each VLAN */
2508 rcu_read_lock();
2509 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2510 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2511 }
2512 rcu_read_unlock();
2513 }
2514
2515 /**
2516 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2517 * @bat_priv: the bat priv with all the soft interface information
2518 * @orig_node: the orig_node for which the CRCs have to be updated
2519 */
2520 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2521 struct batadv_orig_node *orig_node)
2522 {
2523 struct batadv_orig_node_vlan *vlan;
2524 u32 crc;
2525
2526 /* recompute the global CRC for each VLAN */
2527 rcu_read_lock();
2528 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2529 /* if orig_node is a backbone node for this VLAN, don't compute
2530 * the CRC as we ignore all the global entries over it
2531 */
2532 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2533 vlan->vid))
2534 continue;
2535
2536 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2537 vlan->tt.crc = crc;
2538 }
2539 rcu_read_unlock();
2540 }
2541
2542 /**
2543 * batadv_send_tt_request - send a TT Request message to a given node
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @dst_orig_node: the destination of the message
2546 * @ttvn: the version number that the source of the message is looking for
2547 * @tt_vlan: pointer to the first tvlv VLAN object to request
2548 * @num_vlan: number of tvlv VLAN entries
2549 * @full_table: ask for the entire translation table if true, while only for the
2550 * last TT diff otherwise
2551 *
2552 * Return: true if the TT Request was sent, false otherwise
2553 */
2554 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2555 struct batadv_orig_node *dst_orig_node,
2556 u8 ttvn,
2557 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2558 u16 num_vlan, bool full_table)
2559 {
2560 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2561 struct batadv_tt_req_node *tt_req_node = NULL;
2562 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2563 struct batadv_hard_iface *primary_if;
2564 bool ret = false;
2565 int i, size;
2566
2567 primary_if = batadv_primary_if_get_selected(bat_priv);
2568 if (!primary_if)
2569 goto out;
2570
2571 /* The new tt_req will be issued only if I'm not waiting for a
2572 * reply from the same orig_node yet
2573 */
2574 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2575 if (!tt_req_node)
2576 goto out;
2577
2578 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2579 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2580 if (!tvlv_tt_data)
2581 goto out;
2582
2583 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2584 tvlv_tt_data->ttvn = ttvn;
2585 tvlv_tt_data->num_vlan = htons(num_vlan);
2586
2587 /* send all the CRCs within the request. This is needed by intermediate
2588 * nodes to ensure they have the correct table before replying
2589 */
2590 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2591 for (i = 0; i < num_vlan; i++) {
2592 tt_vlan_req->vid = tt_vlan->vid;
2593 tt_vlan_req->crc = tt_vlan->crc;
2594
2595 tt_vlan_req++;
2596 tt_vlan++;
2597 }
2598
2599 if (full_table)
2600 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2601
2602 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2603 dst_orig_node->orig, full_table ? 'F' : '.');
2604
2605 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2606 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2607 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2608 tvlv_tt_data, size);
2609 ret = true;
2610
2611 out:
2612 if (primary_if)
2613 batadv_hardif_put(primary_if);
2614 if (ret && tt_req_node) {
2615 spin_lock_bh(&bat_priv->tt.req_list_lock);
2616 /* hlist_del_init() verifies tt_req_node still is in the list */
2617 hlist_del_init(&tt_req_node->list);
2618 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2619 kfree(tt_req_node);
2620 }
2621 kfree(tvlv_tt_data);
2622 return ret;
2623 }
2624
2625 /**
2626 * batadv_send_other_tt_response - send reply to tt request concerning another
2627 * node's translation table
2628 * @bat_priv: the bat priv with all the soft interface information
2629 * @tt_data: tt data containing the tt request information
2630 * @req_src: mac address of tt request sender
2631 * @req_dst: mac address of tt request recipient
2632 *
2633 * Return: true if tt request reply was sent, false otherwise.
2634 */
2635 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2636 struct batadv_tvlv_tt_data *tt_data,
2637 u8 *req_src, u8 *req_dst)
2638 {
2639 struct batadv_orig_node *req_dst_orig_node;
2640 struct batadv_orig_node *res_dst_orig_node = NULL;
2641 struct batadv_tvlv_tt_change *tt_change;
2642 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2643 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2644 bool ret = false, full_table;
2645 u8 orig_ttvn, req_ttvn;
2646 u16 tvlv_len;
2647 s32 tt_len;
2648
2649 batadv_dbg(BATADV_DBG_TT, bat_priv,
2650 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2651 req_src, tt_data->ttvn, req_dst,
2652 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2653
2654 /* Let's get the orig node of the REAL destination */
2655 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2656 if (!req_dst_orig_node)
2657 goto out;
2658
2659 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2660 if (!res_dst_orig_node)
2661 goto out;
2662
2663 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2664 req_ttvn = tt_data->ttvn;
2665
2666 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2667 /* this node doesn't have the requested data */
2668 if (orig_ttvn != req_ttvn ||
2669 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2670 ntohs(tt_data->num_vlan)))
2671 goto out;
2672
2673 /* If the full table has been explicitly requested */
2674 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2675 !req_dst_orig_node->tt_buff)
2676 full_table = true;
2677 else
2678 full_table = false;
2679
2680 /* TT fragmentation hasn't been implemented yet, so send as many
2681 * TT entries fit a single packet as possible only
2682 */
2683 if (!full_table) {
2684 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2685 tt_len = req_dst_orig_node->tt_buff_len;
2686
2687 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2688 &tvlv_tt_data,
2689 &tt_change,
2690 &tt_len);
2691 if (!tt_len)
2692 goto unlock;
2693
2694 /* Copy the last orig_node's OGM buffer */
2695 memcpy(tt_change, req_dst_orig_node->tt_buff,
2696 req_dst_orig_node->tt_buff_len);
2697 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2698 } else {
2699 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2700 * in the initial part
2701 */
2702 tt_len = -1;
2703 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2704 &tvlv_tt_data,
2705 &tt_change,
2706 &tt_len);
2707 if (!tt_len)
2708 goto out;
2709
2710 /* fill the rest of the tvlv with the real TT entries */
2711 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2712 tt_change, tt_len,
2713 batadv_tt_global_valid,
2714 req_dst_orig_node);
2715 }
2716
2717 /* Don't send the response, if larger than fragmented packet. */
2718 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2719 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2720 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2721 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2722 res_dst_orig_node->orig);
2723 goto out;
2724 }
2725
2726 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2727 tvlv_tt_data->ttvn = req_ttvn;
2728
2729 if (full_table)
2730 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2731
2732 batadv_dbg(BATADV_DBG_TT, bat_priv,
2733 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2734 res_dst_orig_node->orig, req_dst_orig_node->orig,
2735 full_table ? 'F' : '.', req_ttvn);
2736
2737 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2738
2739 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2740 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2741 tvlv_len);
2742
2743 ret = true;
2744 goto out;
2745
2746 unlock:
2747 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2748
2749 out:
2750 if (res_dst_orig_node)
2751 batadv_orig_node_put(res_dst_orig_node);
2752 if (req_dst_orig_node)
2753 batadv_orig_node_put(req_dst_orig_node);
2754 kfree(tvlv_tt_data);
2755 return ret;
2756 }
2757
2758 /**
2759 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2760 * translation table
2761 * @bat_priv: the bat priv with all the soft interface information
2762 * @tt_data: tt data containing the tt request information
2763 * @req_src: mac address of tt request sender
2764 *
2765 * Return: true if tt request reply was sent, false otherwise.
2766 */
2767 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2768 struct batadv_tvlv_tt_data *tt_data,
2769 u8 *req_src)
2770 {
2771 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2772 struct batadv_hard_iface *primary_if = NULL;
2773 struct batadv_tvlv_tt_change *tt_change;
2774 struct batadv_orig_node *orig_node;
2775 u8 my_ttvn, req_ttvn;
2776 u16 tvlv_len;
2777 bool full_table;
2778 s32 tt_len;
2779
2780 batadv_dbg(BATADV_DBG_TT, bat_priv,
2781 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2782 req_src, tt_data->ttvn,
2783 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2784
2785 spin_lock_bh(&bat_priv->tt.commit_lock);
2786
2787 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2788 req_ttvn = tt_data->ttvn;
2789
2790 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2791 if (!orig_node)
2792 goto out;
2793
2794 primary_if = batadv_primary_if_get_selected(bat_priv);
2795 if (!primary_if)
2796 goto out;
2797
2798 /* If the full table has been explicitly requested or the gap
2799 * is too big send the whole local translation table
2800 */
2801 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2802 !bat_priv->tt.last_changeset)
2803 full_table = true;
2804 else
2805 full_table = false;
2806
2807 /* TT fragmentation hasn't been implemented yet, so send as many
2808 * TT entries fit a single packet as possible only
2809 */
2810 if (!full_table) {
2811 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2812
2813 tt_len = bat_priv->tt.last_changeset_len;
2814 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2815 &tvlv_tt_data,
2816 &tt_change,
2817 &tt_len);
2818 if (!tt_len)
2819 goto unlock;
2820
2821 /* Copy the last orig_node's OGM buffer */
2822 memcpy(tt_change, bat_priv->tt.last_changeset,
2823 bat_priv->tt.last_changeset_len);
2824 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2825 } else {
2826 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2827
2828 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2829 * in the initial part
2830 */
2831 tt_len = -1;
2832 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2833 &tvlv_tt_data,
2834 &tt_change,
2835 &tt_len);
2836 if (!tt_len)
2837 goto out;
2838
2839 /* fill the rest of the tvlv with the real TT entries */
2840 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2841 tt_change, tt_len,
2842 batadv_tt_local_valid, NULL);
2843 }
2844
2845 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2846 tvlv_tt_data->ttvn = req_ttvn;
2847
2848 if (full_table)
2849 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2850
2851 batadv_dbg(BATADV_DBG_TT, bat_priv,
2852 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2853 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2854
2855 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2856
2857 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2858 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2859 tvlv_len);
2860
2861 goto out;
2862
2863 unlock:
2864 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2865 out:
2866 spin_unlock_bh(&bat_priv->tt.commit_lock);
2867 if (orig_node)
2868 batadv_orig_node_put(orig_node);
2869 if (primary_if)
2870 batadv_hardif_put(primary_if);
2871 kfree(tvlv_tt_data);
2872 /* The packet was for this host, so it doesn't need to be re-routed */
2873 return true;
2874 }
2875
2876 /**
2877 * batadv_send_tt_response - send reply to tt request
2878 * @bat_priv: the bat priv with all the soft interface information
2879 * @tt_data: tt data containing the tt request information
2880 * @req_src: mac address of tt request sender
2881 * @req_dst: mac address of tt request recipient
2882 *
2883 * Return: true if tt request reply was sent, false otherwise.
2884 */
2885 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2886 struct batadv_tvlv_tt_data *tt_data,
2887 u8 *req_src, u8 *req_dst)
2888 {
2889 if (batadv_is_my_mac(bat_priv, req_dst))
2890 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2891 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2892 req_dst);
2893 }
2894
2895 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2896 struct batadv_orig_node *orig_node,
2897 struct batadv_tvlv_tt_change *tt_change,
2898 u16 tt_num_changes, u8 ttvn)
2899 {
2900 int i;
2901 int roams;
2902
2903 for (i = 0; i < tt_num_changes; i++) {
2904 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2905 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2906 batadv_tt_global_del(bat_priv, orig_node,
2907 (tt_change + i)->addr,
2908 ntohs((tt_change + i)->vid),
2909 "tt removed by changes",
2910 roams);
2911 } else {
2912 if (!batadv_tt_global_add(bat_priv, orig_node,
2913 (tt_change + i)->addr,
2914 ntohs((tt_change + i)->vid),
2915 (tt_change + i)->flags, ttvn))
2916 /* In case of problem while storing a
2917 * global_entry, we stop the updating
2918 * procedure without committing the
2919 * ttvn change. This will avoid to send
2920 * corrupted data on tt_request
2921 */
2922 return;
2923 }
2924 }
2925 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2926 }
2927
2928 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2929 struct batadv_tvlv_tt_change *tt_change,
2930 u8 ttvn, u8 *resp_src,
2931 u16 num_entries)
2932 {
2933 struct batadv_orig_node *orig_node;
2934
2935 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2936 if (!orig_node)
2937 goto out;
2938
2939 /* Purge the old table first.. */
2940 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2941 "Received full table");
2942
2943 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2944 ttvn);
2945
2946 spin_lock_bh(&orig_node->tt_buff_lock);
2947 kfree(orig_node->tt_buff);
2948 orig_node->tt_buff_len = 0;
2949 orig_node->tt_buff = NULL;
2950 spin_unlock_bh(&orig_node->tt_buff_lock);
2951
2952 atomic_set(&orig_node->last_ttvn, ttvn);
2953
2954 out:
2955 if (orig_node)
2956 batadv_orig_node_put(orig_node);
2957 }
2958
2959 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2960 struct batadv_orig_node *orig_node,
2961 u16 tt_num_changes, u8 ttvn,
2962 struct batadv_tvlv_tt_change *tt_change)
2963 {
2964 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2965 tt_num_changes, ttvn);
2966
2967 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2968 batadv_tt_len(tt_num_changes));
2969 atomic_set(&orig_node->last_ttvn, ttvn);
2970 }
2971
2972 /**
2973 * batadv_is_my_client - check if a client is served by the local node
2974 * @bat_priv: the bat priv with all the soft interface information
2975 * @addr: the mac address of the client to check
2976 * @vid: VLAN identifier
2977 *
2978 * Return: true if the client is served by this node, false otherwise.
2979 */
2980 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
2981 unsigned short vid)
2982 {
2983 struct batadv_tt_local_entry *tt_local_entry;
2984 bool ret = false;
2985
2986 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2987 if (!tt_local_entry)
2988 goto out;
2989 /* Check if the client has been logically deleted (but is kept for
2990 * consistency purpose)
2991 */
2992 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2993 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2994 goto out;
2995 ret = true;
2996 out:
2997 if (tt_local_entry)
2998 batadv_tt_local_entry_put(tt_local_entry);
2999 return ret;
3000 }
3001
3002 /**
3003 * batadv_handle_tt_response - process incoming tt reply
3004 * @bat_priv: the bat priv with all the soft interface information
3005 * @tt_data: tt data containing the tt request information
3006 * @resp_src: mac address of tt reply sender
3007 * @num_entries: number of tt change entries appended to the tt data
3008 */
3009 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3010 struct batadv_tvlv_tt_data *tt_data,
3011 u8 *resp_src, u16 num_entries)
3012 {
3013 struct batadv_tt_req_node *node;
3014 struct hlist_node *safe;
3015 struct batadv_orig_node *orig_node = NULL;
3016 struct batadv_tvlv_tt_change *tt_change;
3017 u8 *tvlv_ptr = (u8 *)tt_data;
3018 u16 change_offset;
3019
3020 batadv_dbg(BATADV_DBG_TT, bat_priv,
3021 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3022 resp_src, tt_data->ttvn, num_entries,
3023 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3024
3025 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3026 if (!orig_node)
3027 goto out;
3028
3029 spin_lock_bh(&orig_node->tt_lock);
3030
3031 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3032 change_offset *= ntohs(tt_data->num_vlan);
3033 change_offset += sizeof(*tt_data);
3034 tvlv_ptr += change_offset;
3035
3036 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3037 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3038 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3039 resp_src, num_entries);
3040 } else {
3041 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3042 tt_data->ttvn, tt_change);
3043 }
3044
3045 /* Recalculate the CRC for this orig_node and store it */
3046 batadv_tt_global_update_crc(bat_priv, orig_node);
3047
3048 spin_unlock_bh(&orig_node->tt_lock);
3049
3050 /* Delete the tt_req_node from pending tt_requests list */
3051 spin_lock_bh(&bat_priv->tt.req_list_lock);
3052 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3053 if (!batadv_compare_eth(node->addr, resp_src))
3054 continue;
3055 hlist_del_init(&node->list);
3056 kfree(node);
3057 }
3058
3059 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3060 out:
3061 if (orig_node)
3062 batadv_orig_node_put(orig_node);
3063 }
3064
3065 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3066 {
3067 struct batadv_tt_roam_node *node, *safe;
3068
3069 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3070
3071 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3072 list_del(&node->list);
3073 kfree(node);
3074 }
3075
3076 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3077 }
3078
3079 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3080 {
3081 struct batadv_tt_roam_node *node, *safe;
3082
3083 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3084 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3085 if (!batadv_has_timed_out(node->first_time,
3086 BATADV_ROAMING_MAX_TIME))
3087 continue;
3088
3089 list_del(&node->list);
3090 kfree(node);
3091 }
3092 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3093 }
3094
3095 /**
3096 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3097 * @bat_priv: the bat priv with all the soft interface information
3098 * @client: mac address of the roaming client
3099 *
3100 * This function checks whether the client already reached the
3101 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3102 * will not be sent.
3103 *
3104 * Return: true if the ROAMING_ADV can be sent, false otherwise
3105 */
3106 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3107 {
3108 struct batadv_tt_roam_node *tt_roam_node;
3109 bool ret = false;
3110
3111 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3112 /* The new tt_req will be issued only if I'm not waiting for a
3113 * reply from the same orig_node yet
3114 */
3115 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3116 if (!batadv_compare_eth(tt_roam_node->addr, client))
3117 continue;
3118
3119 if (batadv_has_timed_out(tt_roam_node->first_time,
3120 BATADV_ROAMING_MAX_TIME))
3121 continue;
3122
3123 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3124 /* Sorry, you roamed too many times! */
3125 goto unlock;
3126 ret = true;
3127 break;
3128 }
3129
3130 if (!ret) {
3131 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3132 if (!tt_roam_node)
3133 goto unlock;
3134
3135 tt_roam_node->first_time = jiffies;
3136 atomic_set(&tt_roam_node->counter,
3137 BATADV_ROAMING_MAX_COUNT - 1);
3138 ether_addr_copy(tt_roam_node->addr, client);
3139
3140 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3141 ret = true;
3142 }
3143
3144 unlock:
3145 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3146 return ret;
3147 }
3148
3149 /**
3150 * batadv_send_roam_adv - send a roaming advertisement message
3151 * @bat_priv: the bat priv with all the soft interface information
3152 * @client: mac address of the roaming client
3153 * @vid: VLAN identifier
3154 * @orig_node: message destination
3155 *
3156 * Send a ROAMING_ADV message to the node which was previously serving this
3157 * client. This is done to inform the node that from now on all traffic destined
3158 * for this particular roamed client has to be forwarded to the sender of the
3159 * roaming message.
3160 */
3161 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3162 unsigned short vid,
3163 struct batadv_orig_node *orig_node)
3164 {
3165 struct batadv_hard_iface *primary_if;
3166 struct batadv_tvlv_roam_adv tvlv_roam;
3167
3168 primary_if = batadv_primary_if_get_selected(bat_priv);
3169 if (!primary_if)
3170 goto out;
3171
3172 /* before going on we have to check whether the client has
3173 * already roamed to us too many times
3174 */
3175 if (!batadv_tt_check_roam_count(bat_priv, client))
3176 goto out;
3177
3178 batadv_dbg(BATADV_DBG_TT, bat_priv,
3179 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3180 orig_node->orig, client, BATADV_PRINT_VID(vid));
3181
3182 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3183
3184 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3185 tvlv_roam.vid = htons(vid);
3186
3187 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3188 orig_node->orig, BATADV_TVLV_ROAM, 1,
3189 &tvlv_roam, sizeof(tvlv_roam));
3190
3191 out:
3192 if (primary_if)
3193 batadv_hardif_put(primary_if);
3194 }
3195
3196 static void batadv_tt_purge(struct work_struct *work)
3197 {
3198 struct delayed_work *delayed_work;
3199 struct batadv_priv_tt *priv_tt;
3200 struct batadv_priv *bat_priv;
3201
3202 delayed_work = to_delayed_work(work);
3203 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3204 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3205
3206 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3207 batadv_tt_global_purge(bat_priv);
3208 batadv_tt_req_purge(bat_priv);
3209 batadv_tt_roam_purge(bat_priv);
3210
3211 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3212 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3213 }
3214
3215 void batadv_tt_free(struct batadv_priv *bat_priv)
3216 {
3217 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3218 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3219
3220 cancel_delayed_work_sync(&bat_priv->tt.work);
3221
3222 batadv_tt_local_table_free(bat_priv);
3223 batadv_tt_global_table_free(bat_priv);
3224 batadv_tt_req_list_free(bat_priv);
3225 batadv_tt_changes_list_free(bat_priv);
3226 batadv_tt_roam_list_free(bat_priv);
3227
3228 kfree(bat_priv->tt.last_changeset);
3229 }
3230
3231 /**
3232 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3233 * table and possibly count them in the TT size
3234 * @bat_priv: the bat priv with all the soft interface information
3235 * @flags: the flag to switch
3236 * @enable: whether to set or unset the flag
3237 * @count: whether to increase the TT size by the number of changed entries
3238 */
3239 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3240 bool enable, bool count)
3241 {
3242 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3243 struct batadv_tt_common_entry *tt_common_entry;
3244 u16 changed_num = 0;
3245 struct hlist_head *head;
3246 u32 i;
3247
3248 if (!hash)
3249 return;
3250
3251 for (i = 0; i < hash->size; i++) {
3252 head = &hash->table[i];
3253
3254 rcu_read_lock();
3255 hlist_for_each_entry_rcu(tt_common_entry,
3256 head, hash_entry) {
3257 if (enable) {
3258 if ((tt_common_entry->flags & flags) == flags)
3259 continue;
3260 tt_common_entry->flags |= flags;
3261 } else {
3262 if (!(tt_common_entry->flags & flags))
3263 continue;
3264 tt_common_entry->flags &= ~flags;
3265 }
3266 changed_num++;
3267
3268 if (!count)
3269 continue;
3270
3271 batadv_tt_local_size_inc(bat_priv,
3272 tt_common_entry->vid);
3273 }
3274 rcu_read_unlock();
3275 }
3276 }
3277
3278 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3279 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3280 {
3281 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3282 struct batadv_tt_common_entry *tt_common;
3283 struct batadv_tt_local_entry *tt_local;
3284 struct hlist_node *node_tmp;
3285 struct hlist_head *head;
3286 spinlock_t *list_lock; /* protects write access to the hash lists */
3287 u32 i;
3288
3289 if (!hash)
3290 return;
3291
3292 for (i = 0; i < hash->size; i++) {
3293 head = &hash->table[i];
3294 list_lock = &hash->list_locks[i];
3295
3296 spin_lock_bh(list_lock);
3297 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3298 hash_entry) {
3299 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3300 continue;
3301
3302 batadv_dbg(BATADV_DBG_TT, bat_priv,
3303 "Deleting local tt entry (%pM, vid: %d): pending\n",
3304 tt_common->addr,
3305 BATADV_PRINT_VID(tt_common->vid));
3306
3307 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3308 hlist_del_rcu(&tt_common->hash_entry);
3309 tt_local = container_of(tt_common,
3310 struct batadv_tt_local_entry,
3311 common);
3312
3313 batadv_tt_local_entry_put(tt_local);
3314 }
3315 spin_unlock_bh(list_lock);
3316 }
3317 }
3318
3319 /**
3320 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3321 * which have been queued in the time since the last commit
3322 * @bat_priv: the bat priv with all the soft interface information
3323 *
3324 * Caller must hold tt->commit_lock.
3325 */
3326 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3327 {
3328 lockdep_assert_held(&bat_priv->tt.commit_lock);
3329
3330 /* Update multicast addresses in local translation table */
3331 batadv_mcast_mla_update(bat_priv);
3332
3333 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3334 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3335 batadv_tt_tvlv_container_update(bat_priv);
3336 return;
3337 }
3338
3339 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3340
3341 batadv_tt_local_purge_pending_clients(bat_priv);
3342 batadv_tt_local_update_crc(bat_priv);
3343
3344 /* Increment the TTVN only once per OGM interval */
3345 atomic_inc(&bat_priv->tt.vn);
3346 batadv_dbg(BATADV_DBG_TT, bat_priv,
3347 "Local changes committed, updating to ttvn %u\n",
3348 (u8)atomic_read(&bat_priv->tt.vn));
3349
3350 /* reset the sending counter */
3351 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3352 batadv_tt_tvlv_container_update(bat_priv);
3353 }
3354
3355 /**
3356 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3357 * have been queued in the time since the last commit
3358 * @bat_priv: the bat priv with all the soft interface information
3359 */
3360 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3361 {
3362 spin_lock_bh(&bat_priv->tt.commit_lock);
3363 batadv_tt_local_commit_changes_nolock(bat_priv);
3364 spin_unlock_bh(&bat_priv->tt.commit_lock);
3365 }
3366
3367 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3368 unsigned short vid)
3369 {
3370 struct batadv_tt_local_entry *tt_local_entry = NULL;
3371 struct batadv_tt_global_entry *tt_global_entry = NULL;
3372 struct batadv_softif_vlan *vlan;
3373 bool ret = false;
3374
3375 vlan = batadv_softif_vlan_get(bat_priv, vid);
3376 if (!vlan)
3377 return false;
3378
3379 if (!atomic_read(&vlan->ap_isolation))
3380 goto out;
3381
3382 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3383 if (!tt_local_entry)
3384 goto out;
3385
3386 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3387 if (!tt_global_entry)
3388 goto out;
3389
3390 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3391 goto out;
3392
3393 ret = true;
3394
3395 out:
3396 batadv_softif_vlan_put(vlan);
3397 if (tt_global_entry)
3398 batadv_tt_global_entry_put(tt_global_entry);
3399 if (tt_local_entry)
3400 batadv_tt_local_entry_put(tt_local_entry);
3401 return ret;
3402 }
3403
3404 /**
3405 * batadv_tt_update_orig - update global translation table with new tt
3406 * information received via ogms
3407 * @bat_priv: the bat priv with all the soft interface information
3408 * @orig_node: the orig_node of the ogm
3409 * @tt_buff: pointer to the first tvlv VLAN entry
3410 * @tt_num_vlan: number of tvlv VLAN entries
3411 * @tt_change: pointer to the first entry in the TT buffer
3412 * @tt_num_changes: number of tt changes inside the tt buffer
3413 * @ttvn: translation table version number of this changeset
3414 */
3415 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3416 struct batadv_orig_node *orig_node,
3417 const void *tt_buff, u16 tt_num_vlan,
3418 struct batadv_tvlv_tt_change *tt_change,
3419 u16 tt_num_changes, u8 ttvn)
3420 {
3421 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3422 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3423 bool full_table = true;
3424 bool has_tt_init;
3425
3426 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3427 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3428 &orig_node->capa_initialized);
3429
3430 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3431 * increased by one -> we can apply the attached changes
3432 */
3433 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3434 /* the OGM could not contain the changes due to their size or
3435 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3436 * times.
3437 * In this case send a tt request
3438 */
3439 if (!tt_num_changes) {
3440 full_table = false;
3441 goto request_table;
3442 }
3443
3444 spin_lock_bh(&orig_node->tt_lock);
3445
3446 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3447 ttvn, tt_change);
3448
3449 /* Even if we received the precomputed crc with the OGM, we
3450 * prefer to recompute it to spot any possible inconsistency
3451 * in the global table
3452 */
3453 batadv_tt_global_update_crc(bat_priv, orig_node);
3454
3455 spin_unlock_bh(&orig_node->tt_lock);
3456
3457 /* The ttvn alone is not enough to guarantee consistency
3458 * because a single value could represent different states
3459 * (due to the wrap around). Thus a node has to check whether
3460 * the resulting table (after applying the changes) is still
3461 * consistent or not. E.g. a node could disconnect while its
3462 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3463 * checking the CRC value is mandatory to detect the
3464 * inconsistency
3465 */
3466 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3467 tt_num_vlan))
3468 goto request_table;
3469 } else {
3470 /* if we missed more than one change or our tables are not
3471 * in sync anymore -> request fresh tt data
3472 */
3473 if (!has_tt_init || ttvn != orig_ttvn ||
3474 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3475 tt_num_vlan)) {
3476 request_table:
3477 batadv_dbg(BATADV_DBG_TT, bat_priv,
3478 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3479 orig_node->orig, ttvn, orig_ttvn,
3480 tt_num_changes);
3481 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3482 tt_vlan, tt_num_vlan,
3483 full_table);
3484 return;
3485 }
3486 }
3487 }
3488
3489 /**
3490 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3491 * @bat_priv: the bat priv with all the soft interface information
3492 * @addr: the mac address of the client to check
3493 * @vid: VLAN identifier
3494 *
3495 * Return: true if we know that the client has moved from its old originator
3496 * to another one. This entry is still kept for consistency purposes and will be
3497 * deleted later by a DEL or because of timeout
3498 */
3499 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3500 u8 *addr, unsigned short vid)
3501 {
3502 struct batadv_tt_global_entry *tt_global_entry;
3503 bool ret = false;
3504
3505 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3506 if (!tt_global_entry)
3507 goto out;
3508
3509 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3510 batadv_tt_global_entry_put(tt_global_entry);
3511 out:
3512 return ret;
3513 }
3514
3515 /**
3516 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3517 * @bat_priv: the bat priv with all the soft interface information
3518 * @addr: the mac address of the local client to query
3519 * @vid: VLAN identifier
3520 *
3521 * Return: true if the local client is known to be roaming (it is not served by
3522 * this node anymore) or not. If yes, the client is still present in the table
3523 * to keep the latter consistent with the node TTVN
3524 */
3525 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3526 u8 *addr, unsigned short vid)
3527 {
3528 struct batadv_tt_local_entry *tt_local_entry;
3529 bool ret = false;
3530
3531 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3532 if (!tt_local_entry)
3533 goto out;
3534
3535 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3536 batadv_tt_local_entry_put(tt_local_entry);
3537 out:
3538 return ret;
3539 }
3540
3541 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3542 struct batadv_orig_node *orig_node,
3543 const unsigned char *addr,
3544 unsigned short vid)
3545 {
3546 bool ret = false;
3547
3548 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3549 BATADV_TT_CLIENT_TEMP,
3550 atomic_read(&orig_node->last_ttvn)))
3551 goto out;
3552
3553 batadv_dbg(BATADV_DBG_TT, bat_priv,
3554 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3555 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3556 ret = true;
3557 out:
3558 return ret;
3559 }
3560
3561 /**
3562 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3563 * maximum packet size that can be transported through the mesh
3564 * @soft_iface: netdev struct of the mesh interface
3565 *
3566 * Remove entries older than 'timeout' and half timeout if more entries need
3567 * to be removed.
3568 */
3569 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3570 {
3571 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3572 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3573 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3574 bool reduced = false;
3575
3576 spin_lock_bh(&bat_priv->tt.commit_lock);
3577
3578 while (true) {
3579 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3580 if (packet_size_max >= table_size)
3581 break;
3582
3583 batadv_tt_local_purge(bat_priv, timeout);
3584 batadv_tt_local_purge_pending_clients(bat_priv);
3585
3586 timeout /= 2;
3587 reduced = true;
3588 net_ratelimited_function(batadv_info, soft_iface,
3589 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3590 packet_size_max);
3591 }
3592
3593 /* commit these changes immediately, to avoid synchronization problem
3594 * with the TTVN
3595 */
3596 if (reduced)
3597 batadv_tt_local_commit_changes_nolock(bat_priv);
3598
3599 spin_unlock_bh(&bat_priv->tt.commit_lock);
3600 }
3601
3602 /**
3603 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3604 * @bat_priv: the bat priv with all the soft interface information
3605 * @orig: the orig_node of the ogm
3606 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3607 * @tvlv_value: tvlv buffer containing the gateway data
3608 * @tvlv_value_len: tvlv buffer length
3609 */
3610 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3611 struct batadv_orig_node *orig,
3612 u8 flags, void *tvlv_value,
3613 u16 tvlv_value_len)
3614 {
3615 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3616 struct batadv_tvlv_tt_change *tt_change;
3617 struct batadv_tvlv_tt_data *tt_data;
3618 u16 num_entries, num_vlan;
3619
3620 if (tvlv_value_len < sizeof(*tt_data))
3621 return;
3622
3623 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3624 tvlv_value_len -= sizeof(*tt_data);
3625
3626 num_vlan = ntohs(tt_data->num_vlan);
3627
3628 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3629 return;
3630
3631 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3632 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3633 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3634
3635 num_entries = batadv_tt_entries(tvlv_value_len);
3636
3637 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3638 num_entries, tt_data->ttvn);
3639 }
3640
3641 /**
3642 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3643 * container
3644 * @bat_priv: the bat priv with all the soft interface information
3645 * @src: mac address of tt tvlv sender
3646 * @dst: mac address of tt tvlv recipient
3647 * @tvlv_value: tvlv buffer containing the tt data
3648 * @tvlv_value_len: tvlv buffer length
3649 *
3650 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3651 * otherwise.
3652 */
3653 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3654 u8 *src, u8 *dst,
3655 void *tvlv_value,
3656 u16 tvlv_value_len)
3657 {
3658 struct batadv_tvlv_tt_data *tt_data;
3659 u16 tt_vlan_len, tt_num_entries;
3660 char tt_flag;
3661 bool ret;
3662
3663 if (tvlv_value_len < sizeof(*tt_data))
3664 return NET_RX_SUCCESS;
3665
3666 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3667 tvlv_value_len -= sizeof(*tt_data);
3668
3669 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3670 tt_vlan_len *= ntohs(tt_data->num_vlan);
3671
3672 if (tvlv_value_len < tt_vlan_len)
3673 return NET_RX_SUCCESS;
3674
3675 tvlv_value_len -= tt_vlan_len;
3676 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3677
3678 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3679 case BATADV_TT_REQUEST:
3680 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3681
3682 /* If this node cannot provide a TT response the tt_request is
3683 * forwarded
3684 */
3685 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3686 if (!ret) {
3687 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3688 tt_flag = 'F';
3689 else
3690 tt_flag = '.';
3691
3692 batadv_dbg(BATADV_DBG_TT, bat_priv,
3693 "Routing TT_REQUEST to %pM [%c]\n",
3694 dst, tt_flag);
3695 /* tvlv API will re-route the packet */
3696 return NET_RX_DROP;
3697 }
3698 break;
3699 case BATADV_TT_RESPONSE:
3700 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3701
3702 if (batadv_is_my_mac(bat_priv, dst)) {
3703 batadv_handle_tt_response(bat_priv, tt_data,
3704 src, tt_num_entries);
3705 return NET_RX_SUCCESS;
3706 }
3707
3708 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3709 tt_flag = 'F';
3710 else
3711 tt_flag = '.';
3712
3713 batadv_dbg(BATADV_DBG_TT, bat_priv,
3714 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3715
3716 /* tvlv API will re-route the packet */
3717 return NET_RX_DROP;
3718 }
3719
3720 return NET_RX_SUCCESS;
3721 }
3722
3723 /**
3724 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3725 * @bat_priv: the bat priv with all the soft interface information
3726 * @src: mac address of tt tvlv sender
3727 * @dst: mac address of tt tvlv recipient
3728 * @tvlv_value: tvlv buffer containing the tt data
3729 * @tvlv_value_len: tvlv buffer length
3730 *
3731 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3732 * otherwise.
3733 */
3734 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3735 u8 *src, u8 *dst,
3736 void *tvlv_value,
3737 u16 tvlv_value_len)
3738 {
3739 struct batadv_tvlv_roam_adv *roaming_adv;
3740 struct batadv_orig_node *orig_node = NULL;
3741
3742 /* If this node is not the intended recipient of the
3743 * roaming advertisement the packet is forwarded
3744 * (the tvlv API will re-route the packet).
3745 */
3746 if (!batadv_is_my_mac(bat_priv, dst))
3747 return NET_RX_DROP;
3748
3749 if (tvlv_value_len < sizeof(*roaming_adv))
3750 goto out;
3751
3752 orig_node = batadv_orig_hash_find(bat_priv, src);
3753 if (!orig_node)
3754 goto out;
3755
3756 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3757 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3758
3759 batadv_dbg(BATADV_DBG_TT, bat_priv,
3760 "Received ROAMING_ADV from %pM (client %pM)\n",
3761 src, roaming_adv->client);
3762
3763 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3764 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3765 atomic_read(&orig_node->last_ttvn) + 1);
3766
3767 out:
3768 if (orig_node)
3769 batadv_orig_node_put(orig_node);
3770 return NET_RX_SUCCESS;
3771 }
3772
3773 /**
3774 * batadv_tt_init - initialise the translation table internals
3775 * @bat_priv: the bat priv with all the soft interface information
3776 *
3777 * Return: 0 on success or negative error number in case of failure.
3778 */
3779 int batadv_tt_init(struct batadv_priv *bat_priv)
3780 {
3781 int ret;
3782
3783 /* synchronized flags must be remote */
3784 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3785
3786 ret = batadv_tt_local_init(bat_priv);
3787 if (ret < 0)
3788 return ret;
3789
3790 ret = batadv_tt_global_init(bat_priv);
3791 if (ret < 0)
3792 return ret;
3793
3794 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3795 batadv_tt_tvlv_unicast_handler_v1,
3796 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3797
3798 batadv_tvlv_handler_register(bat_priv, NULL,
3799 batadv_roam_tvlv_unicast_handler_v1,
3800 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3801
3802 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3803 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3804 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3805
3806 return 1;
3807 }
3808
3809 /**
3810 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3811 * @bat_priv: the bat priv with all the soft interface information
3812 * @addr: the mac address of the client
3813 * @vid: the identifier of the VLAN where this client is connected
3814 *
3815 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3816 * otherwise
3817 */
3818 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3819 const u8 *addr, unsigned short vid)
3820 {
3821 struct batadv_tt_global_entry *tt;
3822 bool ret;
3823
3824 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3825 if (!tt)
3826 return false;
3827
3828 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3829
3830 batadv_tt_global_entry_put(tt);
3831
3832 return ret;
3833 }
This page took 0.158193 seconds and 4 git commands to generate.