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 | ||
7ea7b4a1 AQ |
47 | /** |
48 | * batadv_orig_node_vlan_get - get an orig_node_vlan object | |
49 | * @orig_node: the originator serving the VLAN | |
50 | * @vid: the VLAN identifier | |
51 | * | |
52 | * Returns the vlan object identified by vid and belonging to orig_node or NULL | |
53 | * if it does not exist. | |
54 | */ | |
55 | struct batadv_orig_node_vlan * | |
56 | batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node, | |
57 | unsigned short vid) | |
58 | { | |
59 | struct batadv_orig_node_vlan *vlan = NULL, *tmp; | |
60 | ||
61 | rcu_read_lock(); | |
62 | list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) { | |
63 | if (tmp->vid != vid) | |
64 | continue; | |
65 | ||
66 | if (!atomic_inc_not_zero(&tmp->refcount)) | |
67 | continue; | |
68 | ||
69 | vlan = tmp; | |
70 | ||
71 | break; | |
72 | } | |
73 | rcu_read_unlock(); | |
74 | ||
75 | return vlan; | |
76 | } | |
77 | ||
78 | /** | |
79 | * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan | |
80 | * object | |
81 | * @orig_node: the originator serving the VLAN | |
82 | * @vid: the VLAN identifier | |
83 | * | |
84 | * Returns NULL in case of failure or the vlan object identified by vid and | |
85 | * belonging to orig_node otherwise. The object is created and added to the list | |
86 | * if it does not exist. | |
87 | * | |
88 | * The object is returned with refcounter increased by 1. | |
89 | */ | |
90 | struct batadv_orig_node_vlan * | |
91 | batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node, | |
92 | unsigned short vid) | |
93 | { | |
94 | struct batadv_orig_node_vlan *vlan; | |
95 | ||
96 | spin_lock_bh(&orig_node->vlan_list_lock); | |
97 | ||
98 | /* first look if an object for this vid already exists */ | |
99 | vlan = batadv_orig_node_vlan_get(orig_node, vid); | |
100 | if (vlan) | |
101 | goto out; | |
102 | ||
103 | vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC); | |
104 | if (!vlan) | |
105 | goto out; | |
106 | ||
107 | atomic_set(&vlan->refcount, 2); | |
108 | vlan->vid = vid; | |
109 | ||
110 | list_add_rcu(&vlan->list, &orig_node->vlan_list); | |
111 | ||
112 | out: | |
113 | spin_unlock_bh(&orig_node->vlan_list_lock); | |
114 | ||
115 | return vlan; | |
116 | } | |
117 | ||
118 | /** | |
119 | * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free | |
120 | * the originator-vlan object | |
121 | * @orig_vlan: the originator-vlan object to release | |
122 | */ | |
123 | void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan) | |
124 | { | |
125 | if (atomic_dec_and_test(&orig_vlan->refcount)) | |
126 | kfree_rcu(orig_vlan, rcu); | |
127 | } | |
128 | ||
56303d34 | 129 | int batadv_originator_init(struct batadv_priv *bat_priv) |
c6c8fea2 SE |
130 | { |
131 | if (bat_priv->orig_hash) | |
5346c35e | 132 | return 0; |
c6c8fea2 | 133 | |
1a8eaf07 | 134 | bat_priv->orig_hash = batadv_hash_new(1024); |
c6c8fea2 SE |
135 | |
136 | if (!bat_priv->orig_hash) | |
137 | goto err; | |
138 | ||
dec05074 AQ |
139 | batadv_hash_set_lock_class(bat_priv->orig_hash, |
140 | &batadv_orig_hash_lock_class_key); | |
141 | ||
72414442 AQ |
142 | INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); |
143 | queue_delayed_work(batadv_event_workqueue, | |
144 | &bat_priv->orig_work, | |
145 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); | |
146 | ||
5346c35e | 147 | return 0; |
c6c8fea2 SE |
148 | |
149 | err: | |
5346c35e | 150 | return -ENOMEM; |
c6c8fea2 SE |
151 | } |
152 | ||
56303d34 | 153 | void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node) |
a4c135c5 | 154 | { |
44524fcd | 155 | if (atomic_dec_and_test(&neigh_node->refcount)) |
ae179ae4 | 156 | kfree_rcu(neigh_node, rcu); |
a4c135c5 SW |
157 | } |
158 | ||
e1a5382f | 159 | /* increases the refcounter of a found router */ |
56303d34 SE |
160 | struct batadv_neigh_node * |
161 | batadv_orig_node_get_router(struct batadv_orig_node *orig_node) | |
e1a5382f | 162 | { |
56303d34 | 163 | struct batadv_neigh_node *router; |
e1a5382f LL |
164 | |
165 | rcu_read_lock(); | |
166 | router = rcu_dereference(orig_node->router); | |
167 | ||
168 | if (router && !atomic_inc_not_zero(&router->refcount)) | |
169 | router = NULL; | |
170 | ||
171 | rcu_read_unlock(); | |
172 | return router; | |
173 | } | |
174 | ||
0538f759 AQ |
175 | /** |
176 | * batadv_neigh_node_new - create and init a new neigh_node object | |
177 | * @hard_iface: the interface where the neighbour is connected to | |
178 | * @neigh_addr: the mac address of the neighbour interface | |
179 | * @orig_node: originator object representing the neighbour | |
180 | * | |
181 | * Allocates a new neigh_node object and initialises all the generic fields. | |
182 | * Returns the new object or NULL on failure. | |
183 | */ | |
56303d34 SE |
184 | struct batadv_neigh_node * |
185 | batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, | |
0538f759 AQ |
186 | const uint8_t *neigh_addr, |
187 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 188 | { |
56303d34 | 189 | struct batadv_neigh_node *neigh_node; |
c6c8fea2 | 190 | |
704509b8 | 191 | neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); |
c6c8fea2 | 192 | if (!neigh_node) |
7ae8b285 | 193 | goto out; |
c6c8fea2 | 194 | |
9591a79f | 195 | INIT_HLIST_NODE(&neigh_node->list); |
c6c8fea2 | 196 | |
7ae8b285 | 197 | memcpy(neigh_node->addr, neigh_addr, ETH_ALEN); |
0538f759 AQ |
198 | neigh_node->if_incoming = hard_iface; |
199 | neigh_node->orig_node = orig_node; | |
200 | ||
201 | INIT_LIST_HEAD(&neigh_node->bonding_list); | |
1605d0d6 ML |
202 | |
203 | /* extra reference for return */ | |
204 | atomic_set(&neigh_node->refcount, 2); | |
c6c8fea2 | 205 | |
7ae8b285 | 206 | out: |
c6c8fea2 SE |
207 | return neigh_node; |
208 | } | |
209 | ||
03fc7f86 | 210 | static void batadv_orig_node_free_rcu(struct rcu_head *rcu) |
c6c8fea2 | 211 | { |
b67bfe0d | 212 | struct hlist_node *node_tmp; |
56303d34 SE |
213 | struct batadv_neigh_node *neigh_node, *tmp_neigh_node; |
214 | struct batadv_orig_node *orig_node; | |
16b1aba8 | 215 | |
56303d34 | 216 | orig_node = container_of(rcu, struct batadv_orig_node, rcu); |
c6c8fea2 | 217 | |
f987ed6e ML |
218 | spin_lock_bh(&orig_node->neigh_list_lock); |
219 | ||
a4c135c5 SW |
220 | /* for all bonding members ... */ |
221 | list_for_each_entry_safe(neigh_node, tmp_neigh_node, | |
222 | &orig_node->bond_list, bonding_list) { | |
223 | list_del_rcu(&neigh_node->bonding_list); | |
7d211efc | 224 | batadv_neigh_node_free_ref(neigh_node); |
a4c135c5 SW |
225 | } |
226 | ||
c6c8fea2 | 227 | /* for all neighbors towards this originator ... */ |
b67bfe0d | 228 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
9591a79f | 229 | &orig_node->neigh_list, list) { |
f987ed6e | 230 | hlist_del_rcu(&neigh_node->list); |
7d211efc | 231 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 SE |
232 | } |
233 | ||
f987ed6e ML |
234 | spin_unlock_bh(&orig_node->neigh_list_lock); |
235 | ||
d56b1705 MH |
236 | /* Free nc_nodes */ |
237 | batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); | |
238 | ||
610bfc6b MH |
239 | batadv_frag_purge_orig(orig_node, NULL); |
240 | ||
95fb130d | 241 | batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1, |
08c36d3e | 242 | "originator timed out"); |
c6c8fea2 | 243 | |
a73105b8 | 244 | kfree(orig_node->tt_buff); |
c6c8fea2 SE |
245 | kfree(orig_node->bcast_own); |
246 | kfree(orig_node->bcast_own_sum); | |
247 | kfree(orig_node); | |
248 | } | |
249 | ||
72822225 LL |
250 | /** |
251 | * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly | |
252 | * schedule an rcu callback for freeing it | |
253 | * @orig_node: the orig node to free | |
254 | */ | |
56303d34 | 255 | void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) |
7b36e8ee ML |
256 | { |
257 | if (atomic_dec_and_test(&orig_node->refcount)) | |
03fc7f86 | 258 | call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); |
7b36e8ee ML |
259 | } |
260 | ||
72822225 LL |
261 | /** |
262 | * batadv_orig_node_free_ref_now - decrement the orig node refcounter and | |
263 | * possibly free it (without rcu callback) | |
264 | * @orig_node: the orig node to free | |
265 | */ | |
266 | void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node) | |
267 | { | |
268 | if (atomic_dec_and_test(&orig_node->refcount)) | |
269 | batadv_orig_node_free_rcu(&orig_node->rcu); | |
270 | } | |
271 | ||
56303d34 | 272 | void batadv_originator_free(struct batadv_priv *bat_priv) |
c6c8fea2 | 273 | { |
5bf74e9c | 274 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
b67bfe0d | 275 | struct hlist_node *node_tmp; |
16b1aba8 | 276 | struct hlist_head *head; |
16b1aba8 | 277 | spinlock_t *list_lock; /* spinlock to protect write access */ |
56303d34 | 278 | struct batadv_orig_node *orig_node; |
c90681b8 | 279 | uint32_t i; |
16b1aba8 ML |
280 | |
281 | if (!hash) | |
c6c8fea2 SE |
282 | return; |
283 | ||
284 | cancel_delayed_work_sync(&bat_priv->orig_work); | |
285 | ||
c6c8fea2 | 286 | bat_priv->orig_hash = NULL; |
16b1aba8 ML |
287 | |
288 | for (i = 0; i < hash->size; i++) { | |
289 | head = &hash->table[i]; | |
290 | list_lock = &hash->list_locks[i]; | |
291 | ||
292 | spin_lock_bh(list_lock); | |
b67bfe0d | 293 | hlist_for_each_entry_safe(orig_node, node_tmp, |
7aadf889 | 294 | head, hash_entry) { |
b67bfe0d | 295 | hlist_del_rcu(&orig_node->hash_entry); |
7d211efc | 296 | batadv_orig_node_free_ref(orig_node); |
16b1aba8 ML |
297 | } |
298 | spin_unlock_bh(list_lock); | |
299 | } | |
300 | ||
1a8eaf07 | 301 | batadv_hash_destroy(hash); |
c6c8fea2 SE |
302 | } |
303 | ||
304 | /* this function finds or creates an originator entry for the given | |
9cfc7bd6 SE |
305 | * address if it does not exits |
306 | */ | |
56303d34 SE |
307 | struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv, |
308 | const uint8_t *addr) | |
c6c8fea2 | 309 | { |
56303d34 | 310 | struct batadv_orig_node *orig_node; |
7ea7b4a1 | 311 | struct batadv_orig_node_vlan *vlan; |
610bfc6b | 312 | int size, i; |
c6c8fea2 | 313 | int hash_added; |
42d0b044 | 314 | unsigned long reset_time; |
c6c8fea2 | 315 | |
da641193 | 316 | orig_node = batadv_orig_hash_find(bat_priv, addr); |
7aadf889 | 317 | if (orig_node) |
c6c8fea2 SE |
318 | return orig_node; |
319 | ||
39c75a51 SE |
320 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
321 | "Creating new originator: %pM\n", addr); | |
c6c8fea2 | 322 | |
704509b8 | 323 | orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); |
c6c8fea2 SE |
324 | if (!orig_node) |
325 | return NULL; | |
326 | ||
9591a79f | 327 | INIT_HLIST_HEAD(&orig_node->neigh_list); |
a4c135c5 | 328 | INIT_LIST_HEAD(&orig_node->bond_list); |
7ea7b4a1 | 329 | INIT_LIST_HEAD(&orig_node->vlan_list); |
2ae2daf6 | 330 | spin_lock_init(&orig_node->ogm_cnt_lock); |
f3e0008f | 331 | spin_lock_init(&orig_node->bcast_seqno_lock); |
f987ed6e | 332 | spin_lock_init(&orig_node->neigh_list_lock); |
a73105b8 | 333 | spin_lock_init(&orig_node->tt_buff_lock); |
a70a9aa9 | 334 | spin_lock_init(&orig_node->tt_lock); |
7ea7b4a1 | 335 | spin_lock_init(&orig_node->vlan_list_lock); |
7b36e8ee | 336 | |
d56b1705 MH |
337 | batadv_nc_init_orig(orig_node); |
338 | ||
7b36e8ee ML |
339 | /* extra reference for return */ |
340 | atomic_set(&orig_node->refcount, 2); | |
c6c8fea2 | 341 | |
17071578 | 342 | orig_node->tt_initialised = false; |
16b1aba8 | 343 | orig_node->bat_priv = bat_priv; |
c6c8fea2 | 344 | memcpy(orig_node->orig, addr, ETH_ALEN); |
785ea114 | 345 | batadv_dat_init_orig_node_addr(orig_node); |
c6c8fea2 | 346 | orig_node->router = NULL; |
c8c991bf | 347 | atomic_set(&orig_node->last_ttvn, 0); |
2dafb49d | 348 | orig_node->tt_buff = NULL; |
a73105b8 | 349 | orig_node->tt_buff_len = 0; |
42d0b044 SE |
350 | reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); |
351 | orig_node->bcast_seqno_reset = reset_time; | |
352 | orig_node->batman_seqno_reset = reset_time; | |
c6c8fea2 | 353 | |
a4c135c5 SW |
354 | atomic_set(&orig_node->bond_candidates, 0); |
355 | ||
7ea7b4a1 AQ |
356 | /* create a vlan object for the "untagged" LAN */ |
357 | vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS); | |
358 | if (!vlan) | |
359 | goto free_orig_node; | |
360 | /* batadv_orig_node_vlan_new() increases the refcounter. | |
361 | * Immediately release vlan since it is not needed anymore in this | |
362 | * context | |
363 | */ | |
364 | batadv_orig_node_vlan_free_ref(vlan); | |
365 | ||
42d0b044 | 366 | size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; |
c6c8fea2 SE |
367 | |
368 | orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); | |
369 | if (!orig_node->bcast_own) | |
7ea7b4a1 | 370 | goto free_vlan; |
c6c8fea2 SE |
371 | |
372 | size = bat_priv->num_ifaces * sizeof(uint8_t); | |
373 | orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); | |
374 | ||
610bfc6b MH |
375 | for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) { |
376 | INIT_HLIST_HEAD(&orig_node->fragments[i].head); | |
377 | spin_lock_init(&orig_node->fragments[i].lock); | |
378 | orig_node->fragments[i].size = 0; | |
379 | } | |
380 | ||
c6c8fea2 SE |
381 | if (!orig_node->bcast_own_sum) |
382 | goto free_bcast_own; | |
383 | ||
03fc7f86 | 384 | hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, |
da641193 | 385 | batadv_choose_orig, orig_node, |
c0a55929 | 386 | &orig_node->hash_entry); |
1a1f37d9 | 387 | if (hash_added != 0) |
c6c8fea2 SE |
388 | goto free_bcast_own_sum; |
389 | ||
390 | return orig_node; | |
391 | free_bcast_own_sum: | |
392 | kfree(orig_node->bcast_own_sum); | |
393 | free_bcast_own: | |
394 | kfree(orig_node->bcast_own); | |
7ea7b4a1 AQ |
395 | free_vlan: |
396 | batadv_orig_node_vlan_free_ref(vlan); | |
c6c8fea2 SE |
397 | free_orig_node: |
398 | kfree(orig_node); | |
399 | return NULL; | |
400 | } | |
401 | ||
56303d34 SE |
402 | static bool |
403 | batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, | |
404 | struct batadv_orig_node *orig_node, | |
405 | struct batadv_neigh_node **best_neigh_node) | |
c6c8fea2 | 406 | { |
b67bfe0d | 407 | struct hlist_node *node_tmp; |
56303d34 | 408 | struct batadv_neigh_node *neigh_node; |
c6c8fea2 | 409 | bool neigh_purged = false; |
0b0094e0 | 410 | unsigned long last_seen; |
56303d34 | 411 | struct batadv_hard_iface *if_incoming; |
0538f759 | 412 | uint8_t best_metric = 0; |
c6c8fea2 SE |
413 | |
414 | *best_neigh_node = NULL; | |
415 | ||
f987ed6e ML |
416 | spin_lock_bh(&orig_node->neigh_list_lock); |
417 | ||
c6c8fea2 | 418 | /* for all neighbors towards this originator ... */ |
b67bfe0d | 419 | hlist_for_each_entry_safe(neigh_node, node_tmp, |
9591a79f | 420 | &orig_node->neigh_list, list) { |
1eda58bf SE |
421 | last_seen = neigh_node->last_seen; |
422 | if_incoming = neigh_node->if_incoming; | |
423 | ||
42d0b044 | 424 | if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) || |
e9a4f295 SE |
425 | (if_incoming->if_status == BATADV_IF_INACTIVE) || |
426 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || | |
427 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) { | |
e9a4f295 SE |
428 | if ((if_incoming->if_status == BATADV_IF_INACTIVE) || |
429 | (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || | |
430 | (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) | |
39c75a51 | 431 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
432 | "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", |
433 | orig_node->orig, neigh_node->addr, | |
434 | if_incoming->net_dev->name); | |
c6c8fea2 | 435 | else |
39c75a51 | 436 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
437 | "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", |
438 | orig_node->orig, neigh_node->addr, | |
439 | jiffies_to_msecs(last_seen)); | |
c6c8fea2 SE |
440 | |
441 | neigh_purged = true; | |
9591a79f | 442 | |
f987ed6e | 443 | hlist_del_rcu(&neigh_node->list); |
30d3c511 | 444 | batadv_bonding_candidate_del(orig_node, neigh_node); |
7d211efc | 445 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 SE |
446 | } else { |
447 | if ((!*best_neigh_node) || | |
0538f759 | 448 | (neigh_node->bat_iv.tq_avg > best_metric)) { |
c6c8fea2 | 449 | *best_neigh_node = neigh_node; |
0538f759 AQ |
450 | best_metric = neigh_node->bat_iv.tq_avg; |
451 | } | |
c6c8fea2 SE |
452 | } |
453 | } | |
f987ed6e ML |
454 | |
455 | spin_unlock_bh(&orig_node->neigh_list_lock); | |
c6c8fea2 SE |
456 | return neigh_purged; |
457 | } | |
458 | ||
56303d34 SE |
459 | static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, |
460 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 461 | { |
56303d34 | 462 | struct batadv_neigh_node *best_neigh_node; |
c6c8fea2 | 463 | |
42d0b044 SE |
464 | if (batadv_has_timed_out(orig_node->last_seen, |
465 | 2 * BATADV_PURGE_TIMEOUT)) { | |
39c75a51 | 466 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
467 | "Originator timeout: originator %pM, last_seen %u\n", |
468 | orig_node->orig, | |
469 | jiffies_to_msecs(orig_node->last_seen)); | |
c6c8fea2 SE |
470 | return true; |
471 | } else { | |
03fc7f86 SE |
472 | if (batadv_purge_orig_neighbors(bat_priv, orig_node, |
473 | &best_neigh_node)) | |
30d3c511 SE |
474 | batadv_update_route(bat_priv, orig_node, |
475 | best_neigh_node); | |
c6c8fea2 SE |
476 | } |
477 | ||
478 | return false; | |
479 | } | |
480 | ||
56303d34 | 481 | static void _batadv_purge_orig(struct batadv_priv *bat_priv) |
c6c8fea2 | 482 | { |
5bf74e9c | 483 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
b67bfe0d | 484 | struct hlist_node *node_tmp; |
c6c8fea2 | 485 | struct hlist_head *head; |
fb778ea1 | 486 | spinlock_t *list_lock; /* spinlock to protect write access */ |
56303d34 | 487 | struct batadv_orig_node *orig_node; |
c90681b8 | 488 | uint32_t i; |
c6c8fea2 SE |
489 | |
490 | if (!hash) | |
491 | return; | |
492 | ||
c6c8fea2 SE |
493 | /* for all origins... */ |
494 | for (i = 0; i < hash->size; i++) { | |
495 | head = &hash->table[i]; | |
fb778ea1 | 496 | list_lock = &hash->list_locks[i]; |
c6c8fea2 | 497 | |
fb778ea1 | 498 | spin_lock_bh(list_lock); |
b67bfe0d | 499 | hlist_for_each_entry_safe(orig_node, node_tmp, |
7aadf889 | 500 | head, hash_entry) { |
03fc7f86 | 501 | if (batadv_purge_orig_node(bat_priv, orig_node)) { |
414254e3 | 502 | batadv_gw_node_delete(bat_priv, orig_node); |
b67bfe0d | 503 | hlist_del_rcu(&orig_node->hash_entry); |
7d211efc | 504 | batadv_orig_node_free_ref(orig_node); |
fb778ea1 | 505 | continue; |
c6c8fea2 | 506 | } |
610bfc6b MH |
507 | |
508 | batadv_frag_purge_orig(orig_node, | |
509 | batadv_frag_check_entry); | |
c6c8fea2 | 510 | } |
fb778ea1 | 511 | spin_unlock_bh(list_lock); |
c6c8fea2 SE |
512 | } |
513 | ||
7cf06bc6 SE |
514 | batadv_gw_node_purge(bat_priv); |
515 | batadv_gw_election(bat_priv); | |
c6c8fea2 SE |
516 | } |
517 | ||
03fc7f86 | 518 | static void batadv_purge_orig(struct work_struct *work) |
c6c8fea2 | 519 | { |
56303d34 SE |
520 | struct delayed_work *delayed_work; |
521 | struct batadv_priv *bat_priv; | |
c6c8fea2 | 522 | |
56303d34 SE |
523 | delayed_work = container_of(work, struct delayed_work, work); |
524 | bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); | |
03fc7f86 | 525 | _batadv_purge_orig(bat_priv); |
72414442 AQ |
526 | queue_delayed_work(batadv_event_workqueue, |
527 | &bat_priv->orig_work, | |
528 | msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD)); | |
c6c8fea2 SE |
529 | } |
530 | ||
56303d34 | 531 | void batadv_purge_orig_ref(struct batadv_priv *bat_priv) |
c6c8fea2 | 532 | { |
03fc7f86 | 533 | _batadv_purge_orig(bat_priv); |
c6c8fea2 SE |
534 | } |
535 | ||
7d211efc | 536 | int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) |
c6c8fea2 SE |
537 | { |
538 | struct net_device *net_dev = (struct net_device *)seq->private; | |
56303d34 | 539 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
5bf74e9c | 540 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 541 | struct hlist_head *head; |
56303d34 SE |
542 | struct batadv_hard_iface *primary_if; |
543 | struct batadv_orig_node *orig_node; | |
544 | struct batadv_neigh_node *neigh_node, *neigh_node_tmp; | |
c6c8fea2 SE |
545 | int batman_count = 0; |
546 | int last_seen_secs; | |
547 | int last_seen_msecs; | |
0aca2369 | 548 | unsigned long last_seen_jiffies; |
c90681b8 | 549 | uint32_t i; |
32ae9b22 | 550 | |
30da63a6 ML |
551 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
552 | if (!primary_if) | |
32ae9b22 | 553 | goto out; |
c6c8fea2 | 554 | |
44c4349a | 555 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", |
42d0b044 | 556 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
32ae9b22 | 557 | primary_if->net_dev->dev_addr, net_dev->name); |
c6c8fea2 | 558 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", |
42d0b044 SE |
559 | "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, |
560 | "Nexthop", "outgoingIF", "Potential nexthops"); | |
c6c8fea2 | 561 | |
c6c8fea2 SE |
562 | for (i = 0; i < hash->size; i++) { |
563 | head = &hash->table[i]; | |
564 | ||
fb778ea1 | 565 | rcu_read_lock(); |
b67bfe0d | 566 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
7d211efc | 567 | neigh_node = batadv_orig_node_get_router(orig_node); |
e1a5382f | 568 | if (!neigh_node) |
c6c8fea2 SE |
569 | continue; |
570 | ||
0538f759 | 571 | if (neigh_node->bat_iv.tq_avg == 0) |
e1a5382f | 572 | goto next; |
c6c8fea2 | 573 | |
0aca2369 SE |
574 | last_seen_jiffies = jiffies - orig_node->last_seen; |
575 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); | |
576 | last_seen_secs = last_seen_msecs / 1000; | |
577 | last_seen_msecs = last_seen_msecs % 1000; | |
c6c8fea2 | 578 | |
c6c8fea2 SE |
579 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
580 | orig_node->orig, last_seen_secs, | |
0538f759 | 581 | last_seen_msecs, neigh_node->bat_iv.tq_avg, |
c6c8fea2 SE |
582 | neigh_node->addr, |
583 | neigh_node->if_incoming->net_dev->name); | |
584 | ||
b67bfe0d | 585 | hlist_for_each_entry_rcu(neigh_node_tmp, |
f987ed6e | 586 | &orig_node->neigh_list, list) { |
e1a5382f LL |
587 | seq_printf(seq, " %pM (%3i)", |
588 | neigh_node_tmp->addr, | |
0538f759 | 589 | neigh_node_tmp->bat_iv.tq_avg); |
c6c8fea2 SE |
590 | } |
591 | ||
0c814653 | 592 | seq_puts(seq, "\n"); |
c6c8fea2 | 593 | batman_count++; |
e1a5382f LL |
594 | |
595 | next: | |
7d211efc | 596 | batadv_neigh_node_free_ref(neigh_node); |
c6c8fea2 | 597 | } |
fb778ea1 | 598 | rcu_read_unlock(); |
c6c8fea2 SE |
599 | } |
600 | ||
e1a5382f | 601 | if (batman_count == 0) |
0c814653 | 602 | seq_puts(seq, "No batman nodes in range ...\n"); |
c6c8fea2 | 603 | |
32ae9b22 ML |
604 | out: |
605 | if (primary_if) | |
e5d89254 | 606 | batadv_hardif_free_ref(primary_if); |
30da63a6 | 607 | return 0; |
c6c8fea2 SE |
608 | } |
609 | ||
56303d34 SE |
610 | static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node, |
611 | int max_if_num) | |
c6c8fea2 SE |
612 | { |
613 | void *data_ptr; | |
42d0b044 | 614 | size_t data_size, old_size; |
c6c8fea2 | 615 | |
42d0b044 SE |
616 | data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; |
617 | old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; | |
618 | data_ptr = kmalloc(data_size, GFP_ATOMIC); | |
320f422f | 619 | if (!data_ptr) |
5346c35e | 620 | return -ENOMEM; |
c6c8fea2 | 621 | |
42d0b044 | 622 | memcpy(data_ptr, orig_node->bcast_own, old_size); |
c6c8fea2 SE |
623 | kfree(orig_node->bcast_own); |
624 | orig_node->bcast_own = data_ptr; | |
625 | ||
626 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
320f422f | 627 | if (!data_ptr) |
5346c35e | 628 | return -ENOMEM; |
c6c8fea2 SE |
629 | |
630 | memcpy(data_ptr, orig_node->bcast_own_sum, | |
631 | (max_if_num - 1) * sizeof(uint8_t)); | |
632 | kfree(orig_node->bcast_own_sum); | |
633 | orig_node->bcast_own_sum = data_ptr; | |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
56303d34 SE |
638 | int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, |
639 | int max_if_num) | |
c6c8fea2 | 640 | { |
56303d34 | 641 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
5bf74e9c | 642 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 643 | struct hlist_head *head; |
56303d34 | 644 | struct batadv_orig_node *orig_node; |
c90681b8 AQ |
645 | uint32_t i; |
646 | int ret; | |
c6c8fea2 SE |
647 | |
648 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on | |
9cfc7bd6 SE |
649 | * if_num |
650 | */ | |
c6c8fea2 SE |
651 | for (i = 0; i < hash->size; i++) { |
652 | head = &hash->table[i]; | |
653 | ||
fb778ea1 | 654 | rcu_read_lock(); |
b67bfe0d | 655 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
2ae2daf6 | 656 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
03fc7f86 | 657 | ret = batadv_orig_node_add_if(orig_node, max_if_num); |
2ae2daf6 ML |
658 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
659 | ||
5346c35e | 660 | if (ret == -ENOMEM) |
c6c8fea2 SE |
661 | goto err; |
662 | } | |
fb778ea1 | 663 | rcu_read_unlock(); |
c6c8fea2 SE |
664 | } |
665 | ||
c6c8fea2 SE |
666 | return 0; |
667 | ||
668 | err: | |
fb778ea1 | 669 | rcu_read_unlock(); |
c6c8fea2 SE |
670 | return -ENOMEM; |
671 | } | |
672 | ||
56303d34 | 673 | static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node, |
03fc7f86 | 674 | int max_if_num, int del_if_num) |
c6c8fea2 SE |
675 | { |
676 | void *data_ptr = NULL; | |
677 | int chunk_size; | |
678 | ||
679 | /* last interface was removed */ | |
680 | if (max_if_num == 0) | |
681 | goto free_bcast_own; | |
682 | ||
42d0b044 | 683 | chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; |
c6c8fea2 | 684 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); |
320f422f | 685 | if (!data_ptr) |
5346c35e | 686 | return -ENOMEM; |
c6c8fea2 SE |
687 | |
688 | /* copy first part */ | |
689 | memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size); | |
690 | ||
691 | /* copy second part */ | |
38e3c5f0 | 692 | memcpy((char *)data_ptr + del_if_num * chunk_size, |
c6c8fea2 SE |
693 | orig_node->bcast_own + ((del_if_num + 1) * chunk_size), |
694 | (max_if_num - del_if_num) * chunk_size); | |
695 | ||
696 | free_bcast_own: | |
697 | kfree(orig_node->bcast_own); | |
698 | orig_node->bcast_own = data_ptr; | |
699 | ||
700 | if (max_if_num == 0) | |
701 | goto free_own_sum; | |
702 | ||
703 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
320f422f | 704 | if (!data_ptr) |
5346c35e | 705 | return -ENOMEM; |
c6c8fea2 SE |
706 | |
707 | memcpy(data_ptr, orig_node->bcast_own_sum, | |
708 | del_if_num * sizeof(uint8_t)); | |
709 | ||
38e3c5f0 | 710 | memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), |
c6c8fea2 SE |
711 | orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)), |
712 | (max_if_num - del_if_num) * sizeof(uint8_t)); | |
713 | ||
714 | free_own_sum: | |
715 | kfree(orig_node->bcast_own_sum); | |
716 | orig_node->bcast_own_sum = data_ptr; | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
56303d34 SE |
721 | int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, |
722 | int max_if_num) | |
c6c8fea2 | 723 | { |
56303d34 | 724 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
5bf74e9c | 725 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
c6c8fea2 | 726 | struct hlist_head *head; |
56303d34 SE |
727 | struct batadv_hard_iface *hard_iface_tmp; |
728 | struct batadv_orig_node *orig_node; | |
c90681b8 AQ |
729 | uint32_t i; |
730 | int ret; | |
c6c8fea2 SE |
731 | |
732 | /* resize all orig nodes because orig_node->bcast_own(_sum) depend on | |
9cfc7bd6 SE |
733 | * if_num |
734 | */ | |
c6c8fea2 SE |
735 | for (i = 0; i < hash->size; i++) { |
736 | head = &hash->table[i]; | |
737 | ||
fb778ea1 | 738 | rcu_read_lock(); |
b67bfe0d | 739 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
2ae2daf6 | 740 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
03fc7f86 SE |
741 | ret = batadv_orig_node_del_if(orig_node, max_if_num, |
742 | hard_iface->if_num); | |
2ae2daf6 | 743 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
c6c8fea2 | 744 | |
5346c35e | 745 | if (ret == -ENOMEM) |
c6c8fea2 SE |
746 | goto err; |
747 | } | |
fb778ea1 | 748 | rcu_read_unlock(); |
c6c8fea2 SE |
749 | } |
750 | ||
751 | /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ | |
752 | rcu_read_lock(); | |
3193e8fd | 753 | list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { |
e9a4f295 | 754 | if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
755 | continue; |
756 | ||
e6c10f43 | 757 | if (hard_iface == hard_iface_tmp) |
c6c8fea2 SE |
758 | continue; |
759 | ||
e6c10f43 | 760 | if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) |
c6c8fea2 SE |
761 | continue; |
762 | ||
e6c10f43 ML |
763 | if (hard_iface_tmp->if_num > hard_iface->if_num) |
764 | hard_iface_tmp->if_num--; | |
c6c8fea2 SE |
765 | } |
766 | rcu_read_unlock(); | |
767 | ||
e6c10f43 | 768 | hard_iface->if_num = -1; |
c6c8fea2 SE |
769 | return 0; |
770 | ||
771 | err: | |
fb778ea1 | 772 | rcu_read_unlock(); |
c6c8fea2 SE |
773 | return -ENOMEM; |
774 | } |