]>
Commit | Line | Data |
---|---|---|
c6c8fea2 | 1 | /* |
567db7b0 | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Marek Lindner, Simon Wunderlich | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of version 2 of the GNU General Public | |
8 | * License as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include "main.h" | |
23 | #include "translation-table.h" | |
24 | #include "soft-interface.h" | |
32ae9b22 | 25 | #include "hard-interface.h" |
a73105b8 | 26 | #include "send.h" |
c6c8fea2 SE |
27 | #include "hash.h" |
28 | #include "originator.h" | |
a73105b8 | 29 | #include "routing.h" |
c6c8fea2 | 30 | |
a73105b8 AQ |
31 | #include <linux/crc16.h> |
32 | ||
33 | static void _tt_global_del(struct bat_priv *bat_priv, | |
34 | struct tt_global_entry *tt_global_entry, | |
35 | const char *message); | |
36 | static void tt_purge(struct work_struct *work); | |
c6c8fea2 | 37 | |
7aadf889 | 38 | /* returns 1 if they are the same mac addr */ |
48100bac | 39 | static int compare_tt(const struct hlist_node *node, const void *data2) |
7aadf889 | 40 | { |
48100bac | 41 | const void *data1 = container_of(node, struct tt_common_entry, |
747e4221 | 42 | hash_entry); |
7aadf889 ML |
43 | |
44 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); | |
45 | } | |
46 | ||
a73105b8 | 47 | static void tt_start_timer(struct bat_priv *bat_priv) |
c6c8fea2 | 48 | { |
a73105b8 AQ |
49 | INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge); |
50 | queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, | |
51 | msecs_to_jiffies(5000)); | |
c6c8fea2 SE |
52 | } |
53 | ||
48100bac AQ |
54 | static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash, |
55 | const void *data) | |
7aadf889 | 56 | { |
7aadf889 ML |
57 | struct hlist_head *head; |
58 | struct hlist_node *node; | |
48100bac | 59 | struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL; |
c90681b8 | 60 | uint32_t index; |
7aadf889 ML |
61 | |
62 | if (!hash) | |
63 | return NULL; | |
64 | ||
65 | index = choose_orig(data, hash->size); | |
66 | head = &hash->table[index]; | |
67 | ||
68 | rcu_read_lock(); | |
48100bac AQ |
69 | hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) { |
70 | if (!compare_eth(tt_common_entry, data)) | |
7aadf889 ML |
71 | continue; |
72 | ||
48100bac | 73 | if (!atomic_inc_not_zero(&tt_common_entry->refcount)) |
7683fdc1 AQ |
74 | continue; |
75 | ||
48100bac | 76 | tt_common_entry_tmp = tt_common_entry; |
7aadf889 ML |
77 | break; |
78 | } | |
79 | rcu_read_unlock(); | |
80 | ||
48100bac | 81 | return tt_common_entry_tmp; |
7aadf889 ML |
82 | } |
83 | ||
48100bac AQ |
84 | static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv, |
85 | const void *data) | |
7aadf889 | 86 | { |
48100bac AQ |
87 | struct tt_common_entry *tt_common_entry; |
88 | struct tt_local_entry *tt_local_entry = NULL; | |
7aadf889 | 89 | |
48100bac AQ |
90 | tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data); |
91 | if (tt_common_entry) | |
92 | tt_local_entry = container_of(tt_common_entry, | |
93 | struct tt_local_entry, common); | |
94 | return tt_local_entry; | |
95 | } | |
7aadf889 | 96 | |
48100bac AQ |
97 | static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv, |
98 | const void *data) | |
99 | { | |
100 | struct tt_common_entry *tt_common_entry; | |
101 | struct tt_global_entry *tt_global_entry = NULL; | |
7683fdc1 | 102 | |
48100bac AQ |
103 | tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data); |
104 | if (tt_common_entry) | |
105 | tt_global_entry = container_of(tt_common_entry, | |
106 | struct tt_global_entry, common); | |
107 | return tt_global_entry; | |
7aadf889 | 108 | |
7aadf889 ML |
109 | } |
110 | ||
7683fdc1 AQ |
111 | static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry) |
112 | { | |
48100bac AQ |
113 | if (atomic_dec_and_test(&tt_local_entry->common.refcount)) |
114 | kfree_rcu(tt_local_entry, common.rcu); | |
7683fdc1 AQ |
115 | } |
116 | ||
531027fc SW |
117 | static void tt_global_entry_free_rcu(struct rcu_head *rcu) |
118 | { | |
48100bac | 119 | struct tt_common_entry *tt_common_entry; |
531027fc SW |
120 | struct tt_global_entry *tt_global_entry; |
121 | ||
48100bac AQ |
122 | tt_common_entry = container_of(rcu, struct tt_common_entry, rcu); |
123 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, | |
124 | common); | |
531027fc SW |
125 | |
126 | if (tt_global_entry->orig_node) | |
127 | orig_node_free_ref(tt_global_entry->orig_node); | |
128 | ||
129 | kfree(tt_global_entry); | |
130 | } | |
131 | ||
7683fdc1 AQ |
132 | static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry) |
133 | { | |
48100bac AQ |
134 | if (atomic_dec_and_test(&tt_global_entry->common.refcount)) |
135 | call_rcu(&tt_global_entry->common.rcu, | |
136 | tt_global_entry_free_rcu); | |
7683fdc1 AQ |
137 | } |
138 | ||
ff66c975 AQ |
139 | static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr, |
140 | uint8_t flags) | |
a73105b8 AQ |
141 | { |
142 | struct tt_change_node *tt_change_node; | |
143 | ||
144 | tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC); | |
145 | ||
146 | if (!tt_change_node) | |
147 | return; | |
148 | ||
ff66c975 | 149 | tt_change_node->change.flags = flags; |
a73105b8 AQ |
150 | memcpy(tt_change_node->change.addr, addr, ETH_ALEN); |
151 | ||
152 | spin_lock_bh(&bat_priv->tt_changes_list_lock); | |
153 | /* track the change in the OGMinterval list */ | |
154 | list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list); | |
155 | atomic_inc(&bat_priv->tt_local_changes); | |
156 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); | |
157 | ||
158 | atomic_set(&bat_priv->tt_ogm_append_cnt, 0); | |
159 | } | |
160 | ||
161 | int tt_len(int changes_num) | |
162 | { | |
163 | return changes_num * sizeof(struct tt_change); | |
164 | } | |
165 | ||
166 | static int tt_local_init(struct bat_priv *bat_priv) | |
c6c8fea2 | 167 | { |
2dafb49d | 168 | if (bat_priv->tt_local_hash) |
c6c8fea2 SE |
169 | return 1; |
170 | ||
2dafb49d | 171 | bat_priv->tt_local_hash = hash_new(1024); |
c6c8fea2 | 172 | |
2dafb49d | 173 | if (!bat_priv->tt_local_hash) |
c6c8fea2 SE |
174 | return 0; |
175 | ||
c6c8fea2 SE |
176 | return 1; |
177 | } | |
178 | ||
bc279080 AQ |
179 | void tt_local_add(struct net_device *soft_iface, const uint8_t *addr, |
180 | int ifindex) | |
c6c8fea2 SE |
181 | { |
182 | struct bat_priv *bat_priv = netdev_priv(soft_iface); | |
7683fdc1 AQ |
183 | struct tt_local_entry *tt_local_entry = NULL; |
184 | struct tt_global_entry *tt_global_entry = NULL; | |
80b3f58c | 185 | int hash_added; |
c6c8fea2 | 186 | |
2dafb49d | 187 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
c6c8fea2 | 188 | |
2dafb49d AQ |
189 | if (tt_local_entry) { |
190 | tt_local_entry->last_seen = jiffies; | |
7683fdc1 | 191 | goto out; |
c6c8fea2 SE |
192 | } |
193 | ||
704509b8 | 194 | tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC); |
2dafb49d | 195 | if (!tt_local_entry) |
7683fdc1 | 196 | goto out; |
a73105b8 | 197 | |
a73105b8 AQ |
198 | bat_dbg(DBG_TT, bat_priv, |
199 | "Creating new local tt entry: %pM (ttvn: %d)\n", addr, | |
200 | (uint8_t)atomic_read(&bat_priv->ttvn)); | |
c6c8fea2 | 201 | |
48100bac AQ |
202 | memcpy(tt_local_entry->common.addr, addr, ETH_ALEN); |
203 | tt_local_entry->common.flags = NO_FLAGS; | |
bc279080 | 204 | if (is_wifi_iface(ifindex)) |
48100bac AQ |
205 | tt_local_entry->common.flags |= TT_CLIENT_WIFI; |
206 | atomic_set(&tt_local_entry->common.refcount, 2); | |
207 | tt_local_entry->last_seen = jiffies; | |
c6c8fea2 SE |
208 | |
209 | /* the batman interface mac address should never be purged */ | |
39901e71 | 210 | if (compare_eth(addr, soft_iface->dev_addr)) |
48100bac | 211 | tt_local_entry->common.flags |= TT_CLIENT_NOPURGE; |
c6c8fea2 | 212 | |
c40ed2bf AQ |
213 | /* The local entry has to be marked as NEW to avoid to send it in |
214 | * a full table response going out before the next ttvn increment | |
215 | * (consistency check) */ | |
216 | tt_local_entry->common.flags |= TT_CLIENT_NEW; | |
217 | ||
80b3f58c SW |
218 | hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig, |
219 | &tt_local_entry->common, | |
220 | &tt_local_entry->common.hash_entry); | |
221 | ||
222 | if (unlikely(hash_added != 0)) { | |
223 | /* remove the reference for the hash */ | |
224 | tt_local_entry_free_ref(tt_local_entry); | |
225 | goto out; | |
226 | } | |
227 | ||
48100bac | 228 | tt_local_event(bat_priv, addr, tt_local_entry->common.flags); |
ff66c975 | 229 | |
c6c8fea2 | 230 | /* remove address from global hash if present */ |
2dafb49d | 231 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
c6c8fea2 | 232 | |
cc47f66e AQ |
233 | /* Check whether it is a roaming! */ |
234 | if (tt_global_entry) { | |
cc47f66e AQ |
235 | /* This node is probably going to update its tt table */ |
236 | tt_global_entry->orig_node->tt_poss_change = true; | |
03fc3070 | 237 | /* The global entry has to be marked as ROAMING and has to be |
980d55b2 | 238 | * kept for consistency purpose */ |
220b07e9 | 239 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; |
03fc3070 | 240 | tt_global_entry->roam_at = jiffies; |
48100bac | 241 | send_roam_adv(bat_priv, tt_global_entry->common.addr, |
7683fdc1 AQ |
242 | tt_global_entry->orig_node); |
243 | } | |
244 | out: | |
245 | if (tt_local_entry) | |
246 | tt_local_entry_free_ref(tt_local_entry); | |
247 | if (tt_global_entry) | |
248 | tt_global_entry_free_ref(tt_global_entry); | |
c6c8fea2 SE |
249 | } |
250 | ||
a73105b8 AQ |
251 | int tt_changes_fill_buffer(struct bat_priv *bat_priv, |
252 | unsigned char *buff, int buff_len) | |
c6c8fea2 | 253 | { |
a73105b8 AQ |
254 | int count = 0, tot_changes = 0; |
255 | struct tt_change_node *entry, *safe; | |
c6c8fea2 | 256 | |
a73105b8 AQ |
257 | if (buff_len > 0) |
258 | tot_changes = buff_len / tt_len(1); | |
c6c8fea2 | 259 | |
a73105b8 AQ |
260 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
261 | atomic_set(&bat_priv->tt_local_changes, 0); | |
c6c8fea2 | 262 | |
a73105b8 AQ |
263 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
264 | list) { | |
265 | if (count < tot_changes) { | |
266 | memcpy(buff + tt_len(count), | |
267 | &entry->change, sizeof(struct tt_change)); | |
c6c8fea2 SE |
268 | count++; |
269 | } | |
a73105b8 AQ |
270 | list_del(&entry->list); |
271 | kfree(entry); | |
c6c8fea2 | 272 | } |
a73105b8 AQ |
273 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
274 | ||
275 | /* Keep the buffer for possible tt_request */ | |
276 | spin_lock_bh(&bat_priv->tt_buff_lock); | |
277 | kfree(bat_priv->tt_buff); | |
278 | bat_priv->tt_buff_len = 0; | |
279 | bat_priv->tt_buff = NULL; | |
280 | /* We check whether this new OGM has no changes due to size | |
281 | * problems */ | |
282 | if (buff_len > 0) { | |
283 | /** | |
284 | * if kmalloc() fails we will reply with the full table | |
285 | * instead of providing the diff | |
286 | */ | |
287 | bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC); | |
288 | if (bat_priv->tt_buff) { | |
289 | memcpy(bat_priv->tt_buff, buff, buff_len); | |
290 | bat_priv->tt_buff_len = buff_len; | |
291 | } | |
292 | } | |
293 | spin_unlock_bh(&bat_priv->tt_buff_lock); | |
c6c8fea2 | 294 | |
a73105b8 | 295 | return tot_changes; |
c6c8fea2 SE |
296 | } |
297 | ||
2dafb49d | 298 | int tt_local_seq_print_text(struct seq_file *seq, void *offset) |
c6c8fea2 SE |
299 | { |
300 | struct net_device *net_dev = (struct net_device *)seq->private; | |
301 | struct bat_priv *bat_priv = netdev_priv(net_dev); | |
2dafb49d | 302 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
48100bac | 303 | struct tt_common_entry *tt_common_entry; |
32ae9b22 | 304 | struct hard_iface *primary_if; |
7aadf889 | 305 | struct hlist_node *node; |
c6c8fea2 | 306 | struct hlist_head *head; |
c90681b8 AQ |
307 | uint32_t i; |
308 | int ret = 0; | |
32ae9b22 ML |
309 | |
310 | primary_if = primary_if_get_selected(bat_priv); | |
311 | if (!primary_if) { | |
312 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " | |
313 | "please specify interfaces to enable it\n", | |
314 | net_dev->name); | |
315 | goto out; | |
316 | } | |
c6c8fea2 | 317 | |
32ae9b22 ML |
318 | if (primary_if->if_status != IF_ACTIVE) { |
319 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " | |
320 | "primary interface not active\n", | |
321 | net_dev->name); | |
322 | goto out; | |
c6c8fea2 SE |
323 | } |
324 | ||
325 | seq_printf(seq, "Locally retrieved addresses (from %s) " | |
a73105b8 AQ |
326 | "announced via TT (TTVN: %u):\n", |
327 | net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn)); | |
c6c8fea2 | 328 | |
c6c8fea2 SE |
329 | for (i = 0; i < hash->size; i++) { |
330 | head = &hash->table[i]; | |
331 | ||
7aadf889 | 332 | rcu_read_lock(); |
48100bac | 333 | hlist_for_each_entry_rcu(tt_common_entry, node, |
7aadf889 | 334 | head, hash_entry) { |
d099c2c5 | 335 | seq_printf(seq, " * %pM [%c%c%c%c%c]\n", |
48100bac AQ |
336 | tt_common_entry->addr, |
337 | (tt_common_entry->flags & | |
df6edb9e | 338 | TT_CLIENT_ROAM ? 'R' : '.'), |
48100bac | 339 | (tt_common_entry->flags & |
df6edb9e | 340 | TT_CLIENT_NOPURGE ? 'P' : '.'), |
48100bac | 341 | (tt_common_entry->flags & |
df6edb9e | 342 | TT_CLIENT_NEW ? 'N' : '.'), |
48100bac | 343 | (tt_common_entry->flags & |
df6edb9e | 344 | TT_CLIENT_PENDING ? 'X' : '.'), |
48100bac | 345 | (tt_common_entry->flags & |
df6edb9e | 346 | TT_CLIENT_WIFI ? 'W' : '.')); |
c6c8fea2 | 347 | } |
7aadf889 | 348 | rcu_read_unlock(); |
c6c8fea2 | 349 | } |
32ae9b22 ML |
350 | out: |
351 | if (primary_if) | |
352 | hardif_free_ref(primary_if); | |
353 | return ret; | |
c6c8fea2 SE |
354 | } |
355 | ||
058d0e26 AQ |
356 | static void tt_local_set_pending(struct bat_priv *bat_priv, |
357 | struct tt_local_entry *tt_local_entry, | |
358 | uint16_t flags) | |
c6c8fea2 | 359 | { |
48100bac AQ |
360 | tt_local_event(bat_priv, tt_local_entry->common.addr, |
361 | tt_local_entry->common.flags | flags); | |
a73105b8 | 362 | |
015758d0 AQ |
363 | /* The local client has to be marked as "pending to be removed" but has |
364 | * to be kept in the table in order to send it in a full table | |
058d0e26 | 365 | * response issued before the net ttvn increment (consistency check) */ |
48100bac | 366 | tt_local_entry->common.flags |= TT_CLIENT_PENDING; |
c6c8fea2 SE |
367 | } |
368 | ||
a73105b8 | 369 | void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr, |
cc47f66e | 370 | const char *message, bool roaming) |
c6c8fea2 | 371 | { |
7683fdc1 | 372 | struct tt_local_entry *tt_local_entry = NULL; |
c6c8fea2 | 373 | |
2dafb49d | 374 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
7683fdc1 AQ |
375 | if (!tt_local_entry) |
376 | goto out; | |
377 | ||
058d0e26 AQ |
378 | tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL | |
379 | (roaming ? TT_CLIENT_ROAM : NO_FLAGS)); | |
380 | ||
381 | bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: " | |
48100bac | 382 | "%s\n", tt_local_entry->common.addr, message); |
7683fdc1 AQ |
383 | out: |
384 | if (tt_local_entry) | |
385 | tt_local_entry_free_ref(tt_local_entry); | |
c6c8fea2 SE |
386 | } |
387 | ||
a73105b8 | 388 | static void tt_local_purge(struct bat_priv *bat_priv) |
c6c8fea2 | 389 | { |
2dafb49d AQ |
390 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
391 | struct tt_local_entry *tt_local_entry; | |
48100bac | 392 | struct tt_common_entry *tt_common_entry; |
7aadf889 | 393 | struct hlist_node *node, *node_tmp; |
c6c8fea2 | 394 | struct hlist_head *head; |
7683fdc1 | 395 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
c90681b8 | 396 | uint32_t i; |
c6c8fea2 | 397 | |
c6c8fea2 SE |
398 | for (i = 0; i < hash->size; i++) { |
399 | head = &hash->table[i]; | |
7683fdc1 | 400 | list_lock = &hash->list_locks[i]; |
c6c8fea2 | 401 | |
7683fdc1 | 402 | spin_lock_bh(list_lock); |
48100bac | 403 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
7aadf889 | 404 | head, hash_entry) { |
48100bac AQ |
405 | tt_local_entry = container_of(tt_common_entry, |
406 | struct tt_local_entry, | |
407 | common); | |
408 | if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE) | |
7aadf889 | 409 | continue; |
c6c8fea2 | 410 | |
058d0e26 | 411 | /* entry already marked for deletion */ |
48100bac | 412 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
058d0e26 AQ |
413 | continue; |
414 | ||
a04ccd59 | 415 | if (!has_timed_out(tt_local_entry->last_seen, |
032b7969 | 416 | TT_LOCAL_TIMEOUT)) |
7aadf889 ML |
417 | continue; |
418 | ||
058d0e26 AQ |
419 | tt_local_set_pending(bat_priv, tt_local_entry, |
420 | TT_CLIENT_DEL); | |
421 | bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) " | |
422 | "pending to be removed: timed out\n", | |
48100bac | 423 | tt_local_entry->common.addr); |
c6c8fea2 | 424 | } |
7683fdc1 | 425 | spin_unlock_bh(list_lock); |
c6c8fea2 SE |
426 | } |
427 | ||
c6c8fea2 SE |
428 | } |
429 | ||
a73105b8 | 430 | static void tt_local_table_free(struct bat_priv *bat_priv) |
c6c8fea2 | 431 | { |
a73105b8 | 432 | struct hashtable_t *hash; |
a73105b8 | 433 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
48100bac | 434 | struct tt_common_entry *tt_common_entry; |
a73105b8 | 435 | struct tt_local_entry *tt_local_entry; |
7683fdc1 AQ |
436 | struct hlist_node *node, *node_tmp; |
437 | struct hlist_head *head; | |
c90681b8 | 438 | uint32_t i; |
a73105b8 | 439 | |
2dafb49d | 440 | if (!bat_priv->tt_local_hash) |
c6c8fea2 SE |
441 | return; |
442 | ||
a73105b8 AQ |
443 | hash = bat_priv->tt_local_hash; |
444 | ||
445 | for (i = 0; i < hash->size; i++) { | |
446 | head = &hash->table[i]; | |
447 | list_lock = &hash->list_locks[i]; | |
448 | ||
449 | spin_lock_bh(list_lock); | |
48100bac | 450 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
a73105b8 AQ |
451 | head, hash_entry) { |
452 | hlist_del_rcu(node); | |
48100bac AQ |
453 | tt_local_entry = container_of(tt_common_entry, |
454 | struct tt_local_entry, | |
455 | common); | |
7683fdc1 | 456 | tt_local_entry_free_ref(tt_local_entry); |
a73105b8 AQ |
457 | } |
458 | spin_unlock_bh(list_lock); | |
459 | } | |
460 | ||
461 | hash_destroy(hash); | |
462 | ||
2dafb49d | 463 | bat_priv->tt_local_hash = NULL; |
c6c8fea2 SE |
464 | } |
465 | ||
a73105b8 | 466 | static int tt_global_init(struct bat_priv *bat_priv) |
c6c8fea2 | 467 | { |
2dafb49d | 468 | if (bat_priv->tt_global_hash) |
c6c8fea2 SE |
469 | return 1; |
470 | ||
2dafb49d | 471 | bat_priv->tt_global_hash = hash_new(1024); |
c6c8fea2 | 472 | |
2dafb49d | 473 | if (!bat_priv->tt_global_hash) |
c6c8fea2 SE |
474 | return 0; |
475 | ||
476 | return 1; | |
477 | } | |
478 | ||
a73105b8 | 479 | static void tt_changes_list_free(struct bat_priv *bat_priv) |
c6c8fea2 | 480 | { |
a73105b8 | 481 | struct tt_change_node *entry, *safe; |
c6c8fea2 | 482 | |
a73105b8 | 483 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
c6c8fea2 | 484 | |
a73105b8 AQ |
485 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
486 | list) { | |
487 | list_del(&entry->list); | |
488 | kfree(entry); | |
489 | } | |
c6c8fea2 | 490 | |
a73105b8 AQ |
491 | atomic_set(&bat_priv->tt_local_changes, 0); |
492 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); | |
493 | } | |
c6c8fea2 | 494 | |
a73105b8 AQ |
495 | /* caller must hold orig_node refcount */ |
496 | int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node, | |
bc279080 AQ |
497 | const unsigned char *tt_addr, uint8_t ttvn, bool roaming, |
498 | bool wifi) | |
a73105b8 AQ |
499 | { |
500 | struct tt_global_entry *tt_global_entry; | |
a73105b8 | 501 | struct orig_node *orig_node_tmp; |
7683fdc1 | 502 | int ret = 0; |
80b3f58c | 503 | int hash_added; |
c6c8fea2 | 504 | |
a73105b8 AQ |
505 | tt_global_entry = tt_global_hash_find(bat_priv, tt_addr); |
506 | ||
507 | if (!tt_global_entry) { | |
508 | tt_global_entry = | |
509 | kmalloc(sizeof(*tt_global_entry), | |
510 | GFP_ATOMIC); | |
511 | if (!tt_global_entry) | |
7683fdc1 AQ |
512 | goto out; |
513 | ||
48100bac AQ |
514 | memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN); |
515 | tt_global_entry->common.flags = NO_FLAGS; | |
516 | atomic_set(&tt_global_entry->common.refcount, 2); | |
a73105b8 AQ |
517 | /* Assign the new orig_node */ |
518 | atomic_inc(&orig_node->refcount); | |
2dafb49d | 519 | tt_global_entry->orig_node = orig_node; |
a73105b8 | 520 | tt_global_entry->ttvn = ttvn; |
cc47f66e | 521 | tt_global_entry->roam_at = 0; |
7683fdc1 | 522 | |
80b3f58c SW |
523 | hash_added = hash_add(bat_priv->tt_global_hash, compare_tt, |
524 | choose_orig, &tt_global_entry->common, | |
525 | &tt_global_entry->common.hash_entry); | |
526 | ||
527 | if (unlikely(hash_added != 0)) { | |
528 | /* remove the reference for the hash */ | |
529 | tt_global_entry_free_ref(tt_global_entry); | |
530 | goto out_remove; | |
531 | } | |
7683fdc1 | 532 | atomic_inc(&orig_node->tt_size); |
a73105b8 AQ |
533 | } else { |
534 | if (tt_global_entry->orig_node != orig_node) { | |
535 | atomic_dec(&tt_global_entry->orig_node->tt_size); | |
536 | orig_node_tmp = tt_global_entry->orig_node; | |
537 | atomic_inc(&orig_node->refcount); | |
538 | tt_global_entry->orig_node = orig_node; | |
a73105b8 AQ |
539 | orig_node_free_ref(orig_node_tmp); |
540 | atomic_inc(&orig_node->tt_size); | |
541 | } | |
48100bac | 542 | tt_global_entry->common.flags = NO_FLAGS; |
cc47f66e | 543 | tt_global_entry->ttvn = ttvn; |
cc47f66e | 544 | tt_global_entry->roam_at = 0; |
a73105b8 | 545 | } |
c6c8fea2 | 546 | |
bc279080 | 547 | if (wifi) |
48100bac | 548 | tt_global_entry->common.flags |= TT_CLIENT_WIFI; |
bc279080 | 549 | |
a73105b8 AQ |
550 | bat_dbg(DBG_TT, bat_priv, |
551 | "Creating new global tt entry: %pM (via %pM)\n", | |
48100bac | 552 | tt_global_entry->common.addr, orig_node->orig); |
c6c8fea2 | 553 | |
80b3f58c | 554 | out_remove: |
a73105b8 | 555 | /* remove address from local hash if present */ |
48100bac | 556 | tt_local_remove(bat_priv, tt_global_entry->common.addr, |
7683fdc1 AQ |
557 | "global tt received", roaming); |
558 | ret = 1; | |
559 | out: | |
560 | if (tt_global_entry) | |
561 | tt_global_entry_free_ref(tt_global_entry); | |
562 | return ret; | |
c6c8fea2 SE |
563 | } |
564 | ||
2dafb49d | 565 | int tt_global_seq_print_text(struct seq_file *seq, void *offset) |
c6c8fea2 SE |
566 | { |
567 | struct net_device *net_dev = (struct net_device *)seq->private; | |
568 | struct bat_priv *bat_priv = netdev_priv(net_dev); | |
2dafb49d | 569 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
48100bac | 570 | struct tt_common_entry *tt_common_entry; |
2dafb49d | 571 | struct tt_global_entry *tt_global_entry; |
32ae9b22 | 572 | struct hard_iface *primary_if; |
7aadf889 | 573 | struct hlist_node *node; |
c6c8fea2 | 574 | struct hlist_head *head; |
c90681b8 AQ |
575 | uint32_t i; |
576 | int ret = 0; | |
32ae9b22 ML |
577 | |
578 | primary_if = primary_if_get_selected(bat_priv); | |
579 | if (!primary_if) { | |
580 | ret = seq_printf(seq, "BATMAN mesh %s disabled - please " | |
581 | "specify interfaces to enable it\n", | |
582 | net_dev->name); | |
583 | goto out; | |
584 | } | |
c6c8fea2 | 585 | |
32ae9b22 ML |
586 | if (primary_if->if_status != IF_ACTIVE) { |
587 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " | |
588 | "primary interface not active\n", | |
589 | net_dev->name); | |
590 | goto out; | |
c6c8fea2 SE |
591 | } |
592 | ||
2dafb49d AQ |
593 | seq_printf(seq, |
594 | "Globally announced TT entries received via the mesh %s\n", | |
c6c8fea2 | 595 | net_dev->name); |
df6edb9e AQ |
596 | seq_printf(seq, " %-13s %s %-15s %s %s\n", |
597 | "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags"); | |
c6c8fea2 | 598 | |
c6c8fea2 SE |
599 | for (i = 0; i < hash->size; i++) { |
600 | head = &hash->table[i]; | |
601 | ||
7aadf889 | 602 | rcu_read_lock(); |
48100bac | 603 | hlist_for_each_entry_rcu(tt_common_entry, node, |
7aadf889 | 604 | head, hash_entry) { |
48100bac AQ |
605 | tt_global_entry = container_of(tt_common_entry, |
606 | struct tt_global_entry, | |
607 | common); | |
d099c2c5 | 608 | seq_printf(seq, " * %pM (%3u) via %pM (%3u) " |
48100bac AQ |
609 | "[%c%c%c]\n", |
610 | tt_global_entry->common.addr, | |
a73105b8 AQ |
611 | tt_global_entry->ttvn, |
612 | tt_global_entry->orig_node->orig, | |
613 | (uint8_t) atomic_read( | |
614 | &tt_global_entry->orig_node-> | |
df6edb9e | 615 | last_ttvn), |
48100bac | 616 | (tt_global_entry->common.flags & |
df6edb9e | 617 | TT_CLIENT_ROAM ? 'R' : '.'), |
48100bac | 618 | (tt_global_entry->common.flags & |
df6edb9e | 619 | TT_CLIENT_PENDING ? 'X' : '.'), |
48100bac | 620 | (tt_global_entry->common.flags & |
df6edb9e | 621 | TT_CLIENT_WIFI ? 'W' : '.')); |
c6c8fea2 | 622 | } |
7aadf889 | 623 | rcu_read_unlock(); |
c6c8fea2 | 624 | } |
32ae9b22 ML |
625 | out: |
626 | if (primary_if) | |
627 | hardif_free_ref(primary_if); | |
628 | return ret; | |
c6c8fea2 SE |
629 | } |
630 | ||
a73105b8 AQ |
631 | static void _tt_global_del(struct bat_priv *bat_priv, |
632 | struct tt_global_entry *tt_global_entry, | |
633 | const char *message) | |
c6c8fea2 | 634 | { |
a73105b8 | 635 | if (!tt_global_entry) |
7683fdc1 | 636 | goto out; |
a73105b8 AQ |
637 | |
638 | bat_dbg(DBG_TT, bat_priv, | |
2dafb49d | 639 | "Deleting global tt entry %pM (via %pM): %s\n", |
48100bac | 640 | tt_global_entry->common.addr, tt_global_entry->orig_node->orig, |
c6c8fea2 SE |
641 | message); |
642 | ||
a73105b8 | 643 | atomic_dec(&tt_global_entry->orig_node->tt_size); |
7683fdc1 | 644 | |
48100bac AQ |
645 | hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig, |
646 | tt_global_entry->common.addr); | |
7683fdc1 AQ |
647 | out: |
648 | if (tt_global_entry) | |
649 | tt_global_entry_free_ref(tt_global_entry); | |
c6c8fea2 SE |
650 | } |
651 | ||
a73105b8 AQ |
652 | void tt_global_del(struct bat_priv *bat_priv, |
653 | struct orig_node *orig_node, const unsigned char *addr, | |
cc47f66e | 654 | const char *message, bool roaming) |
a73105b8 | 655 | { |
7683fdc1 | 656 | struct tt_global_entry *tt_global_entry = NULL; |
797399b4 | 657 | struct tt_local_entry *tt_local_entry = NULL; |
a73105b8 | 658 | |
a73105b8 | 659 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
92f90f56 | 660 | if (!tt_global_entry || tt_global_entry->orig_node != orig_node) |
7683fdc1 | 661 | goto out; |
a73105b8 | 662 | |
92f90f56 SE |
663 | if (!roaming) |
664 | goto out_del; | |
665 | ||
666 | /* if we are deleting a global entry due to a roam | |
667 | * event, there are two possibilities: | |
668 | * 1) the client roamed from node A to node B => we mark | |
669 | * it with TT_CLIENT_ROAM, we start a timer and we | |
670 | * wait for node B to claim it. In case of timeout | |
671 | * the entry is purged. | |
672 | * 2) the client roamed to us => we can directly delete | |
673 | * the global entry, since it is useless now. */ | |
674 | tt_local_entry = tt_local_hash_find(bat_priv, | |
675 | tt_global_entry->common.addr); | |
676 | if (!tt_local_entry) { | |
677 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; | |
678 | tt_global_entry->roam_at = jiffies; | |
679 | goto out; | |
a73105b8 | 680 | } |
92f90f56 SE |
681 | |
682 | out_del: | |
683 | _tt_global_del(bat_priv, tt_global_entry, message); | |
684 | ||
cc47f66e | 685 | out: |
7683fdc1 AQ |
686 | if (tt_global_entry) |
687 | tt_global_entry_free_ref(tt_global_entry); | |
797399b4 AQ |
688 | if (tt_local_entry) |
689 | tt_local_entry_free_ref(tt_local_entry); | |
a73105b8 AQ |
690 | } |
691 | ||
2dafb49d | 692 | void tt_global_del_orig(struct bat_priv *bat_priv, |
a73105b8 | 693 | struct orig_node *orig_node, const char *message) |
c6c8fea2 | 694 | { |
2dafb49d | 695 | struct tt_global_entry *tt_global_entry; |
48100bac | 696 | struct tt_common_entry *tt_common_entry; |
c90681b8 | 697 | uint32_t i; |
a73105b8 AQ |
698 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
699 | struct hlist_node *node, *safe; | |
700 | struct hlist_head *head; | |
7683fdc1 | 701 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
c6c8fea2 | 702 | |
6e801494 SW |
703 | if (!hash) |
704 | return; | |
705 | ||
a73105b8 AQ |
706 | for (i = 0; i < hash->size; i++) { |
707 | head = &hash->table[i]; | |
7683fdc1 | 708 | list_lock = &hash->list_locks[i]; |
c6c8fea2 | 709 | |
7683fdc1 | 710 | spin_lock_bh(list_lock); |
48100bac | 711 | hlist_for_each_entry_safe(tt_common_entry, node, safe, |
a73105b8 | 712 | head, hash_entry) { |
48100bac AQ |
713 | tt_global_entry = container_of(tt_common_entry, |
714 | struct tt_global_entry, | |
715 | common); | |
7683fdc1 AQ |
716 | if (tt_global_entry->orig_node == orig_node) { |
717 | bat_dbg(DBG_TT, bat_priv, | |
718 | "Deleting global tt entry %pM " | |
87944973 | 719 | "(via %pM): %s\n", |
48100bac | 720 | tt_global_entry->common.addr, |
87944973 AQ |
721 | tt_global_entry->orig_node->orig, |
722 | message); | |
7683fdc1 AQ |
723 | hlist_del_rcu(node); |
724 | tt_global_entry_free_ref(tt_global_entry); | |
725 | } | |
a73105b8 | 726 | } |
7683fdc1 | 727 | spin_unlock_bh(list_lock); |
c6c8fea2 | 728 | } |
a73105b8 | 729 | atomic_set(&orig_node->tt_size, 0); |
17071578 | 730 | orig_node->tt_initialised = false; |
c6c8fea2 SE |
731 | } |
732 | ||
cc47f66e AQ |
733 | static void tt_global_roam_purge(struct bat_priv *bat_priv) |
734 | { | |
735 | struct hashtable_t *hash = bat_priv->tt_global_hash; | |
48100bac | 736 | struct tt_common_entry *tt_common_entry; |
cc47f66e AQ |
737 | struct tt_global_entry *tt_global_entry; |
738 | struct hlist_node *node, *node_tmp; | |
739 | struct hlist_head *head; | |
7683fdc1 | 740 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
c90681b8 | 741 | uint32_t i; |
cc47f66e | 742 | |
cc47f66e AQ |
743 | for (i = 0; i < hash->size; i++) { |
744 | head = &hash->table[i]; | |
7683fdc1 | 745 | list_lock = &hash->list_locks[i]; |
cc47f66e | 746 | |
7683fdc1 | 747 | spin_lock_bh(list_lock); |
48100bac | 748 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
cc47f66e | 749 | head, hash_entry) { |
48100bac AQ |
750 | tt_global_entry = container_of(tt_common_entry, |
751 | struct tt_global_entry, | |
752 | common); | |
753 | if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM)) | |
cc47f66e | 754 | continue; |
a04ccd59 | 755 | if (!has_timed_out(tt_global_entry->roam_at, |
032b7969 | 756 | TT_CLIENT_ROAM_TIMEOUT)) |
cc47f66e AQ |
757 | continue; |
758 | ||
7683fdc1 AQ |
759 | bat_dbg(DBG_TT, bat_priv, "Deleting global " |
760 | "tt entry (%pM): Roaming timeout\n", | |
48100bac | 761 | tt_global_entry->common.addr); |
7683fdc1 AQ |
762 | atomic_dec(&tt_global_entry->orig_node->tt_size); |
763 | hlist_del_rcu(node); | |
764 | tt_global_entry_free_ref(tt_global_entry); | |
cc47f66e | 765 | } |
7683fdc1 | 766 | spin_unlock_bh(list_lock); |
cc47f66e AQ |
767 | } |
768 | ||
cc47f66e AQ |
769 | } |
770 | ||
a73105b8 | 771 | static void tt_global_table_free(struct bat_priv *bat_priv) |
c6c8fea2 | 772 | { |
7683fdc1 AQ |
773 | struct hashtable_t *hash; |
774 | spinlock_t *list_lock; /* protects write access to the hash lists */ | |
48100bac | 775 | struct tt_common_entry *tt_common_entry; |
7683fdc1 AQ |
776 | struct tt_global_entry *tt_global_entry; |
777 | struct hlist_node *node, *node_tmp; | |
778 | struct hlist_head *head; | |
c90681b8 | 779 | uint32_t i; |
7683fdc1 | 780 | |
2dafb49d | 781 | if (!bat_priv->tt_global_hash) |
c6c8fea2 SE |
782 | return; |
783 | ||
7683fdc1 AQ |
784 | hash = bat_priv->tt_global_hash; |
785 | ||
786 | for (i = 0; i < hash->size; i++) { | |
787 | head = &hash->table[i]; | |
788 | list_lock = &hash->list_locks[i]; | |
789 | ||
790 | spin_lock_bh(list_lock); | |
48100bac | 791 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
7683fdc1 AQ |
792 | head, hash_entry) { |
793 | hlist_del_rcu(node); | |
48100bac AQ |
794 | tt_global_entry = container_of(tt_common_entry, |
795 | struct tt_global_entry, | |
796 | common); | |
7683fdc1 AQ |
797 | tt_global_entry_free_ref(tt_global_entry); |
798 | } | |
799 | spin_unlock_bh(list_lock); | |
800 | } | |
801 | ||
802 | hash_destroy(hash); | |
803 | ||
2dafb49d | 804 | bat_priv->tt_global_hash = NULL; |
c6c8fea2 SE |
805 | } |
806 | ||
59b699cd AQ |
807 | static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry, |
808 | struct tt_global_entry *tt_global_entry) | |
809 | { | |
810 | bool ret = false; | |
811 | ||
48100bac AQ |
812 | if (tt_local_entry->common.flags & TT_CLIENT_WIFI && |
813 | tt_global_entry->common.flags & TT_CLIENT_WIFI) | |
59b699cd AQ |
814 | ret = true; |
815 | ||
816 | return ret; | |
817 | } | |
818 | ||
747e4221 | 819 | struct orig_node *transtable_search(struct bat_priv *bat_priv, |
3d393e47 | 820 | const uint8_t *src, const uint8_t *addr) |
c6c8fea2 | 821 | { |
3d393e47 AQ |
822 | struct tt_local_entry *tt_local_entry = NULL; |
823 | struct tt_global_entry *tt_global_entry = NULL; | |
7b36e8ee | 824 | struct orig_node *orig_node = NULL; |
c6c8fea2 | 825 | |
3d393e47 AQ |
826 | if (src && atomic_read(&bat_priv->ap_isolation)) { |
827 | tt_local_entry = tt_local_hash_find(bat_priv, src); | |
828 | if (!tt_local_entry) | |
829 | goto out; | |
830 | } | |
7aadf889 | 831 | |
3d393e47 | 832 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
2dafb49d | 833 | if (!tt_global_entry) |
7b36e8ee | 834 | goto out; |
7aadf889 | 835 | |
3d393e47 AQ |
836 | /* check whether the clients should not communicate due to AP |
837 | * isolation */ | |
838 | if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry)) | |
839 | goto out; | |
840 | ||
2dafb49d | 841 | if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount)) |
3d393e47 | 842 | goto out; |
c6c8fea2 | 843 | |
980d55b2 AQ |
844 | /* A global client marked as PENDING has already moved from that |
845 | * originator */ | |
48100bac | 846 | if (tt_global_entry->common.flags & TT_CLIENT_PENDING) |
3d393e47 | 847 | goto out; |
980d55b2 | 848 | |
2dafb49d | 849 | orig_node = tt_global_entry->orig_node; |
c6c8fea2 | 850 | |
7b36e8ee | 851 | out: |
3d393e47 AQ |
852 | if (tt_global_entry) |
853 | tt_global_entry_free_ref(tt_global_entry); | |
854 | if (tt_local_entry) | |
855 | tt_local_entry_free_ref(tt_local_entry); | |
856 | ||
7b36e8ee | 857 | return orig_node; |
c6c8fea2 | 858 | } |
a73105b8 AQ |
859 | |
860 | /* Calculates the checksum of the local table of a given orig_node */ | |
861 | uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node) | |
862 | { | |
863 | uint16_t total = 0, total_one; | |
864 | struct hashtable_t *hash = bat_priv->tt_global_hash; | |
48100bac | 865 | struct tt_common_entry *tt_common_entry; |
a73105b8 AQ |
866 | struct tt_global_entry *tt_global_entry; |
867 | struct hlist_node *node; | |
868 | struct hlist_head *head; | |
c90681b8 AQ |
869 | uint32_t i; |
870 | int j; | |
a73105b8 AQ |
871 | |
872 | for (i = 0; i < hash->size; i++) { | |
873 | head = &hash->table[i]; | |
874 | ||
875 | rcu_read_lock(); | |
48100bac | 876 | hlist_for_each_entry_rcu(tt_common_entry, node, |
a73105b8 | 877 | head, hash_entry) { |
48100bac AQ |
878 | tt_global_entry = container_of(tt_common_entry, |
879 | struct tt_global_entry, | |
880 | common); | |
a73105b8 AQ |
881 | if (compare_eth(tt_global_entry->orig_node, |
882 | orig_node)) { | |
cc47f66e AQ |
883 | /* Roaming clients are in the global table for |
884 | * consistency only. They don't have to be | |
885 | * taken into account while computing the | |
886 | * global crc */ | |
48100bac | 887 | if (tt_common_entry->flags & TT_CLIENT_ROAM) |
cc47f66e | 888 | continue; |
a73105b8 AQ |
889 | total_one = 0; |
890 | for (j = 0; j < ETH_ALEN; j++) | |
891 | total_one = crc16_byte(total_one, | |
48100bac | 892 | tt_common_entry->addr[j]); |
a73105b8 AQ |
893 | total ^= total_one; |
894 | } | |
895 | } | |
896 | rcu_read_unlock(); | |
897 | } | |
898 | ||
899 | return total; | |
900 | } | |
901 | ||
902 | /* Calculates the checksum of the local table */ | |
903 | uint16_t tt_local_crc(struct bat_priv *bat_priv) | |
904 | { | |
905 | uint16_t total = 0, total_one; | |
906 | struct hashtable_t *hash = bat_priv->tt_local_hash; | |
48100bac | 907 | struct tt_common_entry *tt_common_entry; |
a73105b8 AQ |
908 | struct hlist_node *node; |
909 | struct hlist_head *head; | |
c90681b8 AQ |
910 | uint32_t i; |
911 | int j; | |
a73105b8 AQ |
912 | |
913 | for (i = 0; i < hash->size; i++) { | |
914 | head = &hash->table[i]; | |
915 | ||
916 | rcu_read_lock(); | |
48100bac | 917 | hlist_for_each_entry_rcu(tt_common_entry, node, |
a73105b8 | 918 | head, hash_entry) { |
058d0e26 AQ |
919 | /* not yet committed clients have not to be taken into |
920 | * account while computing the CRC */ | |
48100bac | 921 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
058d0e26 | 922 | continue; |
a73105b8 AQ |
923 | total_one = 0; |
924 | for (j = 0; j < ETH_ALEN; j++) | |
925 | total_one = crc16_byte(total_one, | |
48100bac | 926 | tt_common_entry->addr[j]); |
a73105b8 AQ |
927 | total ^= total_one; |
928 | } | |
a73105b8 AQ |
929 | rcu_read_unlock(); |
930 | } | |
931 | ||
932 | return total; | |
933 | } | |
934 | ||
935 | static void tt_req_list_free(struct bat_priv *bat_priv) | |
936 | { | |
937 | struct tt_req_node *node, *safe; | |
938 | ||
939 | spin_lock_bh(&bat_priv->tt_req_list_lock); | |
940 | ||
941 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { | |
942 | list_del(&node->list); | |
943 | kfree(node); | |
944 | } | |
945 | ||
946 | spin_unlock_bh(&bat_priv->tt_req_list_lock); | |
947 | } | |
948 | ||
949 | void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node, | |
950 | const unsigned char *tt_buff, uint8_t tt_num_changes) | |
951 | { | |
952 | uint16_t tt_buff_len = tt_len(tt_num_changes); | |
953 | ||
954 | /* Replace the old buffer only if I received something in the | |
955 | * last OGM (the OGM could carry no changes) */ | |
956 | spin_lock_bh(&orig_node->tt_buff_lock); | |
957 | if (tt_buff_len > 0) { | |
958 | kfree(orig_node->tt_buff); | |
959 | orig_node->tt_buff_len = 0; | |
960 | orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC); | |
961 | if (orig_node->tt_buff) { | |
962 | memcpy(orig_node->tt_buff, tt_buff, tt_buff_len); | |
963 | orig_node->tt_buff_len = tt_buff_len; | |
964 | } | |
965 | } | |
966 | spin_unlock_bh(&orig_node->tt_buff_lock); | |
967 | } | |
968 | ||
969 | static void tt_req_purge(struct bat_priv *bat_priv) | |
970 | { | |
971 | struct tt_req_node *node, *safe; | |
972 | ||
973 | spin_lock_bh(&bat_priv->tt_req_list_lock); | |
974 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { | |
032b7969 | 975 | if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) { |
a73105b8 AQ |
976 | list_del(&node->list); |
977 | kfree(node); | |
978 | } | |
979 | } | |
980 | spin_unlock_bh(&bat_priv->tt_req_list_lock); | |
981 | } | |
982 | ||
983 | /* returns the pointer to the new tt_req_node struct if no request | |
984 | * has already been issued for this orig_node, NULL otherwise */ | |
985 | static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv, | |
986 | struct orig_node *orig_node) | |
987 | { | |
988 | struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL; | |
989 | ||
990 | spin_lock_bh(&bat_priv->tt_req_list_lock); | |
991 | list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) { | |
992 | if (compare_eth(tt_req_node_tmp, orig_node) && | |
a04ccd59 | 993 | !has_timed_out(tt_req_node_tmp->issued_at, |
032b7969 | 994 | TT_REQUEST_TIMEOUT)) |
a73105b8 AQ |
995 | goto unlock; |
996 | } | |
997 | ||
998 | tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC); | |
999 | if (!tt_req_node) | |
1000 | goto unlock; | |
1001 | ||
1002 | memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN); | |
1003 | tt_req_node->issued_at = jiffies; | |
1004 | ||
1005 | list_add(&tt_req_node->list, &bat_priv->tt_req_list); | |
1006 | unlock: | |
1007 | spin_unlock_bh(&bat_priv->tt_req_list_lock); | |
1008 | return tt_req_node; | |
1009 | } | |
1010 | ||
058d0e26 AQ |
1011 | /* data_ptr is useless here, but has to be kept to respect the prototype */ |
1012 | static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr) | |
1013 | { | |
48100bac | 1014 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
058d0e26 | 1015 | |
48100bac | 1016 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
058d0e26 AQ |
1017 | return 0; |
1018 | return 1; | |
1019 | } | |
1020 | ||
a73105b8 AQ |
1021 | static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr) |
1022 | { | |
48100bac AQ |
1023 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
1024 | const struct tt_global_entry *tt_global_entry; | |
a73105b8 AQ |
1025 | const struct orig_node *orig_node = data_ptr; |
1026 | ||
48100bac | 1027 | if (tt_common_entry->flags & TT_CLIENT_ROAM) |
cc47f66e AQ |
1028 | return 0; |
1029 | ||
48100bac AQ |
1030 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, |
1031 | common); | |
1032 | ||
a73105b8 AQ |
1033 | return (tt_global_entry->orig_node == orig_node); |
1034 | } | |
1035 | ||
1036 | static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, | |
1037 | struct hashtable_t *hash, | |
1038 | struct hard_iface *primary_if, | |
1039 | int (*valid_cb)(const void *, | |
1040 | const void *), | |
1041 | void *cb_data) | |
1042 | { | |
48100bac | 1043 | struct tt_common_entry *tt_common_entry; |
a73105b8 AQ |
1044 | struct tt_query_packet *tt_response; |
1045 | struct tt_change *tt_change; | |
1046 | struct hlist_node *node; | |
1047 | struct hlist_head *head; | |
1048 | struct sk_buff *skb = NULL; | |
1049 | uint16_t tt_tot, tt_count; | |
1050 | ssize_t tt_query_size = sizeof(struct tt_query_packet); | |
c90681b8 | 1051 | uint32_t i; |
a73105b8 AQ |
1052 | |
1053 | if (tt_query_size + tt_len > primary_if->soft_iface->mtu) { | |
1054 | tt_len = primary_if->soft_iface->mtu - tt_query_size; | |
1055 | tt_len -= tt_len % sizeof(struct tt_change); | |
1056 | } | |
1057 | tt_tot = tt_len / sizeof(struct tt_change); | |
1058 | ||
1059 | skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN); | |
1060 | if (!skb) | |
1061 | goto out; | |
1062 | ||
1063 | skb_reserve(skb, ETH_HLEN); | |
1064 | tt_response = (struct tt_query_packet *)skb_put(skb, | |
1065 | tt_query_size + tt_len); | |
1066 | tt_response->ttvn = ttvn; | |
a73105b8 AQ |
1067 | |
1068 | tt_change = (struct tt_change *)(skb->data + tt_query_size); | |
1069 | tt_count = 0; | |
1070 | ||
1071 | rcu_read_lock(); | |
1072 | for (i = 0; i < hash->size; i++) { | |
1073 | head = &hash->table[i]; | |
1074 | ||
48100bac | 1075 | hlist_for_each_entry_rcu(tt_common_entry, node, |
a73105b8 AQ |
1076 | head, hash_entry) { |
1077 | if (tt_count == tt_tot) | |
1078 | break; | |
1079 | ||
48100bac | 1080 | if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) |
a73105b8 AQ |
1081 | continue; |
1082 | ||
48100bac AQ |
1083 | memcpy(tt_change->addr, tt_common_entry->addr, |
1084 | ETH_ALEN); | |
a73105b8 AQ |
1085 | tt_change->flags = NO_FLAGS; |
1086 | ||
1087 | tt_count++; | |
1088 | tt_change++; | |
1089 | } | |
1090 | } | |
1091 | rcu_read_unlock(); | |
1092 | ||
9d852393 AQ |
1093 | /* store in the message the number of entries we have successfully |
1094 | * copied */ | |
1095 | tt_response->tt_data = htons(tt_count); | |
1096 | ||
a73105b8 AQ |
1097 | out: |
1098 | return skb; | |
1099 | } | |
1100 | ||
a943cac1 ML |
1101 | static int send_tt_request(struct bat_priv *bat_priv, |
1102 | struct orig_node *dst_orig_node, | |
1103 | uint8_t ttvn, uint16_t tt_crc, bool full_table) | |
a73105b8 AQ |
1104 | { |
1105 | struct sk_buff *skb = NULL; | |
1106 | struct tt_query_packet *tt_request; | |
1107 | struct neigh_node *neigh_node = NULL; | |
1108 | struct hard_iface *primary_if; | |
1109 | struct tt_req_node *tt_req_node = NULL; | |
1110 | int ret = 1; | |
1111 | ||
1112 | primary_if = primary_if_get_selected(bat_priv); | |
1113 | if (!primary_if) | |
1114 | goto out; | |
1115 | ||
1116 | /* The new tt_req will be issued only if I'm not waiting for a | |
1117 | * reply from the same orig_node yet */ | |
1118 | tt_req_node = new_tt_req_node(bat_priv, dst_orig_node); | |
1119 | if (!tt_req_node) | |
1120 | goto out; | |
1121 | ||
1122 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN); | |
1123 | if (!skb) | |
1124 | goto out; | |
1125 | ||
1126 | skb_reserve(skb, ETH_HLEN); | |
1127 | ||
1128 | tt_request = (struct tt_query_packet *)skb_put(skb, | |
1129 | sizeof(struct tt_query_packet)); | |
1130 | ||
76543d14 SE |
1131 | tt_request->header.packet_type = BAT_TT_QUERY; |
1132 | tt_request->header.version = COMPAT_VERSION; | |
a73105b8 AQ |
1133 | memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
1134 | memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN); | |
76543d14 | 1135 | tt_request->header.ttl = TTL; |
a73105b8 AQ |
1136 | tt_request->ttvn = ttvn; |
1137 | tt_request->tt_data = tt_crc; | |
1138 | tt_request->flags = TT_REQUEST; | |
1139 | ||
1140 | if (full_table) | |
1141 | tt_request->flags |= TT_FULL_TABLE; | |
1142 | ||
1143 | neigh_node = orig_node_get_router(dst_orig_node); | |
1144 | if (!neigh_node) | |
1145 | goto out; | |
1146 | ||
1147 | bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM " | |
1148 | "[%c]\n", dst_orig_node->orig, neigh_node->addr, | |
1149 | (full_table ? 'F' : '.')); | |
1150 | ||
1151 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); | |
1152 | ret = 0; | |
1153 | ||
1154 | out: | |
1155 | if (neigh_node) | |
1156 | neigh_node_free_ref(neigh_node); | |
1157 | if (primary_if) | |
1158 | hardif_free_ref(primary_if); | |
1159 | if (ret) | |
1160 | kfree_skb(skb); | |
1161 | if (ret && tt_req_node) { | |
1162 | spin_lock_bh(&bat_priv->tt_req_list_lock); | |
1163 | list_del(&tt_req_node->list); | |
1164 | spin_unlock_bh(&bat_priv->tt_req_list_lock); | |
1165 | kfree(tt_req_node); | |
1166 | } | |
1167 | return ret; | |
1168 | } | |
1169 | ||
1170 | static bool send_other_tt_response(struct bat_priv *bat_priv, | |
1171 | struct tt_query_packet *tt_request) | |
1172 | { | |
1173 | struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL; | |
1174 | struct neigh_node *neigh_node = NULL; | |
1175 | struct hard_iface *primary_if = NULL; | |
1176 | uint8_t orig_ttvn, req_ttvn, ttvn; | |
1177 | int ret = false; | |
1178 | unsigned char *tt_buff; | |
1179 | bool full_table; | |
1180 | uint16_t tt_len, tt_tot; | |
1181 | struct sk_buff *skb = NULL; | |
1182 | struct tt_query_packet *tt_response; | |
1183 | ||
1184 | bat_dbg(DBG_TT, bat_priv, | |
1185 | "Received TT_REQUEST from %pM for " | |
1186 | "ttvn: %u (%pM) [%c]\n", tt_request->src, | |
1187 | tt_request->ttvn, tt_request->dst, | |
1188 | (tt_request->flags & TT_FULL_TABLE ? 'F' : '.')); | |
1189 | ||
1190 | /* Let's get the orig node of the REAL destination */ | |
eb7e2a1e | 1191 | req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst); |
a73105b8 AQ |
1192 | if (!req_dst_orig_node) |
1193 | goto out; | |
1194 | ||
eb7e2a1e | 1195 | res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src); |
a73105b8 AQ |
1196 | if (!res_dst_orig_node) |
1197 | goto out; | |
1198 | ||
1199 | neigh_node = orig_node_get_router(res_dst_orig_node); | |
1200 | if (!neigh_node) | |
1201 | goto out; | |
1202 | ||
1203 | primary_if = primary_if_get_selected(bat_priv); | |
1204 | if (!primary_if) | |
1205 | goto out; | |
1206 | ||
1207 | orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); | |
1208 | req_ttvn = tt_request->ttvn; | |
1209 | ||
015758d0 | 1210 | /* I don't have the requested data */ |
a73105b8 AQ |
1211 | if (orig_ttvn != req_ttvn || |
1212 | tt_request->tt_data != req_dst_orig_node->tt_crc) | |
1213 | goto out; | |
1214 | ||
015758d0 | 1215 | /* If the full table has been explicitly requested */ |
a73105b8 AQ |
1216 | if (tt_request->flags & TT_FULL_TABLE || |
1217 | !req_dst_orig_node->tt_buff) | |
1218 | full_table = true; | |
1219 | else | |
1220 | full_table = false; | |
1221 | ||
1222 | /* In this version, fragmentation is not implemented, then | |
1223 | * I'll send only one packet with as much TT entries as I can */ | |
1224 | if (!full_table) { | |
1225 | spin_lock_bh(&req_dst_orig_node->tt_buff_lock); | |
1226 | tt_len = req_dst_orig_node->tt_buff_len; | |
1227 | tt_tot = tt_len / sizeof(struct tt_change); | |
1228 | ||
1229 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + | |
1230 | tt_len + ETH_HLEN); | |
1231 | if (!skb) | |
1232 | goto unlock; | |
1233 | ||
1234 | skb_reserve(skb, ETH_HLEN); | |
1235 | tt_response = (struct tt_query_packet *)skb_put(skb, | |
1236 | sizeof(struct tt_query_packet) + tt_len); | |
1237 | tt_response->ttvn = req_ttvn; | |
1238 | tt_response->tt_data = htons(tt_tot); | |
1239 | ||
1240 | tt_buff = skb->data + sizeof(struct tt_query_packet); | |
1241 | /* Copy the last orig_node's OGM buffer */ | |
1242 | memcpy(tt_buff, req_dst_orig_node->tt_buff, | |
1243 | req_dst_orig_node->tt_buff_len); | |
1244 | ||
1245 | spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); | |
1246 | } else { | |
1247 | tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) * | |
1248 | sizeof(struct tt_change); | |
1249 | ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); | |
1250 | ||
1251 | skb = tt_response_fill_table(tt_len, ttvn, | |
1252 | bat_priv->tt_global_hash, | |
1253 | primary_if, tt_global_valid_entry, | |
1254 | req_dst_orig_node); | |
1255 | if (!skb) | |
1256 | goto out; | |
1257 | ||
1258 | tt_response = (struct tt_query_packet *)skb->data; | |
1259 | } | |
1260 | ||
76543d14 SE |
1261 | tt_response->header.packet_type = BAT_TT_QUERY; |
1262 | tt_response->header.version = COMPAT_VERSION; | |
1263 | tt_response->header.ttl = TTL; | |
a73105b8 AQ |
1264 | memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN); |
1265 | memcpy(tt_response->dst, tt_request->src, ETH_ALEN); | |
1266 | tt_response->flags = TT_RESPONSE; | |
1267 | ||
1268 | if (full_table) | |
1269 | tt_response->flags |= TT_FULL_TABLE; | |
1270 | ||
1271 | bat_dbg(DBG_TT, bat_priv, | |
1272 | "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n", | |
1273 | res_dst_orig_node->orig, neigh_node->addr, | |
1274 | req_dst_orig_node->orig, req_ttvn); | |
1275 | ||
1276 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); | |
1277 | ret = true; | |
1278 | goto out; | |
1279 | ||
1280 | unlock: | |
1281 | spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); | |
1282 | ||
1283 | out: | |
1284 | if (res_dst_orig_node) | |
1285 | orig_node_free_ref(res_dst_orig_node); | |
1286 | if (req_dst_orig_node) | |
1287 | orig_node_free_ref(req_dst_orig_node); | |
1288 | if (neigh_node) | |
1289 | neigh_node_free_ref(neigh_node); | |
1290 | if (primary_if) | |
1291 | hardif_free_ref(primary_if); | |
1292 | if (!ret) | |
1293 | kfree_skb(skb); | |
1294 | return ret; | |
1295 | ||
1296 | } | |
1297 | static bool send_my_tt_response(struct bat_priv *bat_priv, | |
1298 | struct tt_query_packet *tt_request) | |
1299 | { | |
1300 | struct orig_node *orig_node = NULL; | |
1301 | struct neigh_node *neigh_node = NULL; | |
1302 | struct hard_iface *primary_if = NULL; | |
1303 | uint8_t my_ttvn, req_ttvn, ttvn; | |
1304 | int ret = false; | |
1305 | unsigned char *tt_buff; | |
1306 | bool full_table; | |
1307 | uint16_t tt_len, tt_tot; | |
1308 | struct sk_buff *skb = NULL; | |
1309 | struct tt_query_packet *tt_response; | |
1310 | ||
1311 | bat_dbg(DBG_TT, bat_priv, | |
1312 | "Received TT_REQUEST from %pM for " | |
1313 | "ttvn: %u (me) [%c]\n", tt_request->src, | |
1314 | tt_request->ttvn, | |
1315 | (tt_request->flags & TT_FULL_TABLE ? 'F' : '.')); | |
1316 | ||
1317 | ||
1318 | my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); | |
1319 | req_ttvn = tt_request->ttvn; | |
1320 | ||
eb7e2a1e | 1321 | orig_node = orig_hash_find(bat_priv, tt_request->src); |
a73105b8 AQ |
1322 | if (!orig_node) |
1323 | goto out; | |
1324 | ||
1325 | neigh_node = orig_node_get_router(orig_node); | |
1326 | if (!neigh_node) | |
1327 | goto out; | |
1328 | ||
1329 | primary_if = primary_if_get_selected(bat_priv); | |
1330 | if (!primary_if) | |
1331 | goto out; | |
1332 | ||
1333 | /* If the full table has been explicitly requested or the gap | |
1334 | * is too big send the whole local translation table */ | |
1335 | if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn || | |
1336 | !bat_priv->tt_buff) | |
1337 | full_table = true; | |
1338 | else | |
1339 | full_table = false; | |
1340 | ||
1341 | /* In this version, fragmentation is not implemented, then | |
1342 | * I'll send only one packet with as much TT entries as I can */ | |
1343 | if (!full_table) { | |
1344 | spin_lock_bh(&bat_priv->tt_buff_lock); | |
1345 | tt_len = bat_priv->tt_buff_len; | |
1346 | tt_tot = tt_len / sizeof(struct tt_change); | |
1347 | ||
1348 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + | |
1349 | tt_len + ETH_HLEN); | |
1350 | if (!skb) | |
1351 | goto unlock; | |
1352 | ||
1353 | skb_reserve(skb, ETH_HLEN); | |
1354 | tt_response = (struct tt_query_packet *)skb_put(skb, | |
1355 | sizeof(struct tt_query_packet) + tt_len); | |
1356 | tt_response->ttvn = req_ttvn; | |
1357 | tt_response->tt_data = htons(tt_tot); | |
1358 | ||
1359 | tt_buff = skb->data + sizeof(struct tt_query_packet); | |
1360 | memcpy(tt_buff, bat_priv->tt_buff, | |
1361 | bat_priv->tt_buff_len); | |
1362 | spin_unlock_bh(&bat_priv->tt_buff_lock); | |
1363 | } else { | |
1364 | tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) * | |
1365 | sizeof(struct tt_change); | |
1366 | ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); | |
1367 | ||
1368 | skb = tt_response_fill_table(tt_len, ttvn, | |
1369 | bat_priv->tt_local_hash, | |
058d0e26 AQ |
1370 | primary_if, tt_local_valid_entry, |
1371 | NULL); | |
a73105b8 AQ |
1372 | if (!skb) |
1373 | goto out; | |
1374 | ||
1375 | tt_response = (struct tt_query_packet *)skb->data; | |
1376 | } | |
1377 | ||
76543d14 SE |
1378 | tt_response->header.packet_type = BAT_TT_QUERY; |
1379 | tt_response->header.version = COMPAT_VERSION; | |
1380 | tt_response->header.ttl = TTL; | |
a73105b8 AQ |
1381 | memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
1382 | memcpy(tt_response->dst, tt_request->src, ETH_ALEN); | |
1383 | tt_response->flags = TT_RESPONSE; | |
1384 | ||
1385 | if (full_table) | |
1386 | tt_response->flags |= TT_FULL_TABLE; | |
1387 | ||
1388 | bat_dbg(DBG_TT, bat_priv, | |
1389 | "Sending TT_RESPONSE to %pM via %pM [%c]\n", | |
1390 | orig_node->orig, neigh_node->addr, | |
1391 | (tt_response->flags & TT_FULL_TABLE ? 'F' : '.')); | |
1392 | ||
1393 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); | |
1394 | ret = true; | |
1395 | goto out; | |
1396 | ||
1397 | unlock: | |
1398 | spin_unlock_bh(&bat_priv->tt_buff_lock); | |
1399 | out: | |
1400 | if (orig_node) | |
1401 | orig_node_free_ref(orig_node); | |
1402 | if (neigh_node) | |
1403 | neigh_node_free_ref(neigh_node); | |
1404 | if (primary_if) | |
1405 | hardif_free_ref(primary_if); | |
1406 | if (!ret) | |
1407 | kfree_skb(skb); | |
1408 | /* This packet was for me, so it doesn't need to be re-routed */ | |
1409 | return true; | |
1410 | } | |
1411 | ||
1412 | bool send_tt_response(struct bat_priv *bat_priv, | |
1413 | struct tt_query_packet *tt_request) | |
1414 | { | |
1415 | if (is_my_mac(tt_request->dst)) | |
1416 | return send_my_tt_response(bat_priv, tt_request); | |
1417 | else | |
1418 | return send_other_tt_response(bat_priv, tt_request); | |
1419 | } | |
1420 | ||
1421 | static void _tt_update_changes(struct bat_priv *bat_priv, | |
1422 | struct orig_node *orig_node, | |
1423 | struct tt_change *tt_change, | |
1424 | uint16_t tt_num_changes, uint8_t ttvn) | |
1425 | { | |
1426 | int i; | |
1427 | ||
1428 | for (i = 0; i < tt_num_changes; i++) { | |
5fbc1598 | 1429 | if ((tt_change + i)->flags & TT_CLIENT_DEL) |
a73105b8 AQ |
1430 | tt_global_del(bat_priv, orig_node, |
1431 | (tt_change + i)->addr, | |
cc47f66e AQ |
1432 | "tt removed by changes", |
1433 | (tt_change + i)->flags & TT_CLIENT_ROAM); | |
a73105b8 AQ |
1434 | else |
1435 | if (!tt_global_add(bat_priv, orig_node, | |
bc279080 AQ |
1436 | (tt_change + i)->addr, ttvn, false, |
1437 | (tt_change + i)->flags & | |
1438 | TT_CLIENT_WIFI)) | |
a73105b8 AQ |
1439 | /* In case of problem while storing a |
1440 | * global_entry, we stop the updating | |
1441 | * procedure without committing the | |
1442 | * ttvn change. This will avoid to send | |
1443 | * corrupted data on tt_request | |
1444 | */ | |
1445 | return; | |
1446 | } | |
17071578 | 1447 | orig_node->tt_initialised = true; |
a73105b8 AQ |
1448 | } |
1449 | ||
1450 | static void tt_fill_gtable(struct bat_priv *bat_priv, | |
1451 | struct tt_query_packet *tt_response) | |
1452 | { | |
1453 | struct orig_node *orig_node = NULL; | |
1454 | ||
1455 | orig_node = orig_hash_find(bat_priv, tt_response->src); | |
1456 | if (!orig_node) | |
1457 | goto out; | |
1458 | ||
1459 | /* Purge the old table first.. */ | |
1460 | tt_global_del_orig(bat_priv, orig_node, "Received full table"); | |
1461 | ||
1462 | _tt_update_changes(bat_priv, orig_node, | |
1463 | (struct tt_change *)(tt_response + 1), | |
1464 | tt_response->tt_data, tt_response->ttvn); | |
1465 | ||
1466 | spin_lock_bh(&orig_node->tt_buff_lock); | |
1467 | kfree(orig_node->tt_buff); | |
1468 | orig_node->tt_buff_len = 0; | |
1469 | orig_node->tt_buff = NULL; | |
1470 | spin_unlock_bh(&orig_node->tt_buff_lock); | |
1471 | ||
1472 | atomic_set(&orig_node->last_ttvn, tt_response->ttvn); | |
1473 | ||
1474 | out: | |
1475 | if (orig_node) | |
1476 | orig_node_free_ref(orig_node); | |
1477 | } | |
1478 | ||
a943cac1 ML |
1479 | static void tt_update_changes(struct bat_priv *bat_priv, |
1480 | struct orig_node *orig_node, | |
1481 | uint16_t tt_num_changes, uint8_t ttvn, | |
1482 | struct tt_change *tt_change) | |
a73105b8 AQ |
1483 | { |
1484 | _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes, | |
1485 | ttvn); | |
1486 | ||
1487 | tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change, | |
1488 | tt_num_changes); | |
1489 | atomic_set(&orig_node->last_ttvn, ttvn); | |
1490 | } | |
1491 | ||
1492 | bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr) | |
1493 | { | |
7683fdc1 AQ |
1494 | struct tt_local_entry *tt_local_entry = NULL; |
1495 | bool ret = false; | |
a73105b8 | 1496 | |
a73105b8 | 1497 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
7683fdc1 AQ |
1498 | if (!tt_local_entry) |
1499 | goto out; | |
058d0e26 AQ |
1500 | /* Check if the client has been logically deleted (but is kept for |
1501 | * consistency purpose) */ | |
48100bac | 1502 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
058d0e26 | 1503 | goto out; |
7683fdc1 AQ |
1504 | ret = true; |
1505 | out: | |
a73105b8 | 1506 | if (tt_local_entry) |
7683fdc1 AQ |
1507 | tt_local_entry_free_ref(tt_local_entry); |
1508 | return ret; | |
a73105b8 AQ |
1509 | } |
1510 | ||
1511 | void handle_tt_response(struct bat_priv *bat_priv, | |
1512 | struct tt_query_packet *tt_response) | |
1513 | { | |
1514 | struct tt_req_node *node, *safe; | |
1515 | struct orig_node *orig_node = NULL; | |
1516 | ||
1517 | bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for " | |
1518 | "ttvn %d t_size: %d [%c]\n", | |
1519 | tt_response->src, tt_response->ttvn, | |
1520 | tt_response->tt_data, | |
1521 | (tt_response->flags & TT_FULL_TABLE ? 'F' : '.')); | |
1522 | ||
1523 | orig_node = orig_hash_find(bat_priv, tt_response->src); | |
1524 | if (!orig_node) | |
1525 | goto out; | |
1526 | ||
1527 | if (tt_response->flags & TT_FULL_TABLE) | |
1528 | tt_fill_gtable(bat_priv, tt_response); | |
1529 | else | |
1530 | tt_update_changes(bat_priv, orig_node, tt_response->tt_data, | |
1531 | tt_response->ttvn, | |
1532 | (struct tt_change *)(tt_response + 1)); | |
1533 | ||
1534 | /* Delete the tt_req_node from pending tt_requests list */ | |
1535 | spin_lock_bh(&bat_priv->tt_req_list_lock); | |
1536 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { | |
1537 | if (!compare_eth(node->addr, tt_response->src)) | |
1538 | continue; | |
1539 | list_del(&node->list); | |
1540 | kfree(node); | |
1541 | } | |
1542 | spin_unlock_bh(&bat_priv->tt_req_list_lock); | |
1543 | ||
1544 | /* Recalculate the CRC for this orig_node and store it */ | |
a73105b8 | 1545 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); |
cc47f66e AQ |
1546 | /* Roaming phase is over: tables are in sync again. I can |
1547 | * unset the flag */ | |
1548 | orig_node->tt_poss_change = false; | |
a73105b8 AQ |
1549 | out: |
1550 | if (orig_node) | |
1551 | orig_node_free_ref(orig_node); | |
1552 | } | |
1553 | ||
1554 | int tt_init(struct bat_priv *bat_priv) | |
1555 | { | |
1556 | if (!tt_local_init(bat_priv)) | |
1557 | return 0; | |
1558 | ||
1559 | if (!tt_global_init(bat_priv)) | |
1560 | return 0; | |
1561 | ||
1562 | tt_start_timer(bat_priv); | |
1563 | ||
1564 | return 1; | |
1565 | } | |
1566 | ||
cc47f66e | 1567 | static void tt_roam_list_free(struct bat_priv *bat_priv) |
a73105b8 | 1568 | { |
cc47f66e | 1569 | struct tt_roam_node *node, *safe; |
a73105b8 | 1570 | |
cc47f66e | 1571 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
a73105b8 | 1572 | |
cc47f66e AQ |
1573 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { |
1574 | list_del(&node->list); | |
1575 | kfree(node); | |
1576 | } | |
1577 | ||
1578 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); | |
1579 | } | |
1580 | ||
1581 | static void tt_roam_purge(struct bat_priv *bat_priv) | |
1582 | { | |
1583 | struct tt_roam_node *node, *safe; | |
1584 | ||
1585 | spin_lock_bh(&bat_priv->tt_roam_list_lock); | |
1586 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { | |
032b7969 | 1587 | if (!has_timed_out(node->first_time, ROAMING_MAX_TIME)) |
cc47f66e AQ |
1588 | continue; |
1589 | ||
1590 | list_del(&node->list); | |
1591 | kfree(node); | |
1592 | } | |
1593 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); | |
1594 | } | |
1595 | ||
1596 | /* This function checks whether the client already reached the | |
1597 | * maximum number of possible roaming phases. In this case the ROAMING_ADV | |
1598 | * will not be sent. | |
1599 | * | |
1600 | * returns true if the ROAMING_ADV can be sent, false otherwise */ | |
1601 | static bool tt_check_roam_count(struct bat_priv *bat_priv, | |
1602 | uint8_t *client) | |
1603 | { | |
1604 | struct tt_roam_node *tt_roam_node; | |
1605 | bool ret = false; | |
1606 | ||
1607 | spin_lock_bh(&bat_priv->tt_roam_list_lock); | |
1608 | /* The new tt_req will be issued only if I'm not waiting for a | |
1609 | * reply from the same orig_node yet */ | |
1610 | list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) { | |
1611 | if (!compare_eth(tt_roam_node->addr, client)) | |
1612 | continue; | |
1613 | ||
032b7969 | 1614 | if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME)) |
cc47f66e AQ |
1615 | continue; |
1616 | ||
1617 | if (!atomic_dec_not_zero(&tt_roam_node->counter)) | |
1618 | /* Sorry, you roamed too many times! */ | |
1619 | goto unlock; | |
1620 | ret = true; | |
1621 | break; | |
1622 | } | |
1623 | ||
1624 | if (!ret) { | |
1625 | tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC); | |
1626 | if (!tt_roam_node) | |
1627 | goto unlock; | |
1628 | ||
1629 | tt_roam_node->first_time = jiffies; | |
1630 | atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1); | |
1631 | memcpy(tt_roam_node->addr, client, ETH_ALEN); | |
1632 | ||
1633 | list_add(&tt_roam_node->list, &bat_priv->tt_roam_list); | |
1634 | ret = true; | |
1635 | } | |
1636 | ||
1637 | unlock: | |
1638 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); | |
1639 | return ret; | |
1640 | } | |
1641 | ||
1642 | void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client, | |
1643 | struct orig_node *orig_node) | |
1644 | { | |
1645 | struct neigh_node *neigh_node = NULL; | |
1646 | struct sk_buff *skb = NULL; | |
1647 | struct roam_adv_packet *roam_adv_packet; | |
1648 | int ret = 1; | |
1649 | struct hard_iface *primary_if; | |
1650 | ||
1651 | /* before going on we have to check whether the client has | |
1652 | * already roamed to us too many times */ | |
1653 | if (!tt_check_roam_count(bat_priv, client)) | |
1654 | goto out; | |
1655 | ||
1656 | skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN); | |
1657 | if (!skb) | |
1658 | goto out; | |
1659 | ||
1660 | skb_reserve(skb, ETH_HLEN); | |
1661 | ||
1662 | roam_adv_packet = (struct roam_adv_packet *)skb_put(skb, | |
1663 | sizeof(struct roam_adv_packet)); | |
1664 | ||
76543d14 SE |
1665 | roam_adv_packet->header.packet_type = BAT_ROAM_ADV; |
1666 | roam_adv_packet->header.version = COMPAT_VERSION; | |
1667 | roam_adv_packet->header.ttl = TTL; | |
cc47f66e AQ |
1668 | primary_if = primary_if_get_selected(bat_priv); |
1669 | if (!primary_if) | |
1670 | goto out; | |
1671 | memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN); | |
1672 | hardif_free_ref(primary_if); | |
1673 | memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN); | |
1674 | memcpy(roam_adv_packet->client, client, ETH_ALEN); | |
1675 | ||
1676 | neigh_node = orig_node_get_router(orig_node); | |
1677 | if (!neigh_node) | |
1678 | goto out; | |
1679 | ||
1680 | bat_dbg(DBG_TT, bat_priv, | |
1681 | "Sending ROAMING_ADV to %pM (client %pM) via %pM\n", | |
1682 | orig_node->orig, client, neigh_node->addr); | |
1683 | ||
1684 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); | |
1685 | ret = 0; | |
1686 | ||
1687 | out: | |
1688 | if (neigh_node) | |
1689 | neigh_node_free_ref(neigh_node); | |
1690 | if (ret) | |
1691 | kfree_skb(skb); | |
1692 | return; | |
a73105b8 AQ |
1693 | } |
1694 | ||
1695 | static void tt_purge(struct work_struct *work) | |
1696 | { | |
1697 | struct delayed_work *delayed_work = | |
1698 | container_of(work, struct delayed_work, work); | |
1699 | struct bat_priv *bat_priv = | |
1700 | container_of(delayed_work, struct bat_priv, tt_work); | |
1701 | ||
1702 | tt_local_purge(bat_priv); | |
cc47f66e | 1703 | tt_global_roam_purge(bat_priv); |
a73105b8 | 1704 | tt_req_purge(bat_priv); |
cc47f66e | 1705 | tt_roam_purge(bat_priv); |
a73105b8 AQ |
1706 | |
1707 | tt_start_timer(bat_priv); | |
1708 | } | |
cc47f66e AQ |
1709 | |
1710 | void tt_free(struct bat_priv *bat_priv) | |
1711 | { | |
1712 | cancel_delayed_work_sync(&bat_priv->tt_work); | |
1713 | ||
1714 | tt_local_table_free(bat_priv); | |
1715 | tt_global_table_free(bat_priv); | |
1716 | tt_req_list_free(bat_priv); | |
1717 | tt_changes_list_free(bat_priv); | |
1718 | tt_roam_list_free(bat_priv); | |
1719 | ||
1720 | kfree(bat_priv->tt_buff); | |
1721 | } | |
058d0e26 | 1722 | |
697f2531 AQ |
1723 | /* This function will enable or disable the specified flags for all the entries |
1724 | * in the given hash table and returns the number of modified entries */ | |
1725 | static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags, | |
1726 | bool enable) | |
058d0e26 | 1727 | { |
c90681b8 | 1728 | uint32_t i; |
697f2531 | 1729 | uint16_t changed_num = 0; |
058d0e26 AQ |
1730 | struct hlist_head *head; |
1731 | struct hlist_node *node; | |
48100bac | 1732 | struct tt_common_entry *tt_common_entry; |
058d0e26 AQ |
1733 | |
1734 | if (!hash) | |
697f2531 | 1735 | goto out; |
058d0e26 AQ |
1736 | |
1737 | for (i = 0; i < hash->size; i++) { | |
1738 | head = &hash->table[i]; | |
1739 | ||
1740 | rcu_read_lock(); | |
48100bac | 1741 | hlist_for_each_entry_rcu(tt_common_entry, node, |
058d0e26 | 1742 | head, hash_entry) { |
697f2531 AQ |
1743 | if (enable) { |
1744 | if ((tt_common_entry->flags & flags) == flags) | |
1745 | continue; | |
1746 | tt_common_entry->flags |= flags; | |
1747 | } else { | |
1748 | if (!(tt_common_entry->flags & flags)) | |
1749 | continue; | |
1750 | tt_common_entry->flags &= ~flags; | |
1751 | } | |
1752 | changed_num++; | |
058d0e26 AQ |
1753 | } |
1754 | rcu_read_unlock(); | |
1755 | } | |
697f2531 AQ |
1756 | out: |
1757 | return changed_num; | |
058d0e26 AQ |
1758 | } |
1759 | ||
1760 | /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */ | |
1761 | static void tt_local_purge_pending_clients(struct bat_priv *bat_priv) | |
1762 | { | |
1763 | struct hashtable_t *hash = bat_priv->tt_local_hash; | |
48100bac | 1764 | struct tt_common_entry *tt_common_entry; |
058d0e26 AQ |
1765 | struct tt_local_entry *tt_local_entry; |
1766 | struct hlist_node *node, *node_tmp; | |
1767 | struct hlist_head *head; | |
1768 | spinlock_t *list_lock; /* protects write access to the hash lists */ | |
c90681b8 | 1769 | uint32_t i; |
058d0e26 AQ |
1770 | |
1771 | if (!hash) | |
1772 | return; | |
1773 | ||
1774 | for (i = 0; i < hash->size; i++) { | |
1775 | head = &hash->table[i]; | |
1776 | list_lock = &hash->list_locks[i]; | |
1777 | ||
1778 | spin_lock_bh(list_lock); | |
48100bac | 1779 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
058d0e26 | 1780 | head, hash_entry) { |
48100bac | 1781 | if (!(tt_common_entry->flags & TT_CLIENT_PENDING)) |
058d0e26 AQ |
1782 | continue; |
1783 | ||
1784 | bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry " | |
48100bac | 1785 | "(%pM): pending\n", tt_common_entry->addr); |
058d0e26 AQ |
1786 | |
1787 | atomic_dec(&bat_priv->num_local_tt); | |
1788 | hlist_del_rcu(node); | |
48100bac AQ |
1789 | tt_local_entry = container_of(tt_common_entry, |
1790 | struct tt_local_entry, | |
1791 | common); | |
058d0e26 AQ |
1792 | tt_local_entry_free_ref(tt_local_entry); |
1793 | } | |
1794 | spin_unlock_bh(list_lock); | |
1795 | } | |
1796 | ||
1797 | } | |
1798 | ||
1799 | void tt_commit_changes(struct bat_priv *bat_priv) | |
1800 | { | |
697f2531 AQ |
1801 | uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash, |
1802 | TT_CLIENT_NEW, false); | |
1803 | /* all the reset entries have now to be effectively counted as local | |
1804 | * entries */ | |
1805 | atomic_add(changed_num, &bat_priv->num_local_tt); | |
058d0e26 AQ |
1806 | tt_local_purge_pending_clients(bat_priv); |
1807 | ||
1808 | /* Increment the TTVN only once per OGM interval */ | |
1809 | atomic_inc(&bat_priv->ttvn); | |
1810 | bat_priv->tt_poss_change = false; | |
1811 | } | |
59b699cd AQ |
1812 | |
1813 | bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst) | |
1814 | { | |
1815 | struct tt_local_entry *tt_local_entry = NULL; | |
1816 | struct tt_global_entry *tt_global_entry = NULL; | |
1817 | bool ret = true; | |
1818 | ||
1819 | if (!atomic_read(&bat_priv->ap_isolation)) | |
1820 | return false; | |
1821 | ||
1822 | tt_local_entry = tt_local_hash_find(bat_priv, dst); | |
1823 | if (!tt_local_entry) | |
1824 | goto out; | |
1825 | ||
1826 | tt_global_entry = tt_global_hash_find(bat_priv, src); | |
1827 | if (!tt_global_entry) | |
1828 | goto out; | |
1829 | ||
1830 | if (_is_ap_isolated(tt_local_entry, tt_global_entry)) | |
1831 | goto out; | |
1832 | ||
1833 | ret = false; | |
1834 | ||
1835 | out: | |
1836 | if (tt_global_entry) | |
1837 | tt_global_entry_free_ref(tt_global_entry); | |
1838 | if (tt_local_entry) | |
1839 | tt_local_entry_free_ref(tt_local_entry); | |
1840 | return ret; | |
1841 | } | |
a943cac1 ML |
1842 | |
1843 | void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node, | |
1844 | const unsigned char *tt_buff, uint8_t tt_num_changes, | |
1845 | uint8_t ttvn, uint16_t tt_crc) | |
1846 | { | |
1847 | uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); | |
1848 | bool full_table = true; | |
1849 | ||
17071578 AQ |
1850 | /* orig table not initialised AND first diff is in the OGM OR the ttvn |
1851 | * increased by one -> we can apply the attached changes */ | |
1852 | if ((!orig_node->tt_initialised && ttvn == 1) || | |
1853 | ttvn - orig_ttvn == 1) { | |
a943cac1 ML |
1854 | /* the OGM could not contain the changes due to their size or |
1855 | * because they have already been sent TT_OGM_APPEND_MAX times. | |
1856 | * In this case send a tt request */ | |
1857 | if (!tt_num_changes) { | |
1858 | full_table = false; | |
1859 | goto request_table; | |
1860 | } | |
1861 | ||
1862 | tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn, | |
1863 | (struct tt_change *)tt_buff); | |
1864 | ||
1865 | /* Even if we received the precomputed crc with the OGM, we | |
1866 | * prefer to recompute it to spot any possible inconsistency | |
1867 | * in the global table */ | |
1868 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); | |
1869 | ||
1870 | /* The ttvn alone is not enough to guarantee consistency | |
1871 | * because a single value could represent different states | |
1872 | * (due to the wrap around). Thus a node has to check whether | |
1873 | * the resulting table (after applying the changes) is still | |
1874 | * consistent or not. E.g. a node could disconnect while its | |
1875 | * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case | |
1876 | * checking the CRC value is mandatory to detect the | |
1877 | * inconsistency */ | |
1878 | if (orig_node->tt_crc != tt_crc) | |
1879 | goto request_table; | |
1880 | ||
1881 | /* Roaming phase is over: tables are in sync again. I can | |
1882 | * unset the flag */ | |
1883 | orig_node->tt_poss_change = false; | |
1884 | } else { | |
1885 | /* if we missed more than one change or our tables are not | |
1886 | * in sync anymore -> request fresh tt data */ | |
17071578 AQ |
1887 | if (!orig_node->tt_initialised || ttvn != orig_ttvn || |
1888 | orig_node->tt_crc != tt_crc) { | |
a943cac1 ML |
1889 | request_table: |
1890 | bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. " | |
1891 | "Need to retrieve the correct information " | |
1892 | "(ttvn: %u last_ttvn: %u crc: %u last_crc: " | |
1893 | "%u num_changes: %u)\n", orig_node->orig, ttvn, | |
1894 | orig_ttvn, tt_crc, orig_node->tt_crc, | |
1895 | tt_num_changes); | |
1896 | send_tt_request(bat_priv, orig_node, ttvn, tt_crc, | |
1897 | full_table); | |
1898 | return; | |
1899 | } | |
1900 | } | |
1901 | } |