how to convert this pf packet code for ipv6 packets
this is the code i have and we are trying to use pf packet for sending ipv6 packets..would anybody know how to convert the code and make changes according to the ipv6 header..we are getting a ton of errors trying the same...
#include "my_sock.h"
#define perr_quit(str) perror(str)
/* this function generates header checksums */
unsigned short csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main(int argc, char **argv)
{
int sock = 0;
char *smac = "00:1A:80:4A:9F:40", *dmac = "00:0F:B0:C9:7F:9E";
char buf[ETH_FRAME_LEN], pseudo_buf[ETH_FRAME_LEN + 512];
struct ether_header *eth;
struct ip *ip;
struct tcphdr *tcp;
struct ether_addr *source, *dest;
struct pseudohdr *ps;
struct sockaddr_ll sa;
bzero(&sa, sizeof(struct sockaddr_ll)), bzero(buf, ETH_FRAME_LEN), bzero(pseudo_buf, ETH_FRAME_LEN + 512);
//make the socket
if( (sock = socket(PF_PACKET, SOCK_RAW, IPPROTO_TCP)) < 0)
perr_quit("socket");
//start building packet
eth = (struct ether_header *)(buf);
ip = (struct ip *)(buf + ETH_HDRLEN );
tcp = (struct tcphdr *)(buf + ETH_HDRLEN + IP_HDRLEN);
//the ethernet header first
//i hardcoded these for simplicity
if( (source = ether_aton(smac)) == NULL)
perr_quit("ether_aton");
else
bcopy(source->ether_addr_octet, eth->ether_shost, ETH_ALEN);
if( (dest = ether_aton(dmac)) == NULL)
perr_quit("ether_aton");
else
bcopy(dest->ether_addr_octet, eth->ether_dhost, ETH_ALEN);
eth->ether_type = htons(ETH_P_IP);
//now ip
ip->ip_hl = 5;
ip->ip_v = 4; //ip version
ip->ip_len = htons(IP_HDRLEN + TCP_HDRLEN); //total len of header + options
ip->ip_id = 0xbeef; //unique id of packet
ip->ip_ttl = 222; //# of hops b4 it is dropped time to live
ip->ip_p = IPPROTO_TCP; //protocol layer
//ip->ip_src.s_addr = inet_addr("77.77.77.178");
//ip->ip_dst.s_addr = inet_addr("77.77.77.1");
ip->ip_src.s_addr = inet_addr("172.16.2.185");
ip->ip_dst.s_addr = inet_addr("172.16.2.184");
//ip->ip_sum = in_cksum( (u_short *)ip, IP_HDRLEN);
ip->ip_sum = csum( (u_short *)ip, IP_HDRLEN);
//now tcp
tcp->source = htons(80); //dest port
tcp->dest = htons(32773); //source port
tcp->seq = htonl(1519); //tcp sequence number
tcp->ack_seq = htonl(rand()); //tcp ack number
tcp->doff = 5; //offset of the data(length of header) in 32bit words(double words damnit!)
tcp->syn = 1;
tcp->window = htons(512); //the window size(max bytes TCP is willing to accept)
tcp->check = 0; //set to 0 b4 calculating
//now calculate the pseudo header checksum
ps = (struct pseudohdr *) pseudo_buf;
ps->saddr.s_addr = ip->ip_src.s_addr; //from IP
ps->daddr.s_addr = ip->ip_dst.s_addr; //..
ps->protocol = IPPROTO_TCP; // = 6
ps->length = htons(TCP_HDRLEN); // if we had options/data.. htons(TCP_HDRLEN + tcp_datal + tcp_optlen)
//copy in the tcp header and calculate checksum
bcopy( tcp, &ps->tcpheader, TCP_HDRLEN );
//tcp->check = in_cksum( (u_short *) pseudo_buf, TCP_HDRLEN + 12);
tcp->check = csum( (u_short *) pseudo_buf, TCP_HDRLEN + 12);
//set up linklayer address structure
sa.sll_family = PF_PACKET;
sa.sll_protocol = htons(ETH_P_IP);
sa.sll_halen = 6; //length of hardware address
bcopy(source->ether_addr_octet, sa.sll_addr, 6);
sa.sll_ifindex = 2; //this is eth0
sa.sll_pkttype = PACKET_OUTGOING;
//finally sent it
if(sendto(sock, buf, ETH_HDRLEN + IP_HDRLEN + TCP_HDRLEN,
0, (struct sockaddr *) &sa, sizeof(struct sockaddr_ll)) < 0)
perr_quit("sendto");
return 0;
}
|