batman-adv: properly convert flag into a boolean value
[deliverable/linux.git] / net / batman-adv / translation-table.c
1 /* Copyright (C) 2007-2012 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/crc16.h>
31
32 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
34 static void batadv_tt_purge(struct work_struct *work);
35 static void
36 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
37 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
41
42 /* returns 1 if they are the same mac addr */
43 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
44 {
45 const void *data1 = container_of(node, struct batadv_tt_common_entry,
46 hash_entry);
47
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49 }
50
51 static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
52 {
53 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
55 msecs_to_jiffies(5000));
56 }
57
58 static struct batadv_tt_common_entry *
59 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
60 {
61 struct hlist_head *head;
62 struct hlist_node *node;
63 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
65 uint32_t index;
66
67 if (!hash)
68 return NULL;
69
70 index = batadv_choose_orig(data, hash->size);
71 head = &hash->table[index];
72
73 rcu_read_lock();
74 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
75 if (!batadv_compare_eth(tt_common_entry, data))
76 continue;
77
78 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
79 continue;
80
81 tt_common_entry_tmp = tt_common_entry;
82 break;
83 }
84 rcu_read_unlock();
85
86 return tt_common_entry_tmp;
87 }
88
89 static struct batadv_tt_local_entry *
90 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
91 {
92 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
94
95 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
96 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
98 struct batadv_tt_local_entry,
99 common);
100 return tt_local_entry;
101 }
102
103 static struct batadv_tt_global_entry *
104 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
105 {
106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
108
109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
112 struct batadv_tt_global_entry,
113 common);
114 return tt_global_entry;
115
116 }
117
118 static void
119 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
120 {
121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
123 }
124
125 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
126 {
127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
129
130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
133
134 kfree(tt_global_entry);
135 }
136
137 static void
138 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
139 {
140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
141 batadv_tt_global_del_orig_list(tt_global_entry);
142 call_rcu(&tt_global_entry->common.rcu,
143 batadv_tt_global_entry_free_rcu);
144 }
145 }
146
147 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
148 {
149 struct batadv_tt_orig_list_entry *orig_entry;
150
151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
152 batadv_orig_node_free_ref(orig_entry->orig_node);
153 kfree(orig_entry);
154 }
155
156 static void
157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
158 {
159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
164 }
165
166 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
167 const uint8_t *addr, uint8_t flags)
168 {
169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
178 tt_change_node->change.flags = flags;
179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
182
183 /* check for ADD+DEL or DEL+ADD events */
184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
195 * clients
196 */
197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203 del:
204 list_del(&entry->list);
205 kfree(entry);
206 kfree(tt_change_node);
207 event_removed = true;
208 goto unlock;
209 }
210
211 /* track the change in the OGMinterval list */
212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
213
214 unlock:
215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
216
217 if (event_removed)
218 atomic_dec(&bat_priv->tt.local_changes);
219 else
220 atomic_inc(&bat_priv->tt.local_changes);
221 }
222
223 int batadv_tt_len(int changes_num)
224 {
225 return changes_num * sizeof(struct batadv_tt_change);
226 }
227
228 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
229 {
230 if (bat_priv->tt.local_hash)
231 return 0;
232
233 bat_priv->tt.local_hash = batadv_hash_new(1024);
234
235 if (!bat_priv->tt.local_hash)
236 return -ENOMEM;
237
238 return 0;
239 }
240
241 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
242 int ifindex)
243 {
244 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 struct batadv_tt_local_entry *tt_local_entry = NULL;
246 struct batadv_tt_global_entry *tt_global_entry = NULL;
247 struct hlist_head *head;
248 struct hlist_node *node;
249 struct batadv_tt_orig_list_entry *orig_entry;
250 int hash_added;
251
252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
253
254 if (tt_local_entry) {
255 tt_local_entry->last_seen = jiffies;
256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
258 goto out;
259 }
260
261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
262 if (!tt_local_entry)
263 goto out;
264
265 batadv_dbg(BATADV_DBG_TT, bat_priv,
266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
267 (uint8_t)atomic_read(&bat_priv->tt.vn));
268
269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
270 tt_local_entry->common.flags = BATADV_NO_FLAGS;
271 if (batadv_is_wifi_iface(ifindex))
272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
273 atomic_set(&tt_local_entry->common.refcount, 2);
274 tt_local_entry->last_seen = jiffies;
275 tt_local_entry->common.added_at = tt_local_entry->last_seen;
276
277 /* the batman interface mac address should never be purged */
278 if (batadv_compare_eth(addr, soft_iface->dev_addr))
279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
280
281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment
283 * (consistency check)
284 */
285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
286
287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
288 batadv_choose_orig,
289 &tt_local_entry->common,
290 &tt_local_entry->common.hash_entry);
291
292 if (unlikely(hash_added != 0)) {
293 /* remove the reference for the hash */
294 batadv_tt_local_entry_free_ref(tt_local_entry);
295 goto out;
296 }
297
298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
299
300 /* remove address from global hash if present */
301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
302
303 /* Check whether it is a roaming! */
304 if (tt_global_entry) {
305 /* These node are probably going to update their tt table */
306 head = &tt_global_entry->orig_list;
307 rcu_read_lock();
308 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
309 orig_entry->orig_node->tt_poss_change = true;
310
311 batadv_send_roam_adv(bat_priv,
312 tt_global_entry->common.addr,
313 orig_entry->orig_node);
314 }
315 rcu_read_unlock();
316 /* The global entry has to be marked as ROAMING and
317 * has to be kept for consistency purpose
318 */
319 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
320 tt_global_entry->roam_at = jiffies;
321 }
322 out:
323 if (tt_local_entry)
324 batadv_tt_local_entry_free_ref(tt_local_entry);
325 if (tt_global_entry)
326 batadv_tt_global_entry_free_ref(tt_global_entry);
327 }
328
329 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
330 int *packet_buff_len,
331 int min_packet_len,
332 int new_packet_len)
333 {
334 unsigned char *new_buff;
335
336 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
337
338 /* keep old buffer if kmalloc should fail */
339 if (new_buff) {
340 memcpy(new_buff, *packet_buff, min_packet_len);
341 kfree(*packet_buff);
342 *packet_buff = new_buff;
343 *packet_buff_len = new_packet_len;
344 }
345 }
346
347 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
348 unsigned char **packet_buff,
349 int *packet_buff_len,
350 int min_packet_len)
351 {
352 struct batadv_hard_iface *primary_if;
353 int req_len;
354
355 primary_if = batadv_primary_if_get_selected(bat_priv);
356
357 req_len = min_packet_len;
358 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
359
360 /* if we have too many changes for one packet don't send any
361 * and wait for the tt table request which will be fragmented
362 */
363 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
364 req_len = min_packet_len;
365
366 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
367 min_packet_len, req_len);
368
369 if (primary_if)
370 batadv_hardif_free_ref(primary_if);
371 }
372
373 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
374 unsigned char **packet_buff,
375 int *packet_buff_len,
376 int min_packet_len)
377 {
378 struct batadv_tt_change_node *entry, *safe;
379 int count = 0, tot_changes = 0, new_len;
380 unsigned char *tt_buff;
381
382 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
383 packet_buff_len, min_packet_len);
384
385 new_len = *packet_buff_len - min_packet_len;
386 tt_buff = *packet_buff + min_packet_len;
387
388 if (new_len > 0)
389 tot_changes = new_len / batadv_tt_len(1);
390
391 spin_lock_bh(&bat_priv->tt.changes_list_lock);
392 atomic_set(&bat_priv->tt.local_changes, 0);
393
394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
395 list) {
396 if (count < tot_changes) {
397 memcpy(tt_buff + batadv_tt_len(count),
398 &entry->change, sizeof(struct batadv_tt_change));
399 count++;
400 }
401 list_del(&entry->list);
402 kfree(entry);
403 }
404 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
405
406 /* Keep the buffer for possible tt_request */
407 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
408 kfree(bat_priv->tt.last_changeset);
409 bat_priv->tt.last_changeset_len = 0;
410 bat_priv->tt.last_changeset = NULL;
411 /* check whether this new OGM has no changes due to size problems */
412 if (new_len > 0) {
413 /* if kmalloc() fails we will reply with the full table
414 * instead of providing the diff
415 */
416 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
417 if (bat_priv->tt.last_changeset) {
418 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
419 bat_priv->tt.last_changeset_len = new_len;
420 }
421 }
422 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
423
424 return count;
425 }
426
427 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
428 {
429 struct net_device *net_dev = (struct net_device *)seq->private;
430 struct batadv_priv *bat_priv = netdev_priv(net_dev);
431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
432 struct batadv_tt_common_entry *tt_common_entry;
433 struct batadv_hard_iface *primary_if;
434 struct hlist_node *node;
435 struct hlist_head *head;
436 uint32_t i;
437
438 primary_if = batadv_seq_print_text_primary_if_get(seq);
439 if (!primary_if)
440 goto out;
441
442 seq_printf(seq,
443 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
444 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
445
446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448
449 rcu_read_lock();
450 hlist_for_each_entry_rcu(tt_common_entry, node,
451 head, hash_entry) {
452 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
453 tt_common_entry->addr,
454 (tt_common_entry->flags &
455 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
456 (tt_common_entry->flags &
457 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
458 (tt_common_entry->flags &
459 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
460 (tt_common_entry->flags &
461 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
462 (tt_common_entry->flags &
463 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
464 }
465 rcu_read_unlock();
466 }
467 out:
468 if (primary_if)
469 batadv_hardif_free_ref(primary_if);
470 return 0;
471 }
472
473 static void
474 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
475 struct batadv_tt_local_entry *tt_local_entry,
476 uint16_t flags, const char *message)
477 {
478 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
479 tt_local_entry->common.flags | flags);
480
481 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table
483 * response issued before the net ttvn increment (consistency check)
484 */
485 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
486
487 batadv_dbg(BATADV_DBG_TT, bat_priv,
488 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry->common.addr, message);
490 }
491
492 void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
493 const char *message, bool roaming)
494 {
495 struct batadv_tt_local_entry *tt_local_entry = NULL;
496 uint16_t flags;
497
498 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
499 if (!tt_local_entry)
500 goto out;
501
502 flags = BATADV_TT_CLIENT_DEL;
503 if (roaming)
504 flags |= BATADV_TT_CLIENT_ROAM;
505
506 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
507 out:
508 if (tt_local_entry)
509 batadv_tt_local_entry_free_ref(tt_local_entry);
510 }
511
512 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
513 struct hlist_head *head)
514 {
515 struct batadv_tt_local_entry *tt_local_entry;
516 struct batadv_tt_common_entry *tt_common_entry;
517 struct hlist_node *node, *node_tmp;
518
519 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
520 hash_entry) {
521 tt_local_entry = container_of(tt_common_entry,
522 struct batadv_tt_local_entry,
523 common);
524 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
525 continue;
526
527 /* entry already marked for deletion */
528 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
529 continue;
530
531 if (!batadv_has_timed_out(tt_local_entry->last_seen,
532 BATADV_TT_LOCAL_TIMEOUT))
533 continue;
534
535 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
536 BATADV_TT_CLIENT_DEL, "timed out");
537 }
538 }
539
540 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
541 {
542 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
543 struct hlist_head *head;
544 spinlock_t *list_lock; /* protects write access to the hash lists */
545 uint32_t i;
546
547 for (i = 0; i < hash->size; i++) {
548 head = &hash->table[i];
549 list_lock = &hash->list_locks[i];
550
551 spin_lock_bh(list_lock);
552 batadv_tt_local_purge_list(bat_priv, head);
553 spin_unlock_bh(list_lock);
554 }
555
556 }
557
558 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
559 {
560 struct batadv_hashtable *hash;
561 spinlock_t *list_lock; /* protects write access to the hash lists */
562 struct batadv_tt_common_entry *tt_common_entry;
563 struct batadv_tt_local_entry *tt_local;
564 struct hlist_node *node, *node_tmp;
565 struct hlist_head *head;
566 uint32_t i;
567
568 if (!bat_priv->tt.local_hash)
569 return;
570
571 hash = bat_priv->tt.local_hash;
572
573 for (i = 0; i < hash->size; i++) {
574 head = &hash->table[i];
575 list_lock = &hash->list_locks[i];
576
577 spin_lock_bh(list_lock);
578 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
579 head, hash_entry) {
580 hlist_del_rcu(node);
581 tt_local = container_of(tt_common_entry,
582 struct batadv_tt_local_entry,
583 common);
584 batadv_tt_local_entry_free_ref(tt_local);
585 }
586 spin_unlock_bh(list_lock);
587 }
588
589 batadv_hash_destroy(hash);
590
591 bat_priv->tt.local_hash = NULL;
592 }
593
594 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
595 {
596 if (bat_priv->tt.global_hash)
597 return 0;
598
599 bat_priv->tt.global_hash = batadv_hash_new(1024);
600
601 if (!bat_priv->tt.global_hash)
602 return -ENOMEM;
603
604 return 0;
605 }
606
607 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
608 {
609 struct batadv_tt_change_node *entry, *safe;
610
611 spin_lock_bh(&bat_priv->tt.changes_list_lock);
612
613 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
614 list) {
615 list_del(&entry->list);
616 kfree(entry);
617 }
618
619 atomic_set(&bat_priv->tt.local_changes, 0);
620 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
621 }
622
623 /* retrieves the orig_tt_list_entry belonging to orig_node from the
624 * batadv_tt_global_entry list
625 *
626 * returns it with an increased refcounter, NULL if not found
627 */
628 static struct batadv_tt_orig_list_entry *
629 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
630 const struct batadv_orig_node *orig_node)
631 {
632 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
633 const struct hlist_head *head;
634 struct hlist_node *node;
635
636 rcu_read_lock();
637 head = &entry->orig_list;
638 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
639 if (tmp_orig_entry->orig_node != orig_node)
640 continue;
641 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
642 continue;
643
644 orig_entry = tmp_orig_entry;
645 break;
646 }
647 rcu_read_unlock();
648
649 return orig_entry;
650 }
651
652 /* find out if an orig_node is already in the list of a tt_global_entry.
653 * returns true if found, false otherwise
654 */
655 static bool
656 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
657 const struct batadv_orig_node *orig_node)
658 {
659 struct batadv_tt_orig_list_entry *orig_entry;
660 bool found = false;
661
662 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
663 if (orig_entry) {
664 found = true;
665 batadv_tt_orig_list_entry_free_ref(orig_entry);
666 }
667
668 return found;
669 }
670
671 static void
672 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
673 struct batadv_orig_node *orig_node, int ttvn)
674 {
675 struct batadv_tt_orig_list_entry *orig_entry;
676
677 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
678 if (orig_entry) {
679 /* refresh the ttvn: the current value could be a bogus one that
680 * was added during a "temporary client detection"
681 */
682 orig_entry->ttvn = ttvn;
683 goto out;
684 }
685
686 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
687 if (!orig_entry)
688 goto out;
689
690 INIT_HLIST_NODE(&orig_entry->list);
691 atomic_inc(&orig_node->refcount);
692 atomic_inc(&orig_node->tt_size);
693 orig_entry->orig_node = orig_node;
694 orig_entry->ttvn = ttvn;
695 atomic_set(&orig_entry->refcount, 2);
696
697 spin_lock_bh(&tt_global->list_lock);
698 hlist_add_head_rcu(&orig_entry->list,
699 &tt_global->orig_list);
700 spin_unlock_bh(&tt_global->list_lock);
701 out:
702 if (orig_entry)
703 batadv_tt_orig_list_entry_free_ref(orig_entry);
704 }
705
706 /* caller must hold orig_node refcount */
707 int batadv_tt_global_add(struct batadv_priv *bat_priv,
708 struct batadv_orig_node *orig_node,
709 const unsigned char *tt_addr, uint8_t flags,
710 uint8_t ttvn)
711 {
712 struct batadv_tt_global_entry *tt_global_entry = NULL;
713 int ret = 0;
714 int hash_added;
715 struct batadv_tt_common_entry *common;
716
717 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
718
719 if (!tt_global_entry) {
720 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
721 if (!tt_global_entry)
722 goto out;
723
724 common = &tt_global_entry->common;
725 memcpy(common->addr, tt_addr, ETH_ALEN);
726
727 common->flags = flags;
728 tt_global_entry->roam_at = 0;
729 /* node must store current time in case of roaming. This is
730 * needed to purge this entry out on timeout (if nobody claims
731 * it)
732 */
733 if (flags & BATADV_TT_CLIENT_ROAM)
734 tt_global_entry->roam_at = jiffies;
735 atomic_set(&common->refcount, 2);
736 common->added_at = jiffies;
737
738 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
739 spin_lock_init(&tt_global_entry->list_lock);
740
741 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
742 batadv_compare_tt,
743 batadv_choose_orig, common,
744 &common->hash_entry);
745
746 if (unlikely(hash_added != 0)) {
747 /* remove the reference for the hash */
748 batadv_tt_global_entry_free_ref(tt_global_entry);
749 goto out_remove;
750 }
751 } else {
752 /* If there is already a global entry, we can use this one for
753 * our processing.
754 * But if we are trying to add a temporary client we can exit
755 * directly because the temporary information should never
756 * override any already known client state (whatever it is)
757 */
758 if (flags & BATADV_TT_CLIENT_TEMP)
759 goto out;
760
761 /* if the client was temporary added before receiving the first
762 * OGM announcing it, we have to clear the TEMP flag
763 */
764 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
765
766 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
767 * one originator left in the list and we previously received a
768 * delete + roaming change for this originator.
769 *
770 * We should first delete the old originator before adding the
771 * new one.
772 */
773 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
774 batadv_tt_global_del_orig_list(tt_global_entry);
775 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
776 tt_global_entry->roam_at = 0;
777 }
778 }
779 /* add the new orig_entry (if needed) or update it */
780 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
781
782 batadv_dbg(BATADV_DBG_TT, bat_priv,
783 "Creating new global tt entry: %pM (via %pM)\n",
784 tt_global_entry->common.addr, orig_node->orig);
785 ret = 1;
786
787 out_remove:
788 /* remove address from local hash if present */
789 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
790 "global tt received",
791 flags & BATADV_TT_CLIENT_ROAM);
792 out:
793 if (tt_global_entry)
794 batadv_tt_global_entry_free_ref(tt_global_entry);
795 return ret;
796 }
797
798 /* print all orig nodes who announce the address for this global entry.
799 * it is assumed that the caller holds rcu_read_lock();
800 */
801 static void
802 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
803 struct seq_file *seq)
804 {
805 struct hlist_head *head;
806 struct hlist_node *node;
807 struct batadv_tt_orig_list_entry *orig_entry;
808 struct batadv_tt_common_entry *tt_common_entry;
809 uint16_t flags;
810 uint8_t last_ttvn;
811
812 tt_common_entry = &tt_global_entry->common;
813
814 head = &tt_global_entry->orig_list;
815
816 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
817 flags = tt_common_entry->flags;
818 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
819 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
820 tt_global_entry->common.addr, orig_entry->ttvn,
821 orig_entry->orig_node->orig, last_ttvn,
822 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
823 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
824 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
825 }
826 }
827
828 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
829 {
830 struct net_device *net_dev = (struct net_device *)seq->private;
831 struct batadv_priv *bat_priv = netdev_priv(net_dev);
832 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
833 struct batadv_tt_common_entry *tt_common_entry;
834 struct batadv_tt_global_entry *tt_global;
835 struct batadv_hard_iface *primary_if;
836 struct hlist_node *node;
837 struct hlist_head *head;
838 uint32_t i;
839
840 primary_if = batadv_seq_print_text_primary_if_get(seq);
841 if (!primary_if)
842 goto out;
843
844 seq_printf(seq,
845 "Globally announced TT entries received via the mesh %s\n",
846 net_dev->name);
847 seq_printf(seq, " %-13s %s %-15s %s %s\n",
848 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
849
850 for (i = 0; i < hash->size; i++) {
851 head = &hash->table[i];
852
853 rcu_read_lock();
854 hlist_for_each_entry_rcu(tt_common_entry, node,
855 head, hash_entry) {
856 tt_global = container_of(tt_common_entry,
857 struct batadv_tt_global_entry,
858 common);
859 batadv_tt_global_print_entry(tt_global, seq);
860 }
861 rcu_read_unlock();
862 }
863 out:
864 if (primary_if)
865 batadv_hardif_free_ref(primary_if);
866 return 0;
867 }
868
869 /* deletes the orig list of a tt_global_entry */
870 static void
871 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
872 {
873 struct hlist_head *head;
874 struct hlist_node *node, *safe;
875 struct batadv_tt_orig_list_entry *orig_entry;
876
877 spin_lock_bh(&tt_global_entry->list_lock);
878 head = &tt_global_entry->orig_list;
879 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
880 hlist_del_rcu(node);
881 batadv_tt_orig_list_entry_free_ref(orig_entry);
882 }
883 spin_unlock_bh(&tt_global_entry->list_lock);
884
885 }
886
887 static void
888 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
889 struct batadv_tt_global_entry *tt_global_entry,
890 struct batadv_orig_node *orig_node,
891 const char *message)
892 {
893 struct hlist_head *head;
894 struct hlist_node *node, *safe;
895 struct batadv_tt_orig_list_entry *orig_entry;
896
897 spin_lock_bh(&tt_global_entry->list_lock);
898 head = &tt_global_entry->orig_list;
899 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
900 if (orig_entry->orig_node == orig_node) {
901 batadv_dbg(BATADV_DBG_TT, bat_priv,
902 "Deleting %pM from global tt entry %pM: %s\n",
903 orig_node->orig,
904 tt_global_entry->common.addr, message);
905 hlist_del_rcu(node);
906 batadv_tt_orig_list_entry_free_ref(orig_entry);
907 }
908 }
909 spin_unlock_bh(&tt_global_entry->list_lock);
910 }
911
912 static void
913 batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
914 struct batadv_tt_global_entry *tt_global_entry,
915 const char *message)
916 {
917 batadv_dbg(BATADV_DBG_TT, bat_priv,
918 "Deleting global tt entry %pM: %s\n",
919 tt_global_entry->common.addr, message);
920
921 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
922 batadv_choose_orig, tt_global_entry->common.addr);
923 batadv_tt_global_entry_free_ref(tt_global_entry);
924
925 }
926
927 /* If the client is to be deleted, we check if it is the last origantor entry
928 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
929 * timer, otherwise we simply remove the originator scheduled for deletion.
930 */
931 static void
932 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
933 struct batadv_tt_global_entry *tt_global_entry,
934 struct batadv_orig_node *orig_node,
935 const char *message)
936 {
937 bool last_entry = true;
938 struct hlist_head *head;
939 struct hlist_node *node;
940 struct batadv_tt_orig_list_entry *orig_entry;
941
942 /* no local entry exists, case 1:
943 * Check if this is the last one or if other entries exist.
944 */
945
946 rcu_read_lock();
947 head = &tt_global_entry->orig_list;
948 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
949 if (orig_entry->orig_node != orig_node) {
950 last_entry = false;
951 break;
952 }
953 }
954 rcu_read_unlock();
955
956 if (last_entry) {
957 /* its the last one, mark for roaming. */
958 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
959 tt_global_entry->roam_at = jiffies;
960 } else
961 /* there is another entry, we can simply delete this
962 * one and can still use the other one.
963 */
964 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
965 orig_node, message);
966 }
967
968
969
970 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
971 struct batadv_orig_node *orig_node,
972 const unsigned char *addr,
973 const char *message, bool roaming)
974 {
975 struct batadv_tt_global_entry *tt_global_entry = NULL;
976 struct batadv_tt_local_entry *local_entry = NULL;
977
978 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
979 if (!tt_global_entry)
980 goto out;
981
982 if (!roaming) {
983 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
984 orig_node, message);
985
986 if (hlist_empty(&tt_global_entry->orig_list))
987 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
988 message);
989
990 goto out;
991 }
992
993 /* if we are deleting a global entry due to a roam
994 * event, there are two possibilities:
995 * 1) the client roamed from node A to node B => if there
996 * is only one originator left for this client, we mark
997 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
998 * wait for node B to claim it. In case of timeout
999 * the entry is purged.
1000 *
1001 * If there are other originators left, we directly delete
1002 * the originator.
1003 * 2) the client roamed to us => we can directly delete
1004 * the global entry, since it is useless now.
1005 */
1006 local_entry = batadv_tt_local_hash_find(bat_priv,
1007 tt_global_entry->common.addr);
1008 if (local_entry) {
1009 /* local entry exists, case 2: client roamed to us. */
1010 batadv_tt_global_del_orig_list(tt_global_entry);
1011 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
1012 } else
1013 /* no local entry exists, case 1: check for roaming */
1014 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1015 orig_node, message);
1016
1017
1018 out:
1019 if (tt_global_entry)
1020 batadv_tt_global_entry_free_ref(tt_global_entry);
1021 if (local_entry)
1022 batadv_tt_local_entry_free_ref(local_entry);
1023 }
1024
1025 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1026 struct batadv_orig_node *orig_node,
1027 const char *message)
1028 {
1029 struct batadv_tt_global_entry *tt_global;
1030 struct batadv_tt_common_entry *tt_common_entry;
1031 uint32_t i;
1032 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1033 struct hlist_node *node, *safe;
1034 struct hlist_head *head;
1035 spinlock_t *list_lock; /* protects write access to the hash lists */
1036
1037 if (!hash)
1038 return;
1039
1040 for (i = 0; i < hash->size; i++) {
1041 head = &hash->table[i];
1042 list_lock = &hash->list_locks[i];
1043
1044 spin_lock_bh(list_lock);
1045 hlist_for_each_entry_safe(tt_common_entry, node, safe,
1046 head, hash_entry) {
1047 tt_global = container_of(tt_common_entry,
1048 struct batadv_tt_global_entry,
1049 common);
1050
1051 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1052 orig_node, message);
1053
1054 if (hlist_empty(&tt_global->orig_list)) {
1055 batadv_dbg(BATADV_DBG_TT, bat_priv,
1056 "Deleting global tt entry %pM: %s\n",
1057 tt_global->common.addr, message);
1058 hlist_del_rcu(node);
1059 batadv_tt_global_entry_free_ref(tt_global);
1060 }
1061 }
1062 spin_unlock_bh(list_lock);
1063 }
1064 orig_node->tt_initialised = false;
1065 }
1066
1067 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1068 char **msg)
1069 {
1070 bool purge = false;
1071 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1072 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1073
1074 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1075 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1076 purge = true;
1077 *msg = "Roaming timeout\n";
1078 }
1079
1080 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1081 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1082 purge = true;
1083 *msg = "Temporary client timeout\n";
1084 }
1085
1086 return purge;
1087 }
1088
1089 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1090 {
1091 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1092 struct hlist_head *head;
1093 struct hlist_node *node, *node_tmp;
1094 spinlock_t *list_lock; /* protects write access to the hash lists */
1095 uint32_t i;
1096 char *msg = NULL;
1097 struct batadv_tt_common_entry *tt_common;
1098 struct batadv_tt_global_entry *tt_global;
1099
1100 for (i = 0; i < hash->size; i++) {
1101 head = &hash->table[i];
1102 list_lock = &hash->list_locks[i];
1103
1104 spin_lock_bh(list_lock);
1105 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1106 hash_entry) {
1107 tt_global = container_of(tt_common,
1108 struct batadv_tt_global_entry,
1109 common);
1110
1111 if (!batadv_tt_global_to_purge(tt_global, &msg))
1112 continue;
1113
1114 batadv_dbg(BATADV_DBG_TT, bat_priv,
1115 "Deleting global tt entry (%pM): %s\n",
1116 tt_global->common.addr, msg);
1117
1118 hlist_del_rcu(node);
1119
1120 batadv_tt_global_entry_free_ref(tt_global);
1121 }
1122 spin_unlock_bh(list_lock);
1123 }
1124 }
1125
1126 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1127 {
1128 struct batadv_hashtable *hash;
1129 spinlock_t *list_lock; /* protects write access to the hash lists */
1130 struct batadv_tt_common_entry *tt_common_entry;
1131 struct batadv_tt_global_entry *tt_global;
1132 struct hlist_node *node, *node_tmp;
1133 struct hlist_head *head;
1134 uint32_t i;
1135
1136 if (!bat_priv->tt.global_hash)
1137 return;
1138
1139 hash = bat_priv->tt.global_hash;
1140
1141 for (i = 0; i < hash->size; i++) {
1142 head = &hash->table[i];
1143 list_lock = &hash->list_locks[i];
1144
1145 spin_lock_bh(list_lock);
1146 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1147 head, hash_entry) {
1148 hlist_del_rcu(node);
1149 tt_global = container_of(tt_common_entry,
1150 struct batadv_tt_global_entry,
1151 common);
1152 batadv_tt_global_entry_free_ref(tt_global);
1153 }
1154 spin_unlock_bh(list_lock);
1155 }
1156
1157 batadv_hash_destroy(hash);
1158
1159 bat_priv->tt.global_hash = NULL;
1160 }
1161
1162 static bool
1163 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1164 struct batadv_tt_global_entry *tt_global_entry)
1165 {
1166 bool ret = false;
1167
1168 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1169 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1170 ret = true;
1171
1172 return ret;
1173 }
1174
1175 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1176 const uint8_t *src,
1177 const uint8_t *addr)
1178 {
1179 struct batadv_tt_local_entry *tt_local_entry = NULL;
1180 struct batadv_tt_global_entry *tt_global_entry = NULL;
1181 struct batadv_orig_node *orig_node = NULL;
1182 struct batadv_neigh_node *router = NULL;
1183 struct hlist_head *head;
1184 struct hlist_node *node;
1185 struct batadv_tt_orig_list_entry *orig_entry;
1186 int best_tq;
1187
1188 if (src && atomic_read(&bat_priv->ap_isolation)) {
1189 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1190 if (!tt_local_entry)
1191 goto out;
1192 }
1193
1194 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1195 if (!tt_global_entry)
1196 goto out;
1197
1198 /* check whether the clients should not communicate due to AP
1199 * isolation
1200 */
1201 if (tt_local_entry &&
1202 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1203 goto out;
1204
1205 best_tq = 0;
1206
1207 rcu_read_lock();
1208 head = &tt_global_entry->orig_list;
1209 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1210 router = batadv_orig_node_get_router(orig_entry->orig_node);
1211 if (!router)
1212 continue;
1213
1214 if (router->tq_avg > best_tq) {
1215 orig_node = orig_entry->orig_node;
1216 best_tq = router->tq_avg;
1217 }
1218 batadv_neigh_node_free_ref(router);
1219 }
1220 /* found anything? */
1221 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1222 orig_node = NULL;
1223 rcu_read_unlock();
1224 out:
1225 if (tt_global_entry)
1226 batadv_tt_global_entry_free_ref(tt_global_entry);
1227 if (tt_local_entry)
1228 batadv_tt_local_entry_free_ref(tt_local_entry);
1229
1230 return orig_node;
1231 }
1232
1233 /* Calculates the checksum of the local table of a given orig_node */
1234 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1235 struct batadv_orig_node *orig_node)
1236 {
1237 uint16_t total = 0, total_one;
1238 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1239 struct batadv_tt_common_entry *tt_common;
1240 struct batadv_tt_global_entry *tt_global;
1241 struct hlist_node *node;
1242 struct hlist_head *head;
1243 uint32_t i;
1244 int j;
1245
1246 for (i = 0; i < hash->size; i++) {
1247 head = &hash->table[i];
1248
1249 rcu_read_lock();
1250 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1251 tt_global = container_of(tt_common,
1252 struct batadv_tt_global_entry,
1253 common);
1254 /* Roaming clients are in the global table for
1255 * consistency only. They don't have to be
1256 * taken into account while computing the
1257 * global crc
1258 */
1259 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1260 continue;
1261 /* Temporary clients have not been announced yet, so
1262 * they have to be skipped while computing the global
1263 * crc
1264 */
1265 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1266 continue;
1267
1268 /* find out if this global entry is announced by this
1269 * originator
1270 */
1271 if (!batadv_tt_global_entry_has_orig(tt_global,
1272 orig_node))
1273 continue;
1274
1275 total_one = 0;
1276 for (j = 0; j < ETH_ALEN; j++)
1277 total_one = crc16_byte(total_one,
1278 tt_common->addr[j]);
1279 total ^= total_one;
1280 }
1281 rcu_read_unlock();
1282 }
1283
1284 return total;
1285 }
1286
1287 /* Calculates the checksum of the local table */
1288 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1289 {
1290 uint16_t total = 0, total_one;
1291 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1292 struct batadv_tt_common_entry *tt_common;
1293 struct hlist_node *node;
1294 struct hlist_head *head;
1295 uint32_t i;
1296 int j;
1297
1298 for (i = 0; i < hash->size; i++) {
1299 head = &hash->table[i];
1300
1301 rcu_read_lock();
1302 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1303 /* not yet committed clients have not to be taken into
1304 * account while computing the CRC
1305 */
1306 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1307 continue;
1308 total_one = 0;
1309 for (j = 0; j < ETH_ALEN; j++)
1310 total_one = crc16_byte(total_one,
1311 tt_common->addr[j]);
1312 total ^= total_one;
1313 }
1314 rcu_read_unlock();
1315 }
1316
1317 return total;
1318 }
1319
1320 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1321 {
1322 struct batadv_tt_req_node *node, *safe;
1323
1324 spin_lock_bh(&bat_priv->tt.req_list_lock);
1325
1326 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1327 list_del(&node->list);
1328 kfree(node);
1329 }
1330
1331 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1332 }
1333
1334 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1335 struct batadv_orig_node *orig_node,
1336 const unsigned char *tt_buff,
1337 uint8_t tt_num_changes)
1338 {
1339 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1340
1341 /* Replace the old buffer only if I received something in the
1342 * last OGM (the OGM could carry no changes)
1343 */
1344 spin_lock_bh(&orig_node->tt_buff_lock);
1345 if (tt_buff_len > 0) {
1346 kfree(orig_node->tt_buff);
1347 orig_node->tt_buff_len = 0;
1348 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1349 if (orig_node->tt_buff) {
1350 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1351 orig_node->tt_buff_len = tt_buff_len;
1352 }
1353 }
1354 spin_unlock_bh(&orig_node->tt_buff_lock);
1355 }
1356
1357 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1358 {
1359 struct batadv_tt_req_node *node, *safe;
1360
1361 spin_lock_bh(&bat_priv->tt.req_list_lock);
1362 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1363 if (batadv_has_timed_out(node->issued_at,
1364 BATADV_TT_REQUEST_TIMEOUT)) {
1365 list_del(&node->list);
1366 kfree(node);
1367 }
1368 }
1369 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1370 }
1371
1372 /* returns the pointer to the new tt_req_node struct if no request
1373 * has already been issued for this orig_node, NULL otherwise
1374 */
1375 static struct batadv_tt_req_node *
1376 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1377 struct batadv_orig_node *orig_node)
1378 {
1379 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1380
1381 spin_lock_bh(&bat_priv->tt.req_list_lock);
1382 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1383 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1384 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1385 BATADV_TT_REQUEST_TIMEOUT))
1386 goto unlock;
1387 }
1388
1389 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1390 if (!tt_req_node)
1391 goto unlock;
1392
1393 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1394 tt_req_node->issued_at = jiffies;
1395
1396 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1397 unlock:
1398 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1399 return tt_req_node;
1400 }
1401
1402 /* data_ptr is useless here, but has to be kept to respect the prototype */
1403 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1404 const void *data_ptr)
1405 {
1406 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1407
1408 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1409 return 0;
1410 return 1;
1411 }
1412
1413 static int batadv_tt_global_valid(const void *entry_ptr,
1414 const void *data_ptr)
1415 {
1416 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1417 const struct batadv_tt_global_entry *tt_global_entry;
1418 const struct batadv_orig_node *orig_node = data_ptr;
1419
1420 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1421 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1422 return 0;
1423
1424 tt_global_entry = container_of(tt_common_entry,
1425 struct batadv_tt_global_entry,
1426 common);
1427
1428 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1429 }
1430
1431 static struct sk_buff *
1432 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1433 struct batadv_hashtable *hash,
1434 struct batadv_hard_iface *primary_if,
1435 int (*valid_cb)(const void *, const void *),
1436 void *cb_data)
1437 {
1438 struct batadv_tt_common_entry *tt_common_entry;
1439 struct batadv_tt_query_packet *tt_response;
1440 struct batadv_tt_change *tt_change;
1441 struct hlist_node *node;
1442 struct hlist_head *head;
1443 struct sk_buff *skb = NULL;
1444 uint16_t tt_tot, tt_count;
1445 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1446 uint32_t i;
1447 size_t len;
1448
1449 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1450 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1451 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1452 }
1453 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1454
1455 len = tt_query_size + tt_len;
1456 skb = dev_alloc_skb(len + ETH_HLEN);
1457 if (!skb)
1458 goto out;
1459
1460 skb_reserve(skb, ETH_HLEN);
1461 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1462 tt_response->ttvn = ttvn;
1463
1464 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1465 tt_count = 0;
1466
1467 rcu_read_lock();
1468 for (i = 0; i < hash->size; i++) {
1469 head = &hash->table[i];
1470
1471 hlist_for_each_entry_rcu(tt_common_entry, node,
1472 head, hash_entry) {
1473 if (tt_count == tt_tot)
1474 break;
1475
1476 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1477 continue;
1478
1479 memcpy(tt_change->addr, tt_common_entry->addr,
1480 ETH_ALEN);
1481 tt_change->flags = BATADV_NO_FLAGS;
1482
1483 tt_count++;
1484 tt_change++;
1485 }
1486 }
1487 rcu_read_unlock();
1488
1489 /* store in the message the number of entries we have successfully
1490 * copied
1491 */
1492 tt_response->tt_data = htons(tt_count);
1493
1494 out:
1495 return skb;
1496 }
1497
1498 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1499 struct batadv_orig_node *dst_orig_node,
1500 uint8_t ttvn, uint16_t tt_crc,
1501 bool full_table)
1502 {
1503 struct sk_buff *skb = NULL;
1504 struct batadv_tt_query_packet *tt_request;
1505 struct batadv_neigh_node *neigh_node = NULL;
1506 struct batadv_hard_iface *primary_if;
1507 struct batadv_tt_req_node *tt_req_node = NULL;
1508 int ret = 1;
1509 size_t tt_req_len;
1510
1511 primary_if = batadv_primary_if_get_selected(bat_priv);
1512 if (!primary_if)
1513 goto out;
1514
1515 /* The new tt_req will be issued only if I'm not waiting for a
1516 * reply from the same orig_node yet
1517 */
1518 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1519 if (!tt_req_node)
1520 goto out;
1521
1522 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
1523 if (!skb)
1524 goto out;
1525
1526 skb_reserve(skb, ETH_HLEN);
1527
1528 tt_req_len = sizeof(*tt_request);
1529 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1530
1531 tt_request->header.packet_type = BATADV_TT_QUERY;
1532 tt_request->header.version = BATADV_COMPAT_VERSION;
1533 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1534 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1535 tt_request->header.ttl = BATADV_TTL;
1536 tt_request->ttvn = ttvn;
1537 tt_request->tt_data = htons(tt_crc);
1538 tt_request->flags = BATADV_TT_REQUEST;
1539
1540 if (full_table)
1541 tt_request->flags |= BATADV_TT_FULL_TABLE;
1542
1543 neigh_node = batadv_orig_node_get_router(dst_orig_node);
1544 if (!neigh_node)
1545 goto out;
1546
1547 batadv_dbg(BATADV_DBG_TT, bat_priv,
1548 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1549 dst_orig_node->orig, neigh_node->addr,
1550 (full_table ? 'F' : '.'));
1551
1552 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1553
1554 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1555 ret = 0;
1556
1557 out:
1558 if (neigh_node)
1559 batadv_neigh_node_free_ref(neigh_node);
1560 if (primary_if)
1561 batadv_hardif_free_ref(primary_if);
1562 if (ret)
1563 kfree_skb(skb);
1564 if (ret && tt_req_node) {
1565 spin_lock_bh(&bat_priv->tt.req_list_lock);
1566 list_del(&tt_req_node->list);
1567 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1568 kfree(tt_req_node);
1569 }
1570 return ret;
1571 }
1572
1573 static bool
1574 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1575 struct batadv_tt_query_packet *tt_request)
1576 {
1577 struct batadv_orig_node *req_dst_orig_node = NULL;
1578 struct batadv_orig_node *res_dst_orig_node = NULL;
1579 struct batadv_neigh_node *neigh_node = NULL;
1580 struct batadv_hard_iface *primary_if = NULL;
1581 uint8_t orig_ttvn, req_ttvn, ttvn;
1582 int ret = false;
1583 unsigned char *tt_buff;
1584 bool full_table;
1585 uint16_t tt_len, tt_tot;
1586 struct sk_buff *skb = NULL;
1587 struct batadv_tt_query_packet *tt_response;
1588 uint8_t *packet_pos;
1589 size_t len;
1590
1591 batadv_dbg(BATADV_DBG_TT, bat_priv,
1592 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1593 tt_request->src, tt_request->ttvn, tt_request->dst,
1594 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1595
1596 /* Let's get the orig node of the REAL destination */
1597 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1598 if (!req_dst_orig_node)
1599 goto out;
1600
1601 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1602 if (!res_dst_orig_node)
1603 goto out;
1604
1605 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1606 if (!neigh_node)
1607 goto out;
1608
1609 primary_if = batadv_primary_if_get_selected(bat_priv);
1610 if (!primary_if)
1611 goto out;
1612
1613 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1614 req_ttvn = tt_request->ttvn;
1615
1616 /* I don't have the requested data */
1617 if (orig_ttvn != req_ttvn ||
1618 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1619 goto out;
1620
1621 /* If the full table has been explicitly requested */
1622 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1623 !req_dst_orig_node->tt_buff)
1624 full_table = true;
1625 else
1626 full_table = false;
1627
1628 /* In this version, fragmentation is not implemented, then
1629 * I'll send only one packet with as much TT entries as I can
1630 */
1631 if (!full_table) {
1632 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1633 tt_len = req_dst_orig_node->tt_buff_len;
1634 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1635
1636 len = sizeof(*tt_response) + tt_len;
1637 skb = dev_alloc_skb(len + ETH_HLEN);
1638 if (!skb)
1639 goto unlock;
1640
1641 skb_reserve(skb, ETH_HLEN);
1642 packet_pos = skb_put(skb, len);
1643 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1644 tt_response->ttvn = req_ttvn;
1645 tt_response->tt_data = htons(tt_tot);
1646
1647 tt_buff = skb->data + sizeof(*tt_response);
1648 /* Copy the last orig_node's OGM buffer */
1649 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1650 req_dst_orig_node->tt_buff_len);
1651
1652 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1653 } else {
1654 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1655 tt_len *= sizeof(struct batadv_tt_change);
1656 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1657
1658 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1659 bat_priv->tt.global_hash,
1660 primary_if,
1661 batadv_tt_global_valid,
1662 req_dst_orig_node);
1663 if (!skb)
1664 goto out;
1665
1666 tt_response = (struct batadv_tt_query_packet *)skb->data;
1667 }
1668
1669 tt_response->header.packet_type = BATADV_TT_QUERY;
1670 tt_response->header.version = BATADV_COMPAT_VERSION;
1671 tt_response->header.ttl = BATADV_TTL;
1672 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1673 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1674 tt_response->flags = BATADV_TT_RESPONSE;
1675
1676 if (full_table)
1677 tt_response->flags |= BATADV_TT_FULL_TABLE;
1678
1679 batadv_dbg(BATADV_DBG_TT, bat_priv,
1680 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1681 res_dst_orig_node->orig, neigh_node->addr,
1682 req_dst_orig_node->orig, req_ttvn);
1683
1684 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1685
1686 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1687 ret = true;
1688 goto out;
1689
1690 unlock:
1691 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1692
1693 out:
1694 if (res_dst_orig_node)
1695 batadv_orig_node_free_ref(res_dst_orig_node);
1696 if (req_dst_orig_node)
1697 batadv_orig_node_free_ref(req_dst_orig_node);
1698 if (neigh_node)
1699 batadv_neigh_node_free_ref(neigh_node);
1700 if (primary_if)
1701 batadv_hardif_free_ref(primary_if);
1702 if (!ret)
1703 kfree_skb(skb);
1704 return ret;
1705
1706 }
1707
1708 static bool
1709 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1710 struct batadv_tt_query_packet *tt_request)
1711 {
1712 struct batadv_orig_node *orig_node = NULL;
1713 struct batadv_neigh_node *neigh_node = NULL;
1714 struct batadv_hard_iface *primary_if = NULL;
1715 uint8_t my_ttvn, req_ttvn, ttvn;
1716 int ret = false;
1717 unsigned char *tt_buff;
1718 bool full_table;
1719 uint16_t tt_len, tt_tot;
1720 struct sk_buff *skb = NULL;
1721 struct batadv_tt_query_packet *tt_response;
1722 uint8_t *packet_pos;
1723 size_t len;
1724
1725 batadv_dbg(BATADV_DBG_TT, bat_priv,
1726 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1727 tt_request->src, tt_request->ttvn,
1728 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1729
1730
1731 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1732 req_ttvn = tt_request->ttvn;
1733
1734 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1735 if (!orig_node)
1736 goto out;
1737
1738 neigh_node = batadv_orig_node_get_router(orig_node);
1739 if (!neigh_node)
1740 goto out;
1741
1742 primary_if = batadv_primary_if_get_selected(bat_priv);
1743 if (!primary_if)
1744 goto out;
1745
1746 /* If the full table has been explicitly requested or the gap
1747 * is too big send the whole local translation table
1748 */
1749 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1750 !bat_priv->tt.last_changeset)
1751 full_table = true;
1752 else
1753 full_table = false;
1754
1755 /* In this version, fragmentation is not implemented, then
1756 * I'll send only one packet with as much TT entries as I can
1757 */
1758 if (!full_table) {
1759 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1760 tt_len = bat_priv->tt.last_changeset_len;
1761 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1762
1763 len = sizeof(*tt_response) + tt_len;
1764 skb = dev_alloc_skb(len + ETH_HLEN);
1765 if (!skb)
1766 goto unlock;
1767
1768 skb_reserve(skb, ETH_HLEN);
1769 packet_pos = skb_put(skb, len);
1770 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1771 tt_response->ttvn = req_ttvn;
1772 tt_response->tt_data = htons(tt_tot);
1773
1774 tt_buff = skb->data + sizeof(*tt_response);
1775 memcpy(tt_buff, bat_priv->tt.last_changeset,
1776 bat_priv->tt.last_changeset_len);
1777 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1778 } else {
1779 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1780 tt_len *= sizeof(struct batadv_tt_change);
1781 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1782
1783 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1784 bat_priv->tt.local_hash,
1785 primary_if,
1786 batadv_tt_local_valid_entry,
1787 NULL);
1788 if (!skb)
1789 goto out;
1790
1791 tt_response = (struct batadv_tt_query_packet *)skb->data;
1792 }
1793
1794 tt_response->header.packet_type = BATADV_TT_QUERY;
1795 tt_response->header.version = BATADV_COMPAT_VERSION;
1796 tt_response->header.ttl = BATADV_TTL;
1797 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1798 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1799 tt_response->flags = BATADV_TT_RESPONSE;
1800
1801 if (full_table)
1802 tt_response->flags |= BATADV_TT_FULL_TABLE;
1803
1804 batadv_dbg(BATADV_DBG_TT, bat_priv,
1805 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1806 orig_node->orig, neigh_node->addr,
1807 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1808
1809 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1810
1811 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1812 ret = true;
1813 goto out;
1814
1815 unlock:
1816 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1817 out:
1818 if (orig_node)
1819 batadv_orig_node_free_ref(orig_node);
1820 if (neigh_node)
1821 batadv_neigh_node_free_ref(neigh_node);
1822 if (primary_if)
1823 batadv_hardif_free_ref(primary_if);
1824 if (!ret)
1825 kfree_skb(skb);
1826 /* This packet was for me, so it doesn't need to be re-routed */
1827 return true;
1828 }
1829
1830 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1831 struct batadv_tt_query_packet *tt_request)
1832 {
1833 if (batadv_is_my_mac(tt_request->dst)) {
1834 /* don't answer backbone gws! */
1835 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1836 return true;
1837
1838 return batadv_send_my_tt_response(bat_priv, tt_request);
1839 } else {
1840 return batadv_send_other_tt_response(bat_priv, tt_request);
1841 }
1842 }
1843
1844 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1845 struct batadv_orig_node *orig_node,
1846 struct batadv_tt_change *tt_change,
1847 uint16_t tt_num_changes, uint8_t ttvn)
1848 {
1849 int i;
1850 int roams;
1851
1852 for (i = 0; i < tt_num_changes; i++) {
1853 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1854 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1855 batadv_tt_global_del(bat_priv, orig_node,
1856 (tt_change + i)->addr,
1857 "tt removed by changes",
1858 roams);
1859 } else {
1860 if (!batadv_tt_global_add(bat_priv, orig_node,
1861 (tt_change + i)->addr,
1862 (tt_change + i)->flags, ttvn))
1863 /* In case of problem while storing a
1864 * global_entry, we stop the updating
1865 * procedure without committing the
1866 * ttvn change. This will avoid to send
1867 * corrupted data on tt_request
1868 */
1869 return;
1870 }
1871 }
1872 orig_node->tt_initialised = true;
1873 }
1874
1875 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1876 struct batadv_tt_query_packet *tt_response)
1877 {
1878 struct batadv_orig_node *orig_node = NULL;
1879
1880 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1881 if (!orig_node)
1882 goto out;
1883
1884 /* Purge the old table first.. */
1885 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
1886
1887 _batadv_tt_update_changes(bat_priv, orig_node,
1888 (struct batadv_tt_change *)(tt_response + 1),
1889 ntohs(tt_response->tt_data),
1890 tt_response->ttvn);
1891
1892 spin_lock_bh(&orig_node->tt_buff_lock);
1893 kfree(orig_node->tt_buff);
1894 orig_node->tt_buff_len = 0;
1895 orig_node->tt_buff = NULL;
1896 spin_unlock_bh(&orig_node->tt_buff_lock);
1897
1898 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1899
1900 out:
1901 if (orig_node)
1902 batadv_orig_node_free_ref(orig_node);
1903 }
1904
1905 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1906 struct batadv_orig_node *orig_node,
1907 uint16_t tt_num_changes, uint8_t ttvn,
1908 struct batadv_tt_change *tt_change)
1909 {
1910 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1911 tt_num_changes, ttvn);
1912
1913 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1914 (unsigned char *)tt_change, tt_num_changes);
1915 atomic_set(&orig_node->last_ttvn, ttvn);
1916 }
1917
1918 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
1919 {
1920 struct batadv_tt_local_entry *tt_local_entry = NULL;
1921 bool ret = false;
1922
1923 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
1924 if (!tt_local_entry)
1925 goto out;
1926 /* Check if the client has been logically deleted (but is kept for
1927 * consistency purpose)
1928 */
1929 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1930 goto out;
1931 ret = true;
1932 out:
1933 if (tt_local_entry)
1934 batadv_tt_local_entry_free_ref(tt_local_entry);
1935 return ret;
1936 }
1937
1938 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
1939 struct batadv_tt_query_packet *tt_response)
1940 {
1941 struct batadv_tt_req_node *node, *safe;
1942 struct batadv_orig_node *orig_node = NULL;
1943 struct batadv_tt_change *tt_change;
1944
1945 batadv_dbg(BATADV_DBG_TT, bat_priv,
1946 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1947 tt_response->src, tt_response->ttvn,
1948 ntohs(tt_response->tt_data),
1949 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1950
1951 /* we should have never asked a backbone gw */
1952 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1953 goto out;
1954
1955 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1956 if (!orig_node)
1957 goto out;
1958
1959 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
1960 batadv_tt_fill_gtable(bat_priv, tt_response);
1961 } else {
1962 tt_change = (struct batadv_tt_change *)(tt_response + 1);
1963 batadv_tt_update_changes(bat_priv, orig_node,
1964 ntohs(tt_response->tt_data),
1965 tt_response->ttvn, tt_change);
1966 }
1967
1968 /* Delete the tt_req_node from pending tt_requests list */
1969 spin_lock_bh(&bat_priv->tt.req_list_lock);
1970 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1971 if (!batadv_compare_eth(node->addr, tt_response->src))
1972 continue;
1973 list_del(&node->list);
1974 kfree(node);
1975 }
1976 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1977
1978 /* Recalculate the CRC for this orig_node and store it */
1979 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
1980 /* Roaming phase is over: tables are in sync again. I can
1981 * unset the flag
1982 */
1983 orig_node->tt_poss_change = false;
1984 out:
1985 if (orig_node)
1986 batadv_orig_node_free_ref(orig_node);
1987 }
1988
1989 int batadv_tt_init(struct batadv_priv *bat_priv)
1990 {
1991 int ret;
1992
1993 ret = batadv_tt_local_init(bat_priv);
1994 if (ret < 0)
1995 return ret;
1996
1997 ret = batadv_tt_global_init(bat_priv);
1998 if (ret < 0)
1999 return ret;
2000
2001 batadv_tt_start_timer(bat_priv);
2002
2003 return 1;
2004 }
2005
2006 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2007 {
2008 struct batadv_tt_roam_node *node, *safe;
2009
2010 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2011
2012 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2013 list_del(&node->list);
2014 kfree(node);
2015 }
2016
2017 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2018 }
2019
2020 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2021 {
2022 struct batadv_tt_roam_node *node, *safe;
2023
2024 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2025 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2026 if (!batadv_has_timed_out(node->first_time,
2027 BATADV_ROAMING_MAX_TIME))
2028 continue;
2029
2030 list_del(&node->list);
2031 kfree(node);
2032 }
2033 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2034 }
2035
2036 /* This function checks whether the client already reached the
2037 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2038 * will not be sent.
2039 *
2040 * returns true if the ROAMING_ADV can be sent, false otherwise
2041 */
2042 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2043 uint8_t *client)
2044 {
2045 struct batadv_tt_roam_node *tt_roam_node;
2046 bool ret = false;
2047
2048 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2049 /* The new tt_req will be issued only if I'm not waiting for a
2050 * reply from the same orig_node yet
2051 */
2052 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2053 if (!batadv_compare_eth(tt_roam_node->addr, client))
2054 continue;
2055
2056 if (batadv_has_timed_out(tt_roam_node->first_time,
2057 BATADV_ROAMING_MAX_TIME))
2058 continue;
2059
2060 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2061 /* Sorry, you roamed too many times! */
2062 goto unlock;
2063 ret = true;
2064 break;
2065 }
2066
2067 if (!ret) {
2068 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2069 if (!tt_roam_node)
2070 goto unlock;
2071
2072 tt_roam_node->first_time = jiffies;
2073 atomic_set(&tt_roam_node->counter,
2074 BATADV_ROAMING_MAX_COUNT - 1);
2075 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2076
2077 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2078 ret = true;
2079 }
2080
2081 unlock:
2082 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2083 return ret;
2084 }
2085
2086 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2087 struct batadv_orig_node *orig_node)
2088 {
2089 struct batadv_neigh_node *neigh_node = NULL;
2090 struct sk_buff *skb = NULL;
2091 struct batadv_roam_adv_packet *roam_adv_packet;
2092 int ret = 1;
2093 struct batadv_hard_iface *primary_if;
2094 size_t len = sizeof(*roam_adv_packet);
2095
2096 /* before going on we have to check whether the client has
2097 * already roamed to us too many times
2098 */
2099 if (!batadv_tt_check_roam_count(bat_priv, client))
2100 goto out;
2101
2102 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
2103 if (!skb)
2104 goto out;
2105
2106 skb_reserve(skb, ETH_HLEN);
2107
2108 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2109
2110 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2111 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2112 roam_adv_packet->header.ttl = BATADV_TTL;
2113 roam_adv_packet->reserved = 0;
2114 primary_if = batadv_primary_if_get_selected(bat_priv);
2115 if (!primary_if)
2116 goto out;
2117 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2118 batadv_hardif_free_ref(primary_if);
2119 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2120 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2121
2122 neigh_node = batadv_orig_node_get_router(orig_node);
2123 if (!neigh_node)
2124 goto out;
2125
2126 batadv_dbg(BATADV_DBG_TT, bat_priv,
2127 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2128 orig_node->orig, client, neigh_node->addr);
2129
2130 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2131
2132 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2133 ret = 0;
2134
2135 out:
2136 if (neigh_node)
2137 batadv_neigh_node_free_ref(neigh_node);
2138 if (ret)
2139 kfree_skb(skb);
2140 return;
2141 }
2142
2143 static void batadv_tt_purge(struct work_struct *work)
2144 {
2145 struct delayed_work *delayed_work;
2146 struct batadv_priv_tt *priv_tt;
2147 struct batadv_priv *bat_priv;
2148
2149 delayed_work = container_of(work, struct delayed_work, work);
2150 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2151 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2152
2153 batadv_tt_local_purge(bat_priv);
2154 batadv_tt_global_purge(bat_priv);
2155 batadv_tt_req_purge(bat_priv);
2156 batadv_tt_roam_purge(bat_priv);
2157
2158 batadv_tt_start_timer(bat_priv);
2159 }
2160
2161 void batadv_tt_free(struct batadv_priv *bat_priv)
2162 {
2163 cancel_delayed_work_sync(&bat_priv->tt.work);
2164
2165 batadv_tt_local_table_free(bat_priv);
2166 batadv_tt_global_table_free(bat_priv);
2167 batadv_tt_req_list_free(bat_priv);
2168 batadv_tt_changes_list_free(bat_priv);
2169 batadv_tt_roam_list_free(bat_priv);
2170
2171 kfree(bat_priv->tt.last_changeset);
2172 }
2173
2174 /* This function will enable or disable the specified flags for all the entries
2175 * in the given hash table and returns the number of modified entries
2176 */
2177 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2178 uint16_t flags, bool enable)
2179 {
2180 uint32_t i;
2181 uint16_t changed_num = 0;
2182 struct hlist_head *head;
2183 struct hlist_node *node;
2184 struct batadv_tt_common_entry *tt_common_entry;
2185
2186 if (!hash)
2187 goto out;
2188
2189 for (i = 0; i < hash->size; i++) {
2190 head = &hash->table[i];
2191
2192 rcu_read_lock();
2193 hlist_for_each_entry_rcu(tt_common_entry, node,
2194 head, hash_entry) {
2195 if (enable) {
2196 if ((tt_common_entry->flags & flags) == flags)
2197 continue;
2198 tt_common_entry->flags |= flags;
2199 } else {
2200 if (!(tt_common_entry->flags & flags))
2201 continue;
2202 tt_common_entry->flags &= ~flags;
2203 }
2204 changed_num++;
2205 }
2206 rcu_read_unlock();
2207 }
2208 out:
2209 return changed_num;
2210 }
2211
2212 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2213 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2214 {
2215 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2216 struct batadv_tt_common_entry *tt_common;
2217 struct batadv_tt_local_entry *tt_local;
2218 struct hlist_node *node, *node_tmp;
2219 struct hlist_head *head;
2220 spinlock_t *list_lock; /* protects write access to the hash lists */
2221 uint32_t i;
2222
2223 if (!hash)
2224 return;
2225
2226 for (i = 0; i < hash->size; i++) {
2227 head = &hash->table[i];
2228 list_lock = &hash->list_locks[i];
2229
2230 spin_lock_bh(list_lock);
2231 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2232 hash_entry) {
2233 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2234 continue;
2235
2236 batadv_dbg(BATADV_DBG_TT, bat_priv,
2237 "Deleting local tt entry (%pM): pending\n",
2238 tt_common->addr);
2239
2240 atomic_dec(&bat_priv->tt.local_entry_num);
2241 hlist_del_rcu(node);
2242 tt_local = container_of(tt_common,
2243 struct batadv_tt_local_entry,
2244 common);
2245 batadv_tt_local_entry_free_ref(tt_local);
2246 }
2247 spin_unlock_bh(list_lock);
2248 }
2249
2250 }
2251
2252 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2253 unsigned char **packet_buff,
2254 int *packet_buff_len, int packet_min_len)
2255 {
2256 uint16_t changed_num = 0;
2257
2258 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2259 return -ENOENT;
2260
2261 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2262 BATADV_TT_CLIENT_NEW, false);
2263
2264 /* all reset entries have to be counted as local entries */
2265 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2266 batadv_tt_local_purge_pending_clients(bat_priv);
2267 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2268
2269 /* Increment the TTVN only once per OGM interval */
2270 atomic_inc(&bat_priv->tt.vn);
2271 batadv_dbg(BATADV_DBG_TT, bat_priv,
2272 "Local changes committed, updating to ttvn %u\n",
2273 (uint8_t)atomic_read(&bat_priv->tt.vn));
2274 bat_priv->tt.poss_change = false;
2275
2276 /* reset the sending counter */
2277 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2278
2279 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2280 packet_buff_len, packet_min_len);
2281 }
2282
2283 /* when calling this function (hard_iface == primary_if) has to be true */
2284 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2285 unsigned char **packet_buff, int *packet_buff_len,
2286 int packet_min_len)
2287 {
2288 int tt_num_changes;
2289
2290 /* if at least one change happened */
2291 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2292 packet_buff_len,
2293 packet_min_len);
2294
2295 /* if the changes have been sent often enough */
2296 if ((tt_num_changes < 0) &&
2297 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2298 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2299 packet_min_len, packet_min_len);
2300 tt_num_changes = 0;
2301 }
2302
2303 return tt_num_changes;
2304 }
2305
2306 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2307 uint8_t *dst)
2308 {
2309 struct batadv_tt_local_entry *tt_local_entry = NULL;
2310 struct batadv_tt_global_entry *tt_global_entry = NULL;
2311 bool ret = false;
2312
2313 if (!atomic_read(&bat_priv->ap_isolation))
2314 goto out;
2315
2316 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2317 if (!tt_local_entry)
2318 goto out;
2319
2320 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2321 if (!tt_global_entry)
2322 goto out;
2323
2324 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2325 goto out;
2326
2327 ret = true;
2328
2329 out:
2330 if (tt_global_entry)
2331 batadv_tt_global_entry_free_ref(tt_global_entry);
2332 if (tt_local_entry)
2333 batadv_tt_local_entry_free_ref(tt_local_entry);
2334 return ret;
2335 }
2336
2337 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2338 struct batadv_orig_node *orig_node,
2339 const unsigned char *tt_buff, uint8_t tt_num_changes,
2340 uint8_t ttvn, uint16_t tt_crc)
2341 {
2342 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2343 bool full_table = true;
2344 struct batadv_tt_change *tt_change;
2345
2346 /* don't care about a backbone gateways updates. */
2347 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2348 return;
2349
2350 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2351 * increased by one -> we can apply the attached changes
2352 */
2353 if ((!orig_node->tt_initialised && ttvn == 1) ||
2354 ttvn - orig_ttvn == 1) {
2355 /* the OGM could not contain the changes due to their size or
2356 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2357 * times.
2358 * In this case send a tt request
2359 */
2360 if (!tt_num_changes) {
2361 full_table = false;
2362 goto request_table;
2363 }
2364
2365 tt_change = (struct batadv_tt_change *)tt_buff;
2366 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2367 ttvn, tt_change);
2368
2369 /* Even if we received the precomputed crc with the OGM, we
2370 * prefer to recompute it to spot any possible inconsistency
2371 * in the global table
2372 */
2373 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2374
2375 /* The ttvn alone is not enough to guarantee consistency
2376 * because a single value could represent different states
2377 * (due to the wrap around). Thus a node has to check whether
2378 * the resulting table (after applying the changes) is still
2379 * consistent or not. E.g. a node could disconnect while its
2380 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2381 * checking the CRC value is mandatory to detect the
2382 * inconsistency
2383 */
2384 if (orig_node->tt_crc != tt_crc)
2385 goto request_table;
2386
2387 /* Roaming phase is over: tables are in sync again. I can
2388 * unset the flag
2389 */
2390 orig_node->tt_poss_change = false;
2391 } else {
2392 /* if we missed more than one change or our tables are not
2393 * in sync anymore -> request fresh tt data
2394 */
2395 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2396 orig_node->tt_crc != tt_crc) {
2397 request_table:
2398 batadv_dbg(BATADV_DBG_TT, bat_priv,
2399 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2400 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2401 orig_node->tt_crc, tt_num_changes);
2402 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2403 tt_crc, full_table);
2404 return;
2405 }
2406 }
2407 }
2408
2409 /* returns true whether we know that the client has moved from its old
2410 * originator to another one. This entry is kept is still kept for consistency
2411 * purposes
2412 */
2413 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2414 uint8_t *addr)
2415 {
2416 struct batadv_tt_global_entry *tt_global_entry;
2417 bool ret = false;
2418
2419 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2420 if (!tt_global_entry)
2421 goto out;
2422
2423 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2424 batadv_tt_global_entry_free_ref(tt_global_entry);
2425 out:
2426 return ret;
2427 }
2428
2429 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2430 struct batadv_orig_node *orig_node,
2431 const unsigned char *addr)
2432 {
2433 bool ret = false;
2434
2435 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2436 BATADV_TT_CLIENT_TEMP,
2437 atomic_read(&orig_node->last_ttvn)))
2438 goto out;
2439
2440 batadv_dbg(BATADV_DBG_TT, bat_priv,
2441 "Added temporary global client (addr: %pM orig: %pM)\n",
2442 addr, orig_node->orig);
2443 ret = true;
2444 out:
2445 return ret;
2446 }
This page took 0.086308 seconds and 5 git commands to generate.