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);
80 /*Select Next Protocol*/
81 switch(ntohs(ethh->ether_type)){
83 if(!ipv4_encap(&nnew, &nold)){
88 dbgprintf(1, "Unknown Next Protocol at Ethernet\n");
94 new->length=nnew.length + sizeof(struct ether_header);
98 /*IPv4 Encapsulation*/
99 int ipv4_encap(struct packet *new, const struct const_packet *old)
103 struct const_packet nold;
106 if(!new || !old || !new->data || !old->data || !new->h || !old->h){
107 dbgprintf(0,"Error: IPv4 Encapsulation Function given bad data!\n");
110 if(old->length < sizeof(struct iphdr) || new->length < sizeof(struct iphdr)){
111 dbgprintf(0, "Error: IPv4 Encapsulation Function given packet of wrong size!\n");
115 /*Copy IPv4 header over*/
116 memcpy(new->data, old->data, sizeof(struct iphdr));
119 iph=(struct iphdr*)(new->data);
121 /*Adjust pointers and lengths*/
122 nold.data= old->data +iph->ihl*4;
123 nnew.data= new->data +iph->ihl*4;
124 nold.length= old->length -iph->ihl*4;
125 nnew.length= new->length -iph->ihl*4;
129 /*Confirm that this is IPv4*/
131 dbgprintf(1, "Note: Packet is not IPv4\n");
135 /*Select Next Protocol*/
136 switch(iph->protocol){
139 nnew.src_id=iph->saddr;
140 nnew.dest_id=iph->daddr;
141 if(!convert_packet(&nnew, &nold)){
146 dbgprintf(1, "Unknown Next Protocol at IPv4\n");
151 /*set ip to indicate that TCP is next protocol*/
156 new->length=nnew.length + iph->ihl*4;
158 /*Determine if computed length is reasonable*/
159 if(nnew.length > 0xFFFF){
160 dbgprintf(1, "Error: Given TCP header+data length is too large for an IPv4 packet!\n");
164 /*Adjust IPv4 header to account for packet's total length*/
165 iph->tot_len=htons(new->length);
169 int linux_cooked_encap(struct packet *new, const struct const_packet *old)
171 struct sll_header *slh;
173 struct const_packet nold;
177 if(!new|| !old || !new->data || !old->data || !new->h || !old->h){
178 dbgprintf(0,"Error: SLL Encapsulation Function given bad data!\n");
181 if(old->length < sizeof(struct sll_header) || new->length < sizeof(struct sll_header)){
182 dbgprintf(0, "Error: SLL Encapsulation Function given packet of wrong size!\n");
186 /*Copy SLL header over*/
187 memcpy(new->data, old->data, sizeof(struct sll_header));
190 slh=(struct sll_header*)(new->data);
192 /*Adjust pointers and lengths*/
193 nold.data= old->data + sizeof(struct sll_header);
194 nnew.data= new->data + sizeof(struct sll_header);
195 nold.length= old->length - sizeof(struct sll_header);
196 nnew.length= new->length- sizeof(struct sll_header);
200 /*Confirm that this is SLL*/
201 if(ntohs(slh->sll_pkttype) > 4){
202 dbgprintf(1, "Note: Packet is not SLL (Linux Cooked Capture)\n");
206 /*Select Next Protocol*/
207 switch(ntohs(slh->sll_protocol)){
209 if(!ipv4_encap(&nnew, &nold)){
214 dbgprintf(1, "Unknown Next Protocol at SLL\n");
220 new->length=nnew.length + sizeof(struct sll_header);