routes/route_macfilter.c

Go to the documentation of this file.
00001 /***************************************************
00002 * On Some OS's you see all the traffic you just
00003 * sent so you need to filter by mac address.
00004 * If you don't you'll pick a packet up off one
00005 * interface, see it on the other and broadcast
00006 * it back on the first in an infinite loop.
00007 *
00008 * TODO: Make MACS time out
00009 * TODO: Finish handling broadcast packets
00010 ****************************************************/
00011 
00012 #include "route_macfilter.h"
00013 #include <string.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include "../packets/packet.h"
00017 #include "../engine/num_list.h"
00018 #include "../decoders/decode.h"
00019 #include "../decoders/decode_ethernet.h"
00020 #ifdef _SOLARIS_
00021 #include <strings.h>
00022 #endif
00023 
00024 MacRec          Macs[MAX_MACS];
00025 int             NumMacs;
00026 NumList*        MacFilterInterfaceList;
00027 int                     EthernetDecoderID;
00028 
00029 //#define DEBUG
00030 
00031 extern GlobalVars       Globals;
00032 
00033 
00034 /*******************************************
00035 * If the mac exists, get it otherwise
00036 * create it
00037 *******************************************/
00038 MacRec* GetMac(unsigned char* Mac, int Create){
00039         int i;
00040 
00041         DEBUGPATH;
00042 
00043         /*TODO: Make this faster*/
00044         for (i=0;i<NumMacs;i++){
00045                 if (memcmp(Macs[i].MAC, Mac, 6)==0) return &Macs[i];
00046         }
00047 
00048         if (Create){
00049                 if (NumMacs==MAX_MACS) return NULL;
00050 #ifdef DEBUG
00051                 printf("New Mac %02X:%02X:%02X:%02X:%02X:%02X\n",Mac[0],Mac[1],Mac[2],Mac[3],Mac[4],Mac[5]);
00052 #endif  
00053         
00054                 Macs[NumMacs].MAC[0]=Mac[0];
00055                 Macs[NumMacs].MAC[1]=Mac[1];
00056                 Macs[NumMacs].MAC[2]=Mac[2];
00057                 Macs[NumMacs].MAC[3]=Mac[3];
00058                 Macs[NumMacs].MAC[4]=Mac[4];
00059                 Macs[NumMacs].MAC[5]=Mac[5];
00060                 Macs[NumMacs].Count=1;
00061                 Macs[NumMacs].Interface=-1;
00062         
00063 #ifdef DEBUG
00064                 printf("There are now %i Macs\n",NumMacs+1);
00065 #endif
00066         
00067                 return &Macs[NumMacs++];
00068         }
00069         
00070         return NULL;
00071 }
00072 
00073 /*********************************
00074 * filter duplicates by mac address
00075 **********************************/
00076 int RouteMacFilter(int PacketSlot){
00077         MacRec*                 mac;
00078         MacRec*                 mac2;
00079         EthernetData*   EData;
00080         PacketRec*              p;
00081 
00082         DEBUGPATH;
00083         
00084         p=&Globals.Packets[PacketSlot];
00085         
00086         if (!IsInList(MacFilterInterfaceList, p->InterfaceNum)){
00087 #ifdef DEBUG
00088                 printf("MacFilter doesn't handle this interface\n");
00089 #endif          
00090                 return ROUTE_RESULT_CONTINUE;
00091         }
00092 
00093         if (!GetDataByID(p->PacketSlot, EthernetDecoderID, (void**)&EData)){
00094 #ifdef DEBUG
00095                 printf("This isn't an ethernet packet\n");
00096 #endif  
00097                 return ROUTE_RESULT_CONTINUE;
00098         }
00099 
00100 #ifdef DEBUG
00101         printf("%02x:%02x:%02x:%02x:%02x:%02x->",
00102                 EData->Header->SrcMac[0],
00103                 EData->Header->SrcMac[1],
00104                 EData->Header->SrcMac[2],
00105                 EData->Header->SrcMac[3],
00106                 EData->Header->SrcMac[4],
00107                 EData->Header->SrcMac[5]);
00108         printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
00109                 EData->Header->DstMac[0],
00110                 EData->Header->DstMac[1],
00111                 EData->Header->DstMac[2],
00112                 EData->Header->DstMac[3],
00113                 EData->Header->DstMac[4],
00114                 EData->Header->DstMac[5]);              
00115         printf("This packet is from %i(%s)\n",p->InterfaceNum, Globals.Interfaces[p->InterfaceNum].Name);
00116 #endif
00117 
00118         /*check to see if this is an ethernet broadcast*/
00119         if ( (EData->Header->DstMac[0]==0xFF) &&
00120                  (EData->Header->DstMac[1]==0xFF) &&
00121                  (EData->Header->DstMac[2]==0xFF) &&
00122                  (EData->Header->DstMac[3]==0xFF) &&
00123                  (EData->Header->DstMac[4]==0xFF) &&
00124                  (EData->Header->DstMac[5]==0xFF)
00125         ){
00126 #ifdef DEBUG
00127                 printf("This is an ethernet broadcast packet.  Send it to everyone\n");
00128 #endif  
00129                 mac=GetMac(EData->Header->SrcMac, 1);
00130                 mac->Interface=p->InterfaceNum;
00131                 p->TargetInterface=INTERFACE_BROADCAST;
00132                 return ROUTE_RESULT_CONTINUE;
00133         }
00134 
00135         
00136         mac=GetMac(EData->Header->SrcMac, 1);
00137         if (mac->Interface==-1){
00138                 mac->Interface=p->InterfaceNum;
00139                 return ROUTE_RESULT_DROP;
00140         }
00141         
00142         if (mac->Count<50) mac->Count++;
00143         
00144         if (mac->Count<3) return ROUTE_RESULT_DROP;
00145         
00146         mac2=GetMac(EData->Header->DstMac, 0);
00147         
00148         if  (!mac2){
00149 #ifdef DEBUG
00150                 printf("I don't know which interface this goes out.  Broadcast\n");
00151 #endif  
00152                 p->TargetInterface=INTERFACE_BROADCAST;
00153                 return ROUTE_RESULT_CONTINUE;
00154         }else{
00155 #ifdef DEBUG
00156                 printf("The target is on interface %i(%s)\n",mac2->Interface, Globals.Interfaces[mac2->Interface].Name);
00157 #endif          
00158                 p->TargetInterface=mac2->Interface;
00159                 return ROUTE_RESULT_CONTINUE;
00160         }
00161 }
00162 
00163 /*********************************
00164 * Add some members to the macfilter list
00165 **********************************/
00166 int RouteMacFilterAddNode(int RouteID, char* Args){
00167         NumAlias*       Aliases;
00168         int                     i;      
00169         int                     Count;
00170         
00171         DEBUGPATH;
00172 
00173 #ifdef DEBUG
00174         printf("AddNode was called with args %s\n", Args);
00175 #endif      
00176 
00177         if (!Args) return FALSE;
00178         
00179         Aliases=calloc(sizeof(NumAlias),Globals.NumInterfaces);
00180         for (i=0;i<Globals.NumInterfaces;i++){
00181                 snprintf(Aliases[i].Alias, 512, Globals.Interfaces[i].Name);
00182                 Aliases[i].Num=Globals.Interfaces[i].ID;
00183         }
00184         
00185         if (!AddRangesString(MacFilterInterfaceList, Args, Aliases, Globals.NumInterfaces)){
00186                 printf("Failed to parse interface list\n");
00187                 free(Aliases);
00188                 return FALSE;
00189         }
00190 
00191         Count=0;
00192         for (i=0;i<Globals.NumInterfaces;i++)
00193                 if (IsInList(MacFilterInterfaceList, i)){
00194 #ifdef DEBUG
00195                         printf("Interface %s is handled by macfilter\n",Globals.Interfaces[i].Name);
00196 #endif          
00197                         Count++;        
00198                 }else{
00199 #ifdef DEBUG            
00200                         printf("interface %s is not a macfilter interface\n",Globals.Interfaces[i].Name);
00201 #endif                  
00202                 }       
00203         
00204         if (Count<2){
00205                 printf("You must specify at least two interfaces to use macfilter\n");
00206                 free(Aliases);
00207                 return FALSE;
00208         }
00209         
00210         free(Aliases);
00211         return TRUE;
00212 }
00213 
00214 /*********************************
00215 * Set up everything to do mac
00216 * matching
00217 **********************************/
00218 int InitMacFilter(){
00219         int RouteID;
00220 
00221         DEBUGPATH;
00222 
00223         bzero(Macs, sizeof(MacRec) * MAX_MACS);
00224         
00225         if ( (RouteID=CreateRoute("MacFilter"))==ROUTE_NONE){
00226                 printf("Couldn't create route MacFilter\n");
00227                 return FALSE;
00228         }
00229         
00230         Globals.Routes[RouteID].RouteFunc=RouteMacFilter;
00231         Globals.Routes[RouteID].AddNode=RouteMacFilterAddNode;
00232         MacFilterInterfaceList=InitNumList(LIST_TYPE_NORMAL);
00233         
00234         if ( (EthernetDecoderID=GetDecoderByName("Ethernet"))==DECODER_NONE){
00235                 printf("Couldn't find the Ethernet Deoder\n");
00236                 return FALSE;
00237         }
00238         
00239         return TRUE;
00240 }
00241 

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2