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