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