2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
22 /* increase the reference counter for this originator */
25 #include "originator.h"
27 #include "translation-table.h"
29 #include "gateway_client.h"
30 #include "hard-interface.h"
32 #include "soft-interface.h"
34 static void purge_orig(struct work_struct
*work
);
36 static void start_purge_timer(struct bat_priv
*bat_priv
)
38 INIT_DELAYED_WORK(&bat_priv
->orig_work
, purge_orig
);
39 queue_delayed_work(bat_event_workqueue
, &bat_priv
->orig_work
, 1 * HZ
);
42 int originator_init(struct bat_priv
*bat_priv
)
44 if (bat_priv
->orig_hash
)
47 spin_lock_bh(&bat_priv
->orig_hash_lock
);
48 bat_priv
->orig_hash
= hash_new(1024);
50 if (!bat_priv
->orig_hash
)
53 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
54 start_purge_timer(bat_priv
);
58 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
63 create_neighbor(struct orig_node
*orig_node
, struct orig_node
*orig_neigh_node
,
64 uint8_t *neigh
, struct batman_if
*if_incoming
)
66 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
67 struct neigh_node
*neigh_node
;
69 bat_dbg(DBG_BATMAN
, bat_priv
,
70 "Creating new last-hop neighbor of originator\n");
72 neigh_node
= kzalloc(sizeof(struct neigh_node
), GFP_ATOMIC
);
76 INIT_LIST_HEAD(&neigh_node
->list
);
78 memcpy(neigh_node
->addr
, neigh
, ETH_ALEN
);
79 neigh_node
->orig_node
= orig_neigh_node
;
80 neigh_node
->if_incoming
= if_incoming
;
82 list_add_tail(&neigh_node
->list
, &orig_node
->neigh_list
);
86 static void free_orig_node(void *data
, void *arg
)
88 struct list_head
*list_pos
, *list_pos_tmp
;
89 struct neigh_node
*neigh_node
;
90 struct orig_node
*orig_node
= (struct orig_node
*)data
;
91 struct bat_priv
*bat_priv
= (struct bat_priv
*)arg
;
93 /* for all neighbors towards this originator ... */
94 list_for_each_safe(list_pos
, list_pos_tmp
, &orig_node
->neigh_list
) {
95 neigh_node
= list_entry(list_pos
, struct neigh_node
, list
);
101 frag_list_free(&orig_node
->frag_list
);
102 hna_global_del_orig(bat_priv
, orig_node
, "originator timed out");
104 kfree(orig_node
->bcast_own
);
105 kfree(orig_node
->bcast_own_sum
);
109 void originator_free(struct bat_priv
*bat_priv
)
111 if (!bat_priv
->orig_hash
)
114 cancel_delayed_work_sync(&bat_priv
->orig_work
);
116 spin_lock_bh(&bat_priv
->orig_hash_lock
);
117 hash_delete(bat_priv
->orig_hash
, free_orig_node
, bat_priv
);
118 bat_priv
->orig_hash
= NULL
;
119 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
122 /* this function finds or creates an originator entry for the given
123 * address if it does not exits */
124 struct orig_node
*get_orig_node(struct bat_priv
*bat_priv
, uint8_t *addr
)
126 struct orig_node
*orig_node
;
130 orig_node
= ((struct orig_node
*)hash_find(bat_priv
->orig_hash
,
131 compare_orig
, choose_orig
,
137 bat_dbg(DBG_BATMAN
, bat_priv
,
138 "Creating new originator: %pM\n", addr
);
140 orig_node
= kzalloc(sizeof(struct orig_node
), GFP_ATOMIC
);
144 INIT_LIST_HEAD(&orig_node
->neigh_list
);
146 memcpy(orig_node
->orig
, addr
, ETH_ALEN
);
147 orig_node
->router
= NULL
;
148 orig_node
->hna_buff
= NULL
;
149 orig_node
->bcast_seqno_reset
= jiffies
- 1
150 - msecs_to_jiffies(RESET_PROTECTION_MS
);
151 orig_node
->batman_seqno_reset
= jiffies
- 1
152 - msecs_to_jiffies(RESET_PROTECTION_MS
);
154 size
= bat_priv
->num_ifaces
* sizeof(unsigned long) * NUM_WORDS
;
156 orig_node
->bcast_own
= kzalloc(size
, GFP_ATOMIC
);
157 if (!orig_node
->bcast_own
)
160 size
= bat_priv
->num_ifaces
* sizeof(uint8_t);
161 orig_node
->bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
163 INIT_LIST_HEAD(&orig_node
->frag_list
);
164 orig_node
->last_frag_packet
= 0;
166 if (!orig_node
->bcast_own_sum
)
169 hash_added
= hash_add(bat_priv
->orig_hash
, compare_orig
, choose_orig
,
172 goto free_bcast_own_sum
;
176 kfree(orig_node
->bcast_own_sum
);
178 kfree(orig_node
->bcast_own
);
184 static bool purge_orig_neighbors(struct bat_priv
*bat_priv
,
185 struct orig_node
*orig_node
,
186 struct neigh_node
**best_neigh_node
)
188 struct list_head
*list_pos
, *list_pos_tmp
;
189 struct neigh_node
*neigh_node
;
190 bool neigh_purged
= false;
192 *best_neigh_node
= NULL
;
194 /* for all neighbors towards this originator ... */
195 list_for_each_safe(list_pos
, list_pos_tmp
, &orig_node
->neigh_list
) {
196 neigh_node
= list_entry(list_pos
, struct neigh_node
, list
);
198 if ((time_after(jiffies
,
199 neigh_node
->last_valid
+ PURGE_TIMEOUT
* HZ
)) ||
200 (neigh_node
->if_incoming
->if_status
== IF_INACTIVE
) ||
201 (neigh_node
->if_incoming
->if_status
== IF_TO_BE_REMOVED
)) {
203 if (neigh_node
->if_incoming
->if_status
==
205 bat_dbg(DBG_BATMAN
, bat_priv
,
206 "neighbor purge: originator %pM, "
207 "neighbor: %pM, iface: %s\n",
208 orig_node
->orig
, neigh_node
->addr
,
209 neigh_node
->if_incoming
->net_dev
->name
);
211 bat_dbg(DBG_BATMAN
, bat_priv
,
212 "neighbor timeout: originator %pM, "
213 "neighbor: %pM, last_valid: %lu\n",
214 orig_node
->orig
, neigh_node
->addr
,
215 (neigh_node
->last_valid
/ HZ
));
221 if ((!*best_neigh_node
) ||
222 (neigh_node
->tq_avg
> (*best_neigh_node
)->tq_avg
))
223 *best_neigh_node
= neigh_node
;
229 static bool purge_orig_node(struct bat_priv
*bat_priv
,
230 struct orig_node
*orig_node
)
232 struct neigh_node
*best_neigh_node
;
234 if (time_after(jiffies
,
235 orig_node
->last_valid
+ 2 * PURGE_TIMEOUT
* HZ
)) {
237 bat_dbg(DBG_BATMAN
, bat_priv
,
238 "Originator timeout: originator %pM, last_valid %lu\n",
239 orig_node
->orig
, (orig_node
->last_valid
/ HZ
));
242 if (purge_orig_neighbors(bat_priv
, orig_node
,
244 update_routes(bat_priv
, orig_node
,
247 orig_node
->hna_buff_len
);
248 /* update bonding candidates, we could have lost
249 * some candidates. */
250 update_bonding_candidates(bat_priv
, orig_node
);
257 static void _purge_orig(struct bat_priv
*bat_priv
)
259 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
260 struct hlist_node
*walk
, *safe
;
261 struct hlist_head
*head
;
262 struct element_t
*bucket
;
263 struct orig_node
*orig_node
;
269 spin_lock_bh(&bat_priv
->orig_hash_lock
);
271 /* for all origins... */
272 for (i
= 0; i
< hash
->size
; i
++) {
273 head
= &hash
->table
[i
];
275 hlist_for_each_entry_safe(bucket
, walk
, safe
, head
, hlist
) {
276 orig_node
= bucket
->data
;
278 if (purge_orig_node(bat_priv
, orig_node
)) {
279 if (orig_node
->gw_flags
)
280 gw_node_delete(bat_priv
, orig_node
);
283 free_orig_node(orig_node
, bat_priv
);
286 if (time_after(jiffies
, orig_node
->last_frag_packet
+
287 msecs_to_jiffies(FRAG_TIMEOUT
)))
288 frag_list_free(&orig_node
->frag_list
);
292 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
294 gw_node_purge(bat_priv
);
295 gw_election(bat_priv
);
297 softif_neigh_purge(bat_priv
);
300 static void purge_orig(struct work_struct
*work
)
302 struct delayed_work
*delayed_work
=
303 container_of(work
, struct delayed_work
, work
);
304 struct bat_priv
*bat_priv
=
305 container_of(delayed_work
, struct bat_priv
, orig_work
);
307 _purge_orig(bat_priv
);
308 start_purge_timer(bat_priv
);
311 void purge_orig_ref(struct bat_priv
*bat_priv
)
313 _purge_orig(bat_priv
);
316 int orig_seq_print_text(struct seq_file
*seq
, void *offset
)
318 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
319 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
320 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
321 struct hlist_node
*walk
;
322 struct hlist_head
*head
;
323 struct element_t
*bucket
;
324 struct orig_node
*orig_node
;
325 struct neigh_node
*neigh_node
;
326 int batman_count
= 0;
331 if ((!bat_priv
->primary_if
) ||
332 (bat_priv
->primary_if
->if_status
!= IF_ACTIVE
)) {
333 if (!bat_priv
->primary_if
)
334 return seq_printf(seq
, "BATMAN mesh %s disabled - "
335 "please specify interfaces to enable it\n",
338 return seq_printf(seq
, "BATMAN mesh %s "
339 "disabled - primary interface not active\n",
343 seq_printf(seq
, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
344 SOURCE_VERSION
, REVISION_VERSION_STR
,
345 bat_priv
->primary_if
->net_dev
->name
,
346 bat_priv
->primary_if
->net_dev
->dev_addr
, net_dev
->name
);
347 seq_printf(seq
, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
348 "Originator", "last-seen", "#", TQ_MAX_VALUE
, "Nexthop",
349 "outgoingIF", "Potential nexthops");
351 spin_lock_bh(&bat_priv
->orig_hash_lock
);
353 for (i
= 0; i
< hash
->size
; i
++) {
354 head
= &hash
->table
[i
];
356 hlist_for_each_entry(bucket
, walk
, head
, hlist
) {
357 orig_node
= bucket
->data
;
359 if (!orig_node
->router
)
362 if (orig_node
->router
->tq_avg
== 0)
365 last_seen_secs
= jiffies_to_msecs(jiffies
-
366 orig_node
->last_valid
) / 1000;
367 last_seen_msecs
= jiffies_to_msecs(jiffies
-
368 orig_node
->last_valid
) % 1000;
370 neigh_node
= orig_node
->router
;
371 seq_printf(seq
, "%pM %4i.%03is (%3i) %pM [%10s]:",
372 orig_node
->orig
, last_seen_secs
,
373 last_seen_msecs
, neigh_node
->tq_avg
,
375 neigh_node
->if_incoming
->net_dev
->name
);
377 list_for_each_entry(neigh_node
, &orig_node
->neigh_list
,
379 seq_printf(seq
, " %pM (%3i)", neigh_node
->addr
,
383 seq_printf(seq
, "\n");
388 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
390 if ((batman_count
== 0))
391 seq_printf(seq
, "No batman nodes in range ...\n");
396 static int orig_node_add_if(struct orig_node
*orig_node
, int max_if_num
)
400 data_ptr
= kmalloc(max_if_num
* sizeof(unsigned long) * NUM_WORDS
,
403 pr_err("Can't resize orig: out of memory\n");
407 memcpy(data_ptr
, orig_node
->bcast_own
,
408 (max_if_num
- 1) * sizeof(unsigned long) * NUM_WORDS
);
409 kfree(orig_node
->bcast_own
);
410 orig_node
->bcast_own
= data_ptr
;
412 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
414 pr_err("Can't resize orig: out of memory\n");
418 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
419 (max_if_num
- 1) * sizeof(uint8_t));
420 kfree(orig_node
->bcast_own_sum
);
421 orig_node
->bcast_own_sum
= data_ptr
;
426 int orig_hash_add_if(struct batman_if
*batman_if
, int max_if_num
)
428 struct bat_priv
*bat_priv
= netdev_priv(batman_if
->soft_iface
);
429 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
430 struct hlist_node
*walk
;
431 struct hlist_head
*head
;
432 struct element_t
*bucket
;
433 struct orig_node
*orig_node
;
436 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
438 spin_lock_bh(&bat_priv
->orig_hash_lock
);
440 for (i
= 0; i
< hash
->size
; i
++) {
441 head
= &hash
->table
[i
];
443 hlist_for_each_entry(bucket
, walk
, head
, hlist
) {
444 orig_node
= bucket
->data
;
446 if (orig_node_add_if(orig_node
, max_if_num
) == -1)
451 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
455 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
459 static int orig_node_del_if(struct orig_node
*orig_node
,
460 int max_if_num
, int del_if_num
)
462 void *data_ptr
= NULL
;
465 /* last interface was removed */
469 chunk_size
= sizeof(unsigned long) * NUM_WORDS
;
470 data_ptr
= kmalloc(max_if_num
* chunk_size
, GFP_ATOMIC
);
472 pr_err("Can't resize orig: out of memory\n");
476 /* copy first part */
477 memcpy(data_ptr
, orig_node
->bcast_own
, del_if_num
* chunk_size
);
479 /* copy second part */
480 memcpy(data_ptr
+ del_if_num
* chunk_size
,
481 orig_node
->bcast_own
+ ((del_if_num
+ 1) * chunk_size
),
482 (max_if_num
- del_if_num
) * chunk_size
);
485 kfree(orig_node
->bcast_own
);
486 orig_node
->bcast_own
= data_ptr
;
491 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
493 pr_err("Can't resize orig: out of memory\n");
497 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
498 del_if_num
* sizeof(uint8_t));
500 memcpy(data_ptr
+ del_if_num
* sizeof(uint8_t),
501 orig_node
->bcast_own_sum
+ ((del_if_num
+ 1) * sizeof(uint8_t)),
502 (max_if_num
- del_if_num
) * sizeof(uint8_t));
505 kfree(orig_node
->bcast_own_sum
);
506 orig_node
->bcast_own_sum
= data_ptr
;
511 int orig_hash_del_if(struct batman_if
*batman_if
, int max_if_num
)
513 struct bat_priv
*bat_priv
= netdev_priv(batman_if
->soft_iface
);
514 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
515 struct hlist_node
*walk
;
516 struct hlist_head
*head
;
517 struct element_t
*bucket
;
518 struct batman_if
*batman_if_tmp
;
519 struct orig_node
*orig_node
;
522 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
524 spin_lock_bh(&bat_priv
->orig_hash_lock
);
526 for (i
= 0; i
< hash
->size
; i
++) {
527 head
= &hash
->table
[i
];
529 hlist_for_each_entry(bucket
, walk
, head
, hlist
) {
530 orig_node
= bucket
->data
;
532 ret
= orig_node_del_if(orig_node
, max_if_num
,
540 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
542 list_for_each_entry_rcu(batman_if_tmp
, &if_list
, list
) {
543 if (batman_if_tmp
->if_status
== IF_NOT_IN_USE
)
546 if (batman_if
== batman_if_tmp
)
549 if (batman_if
->soft_iface
!= batman_if_tmp
->soft_iface
)
552 if (batman_if_tmp
->if_num
> batman_if
->if_num
)
553 batman_if_tmp
->if_num
--;
557 batman_if
->if_num
= -1;
558 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
562 spin_unlock_bh(&bat_priv
->orig_hash_lock
);