route_table.c

説明を見る。
00001 /*
00002                Kernel AODV  v2.1
00003 National Institute of Standards and Technology
00004                Luke Klein-Berndt
00005 -----------------------------------------------------
00006   Version 2.1 new features:
00007      * Much more stable!
00008      * Added locks around important areas
00009      * Multihop Internet gatewaying now works
00010      * Multicast hack added
00011      * Many bug fixes!
00012 -----------------------------------------------------
00013 
00014 Originally based upon MadHoc code. I am not
00015 sure how much of it is left anymore, but MadHoc
00016 proved to be a great starting point.
00017 
00018 MadHoc was written by - Fredrik Lilieblad,
00019 Oskar Mattsson, Petra Nylund, Dan Ouchterlony
00020 and Anders Roxenhag Mail: mad-hoc@flyinglinux.net
00021 
00022 This software is Open Source under the GNU General Public Licence.
00023 
00024 */
00025 
00026 #include "route_table.h"
00027 /****************************************************
00028 
00029    route_table
00030 ----------------------------------------------------
00031 A table which contains all the needed routing
00032 information to contact other nodes
00033 ****************************************************/
00034 
00035 extern u_int32_t g_broadcast_ip;
00036 extern struct route_table_entry *g_my_entry;
00037 struct route_table_entry *route_table;
00038 rwlock_t route_lock = RW_LOCK_UNLOCKED;
00039 
00040 /* Declaration of internal procedures */
00041 struct precursor_entry *find_precursor_entry(struct route_table_entry* tmp_entry, u_int32_t tmp_ip);
00042 
00043 void route_read_lock()
00044 {
00045   read_lock_bh(&route_lock);
00046 }
00047 
00048 void route_read_unlock()
00049 {
00050   read_unlock_bh(&route_lock);
00051 }
00052 
00053 void route_write_lock()
00054 {
00055   write_lock_bh(&route_lock);
00056 }
00057 
00058 void route_write_unlock()
00059 {
00060   write_unlock_bh(&route_lock);
00061 }
00062 
00063 
00064 
00065 /*
00066  * init_rt
00067  *
00068  * Description:
00069  *   Initializes the main routing table by creating a
00070  *   head for the list.
00071  *
00072  * Arguments: void
00073  *
00074  * Return: void
00075  */
00076 
00077 /****************************************************
00078 
00079    init_route_table
00080 ----------------------------------------------------
00081 Initalizes the route table so it can hold data
00082 ****************************************************/
00083 int init_route_table( void)
00084 {
00085     route_table=NULL;
00086     return 0;
00087 }
00088 
00089 
00090 /****************************************************
00091 
00092    get_first_route_table_entry
00093 ----------------------------------------------------
00094 Returns the first entry in the routing table
00095 ****************************************************/
00096 struct route_table_entry *get_first_route_table_entry()
00097 {
00098     return route_table;
00099 }
00100 
00101 
00102 
00103 
00104 /****************************************************
00105 
00106    find_inactive_route_table_entries
00107 ----------------------------------------------------
00108 Finds expired route table entries and deletes
00109 them
00110 ****************************************************/
00111 void find_inactive_route_table_entries()
00112 {
00113     struct route_table_entry *tmp_route;
00114     struct route_table_entry *dead_route;
00115     u_int64_t curr_time;
00116 
00117     curr_time=getcurrtime();
00118 
00119 
00120     /*lock table*/
00121     route_write_lock();
00122 
00123     tmp_route=route_table;
00124 
00125 
00126 
00127     while(tmp_route!=NULL)
00128     {
00129         if ((tmp_route->lifetime < curr_time) && !tmp_route->self_route)
00130         {
00131 
00132             if(tmp_route->route_valid==FALSE)
00133             {
00134 
00135                 printk(KERN_INFO "ROUTE_TABLE: Route: %s has expired and will be deleted from the Route Table\n",inet_ntoa(tmp_route->dst_ip));
00136 
00137 
00138 
00139                 /* step back before deletion */
00140                 dead_route=tmp_route;
00141 
00142                 tmp_route=tmp_route->next;
00143 
00144                 if (route_table==dead_route)
00145                     route_table=dead_route->next;
00146 
00147                 if (dead_route->prev!=NULL)
00148                     dead_route->prev->next = dead_route->next;
00149 
00150 
00151                 if (dead_route->next!=NULL)
00152                     dead_route->next->prev = dead_route->prev;
00153 
00154                 delete_precursors_from_route_table_entry(dead_route);
00155                 kfree(dead_route);
00156             }
00157             else
00158               {
00159                 if(tmp_route->self_route==0)
00160                   {
00161                     if(tmp_route->next_hop == tmp_route->dst_ip)
00162                       {
00163                         route_write_unlock();
00164                         /* break also performs route_expiry */
00165                         link_break(tmp_route->dst_ip);
00166                         route_write_lock();
00167 
00168                       }
00169                     else
00170                       {
00171 
00172                         printk(KERN_INFO "AODV: Route Table Entry: %s is inactive  and will be marked as expired!!\n",inet_ntoa(tmp_route->dst_ip));
00173                         route_write_unlock();
00174                         route_expiry(tmp_route);
00175                         route_write_lock();
00176                     }
00177                 }
00178                 
00179                 tmp_route=tmp_route->next;
00180                 
00181 
00182             }
00183         }
00184         else
00185           {
00186 
00187             tmp_route=tmp_route->next;
00188             
00189 
00190           }
00191 
00192     }
00193 
00194     route_write_unlock();
00195 
00196 
00197 }
00198 
00199 
00200 /****************************************************
00201 
00202    create_route_table_entry
00203 ----------------------------------------------------
00204 Allocates memory for a new route table entry,
00205 places that entry into the linked list and then
00206 returns a pointer for that entry
00207 ****************************************************/
00208 struct route_table_entry *create_route_table_entry()
00209 {
00210     struct route_table_entry *tmp_entry;
00211 
00212     /* Allocate memory for new entry */
00213 
00214     if((tmp_entry = (struct route_table_entry*)kmalloc(sizeof(struct route_table_entry),GFP_ATOMIC)) == NULL)
00215     {
00216 
00217         printk(KERN_WARNING "AODV: Error getting memory for inserting new rtentry\n");
00218 
00219 
00220         return NULL;
00221     }
00222 
00223 
00224     tmp_entry->precursors=NULL;
00225     tmp_entry->self_route=FALSE;
00226     tmp_entry->rreq_id=0;
00227     tmp_entry->link=255;
00228     tmp_entry->dev=NULL;
00229     tmp_entry->route_valid=FALSE;
00230     tmp_entry->route_seq_valid=FALSE;
00231     tmp_entry->prev = NULL;
00232   
00233     /*lock table*/
00234     route_write_lock();
00235 
00236     
00237     if (route_table!=NULL)
00238         route_table->prev = tmp_entry;
00239     tmp_entry->next = route_table;
00240     route_table=tmp_entry;
00241 
00242     /*unlock table*/
00243     route_write_unlock();
00244 
00245 
00246     return tmp_entry;
00247 }
00248 
00249 
00250 /****************************************************
00251 
00252    find_route_table_entry
00253 ----------------------------------------------------
00254 Returns a pointer to a route table entry which
00255 matches the ip address
00256 ****************************************************/
00257 struct route_table_entry *fast_find_route_table_entry(u_int32_t tmp_ip)
00258 {
00259     struct route_table_entry *tmp_entry;
00260 
00261     tmp_entry=route_table;
00262 
00263     while(tmp_entry!=NULL)
00264     {
00265         if(tmp_entry->dst_ip == tmp_ip)
00266         {
00267 
00268           return tmp_entry;
00269         }
00270         
00271         tmp_entry=tmp_entry->next;
00272     }
00273 
00274 
00275     return NULL;
00276 }
00277 
00278 /****************************************************
00279 
00280    find_route_table_entry
00281 ----------------------------------------------------
00282 Returns a pointer to a route table entry which
00283 matches the ip address
00284 ****************************************************/
00285 struct route_table_entry *find_route_table_entry(u_int32_t tmp_ip)
00286 {
00287     struct route_table_entry *tmp_entry;
00288 
00289     /*lock table*/
00290     route_read_lock();
00291 
00292     tmp_entry=route_table;
00293 
00294     while(tmp_entry!=NULL)
00295     {
00296         if(tmp_entry->dst_ip == tmp_ip)
00297         {
00298           /*unlock table*/
00299           route_read_unlock();
00300 
00301           return tmp_entry;
00302         }
00303         
00304         tmp_entry=tmp_entry->next;
00305     }
00306 
00307     /*unlock table*/
00308     route_read_unlock();
00309 
00310     return NULL;
00311 }
00312 
00313 
00314 
00315 /****************************************************
00316 
00317    delete_route_table_entry
00318 ----------------------------------------------------
00319 Deletes a route table entry which matches the
00320 ip address
00321 ****************************************************/
00322 int delete_route_table_entry(u_int32_t tmp_ip)
00323 {
00324     struct route_table_entry *tmp_entry;
00325 
00326     if ((tmp_entry=find_route_table_entry(tmp_ip))==NULL)
00327         return -ENOENT;
00328 
00329     delete_kernel_route_entry(tmp_entry->dst_ip,tmp_entry->next_hop);
00330 
00331     /*lock table*/
00332     route_write_lock();    
00333 
00334     if (route_table==tmp_entry)
00335         route_table=tmp_entry->next;
00336 
00337 
00338     if (tmp_entry->prev!=NULL)
00339         tmp_entry->prev->next = tmp_entry->next;
00340 
00341     if (tmp_entry->next!=NULL)
00342         tmp_entry->next->prev = tmp_entry->prev;
00343 
00344     /*unlock table*/
00345     route_write_unlock();    
00346 
00347     delete_precursors_from_route_table_entry(tmp_entry);
00348     kfree(tmp_entry);
00349     return 0;
00350 }
00351 
00352 
00353 /****************************************************
00354 
00355    read_monitor_proc
00356 ----------------------------------------------------
00357 Prints out the route table to a buffer. It is called
00358 when the related proc file is read
00359 ****************************************************/
00360 int read_monitor_proc(char *buffer, char **buffer_location, off_t offset, int buffer_length,int *eof,void *data)
00361 {
00362     static char *my_buffer;
00363     char temp_buffer[80];
00364     struct route_table_entry *tmp_entry;
00365     int len;
00366 
00367     char dst[16];
00368     char hop[16];
00369 
00370     /*lock table*/
00371     route_read_lock();    
00372 
00373 
00374     tmp_entry=route_table;
00375 
00376     my_buffer=buffer;
00377 
00378 
00379     sprintf(my_buffer,"\n");
00380     tmp_entry=route_table;
00381     while (tmp_entry!=NULL)
00382     {
00383         if (tmp_entry->hop_count==0)
00384         {
00385             strcpy(hop,inet_ntoa(tmp_entry->next_hop));
00386             strcpy(dst,inet_ntoa(tmp_entry->dst_ip));
00387             sprintf(temp_buffer,"%s %s %d \n",dst ,hop,tmp_entry->hop_count);
00388             strcat(my_buffer,temp_buffer);
00389         }
00390         tmp_entry=tmp_entry->next;
00391     }
00392     tmp_entry=route_table;
00393     while (tmp_entry!=NULL)
00394     {
00395         if (tmp_entry->hop_count==1)
00396         {
00397             strcpy(hop,inet_ntoa(tmp_entry->next_hop));
00398             strcpy(dst,inet_ntoa(tmp_entry->dst_ip));
00399             sprintf(temp_buffer,"%s %s %d \n",dst ,hop,tmp_entry->hop_count);
00400             strcat(my_buffer,temp_buffer);
00401 
00402         }
00403         tmp_entry=tmp_entry->next;
00404     }
00405     tmp_entry=route_table;
00406     while (tmp_entry!=NULL)
00407     {
00408         if (tmp_entry->hop_count>1)
00409         {
00410             strcpy(hop,inet_ntoa(tmp_entry->next_hop));
00411             strcpy(dst,inet_ntoa(tmp_entry->dst_ip));
00412             sprintf(temp_buffer,"%s %s %d \n",dst ,hop,tmp_entry->hop_count);
00413             strcat(my_buffer,temp_buffer);
00414 
00415         }
00416         tmp_entry=tmp_entry->next;
00417     }
00418 
00419     /*unlock table*/
00420     route_read_unlock();    
00421 
00422 
00423     len = strlen(my_buffer);
00424     *buffer_location = my_buffer + offset;
00425     len -= offset;
00426     if (len > buffer_length)
00427         len = buffer_length;
00428     else if (len < 0)
00429         len = 0;
00430     return len;
00431 }
00432 /****************************************************
00433 
00434    read_route_table_proc
00435 ----------------------------------------------------
00436 Prints out the route table to a buffer. It is called
00437 when the related proc file is read
00438 ****************************************************/
00439 int read_route_table_proc(char *buffer, char **buffer_location, off_t offset, int buffer_length,int *eof,void *data)
00440 {
00441 
00442     static char *my_buffer;
00443     char temp_buffer[200];
00444     char temp[40];
00445     struct route_table_entry *tmp_entry;
00446     struct precursor_entry  *tmp_precursor;
00447     u_int64_t remainder, numerator;
00448     u_int64_t tmp_time;
00449     int len,i;
00450     u_int64_t currtime;
00451     char dst[16];
00452     char hop[16];
00453 
00454     currtime=getcurrtime();
00455 
00456     /*lock table*/
00457     route_read_lock();    
00458 
00459 
00460     tmp_entry=route_table;
00461 
00462     my_buffer=buffer;
00463 
00464 
00465     sprintf(my_buffer,"\nRoute Table \n---------------------------------------------------------------------------------\n");
00466     sprintf(temp_buffer,"        IP        |       Seq    |   Hop Count  |     Next Hop\n");
00467     strcat(my_buffer,temp_buffer);
00468     sprintf(temp_buffer,"---------------------------------------------------------------------------------\n");
00469     strcat(my_buffer,temp_buffer);
00470 
00471     while (tmp_entry!=NULL)
00472     {
00473         strcpy(hop,inet_ntoa(tmp_entry->next_hop));
00474         strcpy(dst,inet_ntoa(tmp_entry->dst_ip));
00475         sprintf(temp_buffer,"  %-16s       %5u          %3d         %-16s   ",dst ,tmp_entry->dst_seq,tmp_entry->hop_count,hop);
00476         strcat(my_buffer,temp_buffer);
00477         if (tmp_entry->route_valid)
00478           strcat(my_buffer, " Valid ");
00479         
00480         tmp_time=tmp_entry->lifetime-currtime;
00481         numerator = (tmp_time);
00482         remainder = do_div( numerator, 1000 );
00483         
00484         if (tmp_entry->lifetime < currtime )
00485         {
00486           sprintf(temp," Expired!\n");
00487         }
00488         else
00489           {
00490             sprintf(temp," sec/msec: %lu/%lu %d\n", (unsigned long)numerator, (unsigned long)remainder,tmp_entry->self_route);
00491           }
00492         strcat(my_buffer,temp);
00493 
00494 
00495         tmp_precursor=tmp_entry->precursors;
00496 
00497 #ifndef ARM
00498         while ( tmp_precursor!=NULL)
00499         {
00500             sprintf(temp_buffer,"\t\t-: Precursor -->   %s\n",inet_ntoa(tmp_precursor->ip));
00501             strcat(my_buffer,temp_buffer);
00502             tmp_precursor=tmp_precursor->next;
00503         }
00504 #endif
00505 
00506 
00507         tmp_entry=tmp_entry->next;
00508     
00509 
00510     }
00511 
00512 
00513 /*unlock table*/
00514 route_read_unlock();    
00515 
00516     strcat(my_buffer,"---------------------------------------------------------------------------------\n\n");
00517 
00518     len = strlen(my_buffer);
00519     *buffer_location = my_buffer + offset;
00520     len -= offset;
00521     if (len > buffer_length)
00522         len = buffer_length;
00523     else if (len < 0)
00524         len = 0;
00525     return len;
00526 }
00527 
00528 
00529 
00530 /****************************************************
00531 
00532    print_precursors
00533 ----------------------------------------------------
00534 It will print the precursors for a route table
00535 entry
00536 ****************************************************/
00537 void print_precursors(struct route_table_entry *tmp_entry)
00538 {
00539     struct precursor_entry  *tmp_precursor;
00540 
00541     tmp_precursor=tmp_entry->precursors;
00542     while ( tmp_precursor!=NULL)
00543     {
00544         printk("          -: Precursor -->   %s\n",inet_ntoa(tmp_precursor->ip));
00545         tmp_precursor=tmp_precursor->next;
00546     }
00547 }
00548 
00549 
00550 /****************************************************
00551 
00552    print_route_table
00553 ----------------------------------------------------
00554 Prints out the current routing table to the
00555 console screen
00556 ****************************************************/
00557 void print_route_table()
00558 {
00559     struct route_table_entry *tmp_entry;
00560     char dst[16];
00561     char hop[16];
00562 
00563     tmp_entry=route_table;
00564     printk(KERN_INFO "---------------------------------------------\n");
00565 
00566     while (tmp_entry!=NULL)
00567     {
00568         strcpy(hop,inet_ntoa(tmp_entry->next_hop));
00569         strcpy(dst,inet_ntoa(tmp_entry->dst_ip));
00570         printk(KERN_INFO "(  Dst: %s Dst Seq: %d  Hop Count: %d Next Hop: %s  )\n",dst ,tmp_entry->dst_seq,tmp_entry->hop_count,hop);
00571         print_precursors(tmp_entry);
00572         tmp_entry=tmp_entry->next;
00573         // printk("printed one entry\n");
00574     }
00575     printk(KERN_INFO "---------------------------------------------\n");
00576 
00577 }
00578 
00579 
00580 
00581 
00582 /****************************************************
00583 
00584    cleanup_route_table
00585 ----------------------------------------------------
00586 Erases everything in the routing table and the
00587 kernel routing table
00588 ****************************************************/
00589 int  cleanup_route_table(void)
00590 {
00591 
00592     struct route_table_entry *tmp_entry, *dead_entry;
00593 
00594     int count=0;
00595 
00596     tmp_entry=route_table;
00597 
00598 
00599     print_route_table();
00600 
00601 
00602     while(tmp_entry!=NULL)
00603     {
00604 
00605         if(delete_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop) != 0)
00606           printk(KERN_WARNING "AODV: Error removing Kernel route to: %s\n",inet_ntoa((__u32) tmp_entry->dst_ip));
00607 
00608 
00609         delete_precursors_from_route_table_entry(tmp_entry);
00610         dead_entry=tmp_entry;
00611         tmp_entry=tmp_entry->next;
00612         kfree(dead_entry);
00613 
00614         count++;
00615     }
00616 
00617     route_table=NULL;
00618     return 0;
00619 }
00620 
00621 
00622 
00623 
00624 
00625 /*
00626  * add_precursor
00627  *
00628  * Description:
00629  *   Allocates memory for a new precursor and inserts it in
00630  *   the precursor list for the destination given by the argument.
00631  *
00632  * Arguments:
00633  *   struct artentry *tmp_artentry - Routing table entry for the destination
00634  *   u_intr32_t tmp_ip - IP address for the precursor to insert
00635  *
00636  * Returns:
00637  *   int - 0 if precursor is successfully created
00638  *        -1 otherwise.
00639  */
00640 /****************************************************
00641 
00642    insert_precursor_entry
00643 ----------------------------------------------------
00644 Allocates and adds a precursor to a route
00645 entry
00646 ****************************************************/
00647 int insert_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip)
00648 {
00649     struct precursor_entry *tmp_precursor;
00650 
00651 
00652     if(find_precursor_entry(tmp_entry, tmp_ip) == NULL)
00653     {
00654         if((tmp_precursor = kmalloc(sizeof(struct precursor_entry),GFP_ATOMIC)) == NULL)
00655         {
00656 
00657             printk(KERN_WARNING "AODV: Error adding precusrosr\n");
00658 
00659 
00660             return -ENOMEM;
00661         }
00662         
00663         tmp_precursor->next = tmp_entry->precursors;
00664         tmp_precursor->prev = NULL;
00665         tmp_precursor->ip = tmp_ip;
00666         if (tmp_entry->precursors!=NULL)
00667             tmp_entry->precursors->prev = tmp_precursor;
00668         tmp_entry->precursors=tmp_precursor;
00669         return 0;
00670     }
00671 
00672     return -EINVAL;
00673 }
00674 
00675 
00676 /****************************************************
00677 
00678    delete_precursor
00679 ----------------------------------------------------
00680 Deletes a precurs from a route entries list of
00681 precursors
00682 ****************************************************/
00683 void delete_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip)
00684 {
00685     struct precursor_entry *tmp_precursor;
00686 
00687     if((tmp_precursor = find_precursor_entry(tmp_entry, tmp_ip)) != NULL)
00688     {
00689         if (tmp_precursor->prev!=NULL)
00690             tmp_precursor->prev->next = tmp_precursor->next;
00691         if (tmp_precursor->next!=NULL)
00692             tmp_precursor->next->prev = tmp_precursor->prev;
00693         if (tmp_entry->precursors == tmp_precursor)
00694             tmp_entry->precursors=tmp_precursor->next;
00695         kfree(tmp_precursor);
00696     }
00697 }
00698 
00699 
00700 /****************************************************
00701 
00702    delete_precursor_entry_from_route_table
00703 ----------------------------------------------------
00704 Removes a precursor from all route table entries
00705 ****************************************************/
00706 void delete_precursor_entry_from_route_table(u_int32_t tmp_ip)
00707 {
00708     struct route_table_entry *tmp_entry;
00709     
00710     /*lock table*/
00711     route_read_lock();    
00712     
00713     tmp_entry=route_table;
00714 
00715     while (tmp_entry!=NULL)
00716     {
00717 
00718         delete_precursor_entry(tmp_entry, tmp_ip);
00719         tmp_entry=tmp_entry->next;
00720     }
00721 
00722     /*unlock table*/
00723     route_read_unlock();    
00724 }
00725 
00726 
00727 
00728 /****************************************************
00729 
00730    find_precursor_entry
00731 ----------------------------------------------------
00732 Will find a precursor entry in a route table
00733 entries list of precursors
00734 ****************************************************/
00735 struct precursor_entry* find_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip)
00736 {
00737     struct precursor_entry *tmp_precursor;
00738 
00739     tmp_precursor=tmp_entry->precursors;
00740     while (tmp_precursor!=NULL)
00741     {
00742         if(tmp_precursor->ip == tmp_ip)
00743             return tmp_precursor;
00744         tmp_precursor=tmp_precursor->next;
00745     }
00746     return NULL;
00747 }
00748 
00749 
00750 
00751 
00752 /****************************************************
00753 
00754    update_reverse_route_entry
00755 ----------------------------------------------------
00756 Will create or update a reverse route to node
00757 ****************************************************/
00758 int update_route_entry(u_int32_t ip, u_int32_t next_hop_ip, u_int8_t hop_count, u_int32_t seq, struct net_device *dev)
00759 {
00760     struct route_table_entry  *tmp_entry;
00761     u_int64_t        curr_time;
00762 
00763     /*lock table*/
00764 
00765 
00766     tmp_entry = find_route_table_entry(ip); /* Get entry from RT if there is one */
00767     if ((tmp_entry == NULL) || (!tmp_entry->route_valid) ||
00768             (!tmp_entry->route_seq_valid) ||
00769             (seq_greater( seq , tmp_entry->dst_seq)) ||
00770             ((seq == tmp_entry->dst_seq) &&
00771              (hop_count < tmp_entry->hop_count)))
00772     {
00773 
00774         /* If there didn't exist an entry in RT to the source, create it */
00775         if (tmp_entry == NULL)
00776         {
00777 
00778 
00779           /*unlock table*/
00780 
00781           tmp_entry = create_route_table_entry();
00782           /*lock table*/
00783           if (tmp_entry==NULL) // Thanks to M. Musuvathi for catching this!
00784             return -ENOMEM;
00785 
00786           tmp_entry->dst_ip = ip;
00787 
00788         }
00789         else //Since the entry existed we might want to change krt
00790         {
00791             delete_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop);
00792         }
00793 
00794         if (tmp_entry->self_route)
00795           {
00796             printk("updating a SELF-ROUTE!!! %s hopcount %d\n",inet_ntoa(next_hop_ip),hop_count);
00797             if   (!tmp_entry->route_valid) 
00798               printk("because route was invalid!\n");
00799             
00800             if (!tmp_entry->route_seq_valid) 
00801               printk("because seq was invalid!\n");
00802             
00803             if (seq_greater( seq , tmp_entry->dst_seq))
00804               printk("because seq of route was lower!\n");
00805             if ((seq == tmp_entry->dst_seq) && (hop_count < tmp_entry->hop_count))
00806               printk("becase seq same but hop lower!\n");
00807           }
00808         /* Update values in the RT entry */
00809         tmp_entry->dst_seq = seq;
00810         tmp_entry->next_hop = next_hop_ip;
00811         tmp_entry->hop_count = hop_count;
00812         tmp_entry->dev= dev;
00813         tmp_entry->route_valid=TRUE;
00814         insert_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop,tmp_entry->dev->name);
00815         ipq_send_ip(ip);
00816     }
00817 
00818     curr_time = getcurrtime(); /* Get current time */
00819     /* Check if the lifetime in RT is valid, if not update it */
00820 
00821     tmp_entry->lifetime = curr_time+ACTIVE_ROUTE_TIMEOUT;
00822 
00823 
00824     return 0;
00825 }
00826 
00827 
00828 
00829 /****************************************************
00830 
00831    delete_precursors_from_route_table_entry
00832 ----------------------------------------------------
00833 Removes all of a route table entries precurosrs
00834 ****************************************************/
00835 void delete_precursors_from_route_table_entry(struct route_table_entry *tmp_entry)
00836 {
00837     struct precursor_entry *tmp_precursor;
00838     struct precursor_entry *dead_precursor;
00839 
00840     tmp_precursor=tmp_entry->precursors;
00841 
00842     while (tmp_precursor!=NULL)
00843     {
00844 
00845         dead_precursor=tmp_precursor;
00846         tmp_precursor=tmp_precursor->next;
00847         kfree(dead_precursor);
00848     }
00849     tmp_entry->precursors=NULL;
00850 }
00851 
00852 
00853 /****************************************************
00854 
00855    create_kernel_route_entry
00856 ----------------------------------------------------
00857 Creates a Kernel Routing table entry
00858 ****************************************************/
00859 struct rtentry *create_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip,char *interf)
00860 {
00861     struct rtentry *new_rtentry;
00862     struct sockaddr_in dst;
00863     struct sockaddr_in gw;
00864     struct sockaddr_in genmask;
00865 
00866     if((new_rtentry = kmalloc(sizeof(struct rtentry),GFP_ATOMIC)) == NULL)
00867     {
00868 #ifndef NO_ERROR
00869         printk("KRTABLE: Gen- Error generating a route entry\n");
00870 #endif
00871 
00872         return NULL; /* Malloc failed */
00873     }
00874 
00875     dst.sin_family = AF_INET;
00876     gw.sin_family = AF_INET;
00877     genmask.sin_family = AF_INET;
00878 
00879     dst.sin_addr.s_addr = dst_ip;
00880     gw.sin_addr.s_addr = gw_ip;
00881     genmask.sin_addr.s_addr=g_broadcast_ip;
00882     new_rtentry->rt_flags = RTF_UP | RTF_HOST | RTF_GATEWAY;
00883 
00884     new_rtentry->rt_metric = 0;
00885     //new_rtentry->rt_dev = NULL;
00886     new_rtentry->rt_dev = interf;
00887     new_rtentry->rt_dst = *(struct sockaddr *) &dst;
00888     new_rtentry->rt_gateway = *(struct sockaddr*) &gw;
00889     new_rtentry->rt_genmask = *(struct sockaddr*) &genmask;
00890 
00891     return new_rtentry;
00892 }
00893 
00894 
00895 
00896 
00897 
00898 
00899 /****************************************************
00900 
00901    insert_kernel_route_entry
00902 ----------------------------------------------------
00903 Creates an entry in the kernel routing table
00904 ****************************************************/
00905 int insert_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip,char *interf)
00906 {
00907     struct rtentry  *new_krtentry;
00908     mm_segment_t oldfs;
00909     int error;
00910 #ifdef TRACE
00911 
00912     char dst[16];
00913     char gw[16];
00914 #endif
00915 
00916     if ((new_krtentry = create_kernel_route_entry(dst_ip, gw_ip,interf)) == NULL)
00917     {
00918         #ifndef NO_ERROR
00919         printk("trouble creating route table entry!\n");
00920         #endif
00921         return -1;
00922     }
00923 
00924 
00925     oldfs = get_fs();
00926     set_fs(get_ds());
00927     error=ip_rt_ioctl(SIOCADDRT, (char*) new_krtentry);
00928 
00929 #ifdef TRACE
00930     strcpy(dst,inet_ntoa(dst_ip));
00931     strcpy(gw,inet_ntoa(gw_ip));
00932     printk("ADD_KROUTE: Added kroute to: %s thru: %s  \n",dst,gw);
00933 
00934 #endif
00935 
00936     set_fs(oldfs);
00937 
00938     kfree(new_krtentry);
00939 
00940     return 0;
00941 }
00942 
00943 
00944 
00945 /****************************************************
00946 
00947    delete_kernel_route_entry
00948 ----------------------------------------------------
00949 Removes a entry from the Kernel routing table
00950 ****************************************************/
00951 int delete_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip)
00952 {
00953     struct rtentry  *new_krtentry;
00954     mm_segment_t oldfs;
00955 #ifdef TRACE
00956 
00957     char dst[16];
00958     char gw[16];
00959 #endif
00960 
00961     if ((new_krtentry = create_kernel_route_entry(dst_ip, gw_ip,NULL)) == NULL)
00962         // del_kroute failed
00963         return 1;
00964 
00965     oldfs = get_fs();
00966     set_fs(KERNEL_DS);
00967 
00968     //For Kernel programming you have to use ip_rt_ioctl to insert routes
00969     if(ip_rt_ioctl(SIOCDELRT, (char*) new_krtentry) == -1)
00970     {
00971 #ifndef NO_ERROR
00972         printk("DEL_KRTENTRY: Danger Will Robinson!\n");
00973 #endif
00974 
00975         set_fs(oldfs);
00976 
00977         //SIOCDELRT failed
00978         return 1;
00979     }
00980 #ifdef TRACE
00981     strcpy(dst,inet_ntoa(dst_ip));
00982     strcpy(gw,inet_ntoa(gw_ip));
00983     printk("DEL_KROUTE: Deleted kroute to: %s thru: %s  \n",dst,gw);
00984 
00985 #endif
00986 
00987     set_fs(oldfs);
00988 
00989     kfree(new_krtentry);
00990 
00991     return 0;
00992 }
00993 

kernel_aodvmに対してThu Nov 10 18:53:11 2005に生成されました。  doxygen 1.4.5