diff -Naur tomahawk1.1/packetutil.c tomahawk/packetutil.c --- tomahawk1.1/packetutil.c 2006-04-26 15:21:57.000000000 -0700 +++ tomahawk/packetutil.c 2007-04-20 10:54:20.000000000 -0700 @@ -94,6 +94,9 @@ int i; unsigned char foundId = 0; + if( debugFlag ) + printf("in GetHandlerId()\n"); + for ( i=loopStart; i<=loopEnd ; i++ ) { if ( activeHandlers[i] == (Handler *) NULL ) { foundId = 1; @@ -101,8 +104,12 @@ } } + if( debugFlag ) + printf("loopStart=%d,loopEnd=%d\n",loopStart,loopEnd); + /* If we did not find an ID, return 0 */ if ( !foundId ) { + printf("didn't find an ID"); return 0; } @@ -255,6 +262,7 @@ unsigned short IncrAddr (unsigned short x1, int n) { + in_addr_t tempAddr; x1 += n; tempAddr = (__bswap_16(x1)<<16); @@ -266,6 +274,7 @@ unsigned char addrType = IsBroadcast( tempAddr ); while ( (addrType & BCAST_255) || (addrType & BCAST_0) ) { + printf("broadcast\n"); x1++; tempAddr = (__bswap_16(x1)<<16); addrType = IsBroadcast( tempAddr ); @@ -305,11 +314,21 @@ int i; memcpy (&iph, packetData+14, sizeof(iph)); - wireIP = iph.daddr; + wireIP = htonl(iph.daddr); + + if( debugFlag ) { + printf("GetHandler()\n"); + printf("wireIP = %08x\n",wireIP); + } if ( modAddrs ) { + /* Get the handler ID from the 2nd byte of the IP address */ - i = (wireIP>>8) & 0xFF; + + i = (wireIP) & 0xFF; + + if( debugFlag ) + printf("i = %d\n",(wireIP)&0xFF); /* If the handler ID is not in the range we are responsible for, return NULL */ if ( i < startId || i > endId ) return NULL; @@ -392,7 +411,7 @@ sizeof(TraceIP)*trace->maxIPs); } } - trace->traceIP[trace->numIPs].addr = addr; + trace->traceIP[trace->numIPs].addr = addr; trace->traceIP[trace->numIPs].mapIp = mapIp; trace->traceIP[trace->numIPs].iface = iface; trace->numIPs++; @@ -429,7 +448,15 @@ if (*ether == ':') { ether++; } + + int tmp = (u_char)strtol(temp, NULL, 16); + + if(debugFlag) + printf("dst[%d] = %02x\n",i,tmp&0xff); + dst[i++] = (u_char)strtol(temp, NULL, 16); + + } return (i == 6); } @@ -697,25 +724,26 @@ */ in_addr_t -GetWireIP(unsigned short handlerId, unsigned short wireIPSuffix, in_addr_t addr, unsigned char startAddrByte) +GetWireIP(unsigned short handlerId, unsigned short wireIPSuffix, in_addr_t addr, unsigned short startAddrByte) { - /* Assign the handler ID and wireIPSuffix to the address */ - unsigned int wireIP = (__bswap_16(wireIPSuffix)<<16) | (handlerId<<8); + unsigned int wireIP = 0; + wireIP |= handlerId; + wireIP |= (wireIPSuffix & 0xff) << 8; if ( startAddrByte ) { /* * if startAddrByte is not zero, use it for the first byte of the address */ - wireIP = wireIP | startAddrByte; + wireIP = wireIP | (startAddrByte << 16); } else { /* * Get the first byte from the original address */ - wireIP = wireIP | ( addr & 0xFF ); + wireIP = wireIP | (ntohl(addr) & 0xffffff00); } - return wireIP; + return htonl(wireIP); } /* diff -Naur tomahawk1.1/packetutil.h tomahawk/packetutil.h --- tomahawk1.1/packetutil.h 2006-04-26 14:47:13.000000000 -0700 +++ tomahawk/packetutil.h 2007-04-20 10:54:20.000000000 -0700 @@ -204,7 +204,7 @@ void PrintHandlerSendGroup(Handler *h); void PrintFlowsOutStateTable(Handler *h); int PacketEqual(Packet *p, const u_char *data, int caplen, int actualLen); -in_addr_t GetWireIP(unsigned short handlerId, unsigned short wireIPSuffix, in_addr_t addr, unsigned char startAddrByte); +in_addr_t GetWireIP(unsigned short handlerId, unsigned short wireIPSuffix, in_addr_t addr, unsigned short startAddrByte); unsigned short NewChecksum(unsigned short check, unsigned int old, unsigned int new); unsigned int CRC32(unsigned char *message, int msgLength); unsigned short HashIpData (u_int8_t protocol, unsigned short sip, unsigned short dip); diff -Naur tomahawk1.1/tomahawk.c tomahawk/tomahawk.c --- tomahawk1.1/tomahawk.c 2006-04-26 15:23:37.000000000 -0700 +++ tomahawk/tomahawk.c 2007-04-20 10:58:33.000000000 -0700 @@ -73,6 +73,7 @@ char device[32]; /* Device name (e.g., "eth1") */ FileCB *cb; /* File callback */ Mac eaddr; /* Ethernet address of interface */ + Mac *daddr; /* Ethernet address to send to */ struct sockaddr_ll sa; /* Socket address -- attaches socket to * device */ } Interface; @@ -126,8 +127,8 @@ */ static unsigned char maxHandlerIdInUse = 0; -static char *startAddr = NULL; -static unsigned char firstByte = 0; +static unsigned short serverFirstBytes = 0; +static unsigned short clientFirstBytes = 0; static FileInfo fileInfo[65536]; static Interface if1, if2; static Handler *handlerList = NULL; @@ -257,6 +258,10 @@ fprintf(stderr, " -n maxActive Max number of handlers for a file\n"); fprintf(stderr, " -l loops Number of times to replay file\n"); fprintf(stderr, " -f file Pcap file to play\n"); + fprintf(stderr, " -x out-mac Client side MAC address of router\n"); + fprintf(stderr, " -y in-mac Server side MAC address of router\n"); + fprintf(stderr, " -X out-subnet Client side subnet ( 10.10.0.0 )\n"); + fprintf(stderr, " -Y in-subnet Server side subnet ( 192.168.0.0 )\n"); exit(1); } @@ -1417,25 +1422,45 @@ ph->ether_shost[3] = if1.eaddr[3]; ph->ether_shost[4] = if1.eaddr[4]; ph->ether_shost[5] = if1.eaddr[5]; - ph->ether_dhost[0] = if2.eaddr[0]; - ph->ether_dhost[1] = if2.eaddr[1]; - ph->ether_dhost[2] = if2.eaddr[2]; - ph->ether_dhost[3] = if2.eaddr[3]; - ph->ether_dhost[4] = if2.eaddr[4]; - ph->ether_dhost[5] = if2.eaddr[5]; + if (if1.daddr) { + ph->ether_dhost[0] = (*if1.daddr)[0]; + ph->ether_dhost[1] = (*if1.daddr)[1]; + ph->ether_dhost[2] = (*if1.daddr)[2]; + ph->ether_dhost[3] = (*if1.daddr)[3]; + ph->ether_dhost[4] = (*if1.daddr)[4]; + ph->ether_dhost[5] = (*if1.daddr)[5]; + } + else { + ph->ether_dhost[0] = if2.eaddr[0]; + ph->ether_dhost[1] = if2.eaddr[1]; + ph->ether_dhost[2] = if2.eaddr[2]; + ph->ether_dhost[3] = if2.eaddr[3]; + ph->ether_dhost[4] = if2.eaddr[4]; + ph->ether_dhost[5] = if2.eaddr[5]; + } } else { - ph->ether_dhost[0] = if1.eaddr[0]; - ph->ether_dhost[1] = if1.eaddr[1]; - ph->ether_dhost[2] = if1.eaddr[2]; - ph->ether_dhost[3] = if1.eaddr[3]; - ph->ether_dhost[4] = if1.eaddr[4]; - ph->ether_dhost[5] = if1.eaddr[5]; ph->ether_shost[0] = if2.eaddr[0]; ph->ether_shost[1] = if2.eaddr[1]; ph->ether_shost[2] = if2.eaddr[2]; ph->ether_shost[3] = if2.eaddr[3]; ph->ether_shost[4] = if2.eaddr[4]; ph->ether_shost[5] = if2.eaddr[5]; + if (if2.daddr) { + ph->ether_dhost[0] = (*if2.daddr)[0]; + ph->ether_dhost[1] = (*if2.daddr)[1]; + ph->ether_dhost[2] = (*if2.daddr)[2]; + ph->ether_dhost[3] = (*if2.daddr)[3]; + ph->ether_dhost[4] = (*if2.daddr)[4]; + ph->ether_dhost[5] = (*if2.daddr)[5]; + } + else { + ph->ether_dhost[0] = if1.eaddr[0]; + ph->ether_dhost[1] = if1.eaddr[1]; + ph->ether_dhost[2] = if1.eaddr[2]; + ph->ether_dhost[3] = if1.eaddr[3]; + ph->ether_dhost[4] = if1.eaddr[4]; + ph->ether_dhost[5] = if1.eaddr[5]; + } } /* Compute the FCS on the Ethernet Frame @@ -1935,8 +1960,22 @@ assert (hlen < sizeof(save)); memcpy (&save, p[pktId].buffer+14, hlen); if (t->modAddrs) { - iph.saddr = GetWireIP(h->id, p[pktId].wireSrc, p[pktId].saddr, firstByte); - iph.daddr = GetWireIP(h->id, p[pktId].wireDst, p[pktId].daddr, firstByte); + + if(debugFlag) + printf("modifying addrs...\n"); + + if (h->flowsOutStateTable[ p[pktId].ipHash ].iface) { + if( debugFlag ) + printf("writing out if1...\n"); + iph.saddr = GetWireIP(h->id, p[pktId].wireSrc, p[pktId].saddr, serverFirstBytes); + iph.daddr = GetWireIP(h->id, p[pktId].wireDst, p[pktId].daddr, clientFirstBytes); + } else { + if( debugFlag ) + printf("writing out if2...\n"); + iph.saddr = GetWireIP(h->id, p[pktId].wireSrc, p[pktId].saddr, clientFirstBytes); + iph.daddr = GetWireIP(h->id, p[pktId].wireDst, p[pktId].daddr, serverFirstBytes); + } + /* * Incremental checksum calculation (RFC 1624) @@ -2362,10 +2401,10 @@ if (t->modAddrs) { char buffer[48]; struct in_addr charIP; - charIP.s_addr= GetWireIP(h->id, p[unrecvPktId].wireSrc, p[unrecvPktId].saddr, firstByte); + charIP.s_addr= GetWireIP(h->id, p[unrecvPktId].wireSrc, p[unrecvPktId].saddr, serverFirstBytes); sprintf(buffer," (Wire IP: src %s, ",inet_ntoa(charIP)); strcat(message, buffer); - charIP.s_addr = GetWireIP(h->id, p[unrecvPktId].wireDst, p[unrecvPktId].daddr, firstByte); + charIP.s_addr = GetWireIP(h->id, p[unrecvPktId].wireDst, p[unrecvPktId].daddr, serverFirstBytes); sprintf(buffer,"dst %s)", inet_ntoa(charIP)); strcat(message, buffer); } @@ -2457,6 +2496,7 @@ { char *interface1; char *interface2; + char *mac1=0, *mac2=0; extern char *optarg; extern int optind; int ch, files = 0; @@ -2477,7 +2517,7 @@ interface1 = "eth0"; interface2 = "eth1"; - while ((ch = getopt(argc, argv, "qdZhWN:l:L:t:R:A:r:a:m:n:i:j:e:s:f:w:")) != -1) { + while ((ch = getopt(argc, argv, "qdZhWN:l:L:t:R:A:r:a:m:n:i:j:e:s:f:w:x:y:X:Y:")) != -1) { switch (ch) { case 'N': maxActive = atoi(optarg); @@ -2537,9 +2577,8 @@ modAddrs = atoi(optarg); break; case 'a': - startAddr = optarg; - firstByte = inet_addr(startAddr) & 0xFF; - break; + clientFirstBytes = (ntohl(inet_addr(optarg)) >> 16) & 0xFFFF; + serverFirstBytes = clientFirstBytes; case 'm': maxOutstanding = atoi(optarg); if ( maxOutstanding > MAX_PACKETS_OUTSTANDING ) maxOutstanding = MAX_PACKETS_OUTSTANDING; @@ -2553,12 +2592,24 @@ case 'j': interface2 = optarg; break; + case 'x': + mac1 = optarg; + break; + case 'y': + mac2 = optarg; + break; + case 'X': + serverFirstBytes = (ntohl(inet_addr(optarg)) >> 16) & 0xFFFF; + break; + case 'Y': + clientFirstBytes = (ntohl(inet_addr(optarg)) >> 16) & 0xFFFF; + break; case 'f': fileInfo[numFiles].name = optarg; fileInfo[numFiles].maxActive = traceMaxActive; fileInfo[numFiles].retrans = retrans; fileInfo[numFiles].timeout = timeout; - fileInfo[numFiles].loop = loop; + fileInfo[numFiles].loop = (int)loop; fileInfo[numFiles].modAddrs = modAddrs; numFiles++; break; @@ -2620,6 +2671,28 @@ } InitializeActiveHandlers(startHandlerId,endHandlerId); + if ((mac1 && !mac2) || (!mac1 && mac2)) { + fprintf(stderr, "Need both client and server MAC's\n"); + exit(1); + } + + if (mac1 != 0) { + if1.daddr = calloc(1, sizeof *if1.daddr); + if (ParseEtherAddr(mac1, *if1.daddr) == 0) { + fprintf(stderr, "Invalid mac address '%s'\n", mac1); + exit(1); + } + } + + if (mac2 != 0) { + if2.daddr = calloc(1, sizeof *if2.daddr); + + if (ParseEtherAddr(mac2, *if2.daddr) == 0) { + fprintf(stderr, "Invalid mac address '%s'\n", mac2); + exit(1); + } + } + memset (if1.eaddr, 1, sizeof(if1.eaddr)); memset (if2.eaddr, 2, sizeof(if2.eaddr)); OpenInterface (interface1, &if1);