2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
42 * All these functions are global
48 #include "two_hop_neighbor_table.h"
50 #include "duplicate_set.h"
51 #include "mpr_selector_set.h"
56 #include "scheduler.h"
59 #include "neighbor_table.h"
61 #include "lq_packet.h"
62 #include "common/avl.h"
64 #include "lq_plugin.h"
73 olsr_bool changes_topology;
74 olsr_bool changes_neighborhood;
75 olsr_bool changes_hna;
76 olsr_bool changes_force;
79 * Process changes functions
84 int (*function)(int, int, int);
88 static struct pcf *pcf_list;
90 static olsr_u16_t message_seqno;
91 union olsr_ip_addr all_zero;
94 *Initialize the message sequence number as a random value
99 message_seqno = random() & 0xFFFF;
103 * Get and increment the message sequence number
110 return message_seqno++;
115 register_pcf(int (*f)(int, int, int))
119 OLSR_PRINTF(1, "Registering pcf function\n");
121 new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");
123 new_pcf->function = f;
124 new_pcf->next = pcf_list;
131 *Process changes in neighborhood or/and topology.
132 *Re-calculates the neighborhood/topology if there
133 *are any updates - then calls the right functions to
134 *update the routing table.
138 olsr_process_changes(void)
140 struct pcf *tmp_pc_list;
143 if(changes_neighborhood)
144 OLSR_PRINTF(3, "CHANGES IN NEIGHBORHOOD\n");
146 OLSR_PRINTF(3, "CHANGES IN TOPOLOGY\n");
148 OLSR_PRINTF(3, "CHANGES IN HNA\n");
152 2 <= olsr_cnf->lq_level &&
153 0 >= olsr_cnf->lq_dlimit)
156 if(!changes_neighborhood &&
161 if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
164 printf(" *** %s (%s on %s) ***\n", olsrd_version, build_date, build_host);
167 if (changes_neighborhood) {
168 if (olsr_cnf->lq_level < 1) {
169 olsr_calculate_mpr();
171 olsr_calculate_lq_mpr();
175 /* calculate the routing table */
176 if (changes_neighborhood || changes_topology || changes_hna) {
177 olsr_calculate_routing_table();
180 if (olsr_cnf->debug_level > 0)
182 if (olsr_cnf->debug_level > 2)
184 olsr_print_mid_set();
186 if (olsr_cnf->debug_level > 3)
188 if (olsr_cnf->debug_level > 8)
190 olsr_print_duplicate_table();
192 olsr_print_hna_set();
197 olsr_print_link_set();
198 olsr_print_neighbor_table();
199 olsr_print_two_hop_neighbor_table();
200 olsr_print_tc_table();
204 for(tmp_pc_list = pcf_list;
206 tmp_pc_list = tmp_pc_list->next)
208 tmp_pc_list->function(changes_neighborhood,
213 changes_neighborhood = OLSR_FALSE;
214 changes_topology = OLSR_FALSE;
215 changes_hna = OLSR_FALSE;
216 changes_force = OLSR_FALSE;
220 * Callback for the periodic route calculation.
223 olsr_trigger_forced_update(void *unused __attribute__((unused))) {
225 changes_force = OLSR_TRUE;
226 changes_neighborhood = OLSR_TRUE;
227 changes_topology = OLSR_TRUE;
228 changes_hna = OLSR_TRUE;
230 olsr_process_changes();
234 *Initialize all the tables used(neighbor,
235 *topology, MID, HNA, MPR, dup).
236 *Also initalizes other variables
239 olsr_init_tables(void)
241 changes_topology = OLSR_FALSE;
242 changes_neighborhood = OLSR_FALSE;
243 changes_hna = OLSR_FALSE;
245 /* Set avl tree comparator */
246 if (olsr_cnf->ipsize == 4) {
247 avl_comp_default = avl_comp_ipv4;
248 avl_comp_prefix_default = avl_comp_ipv4_prefix;
250 avl_comp_default = avl_comp_ipv6;
251 avl_comp_prefix_default = avl_comp_ipv6_prefix;
254 /* Initialize lq plugin set */
255 init_lq_handler_tree();
257 /* Initialize link set */
258 olsr_init_link_set();
260 /* Initialize duplicate table */
261 olsr_init_duplicate_set();
263 /* Initialize neighbor table */
264 olsr_init_neighbor_table();
266 /* Initialize routing table */
267 olsr_init_routing_table();
269 /* Initialize two hop table */
270 olsr_init_two_hop_table();
272 /* Initialize topology */
275 /* Initialize mpr selector table */
276 olsr_init_mprs_set();
278 /* Initialize MID set */
281 /* Initialize HNA set */
285 /* Initialize Layer 1/2 database */
286 olsr_initialize_layer12();
289 /* Start periodic SPF and RIB recalculation */
290 if (olsr_cnf->lq_dinter > 0.0) {
291 olsr_start_timer((unsigned int)(olsr_cnf->lq_dinter * MSEC_PER_SEC), 5,
292 OLSR_TIMER_PERIODIC, &olsr_trigger_forced_update, NULL, 0);
297 *Check if a message is to be forwarded and forward
300 *@param m the OLSR message recieved
301 *@param originator the originator of this message
302 *@param seqno the seqno of the message
304 *@returns positive if forwarded
307 olsr_forward_message(union olsr_message *m,
308 union olsr_ip_addr *from_addr)
310 union olsr_ip_addr *src;
311 struct neighbor_entry *neighbor;
313 struct interface *ifn;
316 * Sven-Ola: We should not flood the mesh with overdue messages. Because
317 * of a bug in parser.c:parse_packet, we have a lot of messages because
318 * all older olsrd's have lq_fish enabled.
320 if (AF_INET == olsr_cnf->ip_version)
322 if (2 > m->v4.ttl || 255 < (int)m->v4.hopcnt + (int)m->v4.ttl) return 0;
326 if (2 > m->v6.ttl || 255 < (int)m->v6.hopcnt + (int)m->v6.ttl) return 0;
329 /* Lookup sender address */
330 src = olsr_lookup_main_addr_by_alias(from_addr);
334 neighbor=olsr_lookup_neighbor_table(src);
338 if(neighbor->status != SYM)
342 if(olsr_lookup_mprs_set(src) == NULL)
345 struct ipaddr_str buf;
346 OLSR_PRINTF(5, "Forward - sender %s not MPR selector\n", olsr_ip_to_string(&buf, src));
351 /* Treat TTL hopcnt */
352 if(olsr_cnf->ip_version == AF_INET)
365 /* Update packet data */
366 msgsize = ntohs(m->v4.olsr_msgsize);
368 /* looping trough interfaces */
369 for (ifn = ifnet; ifn ; ifn = ifn->int_next)
371 if(net_output_pending(ifn))
374 * Check if message is to big to be piggybacked
376 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
381 set_buffer_timer(ifn);
383 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
385 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
386 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
392 /* No forwarding pending */
393 set_buffer_timer(ifn);
395 if(net_outbuffer_push(ifn, m, msgsize) != msgsize)
397 OLSR_PRINTF(1, "Received message to big to be forwarded in %s(%d bytes)!", ifn->int_name, msgsize);
398 olsr_syslog(OLSR_LOG_ERR, "Received message to big to be forwarded on %s(%d bytes)!", ifn->int_name, msgsize);
407 set_buffer_timer(struct interface *ifn)
410 ifn->fwdtimer = GET_TIMESTAMP(random() * olsr_cnf->max_jitter * MSEC_PER_SEC / RAND_MAX);
414 olsr_init_willingness(void)
416 if (olsr_cnf->willingness_auto) {
418 /* Run it first and then periodic. */
419 olsr_update_willingness(NULL);
421 olsr_start_timer((unsigned int)olsr_cnf->will_int * MSEC_PER_SEC, 5,
422 OLSR_TIMER_PERIODIC, &olsr_update_willingness, NULL, 0);
427 olsr_update_willingness(void *foo __attribute__((unused)))
429 int tmp_will = olsr_cnf->willingness;
431 /* Re-calculate willingness */
432 olsr_cnf->willingness = olsr_calculate_willingness();
434 if(tmp_will != olsr_cnf->willingness)
436 OLSR_PRINTF(1, "Local willingness updated: old %d new %d\n", tmp_will, olsr_cnf->willingness);
442 *Calculate this nodes willingness to act as a MPR
443 *based on either a fixed value or the power status
444 *of the node using APM
446 *@return a 8bit value from 0-7 representing the willingness
450 olsr_calculate_willingness(void)
452 struct olsr_apm_info ainfo;
454 /* If fixed willingness */
455 if(!olsr_cnf->willingness_auto)
456 return olsr_cnf->willingness;
458 if(apm_read(&ainfo) < 1)
461 apm_printinfo(&ainfo);
464 if(ainfo.ac_line_status == OLSR_AC_POWERED)
467 /* If battery powered
469 * juice > 78% will: 3
470 * 78% > juice > 26% will: 2
471 * 26% > juice will: 1
473 return (ainfo.battery_percentage / 26);
477 olsr_msgtype_to_string(olsr_u8_t msgtype)
479 static char type[20];
491 case(LQ_HELLO_MESSAGE):
499 snprintf(type, sizeof(type), "UNKNOWN(%d)", msgtype);
505 olsr_link_to_string(olsr_u8_t linktype)
507 static char type[20];
525 snprintf(type, sizeof(type), "UNKNOWN(%d)", linktype);
531 olsr_status_to_string(olsr_u8_t status)
533 static char type[20];
547 snprintf(type, sizeof(type), "UNKNOWN(%d)", status);
553 *Termination function to be called whenever a error occures
554 *that requires the daemon to terminate
556 *@param msg the message to write to the syslog and possibly stdout
560 olsr_exit(const char *msg, int val)
562 OLSR_PRINTF(1, "OLSR EXIT: %s\n", msg);
563 olsr_syslog(OLSR_LOG_ERR, "olsrd exit: %s\n", msg);
565 olsr_cnf->exit_value = val;
572 * Wrapper for malloc(3) that does error-checking
574 * @param size the number of bytes to allocalte
575 * @param caller a string identifying the caller for
576 * use in error messaging
578 * @return a void pointer to the memory allocated
581 olsr_malloc(size_t size, const char *id)
586 * Not all the callers do a proper cleaning of memory.
587 * Clean it on behalf of those.
589 ptr = calloc(1, size);
592 const char * const err_msg = strerror(errno);
593 OLSR_PRINTF(1, "OUT OF MEMORY: %s\n", err_msg);
594 olsr_syslog(OLSR_LOG_ERR, "olsrd: out of memory!: %s\n", err_msg);
595 olsr_exit(id, EXIT_FAILURE);
599 /* useful for debugging */
600 olsr_printf(1, "MEMORY: alloc %s %p, %u bytes\n",
609 *Wrapper for printf that prints to a specific
610 *debuglevel upper limit
615 olsr_printf(int loglevel, const char *format, ...)
617 if((loglevel <= olsr_cnf->debug_level) && debug_handle)
620 va_start(arglist, format);
621 vfprintf(debug_handle, format, arglist);