📄 route_table.c
字号:
while ( tmp_precursor!=NULL) { printk(" -: Precursor --> %s\n",inet_ntoa(tmp_precursor->ip)); tmp_precursor=tmp_precursor->next; }}/**************************************************** print_route_table----------------------------------------------------Prints out the current routing table to theconsole screen****************************************************/void print_route_table(){ struct route_table_entry *tmp_entry; char dst[16]; char hop[16]; tmp_entry=route_table; printk("---------------------------------------------\n"); while (tmp_entry!=NULL) { strcpy(hop,inet_ntoa(tmp_entry->next_hop)); strcpy(dst,inet_ntoa(tmp_entry->dst_ip)); printk("( Dst: %s Dst Seq: %d Hop Count: %d Next Hop: %s )\n",dst ,tmp_entry->dst_seq,tmp_entry->hop_count,hop); print_precursors(tmp_entry); tmp_entry=tmp_entry->next; // printk("printed one entry\n"); } printk("---------------------------------------------\n");}/**************************************************** cleanup_route_table----------------------------------------------------Erases everything in the routing table and thekernel routing table****************************************************/int cleanup_route_table(void){ struct route_table_entry *tmp_entry, *dead_entry; int count=0; tmp_entry=route_table;#ifdef MESSAGES print_route_table();#endif while(tmp_entry!=NULL) { if(delete_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop) != 0) #ifndef NO_ERROR printk("Error removing Kernel route to: %s\n",inet_ntoa((__u32) tmp_entry->dst_ip)); #endif delete_precursors_from_route_table_entry(tmp_entry); dead_entry=tmp_entry; tmp_entry=tmp_entry->next; kfree(dead_entry); count++; } route_table=NULL; return 0;}/* * add_precursor * * Description: * Allocates memory for a new precursor and inserts it in * the precursor list for the destination given by the argument. * * Arguments: * struct artentry *tmp_artentry - Routing table entry for the destination * u_intr32_t tmp_ip - IP address for the precursor to insert * * Returns: * int - 0 if precursor is successfully created * -1 otherwise. *//**************************************************** insert_precursor_entry----------------------------------------------------Allocates and adds a precursor to a routeentry****************************************************/int insert_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip){ struct precursor_entry *tmp_precursor; if(find_precursor_entry(tmp_entry, tmp_ip) == NULL) { if((tmp_precursor = kmalloc(sizeof(struct precursor_entry),GFP_ATOMIC)) == NULL) {#ifndef NO_ERROR printk("RT: Error adding precusrosr\n");#endif return 1; } tmp_precursor->next = tmp_entry->precursors; tmp_precursor->prev = NULL; tmp_precursor->ip = tmp_ip; if (tmp_entry->precursors!=NULL) tmp_entry->precursors->prev = tmp_precursor; tmp_entry->precursors=tmp_precursor; return 0; } return 1;}/**************************************************** delete_precursor----------------------------------------------------Deletes a precurs from a route entries list ofprecursors****************************************************/void delete_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip){ struct precursor_entry *tmp_precursor; if((tmp_precursor = find_precursor_entry(tmp_entry, tmp_ip)) != NULL) { if (tmp_precursor->prev!=NULL) tmp_precursor->prev->next = tmp_precursor->next; if (tmp_precursor->next!=NULL) tmp_precursor->next->prev = tmp_precursor->prev; if (tmp_entry->precursors == tmp_precursor) tmp_entry->precursors=tmp_precursor->next; kfree(tmp_precursor); }}/**************************************************** delete_precursor_entry_from_route_table----------------------------------------------------Removes a precursor from all route table entries****************************************************/void delete_precursor_entry_from_route_table(u_int32_t tmp_ip){ struct route_table_entry *tmp_entry; tmp_entry=route_table; while (tmp_entry!=NULL) { delete_precursor_entry(tmp_entry, tmp_ip); tmp_entry=tmp_entry->next; }}/**************************************************** find_precursor_entry----------------------------------------------------Will find a precursor entry in a route tableentries list of precursors****************************************************/struct precursor_entry* find_precursor_entry(struct route_table_entry *tmp_entry, u_int32_t tmp_ip){ struct precursor_entry *tmp_precursor; tmp_precursor=tmp_entry->precursors; while (tmp_precursor!=NULL) { if(tmp_precursor->ip == tmp_ip) return tmp_precursor; tmp_precursor=tmp_precursor->next; } return NULL;}/**************************************************** update_reverse_route_entry----------------------------------------------------Will create or update a reverse route to node****************************************************/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){ struct route_table_entry *tmp_entry; u_int64_t curr_time; tmp_entry = find_route_table_entry(ip); /* Get entry from RT if there is one */ if ((tmp_entry == NULL) || (!tmp_entry->route_valid) || (!tmp_entry->route_seq_valid) || (seq_greater( seq , tmp_entry->dst_seq)) || ((seq == tmp_entry->dst_seq) && (hop_count < tmp_entry->hop_count))) { #ifdef TRACE printk("ROUTE_TABLE: Updating route for: %s \n",inet_ntoa(ip));#endif /* If there didn't exist an entry in RT to the source, create it */ if (tmp_entry == NULL) { tmp_entry = create_route_table_entry(); tmp_entry->dst_ip = ip; } else //Since the entry existed we might want to change krt { delete_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop); } /* Update values in the RT entry */ tmp_entry->dst_seq = seq; tmp_entry->next_hop = next_hop_ip; tmp_entry->hop_count = hop_count; tmp_entry->dev=dev; tmp_entry->route_valid=TRUE; insert_kernel_route_entry(tmp_entry->dst_ip, tmp_entry->next_hop,tmp_entry->dev->name); ipq_send_ip(ip); } curr_time = getcurrtime(); /* Get current time */ /* Check if the lifetime in RT is valid, if not update it */ tmp_entry->lifetime = curr_time+ACTIVE_ROUTE_TIMEOUT; return 0;}/**************************************************** delete_precursors_from_route_table_entry----------------------------------------------------Removes all of a route table entries precurosrs****************************************************/void delete_precursors_from_route_table_entry(struct route_table_entry *tmp_entry){ struct precursor_entry *tmp_precursor; struct precursor_entry *dead_precursor; tmp_precursor=tmp_entry->precursors; while (tmp_precursor!=NULL) { dead_precursor=tmp_precursor; tmp_precursor=tmp_precursor->next; kfree(dead_precursor); } tmp_entry->precursors=NULL;}/**************************************************** create_kernel_route_entry----------------------------------------------------Creates a Kernel Routing table entry****************************************************/struct rtentry *create_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip,char *interf){ struct rtentry *new_rtentry; struct sockaddr_in dst; struct sockaddr_in gw; struct sockaddr_in genmask; if((new_rtentry = kmalloc(sizeof(struct rtentry),GFP_ATOMIC)) == NULL) {#ifndef NO_ERROR printk("KRTABLE: Gen- Error generating a route entry\n");#endif return NULL; /* Malloc failed */ } dst.sin_family = AF_INET; gw.sin_family = AF_INET; genmask.sin_family = AF_INET; dst.sin_addr.s_addr = dst_ip; gw.sin_addr.s_addr = gw_ip; genmask.sin_addr.s_addr=g_broadcast_ip; new_rtentry->rt_flags = RTF_UP | RTF_HOST | RTF_GATEWAY; new_rtentry->rt_metric = 0; //new_rtentry->rt_dev = NULL; new_rtentry->rt_dev = interf; new_rtentry->rt_dst = *(struct sockaddr *) &dst; new_rtentry->rt_gateway = *(struct sockaddr*) &gw; new_rtentry->rt_genmask = *(struct sockaddr*) &genmask; return new_rtentry;}/**************************************************** insert_kernel_route_entry----------------------------------------------------Creates an entry in the kernel routing table****************************************************/int insert_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip,char *interf){ struct rtentry *new_krtentry; mm_segment_t oldfs; int error;#ifdef TRACE char dst[16]; char gw[16];#endif if ((new_krtentry = create_kernel_route_entry(dst_ip, gw_ip,interf)) == NULL) { #ifndef NO_ERROR printk("trouble creating route table entry!\n"); #endif return -1; } oldfs = get_fs(); set_fs(get_ds()); error=ip_rt_ioctl(SIOCADDRT, (char*) new_krtentry);#ifdef TRACE strcpy(dst,inet_ntoa(dst_ip)); strcpy(gw,inet_ntoa(gw_ip)); printk("ADD_KROUTE: Added kroute to: %s thru: %s \n",dst,gw);#endif set_fs(oldfs); return 0;}/**************************************************** delete_kernel_route_entry----------------------------------------------------Removes a entry from the Kernel routing table****************************************************/int delete_kernel_route_entry(u_int32_t dst_ip, u_int32_t gw_ip){ struct rtentry *new_krtentry; mm_segment_t oldfs;#ifdef TRACE char dst[16]; char gw[16];#endif if ((new_krtentry = create_kernel_route_entry(dst_ip, gw_ip,NULL)) == NULL) // del_kroute failed return 1; oldfs = get_fs(); set_fs(KERNEL_DS); //For Kernel programming you have to use ip_rt_ioctl to insert routes if(ip_rt_ioctl(SIOCDELRT, (char*) new_krtentry) == -1) {#ifndef NO_ERROR printk("DEL_KRTENTRY: Danger Will Robinson!\n");#endif set_fs(oldfs); //SIOCDELRT failed return 1; }#ifdef TRACE strcpy(dst,inet_ntoa(dst_ip)); strcpy(gw,inet_ntoa(gw_ip)); printk("DEL_KROUTE: Deleted kroute to: %s thru: %s \n",dst,gw);#endif set_fs(oldfs); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -