Search code examples
clinuxsocketsraw-sockets

UDP RAW_SOCKET packets to itself are captured on loopback interface instead of eth0


Using sources from this article RAW_SOCKET to forge UDP packets I am generating packets from other host to own eth0 ip address, as example, 192.168.10.1 --> 192.168.10.131.

But tcpdump shows, that packets are arriving into lo, not eth0... on lo -

10:10:18.332284 IP (tos 0x0, ttl 64, id 768, offset 0, flags [DF], proto UDP (17), length 71)
192.168.10.1.57961 > 192.168.10.131.12001: [udp sum ok] UDP, length 43

and on eth0 is silence.

How to change the code, to make packets arrive into eth0? direct link to C source file


Solution

  • As I see, you have to use tcp functions instead of udp

    For UDP you can set the optname to SO_BINDTODEVICE

    const char device[] = "eth0";
    rc=setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device, sizeof(device));
    if (rc != 0) 
    {
     printf ("%s: could not set SO_BINDTODEVICE (%s)\n",
        argv[0], strerror(errno));
     exit (EXIT_FAILURE);
    }