00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "interface_list.h"
00027
00028
00029
00030
00031
00032
00033
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
00048
00049
00050
00051
00052 int start_HELLO (u_int32_t ip)
00053 {
00054 struct rrep *tmp_rrep;
00055 u_int32_t random_number;
00056
00057
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
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
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
00094
00095
00096
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
00110
00111
00112 dev= dev_base;
00113 interface_list=NULL;
00114
00115
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
00128
00129
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
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
00153
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
00164 printk(KERN_WARNING "AODV: Not enough memory to create Route Table Entry\n");
00165 return -ENOMEM;
00166 }
00167
00168
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
00179 start_HELLO(new_entry->ip);
00180
00181
00182
00183
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
00223
00224
00225
00226 u_int32_t find_dev_ip(struct net_device *dev)
00227 {
00228 struct in_device *tmp_indev;
00229
00230
00231 if (dev==NULL)
00232 {
00233 printk (KERN_WARNING "AODV: FIND_DEV_IP gotta NULL DEV! ");
00234
00235 return -EFAULT;
00236 }
00237
00238
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
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
00258
00259
00260
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
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
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
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
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
00315
00316
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
00323 while (tmp_entry!=NULL)
00324 {
00325
00326 if (tmp_entry->ip==ip)
00327 return tmp_entry;
00328 tmp_entry=tmp_entry->next;
00329 }
00330 return NULL;
00331 }