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