I don't think you're setting ifr
correctly.
You need to call ioctl(rawsocket, SIOCGIFINDEX, &ifr)
to set the index of the device. The name is not enough.
Your index is left as zero which is the first device which happens to be loopback.
Here's a snippet of code from C Language Examples of IPv4 and IPv6 Raw Sockets for Linux:
// Use ioctl() to look up interface index which we will use to
// bind socket descriptor sd to specified interface with setsockopt() since
// none of the other arguments of sendto() specify which interface to use.
memset (&ifr, 0, sizeof (ifr));
snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
perror ("ioctl() failed to find interface ");
return (EXIT_FAILURE);
}
There may be other problems (your code is too big) try running/modifying the examples in the link above. Hopefully they are not outdated.