1 /******************************************************************************
6 Description: Encapsulation Functions for DCCP conversion to TCP
8 ******************************************************************************/
13 /*Encapsulation start point and link layer selector*/
14 int do_encap(int link, struct packet *new, const struct const_packet *old)
19 if(!ethernet_encap(new, old)){
25 if(!ipv4_encap(new, old)){
30 /*Linux Cooked Capture*/
31 if(!linux_cooked_encap(new, old)){
36 dbgprintf(0, "Unknown Link Layer\n");
40 /*Adjust libpcap header*/
41 if(new->h->caplen >= new->h->len || new->h->caplen >= new->length){
42 new->h->caplen=new->length;
44 new->h->len=new->length;
49 /*Standard Ethernet Encapsulation*/
50 int ethernet_encap(struct packet *new, const struct const_packet *old)
52 struct ether_header *ethh;
53 struct const_packet nold;
57 if(!new || !old || !new->data || !old->data || !new->h || !old->h){
58 dbgprintf(0,"Error: Ethernet Encapsulation Function given bad data!\n");
61 if(old->length < sizeof(struct ether_header) || new->length < sizeof(struct ether_header)){
62 dbgprintf(0, "Error: Ethernet Encapsulation Function given packet of wrong size!\n");
66 /*Copy Ethernet header over*/
67 memcpy(new->data, old->data, sizeof(struct ether_header));
70 ethh=(struct ether_header*)(new->data);
72 /*Adjust pointers and lengths*/
73 nold.data= old->data+ sizeof(struct ether_header);
74 nnew.data= new->data + sizeof(struct ether_header);
75 nold.length= old->length - sizeof(struct ether_header);
76 nnew.length= new->length - sizeof(struct ether_header);
78 /*Select Next Protocol*/
79 switch(ntohs(ethh->ether_type)){
81 if(!ipv4_encap(&nnew, &nold)){
86 dbgprintf(1, "Unknown Next Protocol at Ethernet\n");
92 new->length=nnew.length + sizeof(struct ether_header);
96 /*IPv4 Encapsulation*/
97 int ipv4_encap(struct packet *new, const struct const_packet *old)
101 struct const_packet nold;
104 if(!new || !old || !new->data || !old->data || !new->h || !old->h){
105 dbgprintf(0,"Error: IPv4 Encapsulation Function given bad data!\n");
108 if(old->length < sizeof(struct iphdr) || new->length < sizeof(struct iphdr)){
109 dbgprintf(0, "Error: IPv4 Encapsulation Function given packet of wrong size!\n");
113 /*Copy IPv4 header over*/
114 memcpy(new->data, old->data, sizeof(struct iphdr));
117 iph=(struct iphdr*)(new->data);
119 /*Adjust pointers and lengths*/
120 nold.data= old->data +iph->ihl*4;
121 nnew.data= new->data +iph->ihl*4;
122 nold.length= old->length -iph->ihl*4;
123 nnew.length= new->length -iph->ihl*4;
125 /*Confirm that this is IPv4*/
127 dbgprintf(1, "Note: Packet is not IPv4\n");
131 /*Select Next Protocol*/
132 switch(iph->protocol){
135 nnew.src_id=iph->saddr;
136 nnew.dest_id=iph->daddr;
137 if(!convert_packet(&nnew, &nold)){
142 dbgprintf(1, "Unknown Next Protocol at IPv4\n");
147 /*set ip to indicate that TCP is next protocol*/
152 new->length=nnew.length + iph->ihl*4;
154 /*Determine if computed length is reasonable*/
155 if(nnew.length > 0xFFFF){
156 dbgprintf(1, "Error: Given TCP header+data length is too large for an IPv4 packet!\n");
160 /*Adjust IPv4 header to account for packet's total length*/
161 iph->tot_len=htons(nnew.length);
165 int linux_cooked_encap(struct packet *new, const struct const_packet *old)
167 struct sll_header *slh;
169 struct const_packet nold;
173 if(!new|| !old || !new->data || !old->data || !new->h || !old->h){
174 dbgprintf(0,"Error: SLL Encapsulation Function given bad data!\n");
177 if(old->length < sizeof(struct sll_header) || new->length < sizeof(struct sll_header)){
178 dbgprintf(0, "Error: SLL Encapsulation Function given packet of wrong size!\n");
182 /*Copy SLL header over*/
183 memcpy(new->data, old->data, sizeof(struct sll_header));
186 slh=(struct sll_header*)(new->data);
188 /*Adjust pointers and lengths*/
189 nold.data= old->data + sizeof(struct sll_header);
190 nnew.data= new->data + sizeof(struct sll_header);
191 nold.length= old->length - sizeof(struct sll_header);
192 nnew.length= new->length- sizeof(struct sll_header);
194 /*Confirm that this is SLL*/
195 if(ntohs(slh->sll_pkttype) > 4){
196 dbgprintf(1, "Note: Packet is not SLL (Linux Cooked Capture)\n");
200 /*Select Next Protocol*/
201 switch(ntohs(slh->sll_protocol)){
203 if(!ipv4_encap(&nnew, &nold)){
208 dbgprintf(1, "Unknown Next Protocol at SLL\n");
214 new->length=nnew.length + sizeof(struct sll_header);