interface_list.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 "interface_list.h"
00027 
00028 /****************************************************
00029 
00030    interface_list
00031 ----------------------------------------------------
00032 This is a list of the interfaces available on the
00033 computer
00034 ****************************************************/
00035 
00036 struct interface_list_entry *interface_list;
00037 
00038 extern struct route_table_entry *g_my_entry;
00039 extern u_int32_t g_my_ip;
00040 extern u_int32_t g_broadcast_ip;
00041 
00042 
00043 
00044 
00045 /****************************************************
00046 
00047    start_HELLO
00048 ----------------------------------------------------
00049 Starts sending hello messages out for a given
00050 IP address
00051 ****************************************************/
00052 int start_HELLO (u_int32_t ip)
00053 {
00054     struct rrep *tmp_rrep;
00055     u_int32_t random_number;
00056 
00057     //creates the RREP you wish to send out as the hello message
00058     if ((tmp_rrep = kmalloc(sizeof(struct rrep),GFP_ATOMIC)) == NULL)
00059     {
00060           printk(KERN_WARNING "AODV: Not enough memory to create a Hello Message\n");
00061           return -ENOMEM;
00062 
00063     }
00064 
00065     //fillin the RREP
00066     tmp_rrep->dst_ip=ip;
00067     tmp_rrep->src_ip=ip;
00068     tmp_rrep->hop_count=0;
00069     tmp_rrep->lifetime=htonl (ALLOWED_HELLO_LOSS * HELLO_INTERVAL);
00070     tmp_rrep->dst_seq=0;
00071     tmp_rrep->type=2;
00072     tmp_rrep->r=0;
00073     tmp_rrep->reserved1=0;
00074     tmp_rrep->reserved2=0;
00075     tmp_rrep->prefix_sz=0;
00076     tmp_rrep->a=0;
00077 
00078 
00079     //Insert it into the timer queue!
00080     get_random_bytes(&random_number,sizeof(u_int32_t));
00081     random_number=(43*random_number)%HELLO_INTERVAL;
00082     insert_timer_queue_entry(getcurrtime() + random_number, tmp_rrep, sizeof(struct rrep), tmp_rrep->dst_ip,0,1,EVENT_HELLO);
00083     update_timer_queue();
00084 
00085     return 0;
00086 }
00087 
00088 
00089 
00090 
00091 /****************************************************
00092 
00093    init_interface_list
00094 ----------------------------------------------------
00095 Discover the available interfaces and add them
00096 to the list
00097 ****************************************************/
00098 int init_interface_list()
00099 {
00100 
00101     struct route_table_entry *tmp_route;
00102     struct interface_list_entry *new_entry=NULL;
00103     struct net_device *dev;
00104     struct in_device *tmp_indev;
00105     struct in_device *in_dev;
00106 
00107     int error;
00108 
00109     //dev_base is a kernel variable pointing to a list of
00110     //all the available netdevices and is maintained
00111     //by the kernel
00112     dev= dev_base;
00113     interface_list=NULL;
00114 
00115     //look at all the devices
00116     read_lock(&dev_base_lock);
00117     read_lock(&inetdev_lock);
00118     for (dev=dev_base; dev; dev=dev->next) {
00119       if ((in_dev=__in_dev_get(dev)) == NULL)
00120         continue;
00121       
00122       read_lock(&in_dev->lock);
00123       for_primary_ifa(in_dev) 
00124         {
00125         
00126           
00127           //we don't want to include the loopback device
00128           //or any device which doesn't have a completed
00129           //ip section
00130           
00131           if ((strcmp(dev->name,g_block_dev)!=0) &&
00132               ((strcmp(g_aodv_dev,"")==0) || (strcmp(dev->name,g_aodv_dev)==0)))
00133             {
00134               tmp_route=create_route_table_entry();
00135               tmp_route->dst_ip = ifa->ifa_address;
00136               printk(KERN_INFO "INTERFACE LIST: Adding interface: %s  IP: %s\n",dev->name, inet_ntoa(ifa->ifa_address));
00137               
00138               
00139               //fill in the info for the route
00140               tmp_route->self_route=1;
00141               tmp_route->dst_seq = 1;
00142               tmp_route->old_dst_seq=0;
00143               tmp_route->rreq_id = 1;
00144               tmp_route->hop_count = 0;
00145               tmp_route->next_hop = tmp_route->dst_ip;
00146               tmp_route->lifetime = -1;
00147               tmp_route->route_valid = 1;
00148               tmp_route->route_seq_valid = 1;
00149               tmp_route->precursors=NULL;
00150               tmp_route->dev=dev;
00151             
00152               //save the entry as being our own... so the last dev read in
00153               //becomes our default dev.
00154               
00155               g_my_entry = tmp_route;
00156               g_my_ip=g_my_entry->dst_ip;
00157               
00158               
00159               if ((strcmp(dev->name,"lo")!=0) || USE_LO)
00160                 {
00161                   if ((new_entry = kmalloc(sizeof(struct interface_list_entry),GFP_ATOMIC)) == NULL)
00162                     {
00163                       /* Couldn't create a new entry in the routing table */
00164                       printk(KERN_WARNING "AODV: Not enough memory to create Route Table Entry\n");
00165                       return -ENOMEM;
00166                     }
00167                   
00168                   //fill in the info on the dev
00169                   new_entry->ip=tmp_route->dst_ip;
00170                   new_entry->route_entry=tmp_route;
00171                   new_entry->next=interface_list;
00172                   new_entry->dev=dev;
00173                   new_entry->last_hello=getcurrtime();
00174                   new_entry->last_broadcast=0;
00175                   interface_list=new_entry;
00176                   strncpy(new_entry->name,dev->name,IFNAMSIZ);
00177                   
00178                   //start sending out a hello message for this dev
00179                   start_HELLO(new_entry->ip);
00180                   
00181                   
00182                   //insert a broadcast address into the kernel routing table using this dev
00183                   //create the socket... set it to type datagram
00184                   error = sock_create(PF_INET,SOCK_DGRAM,0,&(new_entry->sock));
00185                   if (error<0)
00186                     {
00187                       kfree(new_entry);
00188                       printk(KERN_ERR "Error during creation of socket; terminating, %d\n",error);
00189                       return error;
00190                     }
00191                 
00192                   init_sock(new_entry->sock,new_entry->ip,dev->name);
00193                   insert_kernel_route_entry(tmp_route->dst_ip,tmp_route->dst_ip,new_entry->name);
00194                 }
00195               
00196             }
00197 
00198         } endfor_ifa(in_dev);
00199                 read_unlock(&in_dev->lock);
00200     }
00201         read_unlock(&inetdev_lock);
00202         read_unlock(&dev_base_lock);
00203         
00204         
00205  
00206 
00207 return 0;
00208 }
00209 
00210 
00211 
00212 
00213 struct interface_list_entry *find_first_interface_entry()
00214 {
00215     return interface_list;
00216 }
00217 
00218 
00219 
00220 /****************************************************
00221 
00222    find_dev_ip
00223 ----------------------------------------------------
00224 It will find the IP for a dev
00225 ****************************************************/
00226 u_int32_t find_dev_ip(struct net_device *dev)
00227 {
00228     struct in_device *tmp_indev;
00229 
00230     //make sure we get a valid DEV
00231     if (dev==NULL)
00232     {
00233         printk (KERN_WARNING "AODV: FIND_DEV_IP gotta NULL DEV! ");
00234 
00235         return -EFAULT;
00236     }
00237 
00238     //make sure that dev has an IP section
00239     if (dev->ip_ptr==NULL)
00240     {
00241         printk (KERN_WARNING "AODV: FIND_DEV_IP gotta NULL ip_ptr!! ");
00242 
00243         return -EFAULT;
00244     }
00245 
00246 
00247     //find that ip!
00248     tmp_indev=(struct in_device *) dev->ip_ptr;
00249     if (tmp_indev && (tmp_indev->ifa_list!=NULL))
00250       return  (tmp_indev->ifa_list->ifa_address);
00251     else
00252       return  0;
00253 }
00254 
00255 /****************************************************
00256 
00257    find_interface_by_dev
00258 ----------------------------------------------------
00259 Finds an interface by matching up the dev to
00260 the dev in the interface list
00261 ****************************************************/
00262 struct interface_list_entry *find_interface_by_dev(struct net_device *dev)
00263 {
00264     struct interface_list_entry *tmp_entry=interface_list;
00265     struct in_device *tmp_indev;
00266     u_int32_t tmp_ip;
00267 
00268 
00269     //Make sure the dev is legit
00270     if (dev==NULL)
00271     {
00272 
00273         printk (KERN_WARNING "AODV: FIND_INTERFACE_BY_DEV gotta NULL DEV! \n");
00274 
00275         return NULL;
00276     }
00277     //make sure it has a legit ip section
00278     if (dev->ip_ptr==NULL)
00279     {
00280         printk (KERN_WARNING "AODV: FIND_INTERFACE_BY_DEV gotta NULL ip_ptr!! \n");
00281 
00282         return NULL;
00283     }
00284 
00285     //find the ip address for the dev
00286     tmp_indev=(struct in_device *) dev->ip_ptr;
00287 
00288  if (tmp_indev->ifa_list==NULL)
00289     {
00290         printk (KERN_WARNING "AODV: FIND_INTERFACE_BY_DEV gotta NULL ifa_list!! \n");
00291 
00292         return NULL;
00293     }
00294 
00295     tmp_ip = tmp_indev->ifa_list->ifa_address;
00296 
00297 
00298 
00299     //search the interface list for a device with the same ip
00300     while (tmp_entry!=NULL)
00301     {
00302         if (tmp_entry->ip==tmp_ip)
00303             return tmp_entry;
00304         tmp_entry=tmp_entry->next;
00305     }
00306 
00307     printk(KERN_WARNING "AODV: Failed search for matching interface for: %s which has an ip of: %s\n",dev->name,inet_ntoa(tmp_ip));
00308 
00309     return NULL;
00310 }
00311 
00312 /****************************************************
00313 
00314    find_interface_by_ip
00315 ----------------------------------------------------
00316 Finds an interface list entry by comparing IPs
00317 ****************************************************/
00318 struct interface_list_entry *find_interface_by_ip(u_int32_t ip)
00319 {
00320     struct interface_list_entry *tmp_entry=interface_list;
00321 
00322     //go through the whole list
00323     while (tmp_entry!=NULL)
00324     {
00325         //if it finds a match
00326         if (tmp_entry->ip==ip)
00327             return tmp_entry;
00328         tmp_entry=tmp_entry->next;
00329     }
00330     return NULL;
00331 }

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