~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/batman-adv/translation-table.c

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 */
 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 
 77         return batadv_compare_eth(data1, data2);
 78 }
 79 
 80 /**
 81  * batadv_choose_tt - return the index of the tt entry in the hash table
 82  * @data: pointer to the tt_common_entry object to map
 83  * @size: the size of the hash table
 84  *
 85  * Returns the hash index where the object represented by 'data' should be
 86  * stored at.
 87  */
 88 static inline u32 batadv_choose_tt(const void *data, u32 size)
 89 {
 90         struct batadv_tt_common_entry *tt;
 91         u32 hash = 0;
 92 
 93         tt = (struct batadv_tt_common_entry *)data;
 94         hash = jhash(&tt->addr, ETH_ALEN, hash);
 95         hash = jhash(&tt->vid, sizeof(tt->vid), hash);
 96 
 97         return hash % size;
 98 }
 99 
100 /**
101  * batadv_tt_hash_find - look for a client in the given hash table
102  * @hash: the hash table to search
103  * @addr: the mac address of the client to look for
104  * @vid: VLAN identifier
105  *
106  * Returns a pointer to the tt_common struct belonging to the searched client if
107  * found, NULL otherwise.
108  */
109 static struct batadv_tt_common_entry *
110 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
111                     unsigned short vid)
112 {
113         struct hlist_head *head;
114         struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
115         u32 index;
116 
117         if (!hash)
118                 return NULL;
119 
120         ether_addr_copy(to_search.addr, addr);
121         to_search.vid = vid;
122 
123         index = batadv_choose_tt(&to_search, hash->size);
124         head = &hash->table[index];
125 
126         rcu_read_lock();
127         hlist_for_each_entry_rcu(tt, head, hash_entry) {
128                 if (!batadv_compare_eth(tt, addr))
129                         continue;
130 
131                 if (tt->vid != vid)
132                         continue;
133 
134                 if (!atomic_inc_not_zero(&tt->refcount))
135                         continue;
136 
137                 tt_tmp = tt;
138                 break;
139         }
140         rcu_read_unlock();
141 
142         return tt_tmp;
143 }
144 
145 /**
146  * batadv_tt_local_hash_find - search the local table for a given client
147  * @bat_priv: the bat priv with all the soft interface information
148  * @addr: the mac address of the client to look for
149  * @vid: VLAN identifier
150  *
151  * Returns a pointer to the corresponding tt_local_entry struct if the client is
152  * found, NULL otherwise.
153  */
154 static struct batadv_tt_local_entry *
155 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
156                           unsigned short vid)
157 {
158         struct batadv_tt_common_entry *tt_common_entry;
159         struct batadv_tt_local_entry *tt_local_entry = NULL;
160 
161         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
162                                               vid);
163         if (tt_common_entry)
164                 tt_local_entry = container_of(tt_common_entry,
165                                               struct batadv_tt_local_entry,
166                                               common);
167         return tt_local_entry;
168 }
169 
170 /**
171  * batadv_tt_global_hash_find - search the global table for a given client
172  * @bat_priv: the bat priv with all the soft interface information
173  * @addr: the mac address of the client to look for
174  * @vid: VLAN identifier
175  *
176  * Returns a pointer to the corresponding tt_global_entry struct if the client
177  * is found, NULL otherwise.
178  */
179 static struct batadv_tt_global_entry *
180 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
181                            unsigned short vid)
182 {
183         struct batadv_tt_common_entry *tt_common_entry;
184         struct batadv_tt_global_entry *tt_global_entry = NULL;
185 
186         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
187                                               vid);
188         if (tt_common_entry)
189                 tt_global_entry = container_of(tt_common_entry,
190                                                struct batadv_tt_global_entry,
191                                                common);
192         return tt_global_entry;
193 }
194 
195 static void
196 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
197 {
198         if (atomic_dec_and_test(&tt_local_entry->common.refcount))
199                 kfree_rcu(tt_local_entry, common.rcu);
200 }
201 
202 /**
203  * batadv_tt_global_entry_free_ref - decrement the refcounter for a
204  *  tt_global_entry and possibly free it
205  * @tt_global_entry: the object to free
206  */
207 static void
208 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
209 {
210         if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
211                 batadv_tt_global_del_orig_list(tt_global_entry);
212                 kfree_rcu(tt_global_entry, common.rcu);
213         }
214 }
215 
216 /**
217  * batadv_tt_global_hash_count - count the number of orig entries
218  * @hash: hash table containing the tt entries
219  * @addr: the mac address of the client to count entries for
220  * @vid: VLAN identifier
221  *
222  * Return the number of originators advertising the given address/data
223  * (excluding ourself).
224  */
225 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
226                                 const u8 *addr, unsigned short vid)
227 {
228         struct batadv_tt_global_entry *tt_global_entry;
229         int count;
230 
231         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
232         if (!tt_global_entry)
233                 return 0;
234 
235         count = atomic_read(&tt_global_entry->orig_list_count);
236         batadv_tt_global_entry_free_ref(tt_global_entry);
237 
238         return count;
239 }
240 
241 /**
242  * batadv_tt_local_size_mod - change the size by v of the local table identified
243  *  by vid
244  * @bat_priv: the bat priv with all the soft interface information
245  * @vid: the VLAN identifier of the sub-table to change
246  * @v: the amount to sum to the local table size
247  */
248 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
249                                      unsigned short vid, int v)
250 {
251         struct batadv_softif_vlan *vlan;
252 
253         vlan = batadv_softif_vlan_get(bat_priv, vid);
254         if (!vlan)
255                 return;
256 
257         atomic_add(v, &vlan->tt.num_entries);
258 
259         batadv_softif_vlan_free_ref(vlan);
260 }
261 
262 /**
263  * batadv_tt_local_size_inc - increase by one the local table size for the given
264  *  vid
265  * @bat_priv: the bat priv with all the soft interface information
266  * @vid: the VLAN identifier
267  */
268 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
269                                      unsigned short vid)
270 {
271         batadv_tt_local_size_mod(bat_priv, vid, 1);
272 }
273 
274 /**
275  * batadv_tt_local_size_dec - decrease by one the local table size for the given
276  *  vid
277  * @bat_priv: the bat priv with all the soft interface information
278  * @vid: the VLAN identifier
279  */
280 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
281                                      unsigned short vid)
282 {
283         batadv_tt_local_size_mod(bat_priv, vid, -1);
284 }
285 
286 /**
287  * batadv_tt_global_size_mod - change the size by v of the local table
288  *  identified by vid
289  * @bat_priv: the bat priv with all the soft interface information
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
1429                  */
1430                 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1431 
1432                 /* the change can carry possible "attribute" flags like the
1433                  * TT_CLIENT_WIFI, therefore they have to be copied in the
1434                  * client entry
1435                  */
1436                 tt_global_entry->common.flags |= flags;
1437 
1438                 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1439                  * one originator left in the list and we previously received a
1440                  * delete + roaming change for this originator.
1441                  *
1442                  * We should first delete the old originator before adding the
1443                  * new one.
1444                  */
1445                 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1446                         batadv_tt_global_del_orig_list(tt_global_entry);
1447                         common->flags &= ~BATADV_TT_CLIENT_ROAM;
1448                         tt_global_entry->roam_at = 0;
1449                 }
1450         }
1451 add_orig_entry:
1452         /* add the new orig_entry (if needed) or update it */
1453         batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1454 
1455         batadv_dbg(BATADV_DBG_TT, bat_priv,
1456                    "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1457                    common->addr, BATADV_PRINT_VID(common->vid),
1458                    orig_node->orig);
1459         ret = true;
1460 
1461 out_remove:
1462         /* Do not remove multicast addresses from the local hash on
1463          * global additions
1464          */
1465         if (is_multicast_ether_addr(tt_addr))
1466                 goto out;
1467 
1468         /* remove address from local hash if present */
1469         local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1470                                              "global tt received",
1471                                              flags & BATADV_TT_CLIENT_ROAM);
1472         tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1473 
1474         if (!(flags & BATADV_TT_CLIENT_ROAM))
1475                 /* this is a normal global add. Therefore the client is not in a
1476                  * roaming state anymore.
1477                  */
1478                 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1479 
1480 out:
1481         if (tt_global_entry)
1482                 batadv_tt_global_entry_free_ref(tt_global_entry);
1483         if (tt_local_entry)
1484                 batadv_tt_local_entry_free_ref(tt_local_entry);
1485         return ret;
1486 }
1487 
1488 /**
1489  * batadv_transtable_best_orig - Get best originator list entry from tt entry
1490  * @bat_priv: the bat priv with all the soft interface information
1491  * @tt_global_entry: global translation table entry to be analyzed
1492  *
1493  * This functon assumes the caller holds rcu_read_lock().
1494  * Returns best originator list entry or NULL on errors.
1495  */
1496 static struct batadv_tt_orig_list_entry *
1497 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1498                             struct batadv_tt_global_entry *tt_global_entry)
1499 {
1500         struct batadv_neigh_node *router, *best_router = NULL;
1501         struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1502         struct hlist_head *head;
1503         struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1504 
1505         head = &tt_global_entry->orig_list;
1506         hlist_for_each_entry_rcu(orig_entry, head, list) {
1507                 router = batadv_orig_router_get(orig_entry->orig_node,
1508                                                 BATADV_IF_DEFAULT);
1509                 if (!router)
1510                         continue;
1511 
1512                 if (best_router &&
1513                     bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1514                                        best_router, BATADV_IF_DEFAULT) <= 0) {
1515                         batadv_neigh_node_free_ref(router);
1516                         continue;
1517                 }
1518 
1519                 /* release the refcount for the "old" best */
1520                 if (best_router)
1521                         batadv_neigh_node_free_ref(best_router);
1522 
1523                 best_entry = orig_entry;
1524                 best_router = router;
1525         }
1526 
1527         if (best_router)
1528                 batadv_neigh_node_free_ref(best_router);
1529 
1530         return best_entry;
1531 }
1532 
1533 /**
1534  * batadv_tt_global_print_entry - print all orig nodes who announce the address
1535  *  for this global entry
1536  * @bat_priv: the bat priv with all the soft interface information
1537  * @tt_global_entry: global translation table entry to be printed
1538  * @seq: debugfs table seq_file struct
1539  *
1540  * This functon assumes the caller holds rcu_read_lock().
1541  */
1542 static void
1543 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1544                              struct batadv_tt_global_entry *tt_global_entry,
1545                              struct seq_file *seq)
1546 {
1547         struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1548         struct batadv_tt_common_entry *tt_common_entry;
1549         struct batadv_orig_node_vlan *vlan;
1550         struct hlist_head *head;
1551         u8 last_ttvn;
1552         u16 flags;
1553 
1554         tt_common_entry = &tt_global_entry->common;
1555         flags = tt_common_entry->flags;
1556 
1557         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1558         if (best_entry) {
1559                 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1560                                                  tt_common_entry->vid);
1561                 if (!vlan) {
1562                         seq_printf(seq,
1563                                    " * Cannot retrieve VLAN %d for originator %pM\n",
1564                                    BATADV_PRINT_VID(tt_common_entry->vid),
1565                                    best_entry->orig_node->orig);
1566                         goto print_list;
1567                 }
1568 
1569                 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1570                 seq_printf(seq,
1571                            " %c %pM %4i   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1572                            '*', tt_global_entry->common.addr,
1573                            BATADV_PRINT_VID(tt_global_entry->common.vid),
1574                            best_entry->ttvn, best_entry->orig_node->orig,
1575                            last_ttvn, vlan->tt.crc,
1576                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1577                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1578                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1579                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1580 
1581                 batadv_orig_node_vlan_free_ref(vlan);
1582         }
1583 
1584 print_list:
1585         head = &tt_global_entry->orig_list;
1586 
1587         hlist_for_each_entry_rcu(orig_entry, head, list) {
1588                 if (best_entry == orig_entry)
1589                         continue;
1590 
1591                 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1592                                                  tt_common_entry->vid);
1593                 if (!vlan) {
1594                         seq_printf(seq,
1595                                    " + Cannot retrieve VLAN %d for originator %pM\n",
1596                                    BATADV_PRINT_VID(tt_common_entry->vid),
1597                                    orig_entry->orig_node->orig);
1598                         continue;
1599                 }
1600 
1601                 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1602                 seq_printf(seq,
1603                            " %c %pM %4d   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1604                            '+', tt_global_entry->common.addr,
1605                            BATADV_PRINT_VID(tt_global_entry->common.vid),
1606                            orig_entry->ttvn, orig_entry->orig_node->orig,
1607                            last_ttvn, vlan->tt.crc,
1608                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1609                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1610                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1611                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1612 
1613                 batadv_orig_node_vlan_free_ref(vlan);
1614         }
1615 }
1616 
1617 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1618 {
1619         struct net_device *net_dev = (struct net_device *)seq->private;
1620         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1621         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1622         struct batadv_tt_common_entry *tt_common_entry;
1623         struct batadv_tt_global_entry *tt_global;
1624         struct batadv_hard_iface *primary_if;
1625         struct hlist_head *head;
1626         u32 i;
1627 
1628         primary_if = batadv_seq_print_text_primary_if_get(seq);
1629         if (!primary_if)
1630                 goto out;
1631 
1632         seq_printf(seq,
1633                    "Globally announced TT entries received via the mesh %s\n",
1634                    net_dev->name);
1635         seq_printf(seq, "       %-13s  %s  %s       %-15s %s (%-10s) %s\n",
1636                    "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1637                    "CRC", "Flags");
1638 
1639         for (i = 0; i < hash->size; i++) {
1640                 head = &hash->table[i];
1641 
1642                 rcu_read_lock();
1643                 hlist_for_each_entry_rcu(tt_common_entry,
1644                                          head, hash_entry) {
1645                         tt_global = container_of(tt_common_entry,
1646                                                  struct batadv_tt_global_entry,
1647                                                  common);
1648                         batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1649                 }
1650                 rcu_read_unlock();
1651         }
1652 out:
1653         if (primary_if)
1654                 batadv_hardif_free_ref(primary_if);
1655         return 0;
1656 }
1657 
1658 /**
1659  * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1660  * @tt_global_entry: the global entry to remove the orig_entry from
1661  * @orig_entry: the orig entry to remove and free
1662  *
1663  * Remove an orig_entry from its list in the given tt_global_entry and
1664  * free this orig_entry afterwards.
1665  *
1666  * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1667  * part of a list.
1668  */
1669 static void
1670 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1671                                  struct batadv_tt_orig_list_entry *orig_entry)
1672 {
1673         lockdep_assert_held(&tt_global_entry->list_lock);
1674 
1675         batadv_tt_global_size_dec(orig_entry->orig_node,
1676                                   tt_global_entry->common.vid);
1677         atomic_dec(&tt_global_entry->orig_list_count);
1678         /* requires holding tt_global_entry->list_lock and orig_entry->list
1679          * being part of a list
1680          */
1681         hlist_del_rcu(&orig_entry->list);
1682         batadv_tt_orig_list_entry_free_ref(orig_entry);
1683 }
1684 
1685 /* deletes the orig list of a tt_global_entry */
1686 static void
1687 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1688 {
1689         struct hlist_head *head;
1690         struct hlist_node *safe;
1691         struct batadv_tt_orig_list_entry *orig_entry;
1692 
1693         spin_lock_bh(&tt_global_entry->list_lock);
1694         head = &tt_global_entry->orig_list;
1695         hlist_for_each_entry_safe(orig_entry, safe, head, list)
1696                 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1697         spin_unlock_bh(&tt_global_entry->list_lock);
1698 }
1699 
1700 /**
1701  * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1702  * @bat_priv: the bat priv with all the soft interface information
1703  * @tt_global_entry: the global entry to remove the orig_node from
1704  * @orig_node: the originator announcing the client
1705  * @message: message to append to the log on deletion
1706  *
1707  * Remove the given orig_node and its according orig_entry from the given
1708  * global tt entry.
1709  */
1710 static void
1711 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1712                                struct batadv_tt_global_entry *tt_global_entry,
1713                                struct batadv_orig_node *orig_node,
1714                                const char *message)
1715 {
1716         struct hlist_head *head;
1717         struct hlist_node *safe;
1718         struct batadv_tt_orig_list_entry *orig_entry;
1719         unsigned short vid;
1720 
1721         spin_lock_bh(&tt_global_entry->list_lock);
1722         head = &tt_global_entry->orig_list;
1723         hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1724                 if (orig_entry->orig_node == orig_node) {
1725                         vid = tt_global_entry->common.vid;
1726                         batadv_dbg(BATADV_DBG_TT, bat_priv,
1727                                    "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1728                                    orig_node->orig,
1729                                    tt_global_entry->common.addr,
1730                                    BATADV_PRINT_VID(vid), message);
1731                         _batadv_tt_global_del_orig_entry(tt_global_entry,
1732                                                          orig_entry);
1733                 }
1734         }
1735         spin_unlock_bh(&tt_global_entry->list_lock);
1736 }
1737 
1738 /* If the client is to be deleted, we check if it is the last origantor entry
1739  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1740  * timer, otherwise we simply remove the originator scheduled for deletion.
1741  */
1742 static void
1743 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1744                              struct batadv_tt_global_entry *tt_global_entry,
1745                              struct batadv_orig_node *orig_node,
1746                              const char *message)
1747 {
1748         bool last_entry = true;
1749         struct hlist_head *head;
1750         struct batadv_tt_orig_list_entry *orig_entry;
1751 
1752         /* no local entry exists, case 1:
1753          * Check if this is the last one or if other entries exist.
1754          */
1755 
1756         rcu_read_lock();
1757         head = &tt_global_entry->orig_list;
1758         hlist_for_each_entry_rcu(orig_entry, head, list) {
1759                 if (orig_entry->orig_node != orig_node) {
1760                         last_entry = false;
1761                         break;
1762                 }
1763         }
1764         rcu_read_unlock();
1765 
1766         if (last_entry) {
1767                 /* its the last one, mark for roaming. */
1768                 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1769                 tt_global_entry->roam_at = jiffies;
1770         } else
1771                 /* there is another entry, we can simply delete this
1772                  * one and can still use the other one.
1773                  */
1774                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1775                                                orig_node, message);
1776 }
1777 
1778 /**
1779  * batadv_tt_global_del - remove a client from the global table
1780  * @bat_priv: the bat priv with all the soft interface information
1781  * @orig_node: an originator serving this client
1782  * @addr: the mac address of the client
1783  * @vid: VLAN identifier
1784  * @message: a message explaining the reason for deleting the client to print
1785  *  for debugging purpose
1786  * @roaming: true if the deletion has been triggered by a roaming event
1787  */
1788 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1789                                  struct batadv_orig_node *orig_node,
1790                                  const unsigned char *addr, unsigned short vid,
1791                                  const char *message, bool roaming)
1792 {
1793         struct batadv_tt_global_entry *tt_global_entry;
1794         struct batadv_tt_local_entry *local_entry = NULL;
1795 
1796         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1797         if (!tt_global_entry)
1798                 goto out;
1799 
1800         if (!roaming) {
1801                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1802                                                orig_node, message);
1803 
1804                 if (hlist_empty(&tt_global_entry->orig_list))
1805                         batadv_tt_global_free(bat_priv, tt_global_entry,
1806                                               message);
1807 
1808                 goto out;
1809         }
1810 
1811         /* if we are deleting a global entry due to a roam
1812          * event, there are two possibilities:
1813          * 1) the client roamed from node A to node B => if there
1814          *    is only one originator left for this client, we mark
1815          *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1816          *    wait for node B to claim it. In case of timeout
1817          *    the entry is purged.
1818          *
1819          *    If there are other originators left, we directly delete
1820          *    the originator.
1821          * 2) the client roamed to us => we can directly delete
1822          *    the global entry, since it is useless now.
1823          */
1824         local_entry = batadv_tt_local_hash_find(bat_priv,
1825                                                 tt_global_entry->common.addr,
1826                                                 vid);
1827         if (local_entry) {
1828                 /* local entry exists, case 2: client roamed to us. */
1829                 batadv_tt_global_del_orig_list(tt_global_entry);
1830                 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1831         } else
1832                 /* no local entry exists, case 1: check for roaming */
1833                 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1834                                              orig_node, message);
1835 
1836 out:
1837         if (tt_global_entry)
1838                 batadv_tt_global_entry_free_ref(tt_global_entry);
1839         if (local_entry)
1840                 batadv_tt_local_entry_free_ref(local_entry);
1841 }
1842 
1843 /**
1844  * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1845  *  given originator matching the provided vid
1846  * @bat_priv: the bat priv with all the soft interface information
1847  * @orig_node: the originator owning the entries to remove
1848  * @match_vid: the VLAN identifier to match. If negative all the entries will be
1849  *  removed
1850  * @message: debug message to print as "reason"
1851  */
1852 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1853                                struct batadv_orig_node *orig_node,
1854                                s32 match_vid,
1855                                const char *message)
1856 {
1857         struct batadv_tt_global_entry *tt_global;
1858         struct batadv_tt_common_entry *tt_common_entry;
1859         u32 i;
1860         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1861         struct hlist_node *safe;
1862         struct hlist_head *head;
1863         spinlock_t *list_lock; /* protects write access to the hash lists */
1864         unsigned short vid;
1865 
1866         if (!hash)
1867                 return;
1868 
1869         for (i = 0; i < hash->size; i++) {
1870                 head = &hash->table[i];
1871                 list_lock = &hash->list_locks[i];
1872 
1873                 spin_lock_bh(list_lock);
1874                 hlist_for_each_entry_safe(tt_common_entry, safe,
1875                                           head, hash_entry) {
1876                         /* remove only matching entries */
1877                         if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1878                                 continue;
1879 
1880                         tt_global = container_of(tt_common_entry,
1881                                                  struct batadv_tt_global_entry,
1882                                                  common);
1883 
1884                         batadv_tt_global_del_orig_node(bat_priv, tt_global,
1885                                                        orig_node, message);
1886 
1887                         if (hlist_empty(&tt_global->orig_list)) {
1888                                 vid = tt_global->common.vid;
1889                                 batadv_dbg(BATADV_DBG_TT, bat_priv,
1890                                            "Deleting global tt entry %pM (vid: %d): %s\n",
1891                                            tt_global->common.addr,
1892                                            BATADV_PRINT_VID(vid), message);
1893                                 hlist_del_rcu(&tt_common_entry->hash_entry);
1894                                 batadv_tt_global_entry_free_ref(tt_global);
1895                         }
1896                 }
1897                 spin_unlock_bh(list_lock);
1898         }
1899         clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1900 }
1901 
1902 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1903                                       char **msg)
1904 {
1905         bool purge = false;
1906         unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1907         unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1908 
1909         if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1910             batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1911                 purge = true;
1912                 *msg = "Roaming timeout\n";
1913         }
1914 
1915         if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1916             batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1917                 purge = true;
1918                 *msg = "Temporary client timeout\n";
1919         }
1920 
1921         return purge;
1922 }
1923 
1924 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1925 {
1926         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1927         struct hlist_head *head;
1928         struct hlist_node *node_tmp;
1929         spinlock_t *list_lock; /* protects write access to the hash lists */
1930         u32 i;
1931         char *msg = NULL;
1932         struct batadv_tt_common_entry *tt_common;
1933         struct batadv_tt_global_entry *tt_global;
1934 
1935         for (i = 0; i < hash->size; i++) {
1936                 head = &hash->table[i];
1937                 list_lock = &hash->list_locks[i];
1938 
1939                 spin_lock_bh(list_lock);
1940                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1941                                           hash_entry) {
1942                         tt_global = container_of(tt_common,
1943                                                  struct batadv_tt_global_entry,
1944                                                  common);
1945 
1946                         if (!batadv_tt_global_to_purge(tt_global, &msg))
1947                                 continue;
1948 
1949                         batadv_dbg(BATADV_DBG_TT, bat_priv,
1950                                    "Deleting global tt entry %pM (vid: %d): %s\n",
1951                                    tt_global->common.addr,
1952                                    BATADV_PRINT_VID(tt_global->common.vid),
1953                                    msg);
1954 
1955                         hlist_del_rcu(&tt_common->hash_entry);
1956 
1957                         batadv_tt_global_entry_free_ref(tt_global);
1958                 }
1959                 spin_unlock_bh(list_lock);
1960         }
1961 }
1962 
1963 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1964 {
1965         struct batadv_hashtable *hash;
1966         spinlock_t *list_lock; /* protects write access to the hash lists */
1967         struct batadv_tt_common_entry *tt_common_entry;
1968         struct batadv_tt_global_entry *tt_global;
1969         struct hlist_node *node_tmp;
1970         struct hlist_head *head;
1971         u32 i;
1972 
1973         if (!bat_priv->tt.global_hash)
1974                 return;
1975 
1976         hash = bat_priv->tt.global_hash;
1977 
1978         for (i = 0; i < hash->size; i++) {
1979                 head = &hash->table[i];
1980                 list_lock = &hash->list_locks[i];
1981 
1982                 spin_lock_bh(list_lock);
1983                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1984                                           head, hash_entry) {
1985                         hlist_del_rcu(&tt_common_entry->hash_entry);
1986                         tt_global = container_of(tt_common_entry,
1987                                                  struct batadv_tt_global_entry,
1988                                                  common);
1989                         batadv_tt_global_entry_free_ref(tt_global);
1990                 }
1991                 spin_unlock_bh(list_lock);
1992         }
1993 
1994         batadv_hash_destroy(hash);
1995 
1996         bat_priv->tt.global_hash = NULL;
1997 }
1998 
1999 static bool
2000 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2001                        struct batadv_tt_global_entry *tt_global_entry)
2002 {
2003         bool ret = false;
2004 
2005         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2006             tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2007                 ret = true;
2008 
2009         /* check if the two clients are marked as isolated */
2010         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2011             tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2012                 ret = true;
2013 
2014         return ret;
2015 }
2016 
2017 /**
2018  * batadv_transtable_search - get the mesh destination for a given client
2019  * @bat_priv: the bat priv with all the soft interface information
2020  * @src: mac address of the source client
2021  * @addr: mac address of the destination client
2022  * @vid: VLAN identifier
2023  *
2024  * Returns a pointer to the originator that was selected as destination in the
2025  * mesh for contacting the client 'addr', NULL otherwise.
2026  * In case of multiple originators serving the same client, the function returns
2027  * the best one (best in terms of metric towards the destination node).
2028  *
2029  * If the two clients are AP isolated the function returns NULL.
2030  */
2031 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2032                                                   const u8 *src,
2033                                                   const u8 *addr,
2034                                                   unsigned short vid)
2035 {
2036         struct batadv_tt_local_entry *tt_local_entry = NULL;
2037         struct batadv_tt_global_entry *tt_global_entry = NULL;
2038         struct batadv_orig_node *orig_node = NULL;
2039         struct batadv_tt_orig_list_entry *best_entry;
2040 
2041         if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2042                 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2043                 if (!tt_local_entry ||
2044                     (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2045                         goto out;
2046         }
2047 
2048         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2049         if (!tt_global_entry)
2050                 goto out;
2051 
2052         /* check whether the clients should not communicate due to AP
2053          * isolation
2054          */
2055         if (tt_local_entry &&
2056             _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2057                 goto out;
2058 
2059         rcu_read_lock();
2060         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2061         /* found anything? */
2062         if (best_entry)
2063                 orig_node = best_entry->orig_node;
2064         if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2065                 orig_node = NULL;
2066         rcu_read_unlock();
2067 
2068 out:
2069         if (tt_global_entry)
2070                 batadv_tt_global_entry_free_ref(tt_global_entry);
2071         if (tt_local_entry)
2072                 batadv_tt_local_entry_free_ref(tt_local_entry);
2073 
2074         return orig_node;
2075 }
2076 
2077 /**
2078  * batadv_tt_global_crc - calculates the checksum of the local table belonging
2079  *  to the given orig_node
2080  * @bat_priv: the bat priv with all the soft interface information
2081  * @orig_node: originator for which the CRC should be computed
2082  * @vid: VLAN identifier for which the CRC32 has to be computed
2083  *
2084  * This function computes the checksum for the global table corresponding to a
2085  * specific originator. In particular, the checksum is computed as follows: For
2086  * each client connected to the originator the CRC32C of the MAC address and the
2087  * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2088  * together.
2089  *
2090  * The idea behind is that CRC32C should be used as much as possible in order to
2091  * produce a unique hash of the table, but since the order which is used to feed
2092  * the CRC32C function affects the result and since every node in the network
2093  * probably sorts the clients differently, the hash function cannot be directly
2094  * computed over the entire table. Hence the CRC32C is used only on
2095  * the single client entry, while all the results are then xor'ed together
2096  * because the XOR operation can combine them all while trying to reduce the
2097  * noise as much as possible.
2098  *
2099  * Returns the checksum of the global table of a given originator.
2100  */
2101 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2102                                 struct batadv_orig_node *orig_node,
2103                                 unsigned short vid)
2104 {
2105         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2106         struct batadv_tt_common_entry *tt_common;
2107         struct batadv_tt_global_entry *tt_global;
2108         struct hlist_head *head;
2109         u32 i, crc_tmp, crc = 0;
2110         u8 flags;
2111         __be16 tmp_vid;
2112 
2113         for (i = 0; i < hash->size; i++) {
2114                 head = &hash->table[i];
2115 
2116                 rcu_read_lock();
2117                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2118                         tt_global = container_of(tt_common,
2119                                                  struct batadv_tt_global_entry,
2120                                                  common);
2121                         /* compute the CRC only for entries belonging to the
2122                          * VLAN identified by the vid passed as parameter
2123                          */
2124                         if (tt_common->vid != vid)
2125                                 continue;
2126 
2127                         /* Roaming clients are in the global table for
2128                          * consistency only. They don't have to be
2129                          * taken into account while computing the
2130                          * global crc
2131                          */
2132                         if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2133                                 continue;
2134                         /* Temporary clients have not been announced yet, so
2135                          * they have to be skipped while computing the global
2136                          * crc
2137                          */
2138                         if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2139                                 continue;
2140 
2141                         /* find out if this global entry is announced by this
2142                          * originator
2143                          */
2144                         if (!batadv_tt_global_entry_has_orig(tt_global,
2145                                                              orig_node))
2146                                 continue;
2147 
2148                         /* use network order to read the VID: this ensures that
2149                          * every node reads the bytes in the same order.
2150                          */
2151                         tmp_vid = htons(tt_common->vid);
2152                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2153 
2154                         /* compute the CRC on flags that have to be kept in sync
2155                          * among nodes
2156                          */
2157                         flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2158                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2159 
2160                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2161                 }
2162                 rcu_read_unlock();
2163         }
2164 
2165         return crc;
2166 }
2167 
2168 /**
2169  * batadv_tt_local_crc - calculates the checksum of the local table
2170  * @bat_priv: the bat priv with all the soft interface information
2171  * @vid: VLAN identifier for which the CRC32 has to be computed
2172  *
2173  * For details about the computation, please refer to the documentation for
2174  * batadv_tt_global_crc().
2175  *
2176  * Returns the checksum of the local table
2177  */
2178 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2179                                unsigned short vid)
2180 {
2181         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2182         struct batadv_tt_common_entry *tt_common;
2183         struct hlist_head *head;
2184         u32 i, crc_tmp, crc = 0;
2185         u8 flags;
2186         __be16 tmp_vid;
2187 
2188         for (i = 0; i < hash->size; i++) {
2189                 head = &hash->table[i];
2190 
2191                 rcu_read_lock();
2192                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2193                         /* compute the CRC only for entries belonging to the
2194                          * VLAN identified by vid
2195                          */
2196                         if (tt_common->vid != vid)
2197                                 continue;
2198 
2199                         /* not yet committed clients have not to be taken into
2200                          * account while computing the CRC
2201                          */
2202                         if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2203                                 continue;
2204 
2205                         /* use network order to read the VID: this ensures that
2206                          * every node reads the bytes in the same order.
2207                          */
2208                         tmp_vid = htons(tt_common->vid);
2209                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2210 
2211                         /* compute the CRC on flags that have to be kept in sync
2212                          * among nodes
2213                          */
2214                         flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2215                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2216 
2217                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2218                 }
2219                 rcu_read_unlock();
2220         }
2221 
2222         return crc;
2223 }
2224 
2225 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2226 {
2227         struct batadv_tt_req_node *node;
2228         struct hlist_node *safe;
2229 
2230         spin_lock_bh(&bat_priv->tt.req_list_lock);
2231 
2232         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2233                 hlist_del_init(&node->list);
2234                 kfree(node);
2235         }
2236 
2237         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2238 }
2239 
2240 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2241                                        struct batadv_orig_node *orig_node,
2242                                        const void *tt_buff,
2243                                        u16 tt_buff_len)
2244 {
2245         /* Replace the old buffer only if I received something in the
2246          * last OGM (the OGM could carry no changes)
2247          */
2248         spin_lock_bh(&orig_node->tt_buff_lock);
2249         if (tt_buff_len > 0) {
2250                 kfree(orig_node->tt_buff);
2251                 orig_node->tt_buff_len = 0;
2252                 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2253                 if (orig_node->tt_buff) {
2254                         memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2255                         orig_node->tt_buff_len = tt_buff_len;
2256                 }
2257         }
2258         spin_unlock_bh(&orig_node->tt_buff_lock);
2259 }
2260 
2261 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2262 {
2263         struct batadv_tt_req_node *node;
2264         struct hlist_node *safe;
2265 
2266         spin_lock_bh(&bat_priv->tt.req_list_lock);
2267         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2268                 if (batadv_has_timed_out(node->issued_at,
2269                                          BATADV_TT_REQUEST_TIMEOUT)) {
2270                         hlist_del_init(&node->list);
2271                         kfree(node);
2272                 }
2273         }
2274         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2275 }
2276 
2277 /**
2278  * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2279  * @bat_priv: the bat priv with all the soft interface information
2280  * @orig_node: orig node this request is being issued for
2281  *
2282  * Returns the pointer to the new tt_req_node struct if no request
2283  * has already been issued for this orig_node, NULL otherwise.
2284  */
2285 static struct batadv_tt_req_node *
2286 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2287                        struct batadv_orig_node *orig_node)
2288 {
2289         struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2290 
2291         spin_lock_bh(&bat_priv->tt.req_list_lock);
2292         hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2293                 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2294                     !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2295                                           BATADV_TT_REQUEST_TIMEOUT))
2296                         goto unlock;
2297         }
2298 
2299         tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2300         if (!tt_req_node)
2301                 goto unlock;
2302 
2303         ether_addr_copy(tt_req_node->addr, orig_node->orig);
2304         tt_req_node->issued_at = jiffies;
2305 
2306         hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2307 unlock:
2308         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2309         return tt_req_node;
2310 }
2311 
2312 /**
2313  * batadv_tt_local_valid - verify that given tt entry is a valid one
2314  * @entry_ptr: to be checked local tt entry
2315  * @data_ptr: not used but definition required to satisfy the callback prototype
2316  *
2317  * Returns 1 if the entry is a valid, 0 otherwise.
2318  */
2319 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2320 {
2321         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2322 
2323         if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2324                 return 0;
2325         return 1;
2326 }
2327 
2328 static int batadv_tt_global_valid(const void *entry_ptr,
2329                                   const void *data_ptr)
2330 {
2331         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2332         const struct batadv_tt_global_entry *tt_global_entry;
2333         const struct batadv_orig_node *orig_node = data_ptr;
2334 
2335         if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2336             tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2337                 return 0;
2338 
2339         tt_global_entry = container_of(tt_common_entry,
2340                                        struct batadv_tt_global_entry,
2341                                        common);
2342 
2343         return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2344 }
2345 
2346 /**
2347  * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2348  *  specified tt hash
2349  * @bat_priv: the bat priv with all the soft interface information
2350  * @hash: hash table containing the tt entries
2351  * @tt_len: expected tvlv tt data buffer length in number of bytes
2352  * @tvlv_buff: pointer to the buffer to fill with the TT data
2353  * @valid_cb: function to filter tt change entries
2354  * @cb_data: data passed to the filter function as argument
2355  */
2356 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2357                                     struct batadv_hashtable *hash,
2358                                     void *tvlv_buff, u16 tt_len,
2359                                     int (*valid_cb)(const void *, const void *),
2360                                     void *cb_data)
2361 {
2362         struct batadv_tt_common_entry *tt_common_entry;
2363         struct batadv_tvlv_tt_change *tt_change;
2364         struct hlist_head *head;
2365         u16 tt_tot, tt_num_entries = 0;
2366         u32 i;
2367 
2368         tt_tot = batadv_tt_entries(tt_len);
2369         tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2370 
2371         rcu_read_lock();
2372         for (i = 0; i < hash->size; i++) {
2373                 head = &hash->table[i];
2374 
2375                 hlist_for_each_entry_rcu(tt_common_entry,
2376                                          head, hash_entry) {
2377                         if (tt_tot == tt_num_entries)
2378                                 break;
2379 
2380                         if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2381                                 continue;
2382 
2383                         ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2384                         tt_change->flags = tt_common_entry->flags;
2385                         tt_change->vid = htons(tt_common_entry->vid);
2386                         memset(tt_change->reserved, 0,
2387                                sizeof(tt_change->reserved));
2388 
2389                         tt_num_entries++;
2390                         tt_change++;
2391                 }
2392         }
2393         rcu_read_unlock();
2394 }
2395 
2396 /**
2397  * batadv_tt_global_check_crc - check if all the CRCs are correct
2398  * @orig_node: originator for which the CRCs have to be checked
2399  * @tt_vlan: pointer to the first tvlv VLAN entry
2400  * @num_vlan: number of tvlv VLAN entries
2401  * @create: if true, create VLAN objects if not found
2402  *
2403  * Return true if all the received CRCs match the locally stored ones, false
2404  * otherwise
2405  */
2406 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2407                                        struct batadv_tvlv_tt_vlan_data *tt_vlan,
2408                                        u16 num_vlan)
2409 {
2410         struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2411         struct batadv_orig_node_vlan *vlan;
2412         u32 crc;
2413         int i;
2414 
2415         /* check if each received CRC matches the locally stored one */
2416         for (i = 0; i < num_vlan; i++) {
2417                 tt_vlan_tmp = tt_vlan + i;
2418 
2419                 /* if orig_node is a backbone node for this VLAN, don't check
2420                  * the CRC as we ignore all the global entries over it
2421                  */
2422                 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2423                                                    orig_node->orig,
2424                                                    ntohs(tt_vlan_tmp->vid)))
2425                         continue;
2426 
2427                 vlan = batadv_orig_node_vlan_get(orig_node,
2428                                                  ntohs(tt_vlan_tmp->vid));
2429                 if (!vlan)
2430                         return false;
2431 
2432                 crc = vlan->tt.crc;
2433                 batadv_orig_node_vlan_free_ref(vlan);
2434 
2435                 if (crc != ntohl(tt_vlan_tmp->crc))
2436                         return false;
2437         }
2438 
2439         return true;
2440 }
2441 
2442 /**
2443  * batadv_tt_local_update_crc - update all the local CRCs
2444  * @bat_priv: the bat priv with all the soft interface information
2445  */
2446 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2447 {
2448         struct batadv_softif_vlan *vlan;
2449 
2450         /* recompute the global CRC for each VLAN */
2451         rcu_read_lock();
2452         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2453                 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2454         }
2455         rcu_read_unlock();
2456 }
2457 
2458 /**
2459  * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2460  * @bat_priv: the bat priv with all the soft interface information
2461  * @orig_node: the orig_node for which the CRCs have to be updated
2462  */
2463 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2464                                         struct batadv_orig_node *orig_node)
2465 {
2466         struct batadv_orig_node_vlan *vlan;
2467         u32 crc;
2468 
2469         /* recompute the global CRC for each VLAN */
2470         rcu_read_lock();
2471         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2472                 /* if orig_node is a backbone node for this VLAN, don't compute
2473                  * the CRC as we ignore all the global entries over it
2474                  */
2475                 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2476                                                    vlan->vid))
2477                         continue;
2478 
2479                 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2480                 vlan->tt.crc = crc;
2481         }
2482         rcu_read_unlock();
2483 }
2484 
2485 /**
2486  * batadv_send_tt_request - send a TT Request message to a given node
2487  * @bat_priv: the bat priv with all the soft interface information
2488  * @dst_orig_node: the destination of the message
2489  * @ttvn: the version number that the source of the message is looking for
2490  * @tt_vlan: pointer to the first tvlv VLAN object to request
2491  * @num_vlan: number of tvlv VLAN entries
2492  * @full_table: ask for the entire translation table if true, while only for the
2493  *  last TT diff otherwise
2494  */
2495 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2496                                   struct batadv_orig_node *dst_orig_node,
2497                                   u8 ttvn,
2498                                   struct batadv_tvlv_tt_vlan_data *tt_vlan,
2499                                   u16 num_vlan, bool full_table)
2500 {
2501         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2502         struct batadv_tt_req_node *tt_req_node = NULL;
2503         struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2504         struct batadv_hard_iface *primary_if;
2505         bool ret = false;
2506         int i, size;
2507 
2508         primary_if = batadv_primary_if_get_selected(bat_priv);
2509         if (!primary_if)
2510                 goto out;
2511 
2512         /* The new tt_req will be issued only if I'm not waiting for a
2513          * reply from the same orig_node yet
2514          */
2515         tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2516         if (!tt_req_node)
2517                 goto out;
2518 
2519         size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2520         tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2521         if (!tvlv_tt_data)
2522                 goto out;
2523 
2524         tvlv_tt_data->flags = BATADV_TT_REQUEST;
2525         tvlv_tt_data->ttvn = ttvn;
2526         tvlv_tt_data->num_vlan = htons(num_vlan);
2527 
2528         /* send all the CRCs within the request. This is needed by intermediate
2529          * nodes to ensure they have the correct table before replying
2530          */
2531         tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2532         for (i = 0; i < num_vlan; i++) {
2533                 tt_vlan_req->vid = tt_vlan->vid;
2534                 tt_vlan_req->crc = tt_vlan->crc;
2535 
2536                 tt_vlan_req++;
2537                 tt_vlan++;
2538         }
2539 
2540         if (full_table)
2541                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2542 
2543         batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2544                    dst_orig_node->orig, full_table ? 'F' : '.');
2545 
2546         batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2547         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2548                                  dst_orig_node->orig, BATADV_TVLV_TT, 1,
2549                                  tvlv_tt_data, size);
2550         ret = true;
2551 
2552 out:
2553         if (primary_if)
2554                 batadv_hardif_free_ref(primary_if);
2555         if (ret && tt_req_node) {
2556                 spin_lock_bh(&bat_priv->tt.req_list_lock);
2557                 /* hlist_del_init() verifies tt_req_node still is in the list */
2558                 hlist_del_init(&tt_req_node->list);
2559                 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2560                 kfree(tt_req_node);
2561         }
2562         kfree(tvlv_tt_data);
2563         return ret;
2564 }
2565 
2566 /**
2567  * batadv_send_other_tt_response - send reply to tt request concerning another
2568  *  node's translation table
2569  * @bat_priv: the bat priv with all the soft interface information
2570  * @tt_data: tt data containing the tt request information
2571  * @req_src: mac address of tt request sender
2572  * @req_dst: mac address of tt request recipient
2573  *
2574  * Returns true if tt request reply was sent, false otherwise.
2575  */
2576 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2577                                           struct batadv_tvlv_tt_data *tt_data,
2578                                           u8 *req_src, u8 *req_dst)
2579 {
2580         struct batadv_orig_node *req_dst_orig_node;
2581         struct batadv_orig_node *res_dst_orig_node = NULL;
2582         struct batadv_tvlv_tt_change *tt_change;
2583         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2584         struct batadv_tvlv_tt_vlan_data *tt_vlan;
2585         bool ret = false, full_table;
2586         u8 orig_ttvn, req_ttvn;
2587         u16 tvlv_len;
2588         s32 tt_len;
2589 
2590         batadv_dbg(BATADV_DBG_TT, bat_priv,
2591                    "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2592                    req_src, tt_data->ttvn, req_dst,
2593                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2594 
2595         /* Let's get the orig node of the REAL destination */
2596         req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2597         if (!req_dst_orig_node)
2598                 goto out;
2599 
2600         res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2601         if (!res_dst_orig_node)
2602                 goto out;
2603 
2604         orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2605         req_ttvn = tt_data->ttvn;
2606 
2607         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2608         /* this node doesn't have the requested data */
2609         if (orig_ttvn != req_ttvn ||
2610             !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2611                                         ntohs(tt_data->num_vlan)))
2612                 goto out;
2613 
2614         /* If the full table has been explicitly requested */
2615         if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2616             !req_dst_orig_node->tt_buff)
2617                 full_table = true;
2618         else
2619                 full_table = false;
2620 
2621         /* TT fragmentation hasn't been implemented yet, so send as many
2622          * TT entries fit a single packet as possible only
2623          */
2624         if (!full_table) {
2625                 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2626                 tt_len = req_dst_orig_node->tt_buff_len;
2627 
2628                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2629                                                               &tvlv_tt_data,
2630                                                               &tt_change,
2631                                                               &tt_len);
2632                 if (!tt_len)
2633                         goto unlock;
2634 
2635                 /* Copy the last orig_node's OGM buffer */
2636                 memcpy(tt_change, req_dst_orig_node->tt_buff,
2637                        req_dst_orig_node->tt_buff_len);
2638                 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2639         } else {
2640                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2641                  * in the initial part
2642                  */
2643                 tt_len = -1;
2644                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2645                                                               &tvlv_tt_data,
2646                                                               &tt_change,
2647                                                               &tt_len);
2648                 if (!tt_len)
2649                         goto out;
2650 
2651                 /* fill the rest of the tvlv with the real TT entries */
2652                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2653                                         tt_change, tt_len,
2654                                         batadv_tt_global_valid,
2655                                         req_dst_orig_node);
2656         }
2657 
2658         /* Don't send the response, if larger than fragmented packet. */
2659         tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2660         if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2661                 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2662                                          "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2663                                          res_dst_orig_node->orig);
2664                 goto out;
2665         }
2666 
2667         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2668         tvlv_tt_data->ttvn = req_ttvn;
2669 
2670         if (full_table)
2671                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2672 
2673         batadv_dbg(BATADV_DBG_TT, bat_priv,
2674                    "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2675                    res_dst_orig_node->orig, req_dst_orig_node->orig,
2676                    full_table ? 'F' : '.', req_ttvn);
2677 
2678         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2679 
2680         batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2681                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2682                                  tvlv_len);
2683 
2684         ret = true;
2685         goto out;
2686 
2687 unlock:
2688         spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2689 
2690 out:
2691         if (res_dst_orig_node)
2692                 batadv_orig_node_free_ref(res_dst_orig_node);
2693         if (req_dst_orig_node)
2694                 batadv_orig_node_free_ref(req_dst_orig_node);
2695         kfree(tvlv_tt_data);
2696         return ret;
2697 }
2698 
2699 /**
2700  * batadv_send_my_tt_response - send reply to tt request concerning this node's
2701  *  translation table
2702  * @bat_priv: the bat priv with all the soft interface information
2703  * @tt_data: tt data containing the tt request information
2704  * @req_src: mac address of tt request sender
2705  *
2706  * Returns true if tt request reply was sent, false otherwise.
2707  */
2708 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2709                                        struct batadv_tvlv_tt_data *tt_data,
2710                                        u8 *req_src)
2711 {
2712         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2713         struct batadv_hard_iface *primary_if = NULL;
2714         struct batadv_tvlv_tt_change *tt_change;
2715         struct batadv_orig_node *orig_node;
2716         u8 my_ttvn, req_ttvn;
2717         u16 tvlv_len;
2718         bool full_table;
2719         s32 tt_len;
2720 
2721         batadv_dbg(BATADV_DBG_TT, bat_priv,
2722                    "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2723                    req_src, tt_data->ttvn,
2724                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2725 
2726         spin_lock_bh(&bat_priv->tt.commit_lock);
2727 
2728         my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2729         req_ttvn = tt_data->ttvn;
2730 
2731         orig_node = batadv_orig_hash_find(bat_priv, req_src);
2732         if (!orig_node)
2733                 goto out;
2734 
2735         primary_if = batadv_primary_if_get_selected(bat_priv);
2736         if (!primary_if)
2737                 goto out;
2738 
2739         /* If the full table has been explicitly requested or the gap
2740          * is too big send the whole local translation table
2741          */
2742         if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2743             !bat_priv->tt.last_changeset)
2744                 full_table = true;
2745         else
2746                 full_table = false;
2747 
2748         /* TT fragmentation hasn't been implemented yet, so send as many
2749          * TT entries fit a single packet as possible only
2750          */
2751         if (!full_table) {
2752                 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2753 
2754                 tt_len = bat_priv->tt.last_changeset_len;
2755                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2756                                                              &tvlv_tt_data,
2757                                                              &tt_change,
2758                                                              &tt_len);
2759                 if (!tt_len)
2760                         goto unlock;
2761 
2762                 /* Copy the last orig_node's OGM buffer */
2763                 memcpy(tt_change, bat_priv->tt.last_changeset,
2764                        bat_priv->tt.last_changeset_len);
2765                 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2766         } else {
2767                 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2768 
2769                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2770                  * in the initial part
2771                  */
2772                 tt_len = -1;
2773                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2774                                                              &tvlv_tt_data,
2775                                                              &tt_change,
2776                                                              &tt_len);
2777                 if (!tt_len)
2778                         goto out;
2779 
2780                 /* fill the rest of the tvlv with the real TT entries */
2781                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2782                                         tt_change, tt_len,
2783                                         batadv_tt_local_valid, NULL);
2784         }
2785 
2786         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2787         tvlv_tt_data->ttvn = req_ttvn;
2788 
2789         if (full_table)
2790                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2791 
2792         batadv_dbg(BATADV_DBG_TT, bat_priv,
2793                    "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2794                    orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2795 
2796         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2797 
2798         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2799                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2800                                  tvlv_len);
2801 
2802         goto out;
2803 
2804 unlock:
2805         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2806 out:
2807         spin_unlock_bh(&bat_priv->tt.commit_lock);
2808         if (orig_node)
2809                 batadv_orig_node_free_ref(orig_node);
2810         if (primary_if)
2811                 batadv_hardif_free_ref(primary_if);
2812         kfree(tvlv_tt_data);
2813         /* The packet was for this host, so it doesn't need to be re-routed */
2814         return true;
2815 }
2816 
2817 /**
2818  * batadv_send_tt_response - send reply to tt request
2819  * @bat_priv: the bat priv with all the soft interface information
2820  * @tt_data: tt data containing the tt request information
2821  * @req_src: mac address of tt request sender
2822  * @req_dst: mac address of tt request recipient
2823  *
2824  * Returns true if tt request reply was sent, false otherwise.
2825  */
2826 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2827                                     struct batadv_tvlv_tt_data *tt_data,
2828                                     u8 *req_src, u8 *req_dst)
2829 {
2830         if (batadv_is_my_mac(bat_priv, req_dst))
2831                 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2832         return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2833                                              req_dst);
2834 }
2835 
2836 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2837                                       struct batadv_orig_node *orig_node,
2838                                       struct batadv_tvlv_tt_change *tt_change,
2839                                       u16 tt_num_changes, u8 ttvn)
2840 {
2841         int i;
2842         int roams;
2843 
2844         for (i = 0; i < tt_num_changes; i++) {
2845                 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2846                         roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2847                         batadv_tt_global_del(bat_priv, orig_node,
2848                                              (tt_change + i)->addr,
2849                                              ntohs((tt_change + i)->vid),
2850                                              "tt removed by changes",
2851                                              roams);
2852                 } else {
2853                         if (!batadv_tt_global_add(bat_priv, orig_node,
2854                                                   (tt_change + i)->addr,
2855                                                   ntohs((tt_change + i)->vid),
2856                                                   (tt_change + i)->flags, ttvn))
2857                                 /* In case of problem while storing a
2858                                  * global_entry, we stop the updating
2859                                  * procedure without committing the
2860                                  * ttvn change. This will avoid to send
2861                                  * corrupted data on tt_request
2862                                  */
2863                                 return;
2864                 }
2865         }
2866         set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2867 }
2868 
2869 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2870                                   struct batadv_tvlv_tt_change *tt_change,
2871                                   u8 ttvn, u8 *resp_src,
2872                                   u16 num_entries)
2873 {
2874         struct batadv_orig_node *orig_node;
2875 
2876         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2877         if (!orig_node)
2878                 goto out;
2879 
2880         /* Purge the old table first.. */
2881         batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2882                                   "Received full table");
2883 
2884         _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2885                                   ttvn);
2886 
2887         spin_lock_bh(&orig_node->tt_buff_lock);
2888         kfree(orig_node->tt_buff);
2889         orig_node->tt_buff_len = 0;
2890         orig_node->tt_buff = NULL;
2891         spin_unlock_bh(&orig_node->tt_buff_lock);
2892 
2893         atomic_set(&orig_node->last_ttvn, ttvn);
2894 
2895 out:
2896         if (orig_node)
2897                 batadv_orig_node_free_ref(orig_node);
2898 }
2899 
2900 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2901                                      struct batadv_orig_node *orig_node,
2902                                      u16 tt_num_changes, u8 ttvn,
2903                                      struct batadv_tvlv_tt_change *tt_change)
2904 {
2905         _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2906                                   tt_num_changes, ttvn);
2907 
2908         batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2909                                    batadv_tt_len(tt_num_changes));
2910         atomic_set(&orig_node->last_ttvn, ttvn);
2911 }
2912 
2913 /**
2914  * batadv_is_my_client - check if a client is served by the local node
2915  * @bat_priv: the bat priv with all the soft interface information
2916  * @addr: the mac address of the client to check
2917  * @vid: VLAN identifier
2918  *
2919  * Returns true if the client is served by this node, false otherwise.
2920  */
2921 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
2922                          unsigned short vid)
2923 {
2924         struct batadv_tt_local_entry *tt_local_entry;
2925         bool ret = false;
2926 
2927         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2928         if (!tt_local_entry)
2929                 goto out;
2930         /* Check if the client has been logically deleted (but is kept for
2931          * consistency purpose)
2932          */
2933         if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2934             (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2935                 goto out;
2936         ret = true;
2937 out:
2938         if (tt_local_entry)
2939                 batadv_tt_local_entry_free_ref(tt_local_entry);
2940         return ret;
2941 }
2942 
2943 /**
2944  * batadv_handle_tt_response - process incoming tt reply
2945  * @bat_priv: the bat priv with all the soft interface information
2946  * @tt_data: tt data containing the tt request information
2947  * @resp_src: mac address of tt reply sender
2948  * @num_entries: number of tt change entries appended to the tt data
2949  */
2950 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2951                                       struct batadv_tvlv_tt_data *tt_data,
2952                                       u8 *resp_src, u16 num_entries)
2953 {
2954         struct batadv_tt_req_node *node;
2955         struct hlist_node *safe;
2956         struct batadv_orig_node *orig_node = NULL;
2957         struct batadv_tvlv_tt_change *tt_change;
2958         u8 *tvlv_ptr = (u8 *)tt_data;
2959         u16 change_offset;
2960 
2961         batadv_dbg(BATADV_DBG_TT, bat_priv,
2962                    "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2963                    resp_src, tt_data->ttvn, num_entries,
2964                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2965 
2966         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2967         if (!orig_node)
2968                 goto out;
2969 
2970         spin_lock_bh(&orig_node->tt_lock);
2971 
2972         change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2973         change_offset *= ntohs(tt_data->num_vlan);
2974         change_offset += sizeof(*tt_data);
2975         tvlv_ptr += change_offset;
2976 
2977         tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2978         if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2979                 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2980                                       resp_src, num_entries);
2981         } else {
2982                 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2983                                          tt_data->ttvn, tt_change);
2984         }
2985 
2986         /* Recalculate the CRC for this orig_node and store it */
2987         batadv_tt_global_update_crc(bat_priv, orig_node);
2988 
2989         spin_unlock_bh(&orig_node->tt_lock);
2990 
2991         /* Delete the tt_req_node from pending tt_requests list */
2992         spin_lock_bh(&bat_priv->tt.req_list_lock);
2993         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2994                 if (!batadv_compare_eth(node->addr, resp_src))
2995                         continue;
2996                 hlist_del_init(&node->list);
2997                 kfree(node);
2998         }
2999 
3000         spin_unlock_bh(&bat_priv->tt.req_list_lock);
3001 out:
3002         if (orig_node)
3003                 batadv_orig_node_free_ref(orig_node);
3004 }
3005 
3006 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3007 {
3008         struct batadv_tt_roam_node *node, *safe;
3009 
3010         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3011 
3012         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3013                 list_del(&node->list);
3014                 kfree(node);
3015         }
3016 
3017         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3018 }
3019 
3020 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3021 {
3022         struct batadv_tt_roam_node *node, *safe;
3023 
3024         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3025         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3026                 if (!batadv_has_timed_out(node->first_time,
3027                                           BATADV_ROAMING_MAX_TIME))
3028                         continue;
3029 
3030                 list_del(&node->list);
3031                 kfree(node);
3032         }
3033         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3034 }
3035 
3036 /* This function checks whether the client already reached the
3037  * maximum number of possible roaming phases. In this case the ROAMING_ADV
3038  * will not be sent.
3039  *
3040  * returns true if the ROAMING_ADV can be sent, false otherwise
3041  */
3042 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3043 {
3044         struct batadv_tt_roam_node *tt_roam_node;
3045         bool ret = false;
3046 
3047         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3048         /* The new tt_req will be issued only if I'm not waiting for a
3049          * reply from the same orig_node yet
3050          */
3051         list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3052                 if (!batadv_compare_eth(tt_roam_node->addr, client))
3053                         continue;
3054 
3055                 if (batadv_has_timed_out(tt_roam_node->first_time,
3056                                          BATADV_ROAMING_MAX_TIME))
3057                         continue;
3058 
3059                 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3060                         /* Sorry, you roamed too many times! */
3061                         goto unlock;
3062                 ret = true;
3063                 break;
3064         }
3065 
3066         if (!ret) {
3067                 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3068                 if (!tt_roam_node)
3069                         goto unlock;
3070 
3071                 tt_roam_node->first_time = jiffies;
3072                 atomic_set(&tt_roam_node->counter,
3073                            BATADV_ROAMING_MAX_COUNT - 1);
3074                 ether_addr_copy(tt_roam_node->addr, client);
3075 
3076                 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3077                 ret = true;
3078         }
3079 
3080 unlock:
3081         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3082         return ret;
3083 }
3084 
3085 /**
3086  * batadv_send_roam_adv - send a roaming advertisement message
3087  * @bat_priv: the bat priv with all the soft interface information
3088  * @client: mac address of the roaming client
3089  * @vid: VLAN identifier
3090  * @orig_node: message destination
3091  *
3092  * Send a ROAMING_ADV message to the node which was previously serving this
3093  * client. This is done to inform the node that from now on all traffic destined
3094  * for this particular roamed client has to be forwarded to the sender of the
3095  * roaming message.
3096  */
3097 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3098                                  unsigned short vid,
3099                                  struct batadv_orig_node *orig_node)
3100 {
3101         struct batadv_hard_iface *primary_if;
3102         struct batadv_tvlv_roam_adv tvlv_roam;
3103 
3104         primary_if = batadv_primary_if_get_selected(bat_priv);
3105         if (!primary_if)
3106                 goto out;
3107 
3108         /* before going on we have to check whether the client has
3109          * already roamed to us too many times
3110          */
3111         if (!batadv_tt_check_roam_count(bat_priv, client))
3112                 goto out;
3113 
3114         batadv_dbg(BATADV_DBG_TT, bat_priv,
3115                    "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3116                    orig_node->orig, client, BATADV_PRINT_VID(vid));
3117 
3118         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3119 
3120         memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3121         tvlv_roam.vid = htons(vid);
3122 
3123         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3124                                  orig_node->orig, BATADV_TVLV_ROAM, 1,
3125                                  &tvlv_roam, sizeof(tvlv_roam));
3126 
3127 out:
3128         if (primary_if)
3129                 batadv_hardif_free_ref(primary_if);
3130 }
3131 
3132 static void batadv_tt_purge(struct work_struct *work)
3133 {
3134         struct delayed_work *delayed_work;
3135         struct batadv_priv_tt *priv_tt;
3136         struct batadv_priv *bat_priv;
3137 
3138         delayed_work = container_of(work, struct delayed_work, work);
3139         priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3140         bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3141 
3142         batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3143         batadv_tt_global_purge(bat_priv);
3144         batadv_tt_req_purge(bat_priv);
3145         batadv_tt_roam_purge(bat_priv);
3146 
3147         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3148                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3149 }
3150 
3151 void batadv_tt_free(struct batadv_priv *bat_priv)
3152 {
3153         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3154         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3155 
3156         cancel_delayed_work_sync(&bat_priv->tt.work);
3157 
3158         batadv_tt_local_table_free(bat_priv);
3159         batadv_tt_global_table_free(bat_priv);
3160         batadv_tt_req_list_free(bat_priv);
3161         batadv_tt_changes_list_free(bat_priv);
3162         batadv_tt_roam_list_free(bat_priv);
3163 
3164         kfree(bat_priv->tt.last_changeset);
3165 }
3166 
3167 /**
3168  * batadv_tt_local_set_flags - set or unset the specified flags on the local
3169  *  table and possibly count them in the TT size
3170  * @bat_priv: the bat priv with all the soft interface information
3171  * @flags: the flag to switch
3172  * @enable: whether to set or unset the flag
3173  * @count: whether to increase the TT size by the number of changed entries
3174  */
3175 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3176                                       bool enable, bool count)
3177 {
3178         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3179         struct batadv_tt_common_entry *tt_common_entry;
3180         u16 changed_num = 0;
3181         struct hlist_head *head;
3182         u32 i;
3183 
3184         if (!hash)
3185                 return;
3186 
3187         for (i = 0; i < hash->size; i++) {
3188                 head = &hash->table[i];
3189 
3190                 rcu_read_lock();
3191                 hlist_for_each_entry_rcu(tt_common_entry,
3192                                          head, hash_entry) {
3193                         if (enable) {
3194                                 if ((tt_common_entry->flags & flags) == flags)
3195                                         continue;
3196                                 tt_common_entry->flags |= flags;
3197                         } else {
3198                                 if (!(tt_common_entry->flags & flags))
3199                                         continue;
3200                                 tt_common_entry->flags &= ~flags;
3201                         }
3202                         changed_num++;
3203 
3204                         if (!count)
3205                                 continue;
3206 
3207                         batadv_tt_local_size_inc(bat_priv,
3208                                                  tt_common_entry->vid);
3209                 }
3210                 rcu_read_unlock();
3211         }
3212 }
3213 
3214 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3215 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3216 {
3217         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3218         struct batadv_tt_common_entry *tt_common;
3219         struct batadv_tt_local_entry *tt_local;
3220         struct batadv_softif_vlan *vlan;
3221         struct hlist_node *node_tmp;
3222         struct hlist_head *head;
3223         spinlock_t *list_lock; /* protects write access to the hash lists */
3224         u32 i;
3225 
3226         if (!hash)
3227                 return;
3228 
3229         for (i = 0; i < hash->size; i++) {
3230                 head = &hash->table[i];
3231                 list_lock = &hash->list_locks[i];
3232 
3233                 spin_lock_bh(list_lock);
3234                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3235                                           hash_entry) {
3236                         if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3237                                 continue;
3238 
3239                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3240                                    "Deleting local tt entry (%pM, vid: %d): pending\n",
3241                                    tt_common->addr,
3242                                    BATADV_PRINT_VID(tt_common->vid));
3243 
3244                         batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3245                         hlist_del_rcu(&tt_common->hash_entry);
3246                         tt_local = container_of(tt_common,
3247                                                 struct batadv_tt_local_entry,
3248                                                 common);
3249 
3250                         /* decrease the reference held for this vlan */
3251                         vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3252                         if (vlan) {
3253                                 batadv_softif_vlan_free_ref(vlan);
3254                                 batadv_softif_vlan_free_ref(vlan);
3255                         }
3256 
3257                         batadv_tt_local_entry_free_ref(tt_local);
3258                 }
3259                 spin_unlock_bh(list_lock);
3260         }
3261 }
3262 
3263 /**
3264  * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3265  *  which have been queued in the time since the last commit
3266  * @bat_priv: the bat priv with all the soft interface information
3267  *
3268  * Caller must hold tt->commit_lock.
3269  */
3270 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3271 {
3272         lockdep_assert_held(&bat_priv->tt.commit_lock);
3273 
3274         /* Update multicast addresses in local translation table */
3275         batadv_mcast_mla_update(bat_priv);
3276 
3277         if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3278                 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3279                         batadv_tt_tvlv_container_update(bat_priv);
3280                 return;
3281         }
3282 
3283         batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3284 
3285         batadv_tt_local_purge_pending_clients(bat_priv);
3286         batadv_tt_local_update_crc(bat_priv);
3287 
3288         /* Increment the TTVN only once per OGM interval */
3289         atomic_inc(&bat_priv->tt.vn);
3290         batadv_dbg(BATADV_DBG_TT, bat_priv,
3291                    "Local changes committed, updating to ttvn %u\n",
3292                    (u8)atomic_read(&bat_priv->tt.vn));
3293 
3294         /* reset the sending counter */
3295         atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3296         batadv_tt_tvlv_container_update(bat_priv);
3297 }
3298 
3299 /**
3300  * batadv_tt_local_commit_changes - commit all pending local tt changes which
3301  *  have been queued in the time since the last commit
3302  * @bat_priv: the bat priv with all the soft interface information
3303  */
3304 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3305 {
3306         spin_lock_bh(&bat_priv->tt.commit_lock);
3307         batadv_tt_local_commit_changes_nolock(bat_priv);
3308         spin_unlock_bh(&bat_priv->tt.commit_lock);
3309 }
3310 
3311 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3312                            unsigned short vid)
3313 {
3314         struct batadv_tt_local_entry *tt_local_entry = NULL;
3315         struct batadv_tt_global_entry *tt_global_entry = NULL;
3316         struct batadv_softif_vlan *vlan;
3317         bool ret = false;
3318 
3319         vlan = batadv_softif_vlan_get(bat_priv, vid);
3320         if (!vlan || !atomic_read(&vlan->ap_isolation))
3321                 goto out;
3322 
3323         tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3324         if (!tt_local_entry)
3325                 goto out;
3326 
3327         tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3328         if (!tt_global_entry)
3329                 goto out;
3330 
3331         if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3332                 goto out;
3333 
3334         ret = true;
3335 
3336 out:
3337         if (vlan)
3338                 batadv_softif_vlan_free_ref(vlan);
3339         if (tt_global_entry)
3340                 batadv_tt_global_entry_free_ref(tt_global_entry);
3341         if (tt_local_entry)
3342                 batadv_tt_local_entry_free_ref(tt_local_entry);
3343         return ret;
3344 }
3345 
3346 /**
3347  * batadv_tt_update_orig - update global translation table with new tt
3348  *  information received via ogms
3349  * @bat_priv: the bat priv with all the soft interface information
3350  * @orig: the orig_node of the ogm
3351  * @tt_vlan: pointer to the first tvlv VLAN entry
3352  * @tt_num_vlan: number of tvlv VLAN entries
3353  * @tt_change: pointer to the first entry in the TT buffer
3354  * @tt_num_changes: number of tt changes inside the tt buffer
3355  * @ttvn: translation table version number of this changeset
3356  * @tt_crc: crc32 checksum of orig node's translation table
3357  */
3358 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3359                                   struct batadv_orig_node *orig_node,
3360                                   const void *tt_buff, u16 tt_num_vlan,
3361                                   struct batadv_tvlv_tt_change *tt_change,
3362                                   u16 tt_num_changes, u8 ttvn)
3363 {
3364         u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3365         struct batadv_tvlv_tt_vlan_data *tt_vlan;
3366         bool full_table = true;
3367         bool has_tt_init;
3368 
3369         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3370         has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3371                                &orig_node->capa_initialized);
3372 
3373         /* orig table not initialised AND first diff is in the OGM OR the ttvn
3374          * increased by one -> we can apply the attached changes
3375          */
3376         if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3377                 /* the OGM could not contain the changes due to their size or
3378                  * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3379                  * times.
3380                  * In this case send a tt request
3381                  */
3382                 if (!tt_num_changes) {
3383                         full_table = false;
3384                         goto request_table;
3385                 }
3386 
3387                 spin_lock_bh(&orig_node->tt_lock);
3388 
3389                 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3390                                          ttvn, tt_change);
3391 
3392                 /* Even if we received the precomputed crc with the OGM, we
3393                  * prefer to recompute it to spot any possible inconsistency
3394                  * in the global table
3395                  */
3396                 batadv_tt_global_update_crc(bat_priv, orig_node);
3397 
3398                 spin_unlock_bh(&orig_node->tt_lock);
3399 
3400                 /* The ttvn alone is not enough to guarantee consistency
3401                  * because a single value could represent different states
3402                  * (due to the wrap around). Thus a node has to check whether
3403                  * the resulting table (after applying the changes) is still
3404                  * consistent or not. E.g. a node could disconnect while its
3405                  * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3406                  * checking the CRC value is mandatory to detect the
3407                  * inconsistency
3408                  */
3409                 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3410                                                 tt_num_vlan))
3411                         goto request_table;
3412         } else {
3413                 /* if we missed more than one change or our tables are not
3414                  * in sync anymore -> request fresh tt data
3415                  */
3416                 if (!has_tt_init || ttvn != orig_ttvn ||
3417                     !batadv_tt_global_check_crc(orig_node, tt_vlan,
3418                                                 tt_num_vlan)) {
3419 request_table:
3420                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3421                                    "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3422                                    orig_node->orig, ttvn, orig_ttvn,
3423                                    tt_num_changes);
3424                         batadv_send_tt_request(bat_priv, orig_node, ttvn,
3425                                                tt_vlan, tt_num_vlan,
3426                                                full_table);
3427                         return;
3428                 }
3429         }
3430 }
3431 
3432 /**
3433  * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3434  * @bat_priv: the bat priv with all the soft interface information
3435  * @addr: the mac address of the client to check
3436  * @vid: VLAN identifier
3437  *
3438  * Returns true if we know that the client has moved from its old originator
3439  * to another one. This entry is still kept for consistency purposes and will be
3440  * deleted later by a DEL or because of timeout
3441  */
3442 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3443                                         u8 *addr, unsigned short vid)
3444 {
3445         struct batadv_tt_global_entry *tt_global_entry;
3446         bool ret = false;
3447 
3448         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3449         if (!tt_global_entry)
3450                 goto out;
3451 
3452         ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3453         batadv_tt_global_entry_free_ref(tt_global_entry);
3454 out:
3455         return ret;
3456 }
3457 
3458 /**
3459  * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3460  * @bat_priv: the bat priv with all the soft interface information
3461  * @addr: the mac address of the local client to query
3462  * @vid: VLAN identifier
3463  *
3464  * Returns true if the local client is known to be roaming (it is not served by
3465  * this node anymore) or not. If yes, the client is still present in the table
3466  * to keep the latter consistent with the node TTVN
3467  */
3468 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3469                                        u8 *addr, unsigned short vid)
3470 {
3471         struct batadv_tt_local_entry *tt_local_entry;
3472         bool ret = false;
3473 
3474         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3475         if (!tt_local_entry)
3476                 goto out;
3477 
3478         ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3479         batadv_tt_local_entry_free_ref(tt_local_entry);
3480 out:
3481         return ret;
3482 }
3483 
3484 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3485                                           struct batadv_orig_node *orig_node,
3486                                           const unsigned char *addr,
3487                                           unsigned short vid)
3488 {
3489         bool ret = false;
3490 
3491         if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3492                                   BATADV_TT_CLIENT_TEMP,
3493                                   atomic_read(&orig_node->last_ttvn)))
3494                 goto out;
3495 
3496         batadv_dbg(BATADV_DBG_TT, bat_priv,
3497                    "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3498                    addr, BATADV_PRINT_VID(vid), orig_node->orig);
3499         ret = true;
3500 out:
3501         return ret;
3502 }
3503 
3504 /**
3505  * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3506  *  maximum packet size that can be transported through the mesh
3507  * @soft_iface: netdev struct of the mesh interface
3508  *
3509  * Remove entries older than 'timeout' and half timeout if more entries need
3510  * to be removed.
3511  */
3512 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3513 {
3514         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3515         int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3516         int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3517         bool reduced = false;
3518 
3519         spin_lock_bh(&bat_priv->tt.commit_lock);
3520 
3521         while (true) {
3522                 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3523                 if (packet_size_max >= table_size)
3524                         break;
3525 
3526                 batadv_tt_local_purge(bat_priv, timeout);
3527                 batadv_tt_local_purge_pending_clients(bat_priv);
3528 
3529                 timeout /= 2;
3530                 reduced = true;
3531                 net_ratelimited_function(batadv_info, soft_iface,
3532                                          "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3533                                          packet_size_max);
3534         }
3535 
3536         /* commit these changes immediately, to avoid synchronization problem
3537          * with the TTVN
3538          */
3539         if (reduced)
3540                 batadv_tt_local_commit_changes_nolock(bat_priv);
3541 
3542         spin_unlock_bh(&bat_priv->tt.commit_lock);
3543 }
3544 
3545 /**
3546  * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3547  * @bat_priv: the bat priv with all the soft interface information
3548  * @orig: the orig_node of the ogm
3549  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3550  * @tvlv_value: tvlv buffer containing the gateway data
3551  * @tvlv_value_len: tvlv buffer length
3552  */
3553 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3554                                           struct batadv_orig_node *orig,
3555                                           u8 flags, void *tvlv_value,
3556                                           u16 tvlv_value_len)
3557 {
3558         struct batadv_tvlv_tt_vlan_data *tt_vlan;
3559         struct batadv_tvlv_tt_change *tt_change;
3560         struct batadv_tvlv_tt_data *tt_data;
3561         u16 num_entries, num_vlan;
3562 
3563         if (tvlv_value_len < sizeof(*tt_data))
3564                 return;
3565 
3566         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3567         tvlv_value_len -= sizeof(*tt_data);
3568 
3569         num_vlan = ntohs(tt_data->num_vlan);
3570 
3571         if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3572                 return;
3573 
3574         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3575         tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3576         tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3577 
3578         num_entries = batadv_tt_entries(tvlv_value_len);
3579 
3580         batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3581                               num_entries, tt_data->ttvn);
3582 }
3583 
3584 /**
3585  * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3586  *  container
3587  * @bat_priv: the bat priv with all the soft interface information
3588  * @src: mac address of tt tvlv sender
3589  * @dst: mac address of tt tvlv recipient
3590  * @tvlv_value: tvlv buffer containing the tt data
3591  * @tvlv_value_len: tvlv buffer length
3592  *
3593  * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3594  * otherwise.
3595  */
3596 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3597                                              u8 *src, u8 *dst,
3598                                              void *tvlv_value,
3599                                              u16 tvlv_value_len)
3600 {
3601         struct batadv_tvlv_tt_data *tt_data;
3602         u16 tt_vlan_len, tt_num_entries;
3603         char tt_flag;
3604         bool ret;
3605 
3606         if (tvlv_value_len < sizeof(*tt_data))
3607                 return NET_RX_SUCCESS;
3608 
3609         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3610         tvlv_value_len -= sizeof(*tt_data);
3611 
3612         tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3613         tt_vlan_len *= ntohs(tt_data->num_vlan);
3614 
3615         if (tvlv_value_len < tt_vlan_len)
3616                 return NET_RX_SUCCESS;
3617 
3618         tvlv_value_len -= tt_vlan_len;
3619         tt_num_entries = batadv_tt_entries(tvlv_value_len);
3620 
3621         switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3622         case BATADV_TT_REQUEST:
3623                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3624 
3625                 /* If this node cannot provide a TT response the tt_request is
3626                  * forwarded
3627                  */
3628                 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3629                 if (!ret) {
3630                         if (tt_data->flags & BATADV_TT_FULL_TABLE)
3631                                 tt_flag = 'F';
3632                         else
3633                                 tt_flag = '.';
3634 
3635                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3636                                    "Routing TT_REQUEST to %pM [%c]\n",
3637                                    dst, tt_flag);
3638                         /* tvlv API will re-route the packet */
3639                         return NET_RX_DROP;
3640                 }
3641                 break;
3642         case BATADV_TT_RESPONSE:
3643                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3644 
3645                 if (batadv_is_my_mac(bat_priv, dst)) {
3646                         batadv_handle_tt_response(bat_priv, tt_data,
3647                                                   src, tt_num_entries);
3648                         return NET_RX_SUCCESS;
3649                 }
3650 
3651                 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3652                         tt_flag =  'F';
3653                 else
3654                         tt_flag = '.';
3655 
3656                 batadv_dbg(BATADV_DBG_TT, bat_priv,
3657                            "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3658 
3659                 /* tvlv API will re-route the packet */
3660                 return NET_RX_DROP;
3661         }
3662 
3663         return NET_RX_SUCCESS;
3664 }
3665 
3666 /**
3667  * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3668  * @bat_priv: the bat priv with all the soft interface information
3669  * @src: mac address of tt tvlv sender
3670  * @dst: mac address of tt tvlv recipient
3671  * @tvlv_value: tvlv buffer containing the tt data
3672  * @tvlv_value_len: tvlv buffer length
3673  *
3674  * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3675  * otherwise.
3676  */
3677 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3678                                                u8 *src, u8 *dst,
3679                                                void *tvlv_value,
3680                                                u16 tvlv_value_len)
3681 {
3682         struct batadv_tvlv_roam_adv *roaming_adv;
3683         struct batadv_orig_node *orig_node = NULL;
3684 
3685         /* If this node is not the intended recipient of the
3686          * roaming advertisement the packet is forwarded
3687          * (the tvlv API will re-route the packet).
3688          */
3689         if (!batadv_is_my_mac(bat_priv, dst))
3690                 return NET_RX_DROP;
3691 
3692         if (tvlv_value_len < sizeof(*roaming_adv))
3693                 goto out;
3694 
3695         orig_node = batadv_orig_hash_find(bat_priv, src);
3696         if (!orig_node)
3697                 goto out;
3698 
3699         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3700         roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3701 
3702         batadv_dbg(BATADV_DBG_TT, bat_priv,
3703                    "Received ROAMING_ADV from %pM (client %pM)\n",
3704                    src, roaming_adv->client);
3705 
3706         batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3707                              ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3708                              atomic_read(&orig_node->last_ttvn) + 1);
3709 
3710 out:
3711         if (orig_node)
3712                 batadv_orig_node_free_ref(orig_node);
3713         return NET_RX_SUCCESS;
3714 }
3715 
3716 /**
3717  * batadv_tt_init - initialise the translation table internals
3718  * @bat_priv: the bat priv with all the soft interface information
3719  *
3720  * Return 0 on success or negative error number in case of failure.
3721  */
3722 int batadv_tt_init(struct batadv_priv *bat_priv)
3723 {
3724         int ret;
3725 
3726         /* synchronized flags must be remote */
3727         BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3728 
3729         ret = batadv_tt_local_init(bat_priv);
3730         if (ret < 0)
3731                 return ret;
3732 
3733         ret = batadv_tt_global_init(bat_priv);
3734         if (ret < 0)
3735                 return ret;
3736 
3737         batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3738                                      batadv_tt_tvlv_unicast_handler_v1,
3739                                      BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3740 
3741         batadv_tvlv_handler_register(bat_priv, NULL,
3742                                      batadv_roam_tvlv_unicast_handler_v1,
3743                                      BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3744 
3745         INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3746         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3747                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3748 
3749         return 1;
3750 }
3751 
3752 /**
3753  * batadv_tt_global_is_isolated - check if a client is marked as isolated
3754  * @bat_priv: the bat priv with all the soft interface information
3755  * @addr: the mac address of the client
3756  * @vid: the identifier of the VLAN where this client is connected
3757  *
3758  * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3759  * otherwise
3760  */
3761 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3762                                   const u8 *addr, unsigned short vid)
3763 {
3764         struct batadv_tt_global_entry *tt;
3765         bool ret;
3766 
3767         tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3768         if (!tt)
3769                 return false;
3770 
3771         ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3772 
3773         batadv_tt_global_entry_free_ref(tt);
3774 
3775         return ret;
3776 }
3777 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp