3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 * All these functions are global
47 #include "builddata.h"
50 #include "two_hop_neighbor_table.h"
52 #include "duplicate_set.h"
53 #include "mpr_selector_set.h"
58 #include "scheduler.h"
61 #include "neighbor_table.h"
63 #include "lq_packet.h"
64 #include "common/avl.h"
66 #include "lq_plugin.h"
68 #include "duplicate_handler.h"
69 #include "olsr_random.h"
75 bool changes_topology;
76 bool changes_neighborhood;
80 /*COLLECT startup sleeps caused by warnings*/
82 #ifdef OLSR_COLLECT_STARTUP_SLEEP
83 static int max_startup_sleep = 0;
84 #endif /* OLSR_COLLECT_STARTUP_SLEEP */
85 static int sum_startup_sleep = 0;
87 void olsr_startup_sleep(int s)
89 sum_startup_sleep += s;
90 #ifdef OLSR_COLLECT_STARTUP_SLEEP
91 if (s > max_startup_sleep) max_startup_sleep=s;
92 #else /* OLSR_COLLECT_STARTUP_SLEEP */
94 #endif /* OLSR_COLLECT_STARTUP_SLEEP */
97 void olsr_do_startup_sleep(void)
99 #ifdef OLSR_COLLECT_STARTUP_SLEEP
100 if (sum_startup_sleep > max_startup_sleep)
101 printf("OLSR encountered multiple problems on startup, which should delay startup by %i seconds.\nAs this is quite much time, OLSR will sleep only %i seconds.\nBUT YOU SHOULD FIX ABOVE PROBLEMS!\n",
102 sum_startup_sleep,max_startup_sleep);
103 sleep(max_startup_sleep);
104 #else /* OLSR_COLLECT_STARTUP_SLEEP */
105 if (sum_startup_sleep > 0)
106 printf("olsrd startup was delayed %i seconds due to various nasty error messages.\nYOU SHOULD REALLY FIX ABOVE PROBLEMS!\n",
108 #endif /* OLSR_COLLECT_STARTUP_SLEEP */
112 * Process changes functions
116 int (*function) (int, int, int);
120 static struct pcf *pcf_list;
122 static uint16_t message_seqno;
123 union olsr_ip_addr all_zero;
126 *Initialize the message sequence number as a random value
131 message_seqno = olsr_random() & 0xFFFF;
135 * Get and increment the message sequence number
142 return message_seqno++;
146 olsr_is_bad_duplicate_msg_seqno(uint16_t seqno) {
147 int32_t diff = (int32_t) seqno - (int32_t) message_seqno;
152 else if (diff > 32767) {
159 register_pcf(int (*f) (int, int, int))
163 OLSR_PRINTF(1, "Registering pcf function\n");
165 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
167 new_pcf->function = f;
168 new_pcf->next = pcf_list;
174 *Process changes in neighborhood or/and topology.
175 *Re-calculates the neighborhood/topology if there
176 *are any updates - then calls the right functions to
177 *update the routing table.
181 olsr_process_changes(void)
183 struct pcf *tmp_pc_list;
186 if (changes_neighborhood)
187 OLSR_PRINTF(3, "CHANGES IN NEIGHBORHOOD\n");
188 if (changes_topology)
189 OLSR_PRINTF(3, "CHANGES IN TOPOLOGY\n");
191 OLSR_PRINTF(3, "CHANGES IN HNA\n");
194 if (!changes_neighborhood && !changes_topology && !changes_hna)
197 if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1)) {
199 printf(" *** %s (%s on %s) ***\n", olsrd_version, build_date, build_host);
202 if (changes_neighborhood) {
203 if (olsr_cnf->lq_level < 1) {
204 olsr_calculate_mpr();
206 olsr_calculate_lq_mpr();
210 /* calculate the routing table */
211 if (changes_neighborhood || changes_topology || changes_hna) {
212 olsr_calculate_routing_table(false);
215 if (olsr_cnf->debug_level > 0) {
216 if (olsr_cnf->debug_level > 2) {
217 olsr_print_mid_set();
219 olsr_print_gateway_entries();
220 #endif /* __linux__ */
222 if (olsr_cnf->debug_level > 3) {
223 if (olsr_cnf->debug_level > 8) {
224 olsr_print_duplicate_table();
226 olsr_print_hna_set();
229 olsr_print_link_set();
230 olsr_print_neighbor_table();
231 olsr_print_two_hop_neighbor_table();
232 if (olsr_cnf->debug_level > 3) {
233 olsr_print_tc_table();
237 for (tmp_pc_list = pcf_list; tmp_pc_list != NULL; tmp_pc_list = tmp_pc_list->next) {
238 tmp_pc_list->function(changes_neighborhood, changes_topology, changes_hna);
241 changes_neighborhood = false;
242 changes_topology = false;
244 changes_force = false;
248 *Initialize all the tables used(neighbor,
249 *topology, MID, HNA, MPR, dup).
250 *Also initalizes other variables
253 olsr_init_tables(void)
255 changes_topology = false;
256 changes_neighborhood = false;
259 /* Set avl tree comparator */
260 if (olsr_cnf->ipsize == 4) {
261 avl_comp_default = avl_comp_ipv4;
262 avl_comp_prefix_default = avl_comp_ipv4_prefix;
264 avl_comp_default = avl_comp_ipv6;
265 avl_comp_prefix_default = avl_comp_ipv6_prefix;
268 /* Initialize lq plugin set */
269 init_lq_handler_tree();
271 /* Initialize link set */
272 olsr_init_link_set();
274 /* Initialize duplicate table */
275 olsr_init_duplicate_set();
277 /* Initialize neighbor table */
278 olsr_init_neighbor_table();
280 /* Initialize routing table */
281 olsr_init_routing_table();
283 /* Initialize two hop table */
284 olsr_init_two_hop_table();
286 /* Initialize topology */
289 /* Initialize mpr selector table */
290 olsr_init_mprs_set();
292 /* Initialize MID set */
295 /* Initialize HNA set */
298 /* Initialize duplicate handler */
299 #ifndef NO_DUPLICATE_DETECTION_HANDLER
300 olsr_duplicate_handler_init();
301 #endif /* NO_DUPLICATE_DETECTION_HANDLER */
305 *Check if a message is to be forwarded and forward
308 *@param m the OLSR message to be forwarded
309 *@param in_if the incoming interface
310 *@param from_addr neighbour we received message from
312 *@returns positive if forwarded
315 olsr_forward_message(union olsr_message *m, struct interface_olsr *in_if, union olsr_ip_addr *from_addr)
317 union olsr_ip_addr *src;
318 struct neighbor_entry *neighbor;
320 struct interface_olsr *ifn;
321 bool is_ttl_1 = false;
324 * Sven-Ola: We should not flood the mesh with overdue messages. Because
325 * of a bug in parser.c:parse_packet, we have a lot of messages because
326 * all older olsrd's have lq_fish enabled.
328 if (AF_INET == olsr_cnf->ip_version) {
329 if (m->v4.ttl < 2 || 255 < (int)m->v4.hopcnt + (int)m->v4.ttl)
332 if (m->v6.ttl < 2 || 255 < (int)m->v6.hopcnt + (int)m->v6.ttl)
336 /* Lookup sender address */
337 src = mid_lookup_main_addr(from_addr);
341 neighbor = olsr_lookup_neighbor_table(src);
345 if (neighbor->status != SYM)
349 if (olsr_lookup_mprs_set(src) == NULL) {
351 struct ipaddr_str buf;
352 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(&buf, src));
357 if (olsr_message_is_duplicate(m)) {
361 /* Treat TTL hopcnt except for ethernet link */
363 if (olsr_cnf->ip_version == AF_INET) {
374 /* Update packet data */
375 msgsize = ntohs(m->v4.olsr_msgsize);
377 /* looping trough interfaces */
378 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
379 /* do not retransmit out through the same interface if it has mode == ether */
380 if (ifn == in_if && ifn->mode == IF_MODE_ETHER) continue;
382 /* do not forward TTL 1 messages to non-ether interfaces */
383 if (is_ttl_1 && ifn->mode != IF_MODE_ETHER) continue;
385 if (net_output_pending(ifn)) {
387 * Check if message is to big to be piggybacked
389 if (net_outbuffer_push(ifn, m, msgsize) != msgsize) {
393 set_buffer_timer(ifn);
395 if (net_outbuffer_push(ifn, m, msgsize) != msgsize) {
396 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
397 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
401 /* No forwarding pending */
402 set_buffer_timer(ifn);
404 if (net_outbuffer_push(ifn, m, msgsize) != msgsize) {
405 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
406 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
414 set_buffer_timer(struct interface_olsr *ifn)
417 ifn->fwdtimer = GET_TIMESTAMP(olsr_random() * olsr_cnf->max_jitter * MSEC_PER_SEC / OLSR_RANDOM_MAX);
421 olsr_init_willingness(void)
423 if (olsr_cnf->willingness_auto) {
425 /* Run it first and then periodic. */
426 olsr_update_willingness(NULL);
428 olsr_start_timer((unsigned int)olsr_cnf->will_int * MSEC_PER_SEC, 5, OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL, 0);
433 olsr_update_willingness(void *foo __attribute__ ((unused)))
435 int tmp_will = olsr_cnf->willingness;
437 /* Re-calculate willingness */
438 olsr_cnf->willingness = olsr_calculate_willingness();
440 if (tmp_will != olsr_cnf->willingness) {
441 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
446 *Calculate this nodes willingness to act as a MPR
447 *based on either a fixed value or the power status
448 *of the node using APM
450 *@return a 8bit value from 0-7 representing the willingness
454 olsr_calculate_willingness(void)
456 struct olsr_apm_info ainfo;
458 /* If fixed willingness */
459 if (!olsr_cnf->willingness_auto)
460 return olsr_cnf->willingness;
462 if (apm_read(&ainfo) < 1)
465 apm_printinfo(&ainfo);
468 if (ainfo.ac_line_status == OLSR_AC_POWERED)
471 /* If battery powered
473 * juice > 78% will: 3
474 * 78% > juice > 26% will: 2
475 * 26% > juice will: 1
477 return (ainfo.battery_percentage / 26);
481 olsr_msgtype_to_string(uint8_t msgtype)
483 static char type[20];
486 case (HELLO_MESSAGE):
494 case (LQ_HELLO_MESSAGE):
496 case (LQ_TC_MESSAGE):
502 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
507 olsr_link_to_string(uint8_t linktype)
509 static char type[20];
526 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
531 olsr_status_to_string(uint8_t status)
533 static char type[20];
546 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
551 *Termination function to be called whenever a error occures
552 *that requires the daemon to terminate
554 *@param msg the message to write to the syslog and possibly stdout
555 *@param val the exit code
559 olsr_exit(const char *msg, int val)
561 OLSR_PRINTF(1, "OLSR EXIT: %s\n", msg);
562 olsr_syslog(OLSR_LOG_ERR, "olsrd exit: %s\n", msg);
566 olsr_cnf->exit_value = val;
571 /* just to be sure */
576 * Wrapper for malloc(3) that does error-checking
578 * @param size the number of bytes to allocalte
579 * @param id a string identifying the caller for
580 * use in error messaging
582 * @return a void pointer to the memory allocated
585 olsr_malloc(size_t size, const char *id)
590 * Not all the callers do a proper cleaning of memory.
591 * Clean it on behalf of those.
593 ptr = calloc(1, size);
596 const char *const err_msg = strerror(errno);
597 OLSR_PRINTF(1, "OUT OF MEMORY: %s\n", err_msg);
598 olsr_syslog(OLSR_LOG_ERR, "olsrd: out of memory!: %s\n", err_msg);
599 olsr_exit(id, EXIT_FAILURE);
606 *Wrapper for printf that prints to a specific
607 *debuglevel upper limit
612 olsr_printf(int loglevel, const char *format, ...)
614 if ((loglevel <= olsr_cnf->debug_level) && debug_handle) {
616 va_start(arglist, format);
617 vfprintf(debug_handle, format, arglist);
626 * indent-tabs-mode: nil