b59acfa010aeefc5484220219d6b2b2a108f4259
[olsrd.git] / src / routing_table.c
1 /*
2  * The olsr.org Optimized Link-State Routing daemon(olsrd)
3  * Copyright (c) 2004, Andreas T√łnnesen(andreto@olsr.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met:
9  *
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 
15  *   distribution.
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.
19  *
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.
32  *
33  * Visit http://www.olsr.org for more information.
34  *
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.
38  *
39  * $Id: routing_table.c,v 1.12 2005/01/17 20:18:22 kattemat Exp $
40  */
41
42
43
44 #include "defs.h"
45 #include "two_hop_neighbor_table.h"
46 #include "tc_set.h"
47 #include "mid_set.h"
48 #include "mpr.h"
49 #include "olsr.h"
50 #include "link_set.h"
51
52
53 /* Begin:
54  * Prototypes for internal functions 
55  */
56
57 static int
58 olsr_fill_routing_table_with_neighbors(void);
59
60 static struct destination_n *
61 olsr_fill_routing_table_with_two_hop_neighbors(void);
62
63 static struct rt_entry *
64 olsr_check_for_higher_hopcount(struct rt_entry *, struct hna_net *, olsr_u16_t);
65
66 static struct rt_entry *
67 olsr_lookup_routing_table(union olsr_ip_addr *);
68
69 struct rt_entry *
70 olsr_check_for_lower_hopcount(struct rt_entry *, struct hna_net *, olsr_u16_t);
71
72 /* End:
73  * Prototypes for internal functions 
74  */
75
76
77 /**
78  *Initialize the routing table
79  */
80 int
81 olsr_init_routing_table()
82 {
83   int index;
84
85   /*
86    *The hna routes hash will almost always
87    *be indexed to 0
88    *But it is kept as a hash to be compatible
89    *with the functions used on the regular
90    *routing table
91    */
92   for(index=0;index<HASHSIZE;index++)
93     {
94       routingtable[index].next = &routingtable[index];
95       routingtable[index].prev = &routingtable[index];
96       hna_routes[index].next = &hna_routes[index];
97       hna_routes[index].prev = &hna_routes[index];
98     }
99   return 1;
100 }
101
102 /**
103  *Look up an entry in the routing table.
104  *
105  *@param dst the address of the entry
106  *
107  *@return a pointer to a rt_entry struct 
108  *representing the route entry.
109  */
110 static struct rt_entry *
111 olsr_lookup_routing_table(union olsr_ip_addr *dst)
112 {
113
114   struct rt_entry *rt_table;
115   olsr_u32_t      hash;
116
117   hash = olsr_hashing(dst);
118
119   for(rt_table = routingtable[hash].next;
120       rt_table != &routingtable[hash];
121       rt_table = rt_table->next)
122     {
123       if (COMP_IP(&rt_table->rt_dst, dst))
124         {
125           return(rt_table);
126         }
127     }
128   return(NULL);
129   
130 }
131
132
133
134
135 /**
136  *Delete all the entries in the routing table hash
137  *
138  *@param table the routing hash table
139  *
140  *@return nada
141  */
142 void
143 olsr_free_routing_table(struct rt_entry *table)
144 {
145   olsr_u8_t       index;
146   
147   for(index=0;index<HASHSIZE;index++)
148     {
149       struct rt_entry *destination = table[index].next;
150
151       while(destination != &table[index])
152         {
153           struct rt_entry *dst_to_delete = destination;
154           destination = destination->next;
155
156           DEQUEUE_ELEM(dst_to_delete);
157           free(dst_to_delete);
158         }
159     }
160
161 }
162
163
164 /**
165  *Insert an 1 or 2 neighbor-entry into the routing table.
166  *
167  *@param dst the destination
168  *
169  *@return the new rt_entry struct
170  */
171 struct rt_entry *
172 olsr_insert_routing_table(union olsr_ip_addr *dst, union olsr_ip_addr *router, int metric)
173 {
174   struct rt_entry *new_route_entry, *rt_list;
175   olsr_u32_t       hash;
176
177   hash = olsr_hashing(dst);
178   rt_list = &routingtable[hash];
179
180   new_route_entry = olsr_malloc(sizeof(struct rt_entry), "Insert routing table");
181
182   COPY_IP(&new_route_entry->rt_dst, dst);
183   COPY_IP(&new_route_entry->rt_router, router);
184
185   new_route_entry->rt_metric = metric;
186   if(COMP_IP(dst, router))
187     /* Not GW */
188     new_route_entry->rt_flags = (RTF_UP|RTF_HOST);
189   else
190     new_route_entry->rt_flags = (RTF_UP|RTF_HOST|RTF_GATEWAY);
191
192   if(olsr_cnf->ip_version == AF_INET)
193     /* IPv4 */
194     new_route_entry->rt_mask.v4 = NETMASK_HOST;
195   else
196     /* IPv6 */
197     new_route_entry->rt_mask.v6 = 128;
198
199   /* Find interface */
200   if((new_route_entry->rt_if = get_interface_link_set(router)) == NULL)
201     {
202       fprintf(stderr, "ADD ROUTE 1: %s Could not find neighbor interface ", 
203               olsr_ip_to_string(dst));
204       fprintf(stderr, "for %s!!\n", 
205               olsr_ip_to_string(router));
206       free(new_route_entry);
207       return NULL;
208     }
209   
210
211   /* queue */
212   rt_list->next->prev = new_route_entry;
213   new_route_entry->next = rt_list->next;
214   rt_list->next = new_route_entry;
215   new_route_entry->prev = rt_list;
216   
217   return(new_route_entry);
218 }
219
220
221
222 /**
223  *Insert all the one hop neighbors in the routing table.
224  *
225  *@return
226  */
227 static int
228 olsr_fill_routing_table_with_neighbors()
229 {
230   olsr_u8_t              index;
231   struct rt_entry        *new_route_entry=NULL;
232
233 #ifdef DEBUG
234   olsr_printf(7, "FILL ROUTING TABLE WITH NEIGHBORS\n");
235 #endif
236
237   for(index=0;index<HASHSIZE;index++)
238     {
239       struct neighbor_entry *neighbor;
240
241       for(neighbor = neighbortable[index].next;
242           neighbor != &neighbortable[index];
243           neighbor=neighbor->next)     
244         {
245
246           if(neighbor->status == SYM)
247             {
248               static struct addresses addrs;
249               struct addresses *addrs2;
250
251               /*
252                *Insert all the neighbors addresses
253                */
254
255               COPY_IP(&addrs.address, &neighbor->neighbor_main_addr);
256               addrs.next = mid_lookup_aliases(&neighbor->neighbor_main_addr);
257               addrs2 = &addrs;
258
259               while(addrs2!=NULL)
260                 {
261 #ifdef DEBUG
262                   olsr_printf(7, "(ROUTE)Adding neighbor %s\n", olsr_ip_to_string(&addrs->address));
263 #endif
264                   /* New in 0.4.6 */
265                   new_route_entry = olsr_insert_routing_table(&addrs2->address, get_neighbor_nexthop(&addrs2->address), 1);
266               
267                   addrs2 = addrs2->next;
268                 }
269             }
270         }
271     }
272
273
274   return 1;
275 }
276
277
278 /**
279  *Insert all the two hop neighbors that is not already added
280  *in the routing table.
281  *
282  *@return a pointer to a destination_n linked-list of the neighbors.
283  */
284
285 static struct destination_n *
286 olsr_fill_routing_table_with_two_hop_neighbors()
287 {
288   struct destination_n *list_destination_n=NULL;
289   olsr_u8_t            index;
290
291   //printf("FILL ROUTING TABLE WITH TWO HOP NEIGHBORS\n");
292
293   for(index=0;index<HASHSIZE;index++)
294     {
295       struct neighbor_entry *neighbor;
296
297       for(neighbor = neighbortable[index].next;
298           neighbor != &neighbortable[index];
299           neighbor=neighbor->next)     
300         {
301           struct neighbor_2_list_entry *neigh_2_list; 
302
303           if(neighbor->status != SYM)
304             continue;
305           
306           /*
307            *Insert all the two hop neighbors
308            */
309           for(neigh_2_list = neighbor->neighbor_2_list.next;
310               neigh_2_list != &neighbor->neighbor_2_list;
311               neigh_2_list = neigh_2_list->next)
312             {
313               olsr_bool neighbor_ok;
314               union olsr_ip_addr *n2_addr;
315               static struct addresses addrs;
316               struct addresses *addrsp;
317               struct neighbor_list_entry *neighbors;
318               
319               n2_addr = &neigh_2_list->neighbor_2->neighbor_2_addr;
320               
321               if(olsr_lookup_routing_table(n2_addr))
322                 {
323 #ifdef DEBUG
324                   olsr_printf(7, "2hop: %s already added\n", olsr_ip_to_string(n2_addr));
325 #endif
326                   continue;
327                 }           
328
329               /*
330                * Neighbor is only added if avalible trough
331                * a 1 hop neighbor with willingness != WILL_NEVER
332                */
333               neighbor_ok = OLSR_FALSE;
334
335               for(neighbors = neigh_2_list->neighbor_2->neighbor_2_nblist.next;
336                   neighbors != &neigh_2_list->neighbor_2->neighbor_2_nblist;
337                   neighbors = neighbors->next)
338                 {
339                   if((neighbors->neighbor->status != NOT_NEIGH) &&
340                      (neighbors->neighbor->willingness != WILL_NEVER))
341                     neighbor_ok = OLSR_TRUE;
342                 }
343
344               if(!neighbor_ok)
345                 {
346                   olsr_printf(1, "Two hop neighbor %s not added - no one hop neighbors.\n",
347                               olsr_ip_to_string(n2_addr));
348                   continue;
349                 }
350
351               COPY_IP(&addrs.address, n2_addr);
352               addrs.next = mid_lookup_aliases(n2_addr);
353               addrsp = &addrs;
354
355               while(addrsp!=NULL)
356                 {
357                   struct rt_entry *new_route_entry = NULL;
358 #ifdef DEBUG
359                   olsr_printf(7, "(ROUTE)Adding neighbor %s\n", olsr_ip_to_string(&addrsp->address));
360 #endif
361                   /* New in 0.4.6 */
362                   new_route_entry = 
363                     olsr_insert_routing_table(&addrsp->address, 
364                                               get_neighbor_nexthop(&neighbor->neighbor_main_addr), 
365                                               2);
366
367                   if(new_route_entry != NULL)
368                     {
369                       struct destination_n *list_destination_tmp;
370                       list_destination_tmp = olsr_malloc(sizeof(struct destination_n), 
371                                                          "Fill rt table 2 hop tmp");
372                       
373                       list_destination_tmp->destination = new_route_entry;
374                       list_destination_tmp->next = list_destination_n;
375                       list_destination_n = list_destination_tmp;
376                     }
377                   addrsp = addrsp->next; 
378                 }
379             }
380         }
381
382     }
383
384   return(list_destination_n);
385 }
386
387
388
389
390 /**
391  *Recalculate the routing table
392  *
393  *@return nada
394  */
395 void 
396 olsr_calculate_routing_table()
397 {
398   struct destination_n *list_destination_n_1 = NULL;
399
400   olsr_move_route_table(routingtable, old_routes);
401
402   /* Add neighbors */
403   olsr_fill_routing_table_with_neighbors();
404   /* Add two hop enighbors - now they are the "outer rim" */
405   list_destination_n_1 = olsr_fill_routing_table_with_two_hop_neighbors();
406
407   while(list_destination_n_1!=NULL)
408     {
409       /* List_destination_n_1 holds the "outer rim" */
410       struct destination_n *list_destination_n = list_destination_n_1;
411
412       list_destination_n_1=NULL;
413
414       /* Next "outer rim" */
415       while(list_destination_n!=NULL)
416         {
417           struct destination_n *destination_n_1=NULL;
418           struct tc_entry      *topo_entry;
419
420           if((topo_entry = olsr_lookup_tc_entry(&list_destination_n->destination->rt_dst)) != NULL)
421             {
422               struct topo_dst *topo_dest = topo_entry->destinations.next;
423
424               /* Loop trough this nodes MPR selectors */
425               while(topo_dest != &topo_entry->destinations)
426                 {
427                   static struct addresses tmp_addrs;
428                   struct addresses *tmp_addrsp;
429                   
430                   /* Do not add ourselves */
431                   if(if_ifwithaddr(&topo_dest->T_dest_addr))
432                     {
433                       topo_dest=topo_dest->next;
434                       continue;
435                     }
436                   
437                   /* Find mid nodes */            
438                   COPY_IP(&tmp_addrs.address, &topo_dest->T_dest_addr);
439                   tmp_addrs.next = mid_lookup_aliases(&topo_dest->T_dest_addr);
440                   tmp_addrsp = &tmp_addrs;
441                   
442                   while(tmp_addrsp!=NULL)
443                     {
444                       if(NULL==olsr_lookup_routing_table(&tmp_addrsp->address))
445                         {
446                           /* PRINT OUT: Last Hop to Final Destination */
447                           /* The function ip_to_string has to be seperately */
448                           olsr_printf(3, "%s -> ", olsr_ip_to_string(&list_destination_n->destination->rt_dst));
449                           olsr_printf(3, "%s\n", olsr_ip_to_string(&tmp_addrsp->address) );
450                           
451                           destination_n_1 = olsr_malloc(sizeof(struct destination_n), 
452                                                         "Calculate routing table 2");
453                           
454                           /* Add this entry to the "outer rim" */
455                           destination_n_1->destination = 
456                             olsr_insert_routing_table(&tmp_addrsp->address, 
457                                                       &list_destination_n->destination->rt_router, 
458                                                       list_destination_n->destination->rt_metric+1);
459                           if(destination_n_1->destination != NULL)
460                             {
461                               destination_n_1->next=list_destination_n_1;
462                               list_destination_n_1=destination_n_1;
463                             }
464                         }
465                       tmp_addrsp = tmp_addrsp->next;
466                     }
467                   
468                   /* Next MPR selector */
469                   topo_dest=topo_dest->next;
470                   
471                 } /* End loop trought MPR selectors */
472               
473             } /* End check if already added */
474           
475           /* Delete this entry - do next */ 
476           destination_n_1 = list_destination_n;
477           list_destination_n = list_destination_n->next;
478           free(destination_n_1);
479           
480         }
481
482     }
483
484
485   if(olsr_cnf->debug_level > 5)
486     {
487       printf("************** TABLES ****************\n");
488       printf("Routing table:\n");
489       olsr_print_routing_table(routingtable);
490       printf("Old table:\n");
491       olsr_print_routing_table(old_routes);
492       printf("**************************************\n");
493     }
494
495   
496   /* Update routes */
497   olsr_update_kernel_routes();
498
499   olsr_free_routing_table(old_routes);
500 }
501
502
503
504
505 /**
506  *Check for a entry with a higher hopcount than
507  *a given value in a routing table
508  *
509  *@param routes the routingtable to look in
510  *@param net the network entry to look for
511  *@param metric the metric to check for
512  *
513  *@return the localted entry if found. NULL if not
514  */
515 static struct rt_entry *
516 olsr_check_for_higher_hopcount(struct rt_entry *routes, struct hna_net *net, olsr_u16_t metric)
517 {
518   int index;
519
520   for(index=0;index<HASHSIZE;index++)
521     {
522       struct rt_entry *tmp_routes;
523       /* All entries */
524       for(tmp_routes = routes[index].next;
525           tmp_routes != &routes[index];
526           tmp_routes = tmp_routes->next)
527         {
528           if(COMP_IP(&tmp_routes->rt_dst, &net->A_network_addr) &&
529              (memcmp(&tmp_routes->rt_mask, &net->A_netmask, netmask_size) == 0))
530             {
531               /* Found a entry */
532               if(tmp_routes->rt_metric > metric)
533                 return tmp_routes;
534               else
535                 return NULL;
536             }
537         }
538     }
539
540   return NULL;
541 }
542
543
544
545 /**
546  *Check for a entry with a lower or equal hopcount than
547  *a given value in a routing table
548  *
549  *@param routes the routingtable to look in
550  *@param net the network entry to look for
551  *@param metric the metric to check for
552  *
553  *@return the localted entry if found. NULL if not
554  */
555 struct rt_entry *
556 olsr_check_for_lower_hopcount(struct rt_entry *routes, struct hna_net *net, olsr_u16_t metric)
557 {
558   int index;
559
560   for(index=0;index<HASHSIZE;index++)
561     {
562       struct rt_entry *tmp_routes;
563       /* All entries */
564       for(tmp_routes = routes[index].next;
565           tmp_routes != &routes[index];
566           tmp_routes = tmp_routes->next)
567         {
568           if(COMP_IP(&tmp_routes->rt_dst, &net->A_network_addr) &&
569              (memcmp(&tmp_routes->rt_mask, &net->A_netmask, netmask_size) == 0))
570             {
571               /* Found a entry */
572               if(tmp_routes->rt_metric <= metric)
573                 return tmp_routes;
574               else
575                 return NULL;
576             }
577         }
578     }
579
580
581
582   return NULL;
583 }
584
585
586
587
588 /**
589  *Calculate the HNA routes
590  *
591  */
592 void
593 olsr_calculate_hna_routes()
594 {
595   olsr_u32_t index;
596
597 #ifdef DEBUG
598   olsr_printf(3, "Calculating HNA routes\n");
599 #endif
600
601   olsr_move_route_table(hna_routes, old_hna);
602
603   
604   for(index=0;index<HASHSIZE;index++)
605     {
606       struct hna_entry *tmp_hna;
607       /* All entries */
608       for(tmp_hna = hna_set[index].next;
609           tmp_hna != &hna_set[index];
610           tmp_hna = tmp_hna->next)
611         {
612           struct hna_net *tmp_net;
613           /* All networks */
614           for(tmp_net = tmp_hna->networks.next;
615               tmp_net != &tmp_hna->networks;
616               tmp_net = tmp_net->next)
617             {
618               struct rt_entry *tmp_rt, *new_rt;
619               //printf("HNA: checking %s -> ", olsr_ip_to_string(&tmp_hna->A_gateway_addr));
620               //printf("%s", olsr_ip_to_string(&tmp_net->A_network_addr));
621
622               /* If no route to gateway - skip */
623               if((tmp_rt = olsr_lookup_routing_table(&tmp_hna->A_gateway_addr)) == NULL)
624                 {
625                   continue;
626                 }
627
628               /* If there exists a better or equal entry - skip */
629               if(olsr_check_for_lower_hopcount(hna_routes, tmp_net, tmp_rt->rt_metric) != NULL)
630                 {
631                   continue;
632                 }
633
634               /* If we find an entry with higher hopcount we just edit it */
635               if((new_rt = olsr_check_for_higher_hopcount(hna_routes, tmp_net, tmp_rt->rt_metric)) != NULL)
636                 {
637                   /* Fill struct */
638                   /* Net */
639                   COPY_IP(&new_rt->rt_dst, &tmp_net->A_network_addr);
640                   new_rt->rt_mask = tmp_net->A_netmask;
641                   /* Gateway */
642                   COPY_IP(&new_rt->rt_router, &tmp_rt->rt_router);
643                   /* Metric */
644                   new_rt->rt_metric = tmp_rt->rt_metric;
645                   /* Flags */
646                   new_rt->rt_flags = RTF_UP | RTF_GATEWAY;
647
648                 }
649               /* If not - create a new one */
650               else
651                 {
652                   new_rt = olsr_malloc(sizeof(struct rt_entry), "New rt entry");
653
654                   /* Fill struct */
655                   /* Net */
656                   COPY_IP(&new_rt->rt_dst, &tmp_net->A_network_addr);
657                   new_rt->rt_mask = tmp_net->A_netmask;
658                   /* Gateway */
659                   COPY_IP(&new_rt->rt_router, &tmp_rt->rt_router);
660                   /* Metric */
661                   new_rt->rt_metric = tmp_rt->rt_metric;
662                   /* Flags */
663                   new_rt->rt_flags = RTF_UP | RTF_GATEWAY;
664               
665                   /* Find interface */
666                   if((new_rt->rt_if = get_interface_link_set(&new_rt->rt_router)) == NULL)
667                     {
668                       fprintf(stderr, "ADD ROUTE 2: %s Could not find neighbor interface!!!\n", 
669                               olsr_ip_to_string(&new_rt->rt_dst));
670                       /* This hopefullt never happens */
671                       free(new_rt);
672                     }
673                   else
674                     {
675                       olsr_u32_t  hna_hash;
676
677                       /* Queue HASH will almost always be 0 */
678                       hna_hash = olsr_hashing(&tmp_net->A_network_addr);
679                       hna_routes[hna_hash].next->prev = new_rt;
680                       new_rt->next = hna_routes[hna_hash].next;
681                       hna_routes[hna_hash].next = new_rt;
682                       new_rt->prev = &hna_routes[hna_hash];
683                     }
684                 }
685             }
686         }
687     }
688
689   /* Update kernel */
690   olsr_update_kernel_hna_routes();
691
692   if(olsr_cnf->debug_level > 2)
693     {
694       olsr_printf(3, "HNA table:\n");
695       olsr_print_routing_table(hna_routes);
696     }
697
698   olsr_free_routing_table(old_hna);
699
700 }
701
702
703
704
705
706 /**
707  *Print the routingtable to STDOUT
708  *
709  */
710
711 void
712 olsr_print_routing_table(struct rt_entry *table)
713 {
714
715   olsr_u8_t index;
716
717   printf("ROUTING TABLE\n");
718   printf("DESTINATION\tNEXT HOP\n");
719   for(index=0;index<HASHSIZE;index++)
720     {
721       struct rt_entry *destination;
722       for(destination = table[index].next;
723           destination != &table[index];
724           destination = destination->next)
725         {
726           printf("%s\t", olsr_ip_to_string(&destination->rt_dst));
727           printf("%s\n", olsr_ip_to_string(&destination->rt_router));
728         }
729     }
730   fflush(stdout);
731 }
732
733
734
735
736