Incorrect IP header data in RAW Sockets

Incorrect IP header data in RAW Sockets

Post by Devende » Sat, 04 Feb 2006 10:26:30


I am using RAW sockets (Solaris 8) to send out a VRRP protocol packet
out to a multicast group. For some reason, I am not seeing the IP
header information that I am sending out. Instead I see TTL = 1,
Protocol=2 and etc. Can someone help me out in identifying the likely
problem in this code?

int vrrp_send_pkt( vrrp_rt *vsrv, char *buffer, int buflen )
        struct sockaddr_in from;
        int     len;
        int     fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

        if( fd < 0 ){
                perror( "socket" );
                return -1;
        int on = 1;
        int geton = 1;
        int retSetSock = setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on,

        char lBuffer[72];
        struct ip ip;
        struct udphdr udphdr;

        ip.ip_hl                = 5;
        ip.ip_v = 4;
        ip.ip_tos               = 0;
        ip.ip_len       = ip.ip_hl*4 + vrrp_hd_len( vsrv );
        ip.ip_len       = htons(ip.ip_len);
        ip.ip_id                = ++ip_id;
        ip.ip_off       = 0;
        ip.ip_ttl               = VRRP_IP_TTL;
        ip.ip_p = IPPROTO_VRRP;
        ip.ip_src.s_addr = htonl(vsrv->vif.ipaddr);
        ip.ip_dst.s_addr        = htonl(INADDR_VRRP_GROUP);
        /* checksum must be done last */
        ip.ip_sum       = in_csum( (u_short*)(&ip), ip.ip_hl*4, 0 );

        memcpy(lBuffer+sizeof(ip), &udphdr, sizeof(udphdr));
        /* build the address */
        memset( &from, 0 , sizeof(from));
        //strcpy( from.sa_data, vsrv->vif.ifname );
        /* send the data */
        from.sin_family = AF_INET;
        len = sendto( fd, &ip, 40, 0, (struct sockaddr *) &from,
sizeof(from) );
        close( fd );
        return len;



1. raw sockets, IP header

[ This is a repost of the following article:                               ]

[ Subject: raw sockets, IP header                                          ]
[ Newsgroups: comp.unix.programmer                                         ]

Sorry for the multipost, but I posted this on CUP recently, and have
gotten no responses as of yet, and thought some of you guys might
have some information...

So, for learning experience and a future project I'm experimenting
with raw sockets.  I'm trying to send a raw packet with the 'don't
fragment' bit set in the IP header to try to determine the path MTU.
I will embed an ICMP packet as my IP payload, and do an echo request,
and thus hopefully get either:

1) an echo reply
2) a "message too long" error reply

So, I have a buffer that contains my IP header and ICMP header.  I
set the appropriate fields of the IP header and the ICMP header,
and I send the packet out with sendto().  However, recvfrom() blocks
and I never get a reply.  A few of questions (link to code at bottom):

1) Do I call socket() with a protocol of IPPROTO_RAW or IPPROTO_ICMP?
(note that I'm setting the IP_HDRINCL socket option so the kernel knows
that the first byte of data is the first byte of the IP header itself).

2) Do I need any payload for the ICMP packet?

3) Will the kernel do IP and ICMP checksum for me?  My intuition says yes,
but in UNPV1 Stevens manually computes an ICMP checksum (chapter 25).

4) Assuming I can get some response from my remote host at some point,
to actually get the path MTU discovery to work, do I actually have to
send large IP packets or can I just set the ip_len field in the IP header
to a large number to simulate a large packet?

5) Is there a way to get the kernel to fill in the proper source IP
address in the IP header? (supposedly setting ip_id=0 in the header
tells the kernel to set the packet ID, so maybe something for source IP?)
Depending on where this packet goes, it could be one of several IP
addresses for the machine.  If the kernel can't do it, am I going to have
to parse the routing table and find which interface the packet should be
sent out on?

Thanks for any help anyone can offer... the code is here:

 josh(at)  |

 298322 local keystrokes since last reboot (26 days ago)

2. error using vi

3. IP header in Raw socket

4. verbose telnet logs with tcpwrapper

5. raw sockets, IP header

6. Ascend Netwarp Pro

7. raw IP and raw ICMP socket???

8. HTTPD-2.0.44 Returns NULL

9. Reading data from a raw socket

10. receiving data from raw socket

11. read data from raw socket

12. how to capture raw data from socket

13. RAW Sockets, how to read sent data?