Commit | Line | Data |
---|---|---|
0b873931 | 1 | /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
2 | * |
3 | * Marek Lindner, Simon Wunderlich | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | * 02110-1301, USA | |
c6c8fea2 SE |
18 | */ |
19 | ||
c6c8fea2 | 20 | #include "main.h" |
785ea114 | 21 | #include "distributed-arp-table.h" |
c6c8fea2 SE |
22 | #include "originator.h" |
23 | #include "hash.h" | |
24 | #include "translation-table.h" | |
25 | #include "routing.h" | |
26 | #include "gateway_client.h" | |
27 | #include "hard-interface.h" | |
c6c8fea2 | 28 | #include "soft-interface.h" |
23721387 | 29 | #include "bridge_loop_avoidance.h" |
d56b1705 | 30 | #include "network-coding.h" |
610bfc6b | 31 | #include "fragmentation.h" |
c6c8fea2 | 32 | |
dec05074 AQ |
33 | /* hash class keys */ |
34 | static struct lock_class_key batadv_orig_hash_lock_class_key; | |
35 | ||
03fc7f86 | 36 | static void batadv_purge_orig(struct work_struct *work); |
c6c8fea2 | 37 | |
b8e2dd13 | 38 | /* returns 1 if they are the same originator */ |
03fc7f86 | 39 | static int batadv_compare_orig(const struct hlist_node *node, const void *data2) |
b8e2dd13 | 40 | { |
56303d34 SE |
41 | const void *data1 = container_of(node, struct batadv_orig_node, |
42 | hash_entry); | |
b8e2dd13 SE |
43 | |
44 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); | |
45 | } | |
46 | ||
56303d34 | 47 | int batadv_originator_init(struct batadv_priv *bat_priv) |
c6c8fea2 SE |
48 | { |
49 | if (bat_priv->orig_hash) | |
5346c35e | 50 | return 0; |
c6c8fea2 | 51 | |
1a8eaf07 | 52 | bat_priv->orig_hash = batadv_hash_new(1024); |
c6c8fea2 SE |
53 | |
54 | if (!bat_priv->orig_hash) | |
55 | goto err; | |
56 | ||
dec05074 AQ |
57 | batadv_hash_set_lock_class(bat_priv->orig_hash, |
58 | &batadv_orig_hash_lock_class_key); | |
59 | ||
72414442 AQ |
60 | INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); |
61 | queue_delayed_work(batadv_event_workqueue, | |
62 | &bat_priv->orig_work, | |
63 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); | |
64 | ||
5346c35e | 65 | return 0; |
c6c8fea2 SE |
66 | |
67 | err: | |
5346c35e | 68 | return -ENOMEM; |
c6c8fea2 SE |
69 | } |
70 | ||
56303d34 | 71 | void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node) |
a4c135c5 | 72 | { |
44524fcd | 73 | if (atomic_dec_and_test(&neigh_node->refcount)) |
ae179ae4 | 74 | kfree_rcu(neigh_node, rcu); |
a4c135c5 SW |
75 | } |
76 | ||
e1a5382f | 77 | /* increases the refcounter of a found router */ |
56303d34 SE |
78 | struct batadv_neigh_node * |
79 | batadv_orig_node_get_router(struct batadv_orig_node *orig_node) | |
e1a5382f | 80 | { |
56303d34 | 81 | struct batadv_neigh_node *router; |
e1a5382f LL |
82 | |
83 | rcu_read_lock(); | |
84 | router = rcu_dereference(orig_node->router); | |
85 | ||
86 | if (router && !atomic_inc_not_zero(&router->refcount)) | |
87 | router = NULL; | |
88 | ||
89 | rcu_read_unlock(); | |
90 | return router; | |
91 | } | |
92 | ||
56303d34 SE |
93 | struct batadv_neigh_node * |
94 | batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, | |
863dd7a8 | 95 | const uint8_t *neigh_addr) |
c6c8fea2 | 96 | { |
56303d34 SE |
97 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
98 | struct batadv_neigh_node *neigh_node; | |
c6c8fea2 | 99 | |
704509b8 | 100 | neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); |
c6c8fea2 | 101 | if (!neigh_node) |
7ae8b285 | 102 | goto out; |
c6c8fea2 | 103 | |
9591a79f | 104 | INIT_HLIST_NODE(&neigh_node->list); |
c6c8fea2 | 105 | |
7ae8b285 | 106 | memcpy(neigh_node->addr, neigh_addr, ETH_ALEN); |
e3b0d0de | 107 | spin_lock_init(&neigh_node->lq_update_lock); |
1605d0d6 ML |
108 | |
109 | /* extra reference for return */ | |
110 | atomic_set(&neigh_node->refcount, 2); | |
c6c8fea2 | 111 | |
39c75a51 | 112 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
d1dc3073 AQ |
113 | "Creating new neighbor %pM on interface %s\n", neigh_addr, |
114 | hard_iface->net_dev->name); | |
7ae8b285 ML |
115 | |
116 | out: | |
c6c8fea2 SE |
117 | return neigh_node; |
118 | } | |
119 | ||
03fc7f86 | 120 | static void batadv_orig_node_free_rcu(struct rcu_head *rcu) |
c6c8fea2 | 121 | { |
b67bfe0d | 122 | struct hlist_node *node_tmp; |
56303d34 SE |
123 | struct batadv_neigh_node *neigh_node, *tmp_neigh_node; |
124 | struct batadv_orig_node *orig_node; | |
16b1aba8 | 125 | |
56303d34 | 126 | orig_node = container_of(rcu, struct batadv_orig_node, rcu); |
c6c8fea2 | 127 | |
f987ed6e ML |
128 | spin_lock_bh(&orig_node->neigh_list_lock); |
129 | ||
a4c135c5 SW |
130 | /* for all bonding members ... */ |
131 | list_for_each_entry_safe(neigh_node, tmp_neigh_node, | |
132 | &orig_node->bond_list, bonding_list) { | |
133 | list_del_rcu(&neigh_node->bonding_list); | |
7d211efc | 134 | batadv_neigh_node_free_ref(neigh_node); |
a4c135c5 SW |
135 | } |
136 | ||
c6c8fea2 | 137 | /* for all neighbors towards this originator ... */ |
b67bfe0d | 138 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
9591a79f | 139 | &orig_node->neigh_list, list) { |
f987ed6e | 140 | hlist_del_rcu(&neigh_node->list); |
7d211efc | 141 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 SE |
142 | } |
143 | ||
f987ed6e ML |
144 | spin_unlock_bh(&orig_node->neigh_list_lock); |
145 | ||
d56b1705 MH |
146 | /* Free nc_nodes */ |
147 | batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); | |
148 | ||
610bfc6b MH |
149 | batadv_frag_purge_orig(orig_node, NULL); |
150 | ||
08c36d3e SE |
151 | batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, |
152 | "originator timed out"); | |
c6c8fea2 | 153 | |
a73105b8 | 154 | kfree(orig_node->tt_buff); |
c6c8fea2 SE |
155 | kfree(orig_node->bcast_own); |
156 | kfree(orig_node->bcast_own_sum); | |
157 | kfree(orig_node); | |
158 | } | |
159 | ||
72822225 LL |
160 | /** |
161 | * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly | |
162 | * schedule an rcu callback for freeing it | |
163 | * @orig_node: the orig node to free | |
164 | */ | |
56303d34 | 165 | void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) |
7b36e8ee ML |
166 | { |
167 | if (atomic_dec_and_test(&orig_node->refcount)) | |
03fc7f86 | 168 | call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); |
7b36e8ee ML |
169 | } |
170 | ||
72822225 LL |
171 | /** |
172 | * batadv_orig_node_free_ref_now - decrement the orig node refcounter and | |
173 | * possibly free it (without rcu callback) | |
174 | * @orig_node: the orig node to free | |
175 | */ | |
176 | void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node) | |
177 | { | |
178 | if (atomic_dec_and_test(&orig_node->refcount)) | |
179 | batadv_orig_node_free_rcu(&orig_node->rcu); | |
180 | } | |
181 | ||
56303d34 | 182 | void batadv_originator_free(struct batadv_priv *bat_priv) |
c6c8fea2 | 183 | { |
5bf74e9c | 184 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
b67bfe0d | 185 | struct hlist_node *node_tmp; |
16b1aba8 | 186 | struct hlist_head *head; |
16b1aba8 | 187 | spinlock_t *list_lock; /* spinlock to protect write access */ |
56303d34 | 188 | struct batadv_orig_node *orig_node; |
c90681b8 | 189 | uint32_t i; |
16b1aba8 ML |
190 | |
191 | if (!hash) | |
c6c8fea2 SE |
192 | return; |
193 | ||
194 | cancel_delayed_work_sync(&bat_priv->orig_work); | |
195 | ||
c6c8fea2 | 196 | bat_priv->orig_hash = NULL; |
16b1aba8 ML |
197 | |
198 | for (i = 0; i < hash->size; i++) { | |
199 | head = &hash->table[i]; | |
200 | list_lock = &hash->list_locks[i]; | |
201 | ||
202 | spin_lock_bh(list_lock); | |
b67bfe0d | 203 | hlist_for_each_entry_safe(orig_node, node_tmp, |
7aadf889 | 204 | head, hash_entry) { |
b67bfe0d | 205 | hlist_del_rcu(&orig_node->hash_entry); |
7d211efc | 206 | batadv_orig_node_free_ref(orig_node); |
16b1aba8 ML |
207 | } |
208 | spin_unlock_bh(list_lock); | |
209 | } | |
210 | ||
1a8eaf07 | 211 | batadv_hash_destroy(hash); |
c6c8fea2 SE |
212 | } |
213 | ||
214 | /* this function finds or creates an originator entry for the given | |
9cfc7bd6 SE |
215 | * address if it does not exits |
216 | */ | |
56303d34 SE |
217 | struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv, |
218 | const uint8_t *addr) | |
c6c8fea2 | 219 | { |
56303d34 | 220 | struct batadv_orig_node *orig_node; |
610bfc6b | 221 | int size, i; |
c6c8fea2 | 222 | int hash_added; |
42d0b044 | 223 | unsigned long reset_time; |
c6c8fea2 | 224 | |
da641193 | 225 | orig_node = batadv_orig_hash_find(bat_priv, addr); |
7aadf889 | 226 | if (orig_node) |
c6c8fea2 SE |
227 | return orig_node; |
228 | ||
39c75a51 SE |
229 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
230 | "Creating new originator: %pM\n", addr); | |
c6c8fea2 | 231 | |
704509b8 | 232 | orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); |
c6c8fea2 SE |
233 | if (!orig_node) |
234 | return NULL; | |
235 | ||
9591a79f | 236 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
a4c135c5 | 237 | INIT_LIST_HEAD(&orig_node->bond_list); |
2ae2daf6 | 238 | spin_lock_init(&orig_node->ogm_cnt_lock); |
f3e0008f | 239 | spin_lock_init(&orig_node->bcast_seqno_lock); |
f987ed6e | 240 | spin_lock_init(&orig_node->neigh_list_lock); |
a73105b8 | 241 | spin_lock_init(&orig_node->tt_buff_lock); |
a70a9aa9 | 242 | spin_lock_init(&orig_node->tt_lock); |
7b36e8ee | 243 | |
d56b1705 MH |
244 | batadv_nc_init_orig(orig_node); |
245 | ||
7b36e8ee ML |
246 | /* extra reference for return */ |
247 | atomic_set(&orig_node->refcount, 2); | |
c6c8fea2 | 248 | |
17071578 | 249 | orig_node->tt_initialised = false; |
16b1aba8 | 250 | orig_node->bat_priv = bat_priv; |
c6c8fea2 | 251 | memcpy(orig_node->orig, addr, ETH_ALEN); |
785ea114 | 252 | batadv_dat_init_orig_node_addr(orig_node); |
c6c8fea2 | 253 | orig_node->router = NULL; |
c8c991bf AQ |
254 | orig_node->tt_crc = 0; |
255 | atomic_set(&orig_node->last_ttvn, 0); | |
2dafb49d | 256 | orig_node->tt_buff = NULL; |
a73105b8 AQ |
257 | orig_node->tt_buff_len = 0; |
258 | atomic_set(&orig_node->tt_size, 0); | |
42d0b044 SE |
259 | reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
260 | orig_node->bcast_seqno_reset = reset_time; | |
261 | orig_node->batman_seqno_reset = reset_time; | |
c6c8fea2 | 262 | |
a4c135c5 SW |
263 | atomic_set(&orig_node->bond_candidates, 0); |
264 | ||
42d0b044 | 265 | size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; |
c6c8fea2 SE |
266 | |
267 | orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); | |
268 | if (!orig_node->bcast_own) | |
269 | goto free_orig_node; | |
270 | ||
271 | size = bat_priv->num_ifaces * sizeof(uint8_t); | |
272 | orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); | |
273 | ||
610bfc6b MH |
274 | for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) { |
275 | INIT_HLIST_HEAD(&orig_node->fragments[i].head); | |
276 | spin_lock_init(&orig_node->fragments[i].lock); | |
277 | orig_node->fragments[i].size = 0; | |
278 | } | |
279 | ||
c6c8fea2 SE |
280 | if (!orig_node->bcast_own_sum) |
281 | goto free_bcast_own; | |
282 | ||
03fc7f86 | 283 | hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, |
da641193 | 284 | batadv_choose_orig, orig_node, |
c0a55929 | 285 | &orig_node->hash_entry); |
1a1f37d9 | 286 | if (hash_added != 0) |
c6c8fea2 SE |
287 | goto free_bcast_own_sum; |
288 | ||
289 | return orig_node; | |
290 | free_bcast_own_sum: | |
291 | kfree(orig_node->bcast_own_sum); | |
292 | free_bcast_own: | |
293 | kfree(orig_node->bcast_own); | |
294 | free_orig_node: | |
295 | kfree(orig_node); | |
296 | return NULL; | |
297 | } | |
298 | ||
56303d34 SE |
299 | static bool |
300 | batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, | |
301 | struct batadv_orig_node *orig_node, | |
302 | struct batadv_neigh_node **best_neigh_node) | |
c6c8fea2 | 303 | { |
b67bfe0d | 304 | struct hlist_node *node_tmp; |
56303d34 | 305 | struct batadv_neigh_node *neigh_node; |
c6c8fea2 | 306 | bool neigh_purged = false; |
0b0094e0 | 307 | unsigned long last_seen; |
56303d34 | 308 | struct batadv_hard_iface *if_incoming; |
c6c8fea2 SE |
309 | |
310 | *best_neigh_node = NULL; | |
311 | ||
f987ed6e ML |
312 | spin_lock_bh(&orig_node->neigh_list_lock); |
313 | ||
c6c8fea2 | 314 | /* for all neighbors towards this originator ... */ |
b67bfe0d | 315 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
9591a79f | 316 | &orig_node->neigh_list, list) { |
1eda58bf SE |
317 | last_seen = neigh_node->last_seen; |
318 | if_incoming = neigh_node->if_incoming; | |
319 | ||
42d0b044 | 320 | if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) || |
e9a4f295 SE |
321 | (if_incoming->if_status == BATADV_IF_INACTIVE) || |
322 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || | |
323 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) { | |
e9a4f295 SE |
324 | if ((if_incoming->if_status == BATADV_IF_INACTIVE) || |
325 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || | |
326 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) | |
39c75a51 | 327 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
328 | "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", |
329 | orig_node->orig, neigh_node->addr, | |
330 | if_incoming->net_dev->name); | |
c6c8fea2 | 331 | else |
39c75a51 | 332 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
333 | "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", |
334 | orig_node->orig, neigh_node->addr, | |
335 | jiffies_to_msecs(last_seen)); | |
c6c8fea2 SE |
336 | |
337 | neigh_purged = true; | |
9591a79f | 338 | |
f987ed6e | 339 | hlist_del_rcu(&neigh_node->list); |
30d3c511 | 340 | batadv_bonding_candidate_del(orig_node, neigh_node); |
7d211efc | 341 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 SE |
342 | } else { |
343 | if ((!*best_neigh_node) || | |
344 | (neigh_node->tq_avg > (*best_neigh_node)->tq_avg)) | |
345 | *best_neigh_node = neigh_node; | |
346 | } | |
347 | } | |
f987ed6e ML |
348 | |
349 | spin_unlock_bh(&orig_node->neigh_list_lock); | |
c6c8fea2 SE |
350 | return neigh_purged; |
351 | } | |
352 | ||
56303d34 SE |
353 | static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, |
354 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 355 | { |
56303d34 | 356 | struct batadv_neigh_node *best_neigh_node; |
c6c8fea2 | 357 | |
42d0b044 SE |
358 | if (batadv_has_timed_out(orig_node->last_seen, |
359 | 2 * BATADV_PURGE_TIMEOUT)) { | |
39c75a51 | 360 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
361 | "Originator timeout: originator %pM, last_seen %u\n", |
362 | orig_node->orig, | |
363 | jiffies_to_msecs(orig_node->last_seen)); | |
c6c8fea2 SE |
364 | return true; |
365 | } else { | |
03fc7f86 SE |
366 | if (batadv_purge_orig_neighbors(bat_priv, orig_node, |
367 | &best_neigh_node)) | |
30d3c511 SE |
368 | batadv_update_route(bat_priv, orig_node, |
369 | best_neigh_node); | |
c6c8fea2 SE |
370 | } |
371 | ||
372 | return false; | |
373 | } | |
374 | ||
56303d34 | 375 | static void _batadv_purge_orig(struct batadv_priv *bat_priv) |
c6c8fea2 | 376 | { |
5bf74e9c | 377 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
b67bfe0d | 378 | struct hlist_node *node_tmp; |
c6c8fea2 | 379 | struct hlist_head *head; |
fb778ea1 | 380 | spinlock_t *list_lock; /* spinlock to protect write access */ |
56303d34 | 381 | struct batadv_orig_node *orig_node; |
c90681b8 | 382 | uint32_t i; |
c6c8fea2 SE |
383 | |
384 | if (!hash) | |
385 | return; | |
386 | ||
c6c8fea2 SE |
387 | /* for all origins... */ |
388 | for (i = 0; i < hash->size; i++) { | |
389 | head = &hash->table[i]; | |
fb778ea1 | 390 | list_lock = &hash->list_locks[i]; |
c6c8fea2 | 391 | |
fb778ea1 | 392 | spin_lock_bh(list_lock); |
b67bfe0d | 393 | hlist_for_each_entry_safe(orig_node, node_tmp, |
7aadf889 | 394 | head, hash_entry) { |
03fc7f86 | 395 | if (batadv_purge_orig_node(bat_priv, orig_node)) { |
414254e3 | 396 | batadv_gw_node_delete(bat_priv, orig_node); |
b67bfe0d | 397 | hlist_del_rcu(&orig_node->hash_entry); |
7d211efc | 398 | batadv_orig_node_free_ref(orig_node); |
fb778ea1 | 399 | continue; |
c6c8fea2 | 400 | } |
610bfc6b MH |
401 | |
402 | batadv_frag_purge_orig(orig_node, | |
403 | batadv_frag_check_entry); | |
c6c8fea2 | 404 | } |
fb778ea1 | 405 | spin_unlock_bh(list_lock); |
c6c8fea2 SE |
406 | } |
407 | ||
7cf06bc6 SE |
408 | batadv_gw_node_purge(bat_priv); |
409 | batadv_gw_election(bat_priv); | |
c6c8fea2 SE |
410 | } |
411 | ||
03fc7f86 | 412 | static void batadv_purge_orig(struct work_struct *work) |
c6c8fea2 | 413 | { |
56303d34 SE |
414 | struct delayed_work *delayed_work; |
415 | struct batadv_priv *bat_priv; | |
c6c8fea2 | 416 | |
56303d34 SE |
417 | delayed_work = container_of(work, struct delayed_work, work); |
418 | bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); | |
03fc7f86 | 419 | _batadv_purge_orig(bat_priv); |
72414442 AQ |
420 | queue_delayed_work(batadv_event_workqueue, |
421 | &bat_priv->orig_work, | |
422 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); | |
c6c8fea2 SE |
423 | } |
424 | ||
56303d34 | 425 | void batadv_purge_orig_ref(struct batadv_priv *bat_priv) |
c6c8fea2 | 426 | { |
03fc7f86 | 427 | _batadv_purge_orig(bat_priv); |
c6c8fea2 SE |
428 | } |
429 | ||
7d211efc | 430 | int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) |
c6c8fea2 SE |
431 | { |
432 | struct net_device *net_dev = (struct net_device *)seq->private; | |
56303d34 | 433 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
5bf74e9c | 434 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 435 | struct hlist_head *head; |
56303d34 SE |
436 | struct batadv_hard_iface *primary_if; |
437 | struct batadv_orig_node *orig_node; | |
438 | struct batadv_neigh_node *neigh_node, *neigh_node_tmp; | |
c6c8fea2 SE |
439 | int batman_count = 0; |
440 | int last_seen_secs; | |
441 | int last_seen_msecs; | |
0aca2369 | 442 | unsigned long last_seen_jiffies; |
c90681b8 | 443 | uint32_t i; |
32ae9b22 | 444 | |
30da63a6 ML |
445 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
446 | if (!primary_if) | |
32ae9b22 | 447 | goto out; |
c6c8fea2 | 448 | |
44c4349a | 449 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", |
42d0b044 | 450 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
32ae9b22 | 451 | primary_if->net_dev->dev_addr, net_dev->name); |
c6c8fea2 | 452 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", |
42d0b044 SE |
453 | "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, |
454 | "Nexthop", "outgoingIF", "Potential nexthops"); | |
c6c8fea2 | 455 | |
c6c8fea2 SE |
456 | for (i = 0; i < hash->size; i++) { |
457 | head = &hash->table[i]; | |
458 | ||
fb778ea1 | 459 | rcu_read_lock(); |
b67bfe0d | 460 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
7d211efc | 461 | neigh_node = batadv_orig_node_get_router(orig_node); |
e1a5382f | 462 | if (!neigh_node) |
c6c8fea2 SE |
463 | continue; |
464 | ||
e1a5382f LL |
465 | if (neigh_node->tq_avg == 0) |
466 | goto next; | |
c6c8fea2 | 467 | |
0aca2369 SE |
468 | last_seen_jiffies = jiffies - orig_node->last_seen; |
469 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); | |
470 | last_seen_secs = last_seen_msecs / 1000; | |
471 | last_seen_msecs = last_seen_msecs % 1000; | |
c6c8fea2 | 472 | |
c6c8fea2 SE |
473 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
474 | orig_node->orig, last_seen_secs, | |
475 | last_seen_msecs, neigh_node->tq_avg, | |
476 | neigh_node->addr, | |
477 | neigh_node->if_incoming->net_dev->name); | |
478 | ||
b67bfe0d | 479 | hlist_for_each_entry_rcu(neigh_node_tmp, |
f987ed6e | 480 | &orig_node->neigh_list, list) { |
e1a5382f LL |
481 | seq_printf(seq, " %pM (%3i)", |
482 | neigh_node_tmp->addr, | |
483 | neigh_node_tmp->tq_avg); | |
c6c8fea2 SE |
484 | } |
485 | ||
0c814653 | 486 | seq_puts(seq, "\n"); |
c6c8fea2 | 487 | batman_count++; |
e1a5382f LL |
488 | |
489 | next: | |
7d211efc | 490 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 | 491 | } |
fb778ea1 | 492 | rcu_read_unlock(); |
c6c8fea2 SE |
493 | } |
494 | ||
e1a5382f | 495 | if (batman_count == 0) |
0c814653 | 496 | seq_puts(seq, "No batman nodes in range ...\n"); |
c6c8fea2 | 497 | |
32ae9b22 ML |
498 | out: |
499 | if (primary_if) | |
e5d89254 | 500 | batadv_hardif_free_ref(primary_if); |
30da63a6 | 501 | return 0; |
c6c8fea2 SE |
502 | } |
503 | ||
56303d34 SE |
504 | static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node, |
505 | int max_if_num) | |
c6c8fea2 SE |
506 | { |
507 | void *data_ptr; | |
42d0b044 | 508 | size_t data_size, old_size; |
c6c8fea2 | 509 | |
42d0b044 SE |
510 | data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; |
511 | old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; | |
512 | data_ptr = kmalloc(data_size, GFP_ATOMIC); | |
320f422f | 513 | if (!data_ptr) |
5346c35e | 514 | return -ENOMEM; |
c6c8fea2 | 515 | |
42d0b044 | 516 | memcpy(data_ptr, orig_node->bcast_own, old_size); |
c6c8fea2 SE |
517 | kfree(orig_node->bcast_own); |
518 | orig_node->bcast_own = data_ptr; | |
519 | ||
520 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
320f422f | 521 | if (!data_ptr) |
5346c35e | 522 | return -ENOMEM; |
c6c8fea2 SE |
523 | |
524 | memcpy(data_ptr, orig_node->bcast_own_sum, | |
525 | (max_if_num - 1) * sizeof(uint8_t)); | |
526 | kfree(orig_node->bcast_own_sum); | |
527 | orig_node->bcast_own_sum = data_ptr; | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
56303d34 SE |
532 | int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, |
533 | int max_if_num) | |
c6c8fea2 | 534 | { |
56303d34 | 535 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
5bf74e9c | 536 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 537 | struct hlist_head *head; |
56303d34 | 538 | struct batadv_orig_node *orig_node; |
c90681b8 AQ |
539 | uint32_t i; |
540 | int ret; | |
c6c8fea2 SE |
541 | |
542 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on | |
9cfc7bd6 SE |
543 | * if_num |
544 | */ | |
c6c8fea2 SE |
545 | for (i = 0; i < hash->size; i++) { |
546 | head = &hash->table[i]; | |
547 | ||
fb778ea1 | 548 | rcu_read_lock(); |
b67bfe0d | 549 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
2ae2daf6 | 550 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
03fc7f86 | 551 | ret = batadv_orig_node_add_if(orig_node, max_if_num); |
2ae2daf6 ML |
552 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
553 | ||
5346c35e | 554 | if (ret == -ENOMEM) |
c6c8fea2 SE |
555 | goto err; |
556 | } | |
fb778ea1 | 557 | rcu_read_unlock(); |
c6c8fea2 SE |
558 | } |
559 | ||
c6c8fea2 SE |
560 | return 0; |
561 | ||
562 | err: | |
fb778ea1 | 563 | rcu_read_unlock(); |
c6c8fea2 SE |
564 | return -ENOMEM; |
565 | } | |
566 | ||
56303d34 | 567 | static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node, |
03fc7f86 | 568 | int max_if_num, int del_if_num) |
c6c8fea2 SE |
569 | { |
570 | void *data_ptr = NULL; | |
571 | int chunk_size; | |
572 | ||
573 | /* last interface was removed */ | |
574 | if (max_if_num == 0) | |
575 | goto free_bcast_own; | |
576 | ||
42d0b044 | 577 | chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; |
c6c8fea2 | 578 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); |
320f422f | 579 | if (!data_ptr) |
5346c35e | 580 | return -ENOMEM; |
c6c8fea2 SE |
581 | |
582 | /* copy first part */ | |
583 | memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size); | |
584 | ||
585 | /* copy second part */ | |
38e3c5f0 | 586 | memcpy((char *)data_ptr + del_if_num * chunk_size, |
c6c8fea2 SE |
587 | orig_node->bcast_own + ((del_if_num + 1) * chunk_size), |
588 | (max_if_num - del_if_num) * chunk_size); | |
589 | ||
590 | free_bcast_own: | |
591 | kfree(orig_node->bcast_own); | |
592 | orig_node->bcast_own = data_ptr; | |
593 | ||
594 | if (max_if_num == 0) | |
595 | goto free_own_sum; | |
596 | ||
597 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
320f422f | 598 | if (!data_ptr) |
5346c35e | 599 | return -ENOMEM; |
c6c8fea2 SE |
600 | |
601 | memcpy(data_ptr, orig_node->bcast_own_sum, | |
602 | del_if_num * sizeof(uint8_t)); | |
603 | ||
38e3c5f0 | 604 | memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), |
c6c8fea2 SE |
605 | orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)), |
606 | (max_if_num - del_if_num) * sizeof(uint8_t)); | |
607 | ||
608 | free_own_sum: | |
609 | kfree(orig_node->bcast_own_sum); | |
610 | orig_node->bcast_own_sum = data_ptr; | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
56303d34 SE |
615 | int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, |
616 | int max_if_num) | |
c6c8fea2 | 617 | { |
56303d34 | 618 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
5bf74e9c | 619 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 620 | struct hlist_head *head; |
56303d34 SE |
621 | struct batadv_hard_iface *hard_iface_tmp; |
622 | struct batadv_orig_node *orig_node; | |
c90681b8 AQ |
623 | uint32_t i; |
624 | int ret; | |
c6c8fea2 SE |
625 | |
626 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on | |
9cfc7bd6 SE |
627 | * if_num |
628 | */ | |
c6c8fea2 SE |
629 | for (i = 0; i < hash->size; i++) { |
630 | head = &hash->table[i]; | |
631 | ||
fb778ea1 | 632 | rcu_read_lock(); |
b67bfe0d | 633 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
2ae2daf6 | 634 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
03fc7f86 SE |
635 | ret = batadv_orig_node_del_if(orig_node, max_if_num, |
636 | hard_iface->if_num); | |
2ae2daf6 | 637 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
c6c8fea2 | 638 | |
5346c35e | 639 | if (ret == -ENOMEM) |
c6c8fea2 SE |
640 | goto err; |
641 | } | |
fb778ea1 | 642 | rcu_read_unlock(); |
c6c8fea2 SE |
643 | } |
644 | ||
645 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ | |
646 | rcu_read_lock(); | |
3193e8fd | 647 | list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { |
e9a4f295 | 648 | if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
649 | continue; |
650 | ||
e6c10f43 | 651 | if (hard_iface == hard_iface_tmp) |
c6c8fea2 SE |
652 | continue; |
653 | ||
e6c10f43 | 654 | if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) |
c6c8fea2 SE |
655 | continue; |
656 | ||
e6c10f43 ML |
657 | if (hard_iface_tmp->if_num > hard_iface->if_num) |
658 | hard_iface_tmp->if_num--; | |
c6c8fea2 SE |
659 | } |
660 | rcu_read_unlock(); | |
661 | ||
e6c10f43 | 662 | hard_iface->if_num = -1; |
c6c8fea2 SE |
663 | return 0; |
664 | ||
665 | err: | |
fb778ea1 | 666 | rcu_read_unlock(); |
c6c8fea2 SE |
667 | return -ENOMEM; |
668 | } |