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