Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 28 Feb 2003 12:08:33 +0200
From:      ianf@za.uu.net
To:        freebsd-net@FreeBSD.org
Subject:   Wondering about raw sockets.
Message-ID:  <15653.1046426913@wcom.com>

next in thread | raw e-mail | index | archive | help
Hi

Does anyone know why this behaviour is not a bug?

sock_raw = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
optval = 1;
setsockopt(sock_raw, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval));
peer.sin_family = AF_INET;
peer.sin_addr.s_addr = (CAST_HDR(packet))->dest;
peer.sin_port = (CAST_UDP(packet + OFFS_UDP))->dst_port;
len = ntohs((CAST_HDR(packet))->length);
sendto(sock_raw, packet, len, 0x0, (struct sockaddr *)&peer, sizeof(peer));

Ignoring checking return values, which my (real) code does, if I
write the following data pointed to by packet, sendto returns an
error 'Invalid argument'.

4500 004e 2943 0000 3f11 2785 c407 00af
c407 a219 0714 041c 003a 8fb6 0277 0032
9e2d bf05 0ef3 0c0d 7b0d 8441 c1cc cbc6
0606 0000 0002 0706 0000 0001 0906 ffff
ffff 0a06 0000 0000 0c06 0000 05dc

If however, I write the following packet to the same socket and
replace the 'len =' line with 'len = (CAST_HDR(packet))->length;'
the packet gets written.

4500 4e00 2943 0000 3f11 2785 c407 00af
c407 a219 0714 041c 003a 8fb6 0277 0032
9e2d bf05 0ef3 0c0d 7b0d 8441 c1cc cbc6
0606 0000 0002 0706 0000 0001 0906 ffff
ffff 0a06 0000 0000 0c06 0000 05dc

Note that the IP length field must be in host byte order to get the
packet accepted by the raw socket.  tcpdump displays the packet
that was sent by the system exactly matching the first copy of the
packet in this email which is the way I would have expected a raw
IP socket to have accepted the packet.

... or are my expectations just plain wrong?

Ian

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-net" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15653.1046426913>