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