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