]> sjero.net Git - dccp2tcp/blobdiff - encap.c
Checksum computation!
[dccp2tcp] / encap.c
diff --git a/encap.c b/encap.c
index beafa6814466b57b5887e13e445f9cae730bdcc9..f583f0994303568d4aeb2d62ffc19f99a7e594aa 100644 (file)
--- a/encap.c
+++ b/encap.c
@@ -1,14 +1,36 @@
 /******************************************************************************
-Author: Samuel Jero
+Utility to convert a DCCP flow to a TCP flow for DCCP analysis via
+               tcptrace. Encapsulation Functions for DCCP conversion to TCP.
 
-Date: 7/2011
+Copyright (C) 2012  Samuel Jero <sj323707@ohio.edu>
 
-Description: Encapsulation Functions for DCCP conversion to TCP
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Author: Samuel Jero <sj323707@ohio.edu>
+Date: 11/2012
+
+Notes:
+       1)CCID2 ONLY
+       2)DCCP MUST use 48 bit sequence numbers
+       3)DCCP DATA packets are not implemented (Linux doesn't use them)
+       4)DCCP Ack packets show up as TCP packets containing one byte
 ******************************************************************************/
 #include "dccp2tcp.h"
 #include "encap.h"
-#include "pcap/sll.h"
+#include "checksums.h"
+#include <pcap/sll.h>
+#include <netinet/ip6.h>
 
 /*Encapsulation start point and link layer selector*/
 int do_encap(int link, struct packet *new, const struct const_packet *old)
@@ -74,16 +96,21 @@ int ethernet_encap(struct packet *new, const struct const_packet *old)
                nnew.data= new->data + sizeof(struct ether_header);
                nold.length= old->length - sizeof(struct ether_header);
                nnew.length= new->length - sizeof(struct ether_header);
+               nnew.h=new->h;
+               nold.h=old->h;
 
                /*Select Next Protocol*/
                switch(ntohs(ethh->ether_type)){
                        case ETHERTYPE_IP:
-                                       nnew.h=new->h;
-                                       nold.h=old->h;
                                        if(!ipv4_encap(&nnew, &nold)){
                                                        return 0;
                                        }
                                        break;
+                       case ETHERTYPE_IPV6:
+                                       if(!ipv6_encap(&nnew, &nold)){
+                                                       return 0;
+                                       }
+                                       break;
                        default:
                                        dbgprintf(1, "Unknown Next Protocol at Ethernet\n");
                                        return 0;
@@ -95,6 +122,87 @@ int ethernet_encap(struct packet *new, const struct const_packet *old)
 return 1;
 }
 
+/*IPv6 Encapsulation*/
+int ipv6_encap(struct packet *new, const struct const_packet *old)
+{
+               struct ip6_hdr          *iph;
+               struct packet           nnew;
+               struct const_packet     nold;
+
+               /*Safety checks*/
+               if(!new || !old || !new->data || !old->data || !new->h || !old->h){
+                       dbgprintf(0,"Error: IPv6 Encapsulation Function given bad data!\n");
+                       return 0;
+               }
+               if(old->length < sizeof(struct ip6_hdr) || new->length < sizeof(struct ip6_hdr)){
+                       dbgprintf(0, "Error: IPv6 Encapsulation Function given packet of wrong size!\n");
+                       return 0;
+               }
+
+               /*Copy IPv6 header over*/
+               memcpy(new->data, old->data, sizeof(struct ip6_hdr));
+
+               /*Cast Pointer*/
+               iph=(struct ip6_hdr*)(new->data);
+
+               /*Adjust pointers and lengths*/
+               nold.data= old->data + sizeof(struct ip6_hdr);
+               nnew.data= new->data +sizeof(struct ip6_hdr);
+               nold.length= old->length - sizeof(struct ip6_hdr);
+               nnew.length= new->length - sizeof(struct ip6_hdr);
+               nnew.h=new->h;
+               nold.h=old->h;
+
+               /*Confirm that this is IPv6*/
+               if((ntohl(iph->ip6_ctlun.ip6_un1.ip6_un1_flow) & (0xF0000000)) == (60000000)){
+                       dbgprintf(1, "Note: Packet is not IPv6\n");
+                       return 0;
+               }
+
+               /*Select Next Protocol*/
+               switch(iph->ip6_ctlun.ip6_un1.ip6_un1_nxt){
+                       case 33:
+                                       /*DCCP*/
+                                       nnew.id_len=16;
+                                       nnew.src_id=malloc(nnew.id_len);
+                                       nnew.dest_id=malloc(nnew.id_len);
+                                       if(nnew.src_id==NULL||nnew.dest_id==NULL){
+                                               dbgprintf(0,"Error: Couldn't allocate Memory\n");
+                                               exit(1);
+                                       }
+                                       memcpy(nnew.src_id,&iph->ip6_src,nnew.id_len);
+                                       memcpy(nnew.dest_id,&iph->ip6_dst,nnew.id_len);
+                                       if(!convert_packet(&nnew, &nold)){
+                                               return 0;
+                                       }
+                                       break;
+                       default:
+                                       dbgprintf(1, "Unknown Next Protocol at IPv6\n");
+                                       return 0;
+                                       break;
+               }
+
+               /*set ip to indicate that TCP is next protocol*/
+               iph->ip6_ctlun.ip6_un1.ip6_un1_nxt=6;
+
+               /*Determine if computed length is reasonable*/
+               if(nnew.length > 0xFFFF){
+                               dbgprintf(1, "Error: Given TCP data length is too large for an IPv6 packet!\n");
+                               return 0;
+               }
+
+               /*Adjust IPv6 header to account for packet's total length*/
+               iph->ip6_ctlun.ip6_un1.ip6_un1_plen=htons(new->length);
+
+               /*Adjust length*/
+               new->length=nnew.length + sizeof(struct ip6_hdr);
+
+               /*Cleanup*/
+               free(nnew.dest_id);
+               free(nnew.src_id);
+return 1;
+}
+
 /*IPv4 Encapsulation*/
 int ipv4_encap(struct packet *new, const struct const_packet *old)
 {
@@ -123,6 +231,8 @@ int ipv4_encap(struct packet *new, const struct const_packet *old)
                nnew.data= new->data +iph->ihl*4;
                nold.length= old->length -iph->ihl*4;
                nnew.length= new->length -iph->ihl*4;
+               nnew.h=new->h;
+               nold.h=old->h;
 
                /*Confirm that this is IPv4*/
                if(iph->version!=4){
@@ -132,12 +242,17 @@ int ipv4_encap(struct packet *new, const struct const_packet *old)
 
                /*Select Next Protocol*/
                switch(iph->protocol){
-                       case 0x21:
+                       case 33:
                                        /*DCCP*/
-                                       nnew.h=new->h;
-                                       nold.h=old->h;
-                                       nnew.src_id=iph->saddr;
-                                       nnew.dest_id=iph->daddr;
+                                       nnew.id_len=4;
+                                       nnew.src_id=malloc(nnew.id_len);
+                                       nnew.dest_id=malloc(nnew.id_len);
+                                       if(nnew.src_id==NULL||nnew.dest_id==NULL){
+                                               dbgprintf(0,"Error: Couldn't allocate Memory\n");
+                                               exit(1);
+                                       }
+                                       memcpy(nnew.src_id,&iph->saddr,nnew.id_len);
+                                       memcpy(nnew.dest_id,&iph->daddr,nnew.id_len);
                                        if(!convert_packet(&nnew, &nold)){
                                                return 0;
                                        }
@@ -162,7 +277,15 @@ int ipv4_encap(struct packet *new, const struct const_packet *old)
                }
 
                /*Adjust IPv4 header to account for packet's total length*/
-               iph->tot_len=htons(nnew.length);
+               iph->tot_len=htons(new->length);
+
+               /*Compute IPv4 Checksum*/
+               iph->check=0;
+               iph->check=ipv4_chksum(new->data,iph->ihl*4);
+
+               /*Cleanup*/
+               free(nnew.src_id);
+               free(nnew.dest_id);
 return 1;
 }
 
@@ -194,6 +317,8 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old)
        nnew.data= new->data + sizeof(struct sll_header);
        nold.length= old->length - sizeof(struct sll_header);
        nnew.length= new->length- sizeof(struct sll_header);
+       nnew.h=new->h;
+       nold.h=old->h;
 
        /*Confirm that this is SLL*/
        if(ntohs(slh->sll_pkttype) > 4){
@@ -204,12 +329,15 @@ int linux_cooked_encap(struct packet *new, const struct const_packet *old)
        /*Select Next Protocol*/
        switch(ntohs(slh->sll_protocol)){
                case ETHERTYPE_IP:
-                               nnew.h=new->h;
-                               nold.h=old->h;
                                if(!ipv4_encap(&nnew, &nold)){
                                                return 0;
                                }
                                break;
+               case ETHERTYPE_IPV6:
+                               if(!ipv6_encap(&nnew, &nold)){
+                                               return 0;
+                               }
+                               break;
                default:
                                dbgprintf(1, "Unknown Next Protocol at SLL\n");
                                return 0;