Re: Stumped on IPv6 ping in c language with PF_PACKET/SOCK_DGRAM



pdbuchan@xxxxxxxxx writes:
This is a question I posted on linuxquestions.org, but no takers so
far, so I thought I'd widen the audience to newsgroups.

I'm trying to write a c program on Ubuntu to send an ICMP echo request
over IPv6. I'm using 6to4/sit0 to do IPv6 as my ISP doesn't support it
yet. I'm monitoring sit0 with Wireshark.

I can use the ping6 which comes with Ubuntu and successfully ping6
ipv6.google.com and on Wireshark see the IPv6 echo request and reply
on sit0. Basde on this test, I believe I should be able to see the
traffic from my program by monitoring sit0 with Wireshark.

I've used PF_PACKET and SOCK_DGRAM because I don't want to have to
specify the MAC addresses. I do want to specify source and destination
IP addresses. I understand that SOCK_PACKET is "strongly" deprecated,
so I'm trying not to use it.

My program compiles and runs without errors,

I don't believe that. Or rather, I believe it doesn't report any
errors, but that's because your code does not check for them, rather
than because none occur.

but nothing shows up on Wireshark on sit0. In fact, if I ask Wireshark
to monitor all interfaces, I still see no IPv6 traffic.

You have at least two bugs:
(i) The destination address for AF_PACKET needs to be a sockaddr_ll.
(ii) In various places you pass the size of a pointer rather than the
size of the object pointed to.

A few other remarks:

interface = (char *) malloc (10 * sizeof (char));

char (and unsigned char) always have size 1, by definition. There
is no point whatsoever in multiplying by sizeof(char).

malloc() returns "void *" which can be implicitly converted to a pointer
to any object type. Including the cast adds nothing and can actually
hide problems.

Moreover there is actually no point in most of the memory allocation
you're doing. For the strings, just use the string literal. For packet
assembly, the use of malloc() does get you alignment, though a union
could be used to achieve this too. It's really the pointless use of
malloc that has led you into (ii) above.

memset (payload, 0, sizeof (payload));
memset (interface, 0, 10);
memset (src, 0, 39);
memset (target, 0, 80);

These are pointless.

free (interface);
free (src);
free (target);
free (payload);
free (packet);

return (EXIT_SUCCESS);

There is no point calling free() just before exiting. The OS will clean
up for you.

--
http://www.greenend.org.uk/rjk/
.



Relevant Pages

  • Re: GIF tunnel doesnt like fragmented packets?
    ... There is no support for fragmented ipv6 packets in pffor FreeBSD. ... pass in on $ext_if inet proto tcp from any to port $tcp_services ... echo request, length 16, seq 0 ...
    (freebsd-net)
  • Re: strange problems with Internet connection
    ... So I did the same, set up a static IP, but left IPv6 ... Linksys router, or if I connect directly to the cable modem. ... my connection is slow and intermittent. ... I have had the same issues with IPv6 in both Ubuntu and Debian. ...
    (Ubuntu)
  • GIF tunnel doesnt like fragmented packets?
    ... pass in on $ext_if inet proto tcp from any to port $tcp_services ... proto IPv6, length 76) ... echo request, length 16, seq 0 ...
    (freebsd-net)
  • Re: Netzwerk "zu Fuss" einstellen
    ... Allerdings war diese "Unvertraeglichkeit bei Ubuntu 8.04.1 tatsaehlich ... die Variante ohne IPv6 noch funktioniert? ... Bei hoeheren Datenraten habe ich durch Uebertragungsfehler im Down- ... Geringe Paketverluste lassen sich anscheinend bei ADSL und hohen ...
    (de.comp.os.unix.linux.misc)
  • Re: Newbie query: Ubuntu vs openSUSE
    ... Most of the other live CD I have to disable ipv6 thing to get on the Internet with Firefox. ... I did write this but if you try something other that Ubuntu 11.10 you might want it handy. ... The method of disabling appears to be adding the following three ... SUSE is an old distro, ...
    (Ubuntu)