packet_in.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 "packet_in.h"
00027 
00028 /****************************************************
00029 
00030    packet_in
00031 ----------------------------------------------------
00032 When netfilter intercepts a packet coming into
00033 the system it passes it to Packet in to handle
00034 ****************************************************/
00035 
00036 extern u_int32_t  g_broadcast_ip;
00037 extern u_int32_t g_my_ip;
00038 extern struct flood_id_queue_entry *multicast_id_queue;
00039 
00040 #ifdef AODV_MULTICAST
00041 extern struct socket * multicast_sock;
00042 #endif
00043 
00044 /*
00045 ********************************
00046 *
00047 *  check_packet
00048 *
00049 * Description:
00050 *   Checks to make sure that the packet is the correct size for
00051 *   the packet type it says it is
00052 *
00053 ********************************
00054 */
00055 
00056 /****************************************************
00057 
00058    check_packet
00059 ----------------------------------------------------
00060 Checks to make sure that the packet is the correct size for
00061 the packet type it says it is
00062 ****************************************************/
00063 int check_packet (int numbytes, int type, void *data_in)
00064 {
00065 
00066     int *data;
00067 
00068     struct rerr *tmp_rerr;
00069     data=(int *) data_in;
00070 
00071     switch (type)
00072     {
00073         //RREQ
00074     case 1:
00075 
00076         //If it is a normal route rreq
00077         if (numbytes == 24)
00078             return 0;
00079         if (numbytes<=26) //smaller than RREQ plus minimum exstension header
00080             return 1;
00081         if (data[25]==3 && data[26]==8 && numbytes==24+2+8)  //Multicast Group Leader Extension
00082             return 0;
00083         if (data[25]==4 && data[26]==2 && numbytes==24+2+2)  //Multicast Group Rebuild Extension
00084             return 0;
00085         break;
00086 
00087         //RREP
00088     case 2:
00089 
00090         if (numbytes == 20)  //Normal RREP
00091             return 0;
00092         if (numbytes<= 22)  //smaller than RREP plus minimum exstension header
00093             return 1;
00094         if (data[21]==5 && data[22]==6 && numbytes==20+2+6) //Multicast Group Information Exstension
00095 
00096             break;
00097 
00098         //RERR
00099     case 3:
00100 
00101         // Normal RERR
00102         tmp_rerr=data_in;
00103         if (numbytes == sizeof(struct rerr) + (sizeof(struct rerrdst) * tmp_rerr->dst_count))
00104         {
00105             return 0;
00106         }
00107         break;
00108 
00109     case 4:     //Normal RREP-ACK
00110         if (numbytes == 2)
00111             return 0;
00112         break;
00113 
00114     default:
00115         break;
00116     }
00117 
00118     return 1;
00119 }
00120 
00121 
00122 /****************************************************
00123 
00124         packet_in
00125 ----------------------------------------------------
00126 This function handles all the packets which
00127 come and either pass them to the correct function
00128 if they are from another AODV program or allows
00129 them to pass through if they are no of AODV type
00130 ****************************************************/
00131 
00132 int packet_in(struct sk_buff *packet)
00133 {
00134     struct net_device *dev;
00135     struct route_table_entry *tmp_route;
00136     struct iphdr *ip;
00137     u_int32_t tmp_ip;
00138 
00139 
00140     // Create aodv message types
00141     u_int8_t aodv_type;
00142 
00143 
00144     //The packets which come in still have their headers from the IP and UDP
00145     int start_point=sizeof(struct udphdr)+sizeof(struct iphdr);
00146 
00147 
00148     //get pointers to the important parts of the message
00149     ip  = packet->nh.iph;
00150     dev = packet->dev;
00151 
00152 
00153     //don't want to look at packets coming from us!
00154     tmp_ip=find_dev_ip(dev);
00155 
00156 
00157     if (tmp_ip==ip->saddr)
00158       {
00159         return NF_DROP;
00160       }
00161     
00162     if ((strcmp(dev->name,"lo")==0) && !USE_LO)
00163       {
00164         return NF_DROP;
00165       }
00166     
00167     if ((ip->daddr!=g_broadcast_ip) &&  (adhoc_subnet_test(ip->daddr)))
00168       {
00169         tmp_route=find_route_table_entry(ip->daddr);
00170         if ((tmp_route==NULL) || !tmp_route->route_valid)
00171           {
00172             printk( KERN_NOTICE "AODV: Undestination, %s, unable to forward...\n", inet_ntoa(ip->daddr)); 
00173             host_unr(ip->daddr);
00174           }
00175       }
00176     
00177     
00178     
00179     //For all AODV packets the type is the first byte.
00180     aodv_type = (int)packet->data[start_point];
00181     
00182     if (check_packet(packet->len-start_point,aodv_type,packet->data+start_point))
00183       {
00184         printk(KERN_NOTICE "AODV: Packet of type: %d and of size %u failed packet check!\n",aodv_type,packet->len-start_point);
00185         return NF_DROP;
00186         
00187       }
00188 
00189     tmp_route=find_route_table_entry(ip->saddr);
00190     
00191     if ((tmp_route!=NULL) && (tmp_route->route_valid))
00192       {
00193         tmp_route->lifetime=(HELLO_INTERVAL * ALLOWED_HELLO_LOSS) + getcurrtime();
00194       }
00195     
00196     //place packet in the event queue!
00197     insert_event_queue_entry(aodv_type,packet);
00198     
00199     return NF_ACCEPT;
00200 }
00201 
00202 
00203 
00204 /****************************************************
00205 
00206    input_handler
00207 ----------------------------------------------------
00208 The actaul function which gets called by netfilter
00209 ****************************************************/
00210 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 *))
00211 {
00212   struct iphdr *ip = (*skb)->nh.iph;  
00213   void *p = (uint32_t *) ip + ip->ihl;
00214   struct udphdr *hdr=p;// (struct udphdr *) (ip + ip->ihl); //p;
00215   
00216   struct ethhdr *mac = (*skb)->mac.ethernet;  //Thanks to Randy Pitz for adding this extra check...
00217   
00218     
00219 #ifdef AODV_MULTICAST
00220     unsigned char *ucp;
00221 #endif
00222 
00223 
00224  
00225     
00226 
00227 
00228 
00229 #ifdef AODV_MULTICAST
00230     if ((ip!=NULL) &&  (multicast_test(ip->daddr)) && (ip->saddr!=g_my_ip))
00231       {
00232         if (check_check(ip->saddr,ntohs(ip->id)))
00233           {
00234             ucp=(unsigned char *)&(ip->saddr);
00235               
00236             // if (((ucp[3] & 0xff)<111) &&((ucp[3] & 0xff)>100))  
00237             //  {
00238         
00239 
00240                   if (sock_wspace(multicast_sock->sk)>40000)
00241                     insert_rebroadcast_queue_entry(ip->daddr,(*skb)->len,(*skb)->data,ip->ttl);
00242                   //  }
00243           }
00244         else
00245           {
00246             
00247             return NF_DROP;
00248           }
00249 
00250       }
00251 #endif
00252 
00253 
00254     if ((*skb)->h.uh!=NULL)
00255     {
00256         if ((hdr->dest==htons(AODVPORT)) && (mac->h_proto==htons(ETH_P_IP)) )
00257         {
00258 
00259             return packet_in(*(skb));
00260         }
00261     }
00262 
00263     return NF_ACCEPT;
00264 
00265 }
00266 
00267 

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