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

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

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

~ [ 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