📄 packet_in.c
字号:
/* Kernel AODV v2.0National Institute of Standards and Technology Luke Klein-Berndt----------------------------------------------------- Version 2.0 new features: * Updated to AODV draft version 11 * Managed internet gatewaying * Monitor wireles signal strength * Many bug fixes!-----------------------------------------------------Originally based upon MadHoc code. I am notsure how much of it is left anymore, but MadHocproved to be a great starting point.MadHoc was written by - Fredrik Lilieblad,Oskar Mattsson, Petra Nylund, Dan Ouchterlonyand Anders Roxenhag Mail: mad-hoc@flyinglinux.netThis software is Open Source under the GNU General Public Licence.*/#include "packet_in.h"/**************************************************** packet_in----------------------------------------------------When netfilter intercepts a packet coming intothe system it passes it to Packet in to handle****************************************************/extern u_int32_t g_broadcast_ip;/*********************************** check_packet** Description:* Checks to make sure that the packet is the correct size for* the packet type it says it is**********************************//**************************************************** check_packet----------------------------------------------------Checks to make sure that the packet is the correct size forthe packet type it says it is****************************************************/int check_packet (int numbytes, int type, void *data_in){ int *data; struct rerr *tmp_rerr; data=(int *) data_in; switch (type) { //RREQ case 1: //If it is a normal route rreq if (numbytes == 24) return 0; if (numbytes<=26) //smaller than RREQ plus minimum exstension header return 1; if (data[25]==3 && data[26]==8 && numbytes==24+2+8) //Multicast Group Leader Extension return 0; if (data[25]==4 && data[26]==2 && numbytes==24+2+2) //Multicast Group Rebuild Extension return 0; break; //RREP case 2: if (numbytes == 20) //Normal RREP return 0; if (numbytes<= 22) //smaller than RREP plus minimum exstension header return 1; if (data[21]==5 && data[22]==6 && numbytes==20+2+6) //Multicast Group Information Exstension break; //RERR case 3: // Normal RERR tmp_rerr=data_in; if (numbytes == sizeof(struct rerr) + (sizeof(struct rerrdst) * tmp_rerr->dst_count)) { return 0; } break; case 4: //Normal RREP-ACK if (numbytes == 2) return 0; break; default: break; } return 1;}/**************************************************** packet_in----------------------------------------------------This function handles all the packets whichcome and either pass them to the correct functionif they are from another AODV program or allowsthem to pass through if they are no of AODV type****************************************************/int packet_in(struct sk_buff *packet){ struct net_device *dev; struct rrep *tmp_rrep; struct route_table_entry *tmp_route; struct neighbor_list_entry *tmp_entry; struct iphdr *ip,*ip2; struct ethhdr *eth; u_int64_t curr_time; u_int32_t tmp_ip; char src_ip[16]; char dst_ip[16]; int print_it=1; // Create aodv message types u_int8_t aodv_type; u_int8_t ttl; //The packets which come in still have their headers from the IP and UDP int start_offset=sizeof(struct udphdr)+sizeof(struct iphdr); int start_point=sizeof(struct udphdr)+sizeof(struct iphdr); curr_time=getcurrtime();#ifdef TRACE printk("PACKET_IN: Entered\n");#endif //get pointers to the important parts of the message ip = packet->nh.iph; ip2 = packet->h.ipiph; eth = packet->mac.ethernet; dev = packet->dev; ttl = ip->ttl; //converts the IP address to strings strcpy(src_ip,inet_ntoa(ip->saddr)); strcpy(dst_ip,inet_ntoa(ip->daddr)); //don't want to look at packets coming from us! tmp_ip=find_dev_ip(dev); if (tmp_ip==ip->saddr) { print_it=0; goto drop; } if ((strcmp(dev->name,"lo")==0) && !USE_LO) { goto drop; } if (ip->daddr !=g_broadcast_ip) { tmp_route=find_route_table_entry(ip->daddr); if ((tmp_route==NULL) || !tmp_route->route_valid) host_unr(ip->daddr); } //For all AODV packets the type is the first byte. aodv_type = (int)packet->data[start_point]; if (check_packet(packet->len-start_offset,aodv_type,packet->data+start_point)) { #ifdef MESSAGES printk("PACKET_IN: Packet of type: %d and of size %u failed packet check!\n",aodv_type,packet->len-start_offset); #endif goto drop; } tmp_route=find_route_table_entry(ip->saddr); if ((tmp_route!=NULL) && (tmp_route->route_valid)) { tmp_route->lifetime=MAX(tmp_route->lifetime,(HELLO_INTERVAL * ALLOWED_HELLO_LOSS) + curr_time); tmp_entry=find_neighbor_list_entry(ip->saddr); if (tmp_entry!=NULL) { delete_timer_queue_entry_of_id(tmp_entry->ip, EVENT_NEIGHBOR); insert_timer_queue_entry(HELLO_INTERVAL * ALLOWED_HELLO_LOSS + curr_time + 20,NULL,0,ip->saddr,0,0,EVENT_NEIGHBOR); update_timer_queue(); } } //place packet in the event queue! insert_event_queue_entry(ip->saddr,ip->daddr,(unsigned char *) &(eth->h_source),dev,aodv_type,packet->len-start_offset,packet->data+start_point,ttl);drop:#ifdef TRACE if(print_it) printk("PACKET_IN: We recieved a malformed AODV Message\n");#endif return NF_ACCEPT;}/**************************************************** input_handler----------------------------------------------------The actaul function which gets called by netfilter****************************************************/unsigned int input_handler(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)){ struct iphdr *iph=(*skb)->nh.iph; struct udphdr *hdr; void* p; struct iphdr *ip; ip = (*skb)->nh.iph;#ifdef TRACE printk("INPUT_HANDLER: enter\n");#endif if ((*skb)->h.uh!=NULL) { p= (u_int32_t *) iph + iph->ihl; hdr=p; if (hdr->dest==htons(AODVPORT)) { return packet_in(*(skb)); } } return NF_ACCEPT;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -