UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2005-06-16 09:16 PM

ajpug
Member
Registered: 2005-05-14
Posts: 6

Re: incorrect UDP checksum ???

Hi all,

I am using two sockets on the same eth0 interface. I use one raw socket for sniffing and another normal socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP) for sending udp packets to a server.
Everything seems to work, but when I sniff the packets I send by using ethereal, it shows that all the packets have an incorrect UDP checksum value; furthermore the wrong value is one of two values for all the packets (0x24af and 0x24ae) and it does not change.
Now I know that for raw sockets I have to take care of the headers, but I was under the impression that for normal sockets the kernel would take care of it.

Please also note that other UDP packets have the correct checksum, only the UDP packets that my client sends have the wrong checksum.

Anyone has any idea?

Thank you,
ajpug

Offline

#2 2005-06-17 12:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: incorrect UDP checksum ???

You're sure these are packets being sent through your normal UDP (AF_INET,
SOCK_DGRAM) socket??  If so, that's very odd, and I can't understand what would
be causing it...  It doesn't make any sense to me...  It sounds like they must really
be coming from your raw socket, instead...  Maybe some bug is trashing your FDs
or something, so you end up sending the data out the wrong socket? *shrug*  It's
impossible to even really guess without seeing the code...  I'd start by completely
disabling the raw socket, just for testing purposes at least, simply to remove it from
the equation, and then see if the same thing happens...

Offline

#3 2005-06-23 11:16 PM

ajpug
Member
Registered: 2005-05-14
Posts: 6

Re: incorrect UDP checksum ???

After testing and cutting the code to the bone I still get the invalid UDP checksum.
Here is the code I am testing after cutting it down heavily (still getting the same error though):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main () {

        char Interface[10], *write_buf;
        int w_sock, bytes_wrttn, msg_len, tolen;
        struct sockaddr_in to;

        strcpy (Interface, "eth0");

        if ((w_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
                perror ("Failed creating dgram socket (mode 1).\n");
                exit(0);
        }

        write_buf = (char *) malloc(65536*sizeof(char));

  while (1) {
        sprintf(write_buf, "%s%s%s%c", "DHCP_RAI", "ZZ:ZZ:ZZ:ZZ:ZZ:ZZ", "XXX.XX.XX.XXX", '\0');
        msg_len = 44;
        printf("write_buf: %s [len: %d]\n", write_buf, msg_len);

        to.sin_family = AF_INET;
        to.sin_port = htons(10000);
        to.sin_addr.s_addr = inet_addr("128.X.X.X");
        tolen = sizeof(to);
        printf("to.sin_addr: %s\n", inet_ntoa(to.sin_addr));
        printf("to.sin_port: %d\n", ntohs(to.sin_port));

        bytes_wrttn = sendto (w_sock, write_buf, msg_len, 0, (struct sockaddr *) &to, tolen);
        if (bytes_wrttn < 0){
                perror("ERROR sendto:");
        }
        printf("Total bytes sent: %d\n", bytes_wrttn);
  }
}


I hope you guys can find where the error is.

Thanks a LOT,
ajpug

Offline

#4 2005-06-24 12:51 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: incorrect UDP checksum ???

That code works just fine here...  I changed the IP you're sending to to a random
bogus non-routable IP (10.1.1.1), and let it run for a bit, and I didn't see any
checksum errors in Ethereal here...  They were all correct...

Does every single packet you send have an incorrect checksum, or only some of
them?  You said before that other UDP packets had correct checksums, but are
they packets generated locally or remotely?  I mean, if you run any other UDP
client/server on your same machine, are the packets that it sends out ok or not?
I just think it must be a local system issue of some sort, because I don't see any
real problem with that code, and it works just fine here (RH9/Linux 2.4.20+patches)...
The only real bug I see is your failure to check the return value from malloc(), so
maybe if that failed for some reason, you end up trashing memory somehow, or
something?  But, it seems a bit far-fetched to me...  I'd sooner expect a simple
seg-fault, really...  Plus, you really shouldn't fail to malloc() 64K, either... ;-)  But, I
just don't see any other real problem...  So, I don't believe it's your code...  Try
other UDP clients/servers locally, and see if their packets have the same problem...
If so, I'm still not sure what the problem is, but it sounds like something with the
local system itself, not your app code...

Offline

#5 2005-06-24 05:08 PM

ajpug
Member
Registered: 2005-05-14
Posts: 6

Re: incorrect UDP checksum ???

mmm...I am not sure what is happening. I am starting to think that it might be a problem related to the kernel I am using (2.6.3-25). I am also getting "bad udp checksum" message from the kernel, however these packets are received on the other side!
I have also added the check to the malloc. ;)
mmm...I am clueless.

ajpug

Offline

#6 2005-07-05 05:52 PM

harald
Guest

Re: incorrect UDP checksum ???

I have a similar problem with kernel 2.6.8-24.14-default. UDP packets are created by a JAVA application and sent to the local loopback. The UDP checksum is not zero, therefore checksumming is turned on, but the checksum seems only to be done over the header and not the data (same checksum result, if same header but different data part). Both tcpdump and ethereal report the checksum as wrong.

In my case, I will programme a workaround specific to my application, since I want to have the programme running on
different computers/versions.

Best Regards

Harald

#7 2005-07-05 09:50 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: incorrect UDP checksum ???

Works fine here with 2.6.13-rc1 kernel.

Don't program a workaround in your app, fix the real problem.

Offline

#8 2016-11-08 06:55 PM

Kevin
Guest

Re: incorrect UDP checksum ???

In case anyone else comes across this 10+ year old thread: I observed the exact same behavior as ajpug, and came here. Turned out that packets sniffed using Wireshark on the application's end (where they were generated) were reported with bad checksums, but those same packets sniffed on the receiving end were reported to have the correct checksums. Perhaps they were updated by the kernel after Wireshark picked them up, but that is just a guess--bottom line, check what you're getting on the receiving side.

#9 2016-11-10 08:25 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: incorrect UDP checksum ???

It's probably checksum offloading to the NIC card: The kernel always fills in zeroes (for TX) and the network card calculates the actual checksum.  For receive it might be network card/driver dependent what data is left in the checksum field.

Offline

Board footer

Powered by FluxBB