LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2009, 04:11 AM   #1
linuxnewbie1234
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pls convert this windows code into linux code nagendrar Programming 6 06-03-2008 07:00 AM
IPv6 - NS packet shivaligupta Linux - Networking 0 11-02-2006 04:08 AM
ipv6 packet filtering reddy_pradeep Solaris / OpenSolaris 0 09-15-2005 11:27 PM
packet fragmentation in packet forwarding code cranium2004 Linux - Networking 0 05-16-2005 04:05 AM
fowarding packets with ipv6 Third Angel Linux - Networking 0 10-24-2004 04:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration