1 /******************************************************************************
6 Description: <SLL, IPv4, UDP> encapsulation functions
8 ******************************************************************************/
11 #include <netinet/udp.h>
17 /*SLL encapsulation private data structure*/
20 struct pcap_pkthdr header;
21 u_char od[sizeof(struct sll_header)+sizeof(struct iphdr)+sizeof(struct udphdr)];
25 /*Fill the encapsulation structure*/
26 int fill_sllip4_encap(struct sll_en_p *slip, const u_char* data, int dlen, struct pcap_pkthdr *h){
28 if(slip==NULL || data==NULL || h==NULL || dlen < sizeof(struct sll_header)+sizeof(struct iphdr)){
29 dbgprintf(1, "Error: SLL, IPv4 Encapsulation method given bad data!\n");
34 /* First time, allocate memory and copy libpcap header and encap headers
35 * this guarantees the IP "direction" of the encap headers */
36 memcpy(&slip->header, h, sizeof(struct pcap_pkthdr));
37 memcpy(slip->od, data,sizeof(struct sll_header)+sizeof(struct iphdr));
40 /* Just update the libpcap header (and associated timestamp)*/
41 memcpy(&slip->header, h, sizeof(struct pcap_pkthdr));
46 /* encapsulation manipulation previous to packet conversion */
47 int sll_encap_pre(struct pcap_pkthdr *h, const u_char **odata, u_char **ndata, int* olength, int* nlength)
50 struct sll_header *slh;
54 if(!h || !odata || !ndata || !*odata || !*ndata || !olength || !nlength){
55 dbgprintf(0,"Error: SLL Encapsulation given bad data!\n");
58 if(*olength < sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)
59 || *nlength < sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct tcphdr)){
60 dbgprintf(0, "Error: SLL Encapsulation given packet of wrong size!\n");
64 /*initialize encapsulation private data*/
65 if(state.en_priv==NULL){
66 /* First time, allocate memory and copy libpcap header and encap headers
67 * this guarantees the IP "direction" of the encap headers */
68 state.en_priv=malloc(sizeof(struct sll_en_p));
69 if(state.en_priv==NULL){
70 dbgprintf(0,"Error: Couldn't allocate Memory\n");
74 if(fill_sllip4_encap((struct sll_en_p*)state.en_priv, *odata, *olength, h)<0){
78 /*Copy SLL and IPv4 headers over*/
79 memcpy(*ndata, *odata, sizeof(struct sll_header)+sizeof(struct iphdr));
80 *odata+=sizeof(struct sll_header)+ sizeof(struct iphdr);
81 *ndata+=sizeof(struct sll_header)+ sizeof(struct iphdr);
83 /*Confirm that this is Ethernet and that IPv4 is next*/
84 slh=(struct sll_header*)(*odata -sizeof(struct sll_header)- sizeof(struct iphdr));
85 if(slh->sll_protocol!=htons(ETHERTYPE_IP)){
86 dbgprintf(1, "Note: Packet not SLL or Not IPv4 next\n");
90 /* Check That this is IPv4 and that UDP is next*/
91 iph= (struct iphdr *) (*ndata - sizeof(struct iphdr));
93 dbgprintf(1, "Note: Packet is not IPv4\n");
96 if(iph->protocol!=0x11){
97 dbgprintf(1, "Note: Packet is not UDP\n");
101 /*set ip to indicate that tcp is next protocol*/
105 /* Adjust libpcap headers*/
106 h->caplen=sizeof(struct sll_header) +sizeof(struct iphdr);
107 h->len=sizeof(struct sll_header) +sizeof(struct iphdr);
109 /*Adjust packet length*/
110 udph=(struct udphdr*)*odata;
111 *olength=ntohs(udph->len);
113 /*Adjust New Packet Length*/
114 *nlength-=sizeof(struct sll_header) +sizeof(struct iphdr);
116 /*Move Packet Pointer past UDP header*/
117 *odata+=sizeof(struct udphdr);
121 /* encapsulation manipulation after conversion */
122 int sll_encap_post(int tlen, u_char *data)
126 /* Move data pointer to start of IPv4 header*/
127 data+=sizeof(struct sll_header);
129 /*Determine if the given length is reasonable*/
130 if((tlen+sizeof(struct iphdr)) > 0xFFFF){
131 dbgprintf(1, "Error: Given TCP header+data length is too large for an IPv4 packet!\n");
135 /*Adjust IPv4 header to account for packet's total length*/
136 iph=(struct iphdr*)data;
137 iph->tot_len=htons(sizeof(struct iphdr)+tlen);
141 /* Create a TCP three-way handshake */
142 int sll_encap_handshake(struct pcap_pkthdr *h)
148 struct pcap_pkthdr nh;
150 struct sll_en_p *slip=(struct sll_en_p*)state.en_priv;
154 if(h==NULL || state.en_priv==NULL){
155 dbgprintf(1, "Error: SLL, IPv4 Encapsulation handshake method given bad data!\n");
159 /*create new libpcap header*/
160 memcpy(&nh, h, sizeof(struct pcap_pkthdr));
162 /*create buffer for new packet*/
163 ptr=data=malloc(MAX_PACKET);
165 dbgprintf(0,"Error: Couldn't allocate Memory\n");
169 /* 1)Create Syn Packet*/
170 /*make sure the packet is all zero*/
171 memset(data, 0, MAX_PACKET);
174 /*Set the libpcap header*/
175 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr)+4;
176 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr)+4;
177 nh.ts.tv_usec-=3000; /*Time comes from the first packet received, so make these packets earlier*/
179 /* Copy SLL and IP headers from private data area*/
180 /* These are headers from the first packet in the capture*/
181 memcpy(ptr, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
184 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
187 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr)+4);
190 ptr+=sizeof(struct sll_header)+ sizeof(struct iphdr);
191 tcph=(struct tcphdr*)ptr;
192 tcph->source=htons(1113);
193 tcph->dest=htons(1113);
195 tcph->check=htonl(0);
204 /*Initialize Sequence and Acknowledgment Numbers and Window*/
205 tcph->seq=htonl(state.seq_num++);
206 tcph->ack_seq=htonl(0);
207 tcph->window=htons(WIN_FACTOR);
209 /* Add SACK permitted option*/
210 ptr+=sizeof(struct tcphdr);
215 /*Save To Packet Capture*/
216 pcap_dump((u_char*)state.out,&nh, data);
219 /* 2)Create Syn,Ack Packet*/
220 /*make sure the packet is all zero*/
221 memset(data, 0, MAX_PACKET);
224 /*Set the libpcap header*/
225 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr)+4;
226 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr)+4;
227 nh.ts.tv_usec+=1000; /*This packet is 1/3rd closer to the first packet then the previous packet created*/
229 /* Copy SLL and IP headers from private data area*/
230 /* These are headers from the first packet in the capture*/
231 memcpy(data, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
233 /*Adjust IP header, including swapping source and destination*/
234 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
238 iph->saddr=iph->daddr;
240 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr)+4);
243 ptr+=sizeof(struct sll_header)+ sizeof(struct iphdr);
244 tcph=(struct tcphdr*)ptr;
245 tcph->source=htons(1113);
246 tcph->dest=htons(1113);
248 tcph->check=htonl(0);
257 /*Initialize Sequence and Acknowledgement Numbers and Window*/
258 tcph->seq=htonl(state.ack_num++);
259 tcph->ack_seq=htonl(state.seq_num);
260 tcph->window=htons(WIN_FACTOR);
262 /* Add SACK permitted option*/
263 ptr+=sizeof(struct tcphdr);
268 /*Save To Packet Capture*/
269 pcap_dump((u_char*)state.out,&nh, data);
271 /* 3)Create Ack Packet*/
272 /*make sure the packet is all zero*/
273 memset(data, 0, MAX_PACKET);
276 /*Set the libpcap header*/
277 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
278 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
279 nh.ts.tv_usec+=1000; /*This packet is 2/3rds between SYN and first packet*/
281 /* Copy SLL and IP headers from private data area*/
282 /* These are headers from the first packet in the capture*/
283 memcpy(data, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
286 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
289 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr));
292 ptr+=sizeof(struct sll_header)+ sizeof(struct iphdr);
293 tcph=(struct tcphdr*)ptr;
294 tcph->source=htons(1113);
295 tcph->dest=htons(1113);
297 tcph->check=htonl(0);
306 /*Initialize Sequence and Acknowledgement numbers and window*/
307 tcph->seq=htonl(state.seq_num++);
308 tcph->ack_seq=htonl(state.ack_num);
309 tcph->window=htons(WIN_FACTOR);
311 /*Save To Packet Capture*/
312 pcap_dump((u_char*)state.out,&nh, data);
316 /* Create a TCP ending handshake */
323 struct pcap_pkthdr nh;
325 struct sll_en_p *slip=(struct sll_en_p*)state.en_priv;
329 dbgprintf(1,"Error: SLL, IPv4 Encapsulation Finish method given invalid data!\n");
333 /*copy the libpcap header from private data area*/
334 memcpy(&nh, &slip->header, sizeof(struct pcap_pkthdr));
336 /*create buffer for new packet*/
337 ptr=data=malloc(MAX_PACKET);
339 dbgprintf(0,"Error: Couldn't allocate Memory\n");
343 /* 1)Create Fin Packet*/
344 /*make sure the packet is all zero*/
345 memset(data, 0, MAX_PACKET);
348 /*Set the libpcap header*/
349 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
350 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
351 nh.ts.tv_usec+=1000; /*Time is from the last packet in the capture; make this packet after that packet*/
353 /* Copy Ethernet and IP headers from private data area*/
354 /* These are headers from the first packet in the capture*/
355 memcpy(ptr, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
358 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
361 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr));
364 ptr+=sizeof(struct sll_header)+ sizeof(struct iphdr);
365 tcph=(struct tcphdr*)ptr;
366 tcph->source=htons(1113);
367 tcph->dest=htons(1113);
369 tcph->check=htonl(0);
378 /* Adjust Sequence and Acknowledgment numbers and window*/
379 tcph->seq=htonl(++state.seq_num);
380 tcph->ack_seq=htonl(state.ack_num);
381 tcph->window=htons(WIN_FACTOR);
383 /*Update Sequence Number to include the fin packet in the sequence number space*/
386 /* Save To Packet Capture*/
387 pcap_dump((u_char*)state.out,&nh, data);
389 /* 2)Create Fin,Ack Packet*/
390 /*make sure the packet is all zero*/
391 memset(data, 0, MAX_PACKET);
394 /*Set the libpcap header*/
395 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
396 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
397 nh.ts.tv_usec+=1000; /*After the previous packet*/
399 /* Copy Ethernet and IP headers from private data area*/
400 /* These are headers from the first packet in the capture*/
401 memcpy(ptr, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
403 /*Update IP header, including swapping source and destination addresses*/
404 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
408 iph->saddr=iph->daddr;
410 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr));
413 ptr+=sizeof(struct sll_header)+ sizeof(struct iphdr);
414 tcph=(struct tcphdr*)ptr;
415 tcph->source=htons(1113);
416 tcph->dest=htons(1113);
418 tcph->check=htonl(0);
427 /*Adjust Sequence and Acknowledgment numbers and window*/
428 tcph->seq=htonl(state.ack_num++);
429 tcph->ack_seq=htonl(state.seq_num);
430 tcph->window=htons(WIN_FACTOR);
432 /*Save To Packet Capture*/
433 pcap_dump((u_char*)state.out,&nh, data);
435 /* 3)Create Ack Packet*/
436 /*make sure the packet is all zero*/
437 memset(data, 0, MAX_PACKET);
440 /*Set the libpcap header*/
441 nh.caplen=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
442 nh.len=sizeof(struct sll_header) +sizeof(struct iphdr)+sizeof(struct tcphdr);
443 nh.ts.tv_usec+=1000; /*After the previous packet*/
445 /* Copy Ethernet and IP headers from private data area*/
446 /* These are headers from the first packet in the capture*/
447 memcpy(ptr, slip->od, sizeof(struct sll_header)+ sizeof(struct iphdr));
450 iph= (struct iphdr *) (ptr + sizeof(struct sll_header));
453 iph->tot_len=htons(sizeof(struct iphdr)+sizeof(struct tcphdr));
456 ptr+=sizeof(struct ether_header)+ sizeof(struct iphdr);
457 tcph=(struct tcphdr*)ptr;
458 tcph->source=htons(1113);
459 tcph->dest=htons(1113);
461 tcph->check=htonl(0);
470 /*Adjust Sequence and Acknowledgment numbers and window*/
471 tcph->seq=htonl(state.seq_num++);
472 tcph->ack_seq=htonl(state.ack_num);
473 tcph->window=htons(WIN_FACTOR);
475 /*Save To Packet Capture*/
476 pcap_dump((u_char*)state.out,&nh, data);
483 /* The UDP Encapsulation Structure*/
484 struct encap_ops sll_encap = {
486 .post=sll_encap_post,
487 .handshake=sll_encap_handshake,