a1f3fff77f13f87a973142d447380ebb82258557
[olsrd.git] / src / process_package.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: process_package.c,v 1.29 2005/02/04 06:30:46 kattemat Exp $
40  */
41
42
43 #include "defs.h"
44 #include "process_package.h"
45 #include "hysteresis.h"
46 #include "two_hop_neighbor_table.h"
47 #include "tc_set.h"
48 #include "mpr_selector_set.h"
49 #include "mid_set.h"
50 #include "olsr.h"
51 #include "parser.h"
52 #include "duplicate_set.h"
53 #include "rebuild_packet.h"
54
55
56 /**
57  *Initializing the parser functions we are using
58  */
59 void
60 olsr_init_package_process()
61 {
62   if (olsr_cnf->lq_level == 0)
63     {
64       olsr_parser_add_function(&olsr_process_received_hello, HELLO_MESSAGE, 1);
65       olsr_parser_add_function(&olsr_process_received_tc, TC_MESSAGE, 1);
66     }
67
68   else
69     {
70       olsr_parser_add_function(&olsr_input_lq_hello, LQ_HELLO_MESSAGE, 1);
71       olsr_parser_add_function(&olsr_input_lq_tc, LQ_TC_MESSAGE, 1);
72     }
73
74   olsr_parser_add_function(&olsr_process_received_mid, MID_MESSAGE, 1);
75   olsr_parser_add_function(&olsr_process_received_hna, HNA_MESSAGE, 1);
76 }
77
78 void
79 olsr_hello_tap(struct hello_message *message, struct interface *in_if,
80                union olsr_ip_addr *from_addr)
81 {
82   struct link_entry         *link;
83   struct neighbor_entry     *neighbor;
84   struct hello_neighbor *walker;
85   double saved_lq;
86   double rel_lq;
87
88   /*
89    * Update link status
90    */
91   link = update_link_entry(&in_if->ip_addr, from_addr, message, in_if);
92
93   if (olsr_cnf->lq_level > 0)
94     {
95       // just in case our neighbor has changed its HELLO interval
96
97       olsr_update_packet_loss_hello_int(link, message->htime);
98
99       // find the input interface in the list of neighbor interfaces
100
101       for (walker = message->neighbors; walker != NULL; walker = walker->next)
102         if (COMP_IP(&walker->address, &in_if->ip_addr))
103           break;
104
105       // the current reference link quality
106
107       saved_lq = link->saved_neigh_link_quality;
108
109       if (saved_lq == 0.0)
110         saved_lq = -1.0;
111
112       // memorize our neighbour's idea of the link quality, so that we
113       // know the link quality in both directions
114
115       if (walker != NULL)
116         link->neigh_link_quality = walker->link_quality;
117
118       else
119         link->neigh_link_quality = 0.0;
120
121       // if the link quality has changed by more than 10 percent,
122       // print the new link quality table
123
124       rel_lq = link->neigh_link_quality / saved_lq;
125
126       if (rel_lq > 1.1 || rel_lq < 0.9)
127         {
128           link->saved_neigh_link_quality = link->neigh_link_quality;
129
130           changes_neighborhood = OLSR_TRUE;
131           changes_topology = OLSR_TRUE;
132
133           // create a new ANSN
134
135           // XXX - we should check whether we actually
136           // announce this neighbour
137
138           changes = OLSR_TRUE;
139         }
140     }
141   
142   neighbor = link->neighbor;
143
144   /*
145    * Hysteresis
146    */
147   if(olsr_cnf->use_hysteresis)
148     {
149       /* Update HELLO timeout */
150       //printf("MESSAGE HTIME: %f\n", message->htime);
151       olsr_update_hysteresis_hello(link, message->htime);
152     }
153
154   /* Check if we are chosen as MPR */
155   if(olsr_lookup_mpr_status(message, in_if))
156     /* source_addr is always the main addr of a node! */
157     olsr_update_mprs_set(&message->source_addr, (float)message->vtime);
158
159
160
161   /* Check willingness */
162   if(neighbor->willingness != message->willingness)
163     {
164       olsr_printf(1, "Willingness for %s changed from %d to %d - UPDATING\n", 
165                   olsr_ip_to_string(&neighbor->neighbor_main_addr),
166                   neighbor->willingness,
167                   message->willingness);
168       /*
169        *If willingness changed - recalculate
170        */
171       neighbor->willingness = message->willingness;
172       changes_neighborhood = OLSR_TRUE;
173       changes_topology = OLSR_TRUE;
174     }
175
176
177   /* Don't register neighbors of neighbors that announces WILL_NEVER */
178   if(neighbor->willingness != WILL_NEVER)
179     olsr_process_message_neighbors(neighbor, message);
180
181   /* Process changes immedeatly in case of MPR updates */
182   olsr_process_changes();
183
184   olsr_free_hello_packet(message);
185
186   return;
187 }
188
189 /**
190  *Processes a received HELLO message. 
191  *
192  *@param m the incoming OLSR message
193  *@return 0 on sucess
194  */
195
196 void
197 olsr_process_received_hello(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *from_addr)
198 {
199   struct hello_message      message;
200
201   hello_chgestruct(&message, m);
202
203   olsr_hello_tap(&message, in_if, from_addr);
204 }
205
206 void
207 olsr_tc_tap(struct tc_message *message, struct interface *in_if,
208             union olsr_ip_addr *from_addr, union olsr_message *m)
209 {
210   struct tc_mpr_addr              *mpr;
211   struct tc_entry                 *tc_last;
212
213   if(!olsr_check_dup_table_proc(&message->originator, 
214                                 message->packet_seq_number))
215     {
216       goto forward;
217     }
218
219   olsr_printf(3, "Processing TC from %s\n",
220               olsr_ip_to_string(&message->originator));
221
222   /*
223    *      If the sender interface (NB: not originator) of this message
224    *      is not in the symmetric 1-hop neighborhood of this node, the
225    *      message MUST be discarded.
226    */
227
228   if(check_neighbor_link(from_addr) != SYM_LINK)
229     {
230       olsr_printf(2, "Received TC from NON SYM neighbor %s\n",
231                   olsr_ip_to_string(from_addr));
232       olsr_free_tc_packet(message);
233       return;
234     }
235
236   if(olsr_cnf->debug_level > 2)
237     {
238       mpr = message->multipoint_relay_selector_address;
239       olsr_printf(3, "mpr_selector_list:[");
240
241       while(mpr!=NULL)
242         {
243           olsr_printf(3, "%s:", olsr_ip_to_string(&mpr->address));
244           mpr=mpr->next;
245         }
246
247       olsr_printf(3, "]\n");
248     }
249
250   tc_last = olsr_lookup_tc_entry(&message->originator);
251    
252   if(tc_last != NULL)
253     {
254       /* Update entry */
255
256       /* Delete destinations with lower ANSN */
257       if(olsr_tc_delete_mprs(tc_last, message))
258         changes_topology = OLSR_TRUE; 
259
260       /* Update destinations */
261       if(olsr_tc_update_mprs(tc_last, message))
262         changes_topology = OLSR_TRUE;
263
264       /* Delete possible empty TC entry */
265       if(changes_topology)
266         olsr_tc_delete_entry_if_empty(tc_last);
267     }
268
269   else
270     {
271       /*if message is empty then skip it */
272       if(message->multipoint_relay_selector_address != NULL)
273         {
274           /* New entry */
275           tc_last = olsr_add_tc_entry(&message->originator);      
276           
277           /* Update destinations */
278           olsr_tc_update_mprs(tc_last, message);
279           
280           changes_topology = OLSR_TRUE;
281         }
282       else
283         {
284           olsr_printf(3, "Dropping empty TC from %s\n",
285                       olsr_ip_to_string(&message->originator)); 
286         }
287     }
288
289   /* Process changes */
290   //olsr_process_changes();
291
292  forward:
293
294   olsr_forward_message(m, 
295                        &message->originator, 
296                        message->packet_seq_number, 
297                        in_if,
298                        from_addr);
299
300   olsr_free_tc_packet(message);
301
302   return;
303 }
304
305 /**
306  *Process a received TopologyControl message
307  *
308  *
309  *@param m the incoming OLSR message
310  *@return 0 on success
311  */
312 void
313 olsr_process_received_tc(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *from_addr)
314
315   struct tc_message               message;
316
317   tc_chgestruct(&message, m, from_addr);
318
319   olsr_tc_tap(&message, in_if, from_addr, m);
320 }
321
322
323
324
325
326
327 /**
328  *Process a received(and parsed) MID message
329  *For every address check if there is a topology node
330  *registered with it and update its addresses.
331  *
332  *@param m the OLSR message received.
333  *@return 1 on success
334  */
335
336 void
337 olsr_process_received_mid(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *from_addr)
338 {
339   struct mid_alias *tmp_adr;
340   struct mid_message message;
341
342   mid_chgestruct(&message, m);
343
344   if(!olsr_check_dup_table_proc(&message.mid_origaddr, 
345                                 message.mid_seqno))
346     {
347       goto forward;
348     }
349
350 #ifdef DEBUG
351   olsr_printf(5, "Processing MID from %s...\n", olsr_ip_to_string(&message.mid_origaddr));
352 #endif
353   tmp_adr = message.mid_addr;
354
355   /*
356    *      If the sender interface (NB: not originator) of this message
357    *      is not in the symmetric 1-hop neighborhood of this node, the
358    *      message MUST be discarded.
359    */
360
361   if(check_neighbor_link(from_addr) != SYM_LINK)
362     {
363       olsr_printf(2, "Received MID from NON SYM neighbor %s\n", olsr_ip_to_string(from_addr));
364       olsr_free_mid_packet(&message);
365       return;
366     }
367
368   /* Update the timeout of the MID */
369   olsr_update_mid_table(&message.mid_origaddr, (float)message.vtime);
370
371   while(tmp_adr)
372     {
373       if(!mid_lookup_main_addr(&tmp_adr->alias_addr))
374         {
375           olsr_printf(1, "MID new: (%s, ", olsr_ip_to_string(&message.mid_origaddr));
376           olsr_printf(1, "%s)\n", olsr_ip_to_string(&tmp_adr->alias_addr));
377           insert_mid_alias(&message.mid_origaddr, &tmp_adr->alias_addr, (float)message.vtime);
378         }
379
380
381       tmp_adr = tmp_adr->next;
382     } 
383   
384  forward:  
385   olsr_forward_message(m, 
386                        &message.mid_origaddr, 
387                        message.mid_seqno, 
388                        in_if,
389                        from_addr);
390   olsr_free_mid_packet(&message);
391
392   return;
393 }
394
395
396
397
398
399 /**
400  *Process incoming HNA message.
401  *Forwards the message if that is to be done.
402  *
403  *@param m the incoming OLSR message
404  *the OLSR message.
405  *@return 1 on success
406  */
407
408 void
409 olsr_process_received_hna(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *from_addr)
410 {
411   struct hna_net_addr  *hna_tmp;
412   struct  hna_message message;
413
414 #ifdef DEBUG
415   olsr_printf(5, "Processing HNA\n");
416 #endif
417
418   hna_chgestruct(&message, m);
419
420   if(!olsr_check_dup_table_proc(&message.originator, 
421                                 message.packet_seq_number))
422     {
423       goto forward;
424     }
425
426   hna_tmp = message.hna_net;
427
428   /*
429    *      If the sender interface (NB: not originator) of this message
430    *      is not in the symmetric 1-hop neighborhood of this node, the
431    *      message MUST be discarded.
432    */
433   if(check_neighbor_link(from_addr) != SYM_LINK)
434     {
435       olsr_printf(2, "Received HNA from NON SYM neighbor %s\n", olsr_ip_to_string(from_addr));
436       olsr_free_hna_packet(&message);
437       return;
438     }
439
440   while(hna_tmp)
441     {
442       olsr_update_hna_entry(&message.originator, &hna_tmp->net, &hna_tmp->netmask, (float)message.vtime); 
443       
444       hna_tmp = hna_tmp->next;
445     }
446
447  forward:
448   olsr_forward_message(m, 
449                        &message.originator, 
450                        message.packet_seq_number, 
451                        in_if,
452                        from_addr);
453   olsr_free_hna_packet(&message);
454
455   return;
456 }
457
458
459
460
461
462
463
464 /**
465  *Processes an list of neighbors from an incoming HELLO message.
466  *@param neighbor the neighbor who sendt the message.
467  *@param message the HELLO message
468  *@return nada
469  */
470 void
471 olsr_process_message_neighbors(struct neighbor_entry *neighbor,
472                                struct hello_message *message)
473 {
474   struct hello_neighbor        *message_neighbors;
475
476   for(message_neighbors = message->neighbors;
477       message_neighbors != NULL;
478       message_neighbors = message_neighbors->next)
479     {
480       union olsr_ip_addr      *neigh_addr;
481       struct neighbor_2_entry *two_hop_neighbor;
482
483       /*
484        *check all interfaces
485        *so that we don't add ourselves to the
486        *2 hop list
487        *IMPORTANT!
488        */
489       if(if_ifwithaddr(&message_neighbors->address) != NULL)
490         continue;
491
492       /* Get the main address */
493       neigh_addr = mid_lookup_main_addr(&message_neighbors->address);
494
495       if (neigh_addr != NULL)
496         COPY_IP(&message_neighbors->address, neigh_addr);
497       
498       if(((message_neighbors->status == SYM_NEIGH) ||
499           (message_neighbors->status == MPR_NEIGH)))
500         {
501           struct neighbor_2_list_entry *two_hop_neighbor_yet =
502             olsr_lookup_my_neighbors(neighbor, &message_neighbors->address);
503 #ifdef DEBUG
504           olsr_printf(7, "\tProcessing %s\n", olsr_ip_to_string(&message_neighbors->address));
505 #endif
506           if (two_hop_neighbor_yet != NULL)
507             {
508               /* Updating the holding time for this neighbor */
509               two_hop_neighbor_yet->neighbor_2_timer = GET_TIMESTAMP(message->vtime*1000);
510               two_hop_neighbor = two_hop_neighbor_yet->neighbor_2;
511             }
512           else
513             {
514               two_hop_neighbor =
515                 olsr_lookup_two_hop_neighbor_table(&message_neighbors->address);
516               if (two_hop_neighbor == NULL)
517                 {
518 #ifdef DEBUG
519                   olsr_printf(5, 
520                               "Adding 2 hop neighbor %s\n\n", 
521                               olsr_ip_to_string(&message_neighbors->address)); 
522 #endif
523                   changes_neighborhood = OLSR_TRUE;
524                   changes_topology = OLSR_TRUE;
525
526                   two_hop_neighbor =
527                     olsr_malloc(sizeof(struct neighbor_2_entry),
528                                 "Process HELLO");
529                   
530                   two_hop_neighbor->neighbor_2_nblist.next =
531                     &two_hop_neighbor->neighbor_2_nblist;
532
533                   two_hop_neighbor->neighbor_2_nblist.prev =
534                     &two_hop_neighbor->neighbor_2_nblist;
535
536                   two_hop_neighbor->neighbor_2_pointer = 0;
537                   
538                   COPY_IP(&two_hop_neighbor->neighbor_2_addr,
539                           &message_neighbors->address);
540
541                   olsr_insert_two_hop_neighbor_table(two_hop_neighbor);
542
543                   olsr_linking_this_2_entries(neighbor, two_hop_neighbor,
544                                               (float)message->vtime);
545                 }
546               else
547                 {
548                   /*
549                     linking to this two_hop_neighbor entry
550                   */    
551                   changes_neighborhood = OLSR_TRUE;
552                   changes_topology = OLSR_TRUE;
553                   
554                   olsr_linking_this_2_entries(neighbor, two_hop_neighbor,
555                                               (float)message->vtime); 
556                 }
557             }
558
559           if (olsr_cnf->lq_level > 0)
560             {
561               struct neighbor_list_entry *walker;
562               struct link_entry *link;
563
564               link = olsr_neighbor_best_link(&neighbor->neighbor_main_addr);
565               // loop through the one-hop neighbors that see this
566               // two hop neighbour
567
568               for (walker = two_hop_neighbor->neighbor_2_nblist.next;
569                    walker != &two_hop_neighbor->neighbor_2_nblist;
570                    walker = walker->next)
571                 {
572                   // have we found the one-hop neighbor that sent the
573                   // HELLO message that we're current processing?
574
575                   if (walker->neighbor == neighbor)
576                     {
577                       double saved_lq, rel_lq;
578
579                       // saved previous total link quality
580
581                       saved_lq = walker->saved_path_link_quality;
582
583                       if (saved_lq == 0.0)
584                         saved_lq = -1.0;
585
586                       // path link quality = link quality between us
587                       // and our one-hop neighbor x link quality between
588                       // our one-hop neighbor and the two-hop neighbor
589
590                       // let's compare this to ETX:
591
592                       // 1 / LQ1 + 1 / LQ2 < 1 / LQ3 + 1 / LQ4 <=>
593                       // LQ1 * LQ2 > LQ3 * LQ4
594
595                       // so comparing path link quality values with ">" is
596                       // equivalent to comparing ETX values with "<"
597
598                       walker->path_link_quality =
599                         link->loss_link_quality * link->neigh_link_quality *
600                         message_neighbors->link_quality *
601                         message_neighbors->neigh_link_quality;
602
603                       // if the link quality has changed by more than 10
604                       // percent, signal
605
606                       rel_lq = walker->path_link_quality / saved_lq;
607
608                       if (rel_lq > 1.1 || rel_lq < 0.9)
609                         {
610                           walker->saved_path_link_quality =
611                             walker->path_link_quality;
612
613                           changes_neighborhood = OLSR_TRUE;
614                           changes_topology = OLSR_TRUE;
615                         }
616                     }
617                 }
618             }
619         }
620     }
621 }
622
623
624
625
626
627
628
629
630 /**
631  *Links a one-hop neighbor with a 2-hop neighbor.
632  *
633  *@param neighbor the 1-hop neighbor
634  *@param two_hop_neighbor the 2-hop neighbor
635  *@return nada
636  */
637 void
638 olsr_linking_this_2_entries(struct neighbor_entry *neighbor,struct neighbor_2_entry *two_hop_neighbor, float vtime)
639 {
640   struct neighbor_list_entry    *list_of_1_neighbors;
641   struct neighbor_2_list_entry  *list_of_2_neighbors;
642
643   list_of_1_neighbors = olsr_malloc(sizeof(struct neighbor_list_entry), "Link entries 1");
644
645   list_of_2_neighbors = olsr_malloc(sizeof(struct neighbor_2_list_entry), "Link entries 2");
646
647   list_of_1_neighbors->neighbor = neighbor;
648
649   list_of_1_neighbors->path_link_quality = 0.0;
650   list_of_1_neighbors->saved_path_link_quality = 0.0;
651
652   /* Queue */
653   two_hop_neighbor->neighbor_2_nblist.next->prev = list_of_1_neighbors;
654   list_of_1_neighbors->next = two_hop_neighbor->neighbor_2_nblist.next;
655   two_hop_neighbor->neighbor_2_nblist.next = list_of_1_neighbors;
656   list_of_1_neighbors->prev = &two_hop_neighbor->neighbor_2_nblist;
657
658
659   list_of_2_neighbors->neighbor_2 = two_hop_neighbor;
660   
661   list_of_2_neighbors->neighbor_2_timer = GET_TIMESTAMP(vtime*1000);
662
663   /* Queue */
664   neighbor->neighbor_2_list.next->prev = list_of_2_neighbors;
665   list_of_2_neighbors->next = neighbor->neighbor_2_list.next;
666   neighbor->neighbor_2_list.next = list_of_2_neighbors;
667   list_of_2_neighbors->prev = &neighbor->neighbor_2_list;
668   
669   /*increment the pointer counter*/
670   two_hop_neighbor->neighbor_2_pointer++;
671 }
672
673
674
675
676
677
678 /**
679  *Check if a hello message states this node as a MPR.
680  *
681  *@param message the message to check
682  *@param n_link the buffer to put the link status in
683  *@param n_status the buffer to put the status in
684  *
685  *@return 1 if we are selected as MPR 0 if not
686  */
687 int
688 olsr_lookup_mpr_status(struct hello_message *message, struct interface *in_if)
689 {
690   
691   struct hello_neighbor  *neighbors;
692
693   neighbors=message->neighbors;
694   
695   while(neighbors!=NULL)
696     {  
697       //printf("(linkstatus)Checking %s ",olsr_ip_to_string(&neighbors->address));
698       //printf("against %s\n",olsr_ip_to_string(&main_addr));
699
700
701     if(olsr_cnf->ip_version == AF_INET)
702       { 
703         /* IPv4 */  
704         if(COMP_IP(&neighbors->address, &in_if->ip_addr))
705           {
706             //printf("ok");
707             if((neighbors->link == SYM_LINK) && (neighbors->status == MPR_NEIGH))
708               return 1;
709             
710             return 0;
711           }
712       }
713     else
714       { 
715         /* IPv6 */  
716         if(COMP_IP(&neighbors->address, &in_if->int6_addr.sin6_addr))
717           {
718             //printf("ok");
719             if((neighbors->link == SYM_LINK) && (neighbors->status == MPR_NEIGH))
720               return 1;
721             
722             return 0;
723           }
724       }
725  
726       neighbors = neighbors->next; 
727     }
728
729   /* Not found */
730   return 0;
731 }