udp checksum on raw sockets

Charles Swiger cswiger at mac.com
Tue Apr 27 11:48:33 PDT 2004


On Apr 27, 2004, at 12:09 PM, Mark Santcroos wrote:
> * I'm sending UDP packets on a raw socket.
>
> * iphdr->ip_src.s_addr = INADDR_ANY;
>   So I don't know the IP address that will be used as src address.

The kernel ought to pick the src address of the interface the packet 
will be sent from according to the routing table.  At least if you were 
using SOCK_DGRAM, I'm not sure how much the kernel fills in when using 
SOCK_RAW.

>   So as far as I can see, I have a problem generating the checksum in
>   userspace.
>
> Suggestion:
>
> Would it make sense that if:
> sysctl net.udp.checksum=1
> and
> udphdr->uh_sum = 0;
> in a UDP packet created on a raw socket, to do the checksum in the 
> kernel
> instead of leaving it zero? If it does, I can create a patch.

And likewise with computing the checksum, I would expect the kernel to 
compute the packet checksums-- there's ip_sum as well as uh_sum.

> Any other hints as how to do the checksum in userland are welcome too. 
> As
> that is the "ultimate" problem I try to fix.

Consider using libnet to build your packets:

http://www.packetfactory.net/projects/libnet/

...between libnet and libpcap, you've got userland code to handle both 
sending and receiving packets in an OS-independent fashion.  Amoung 
other things, libnet has an implementation of "how to compute packet 
checksums" available for you to consider or use.

-- 
-Chuck



More information about the freebsd-net mailing list