title: Linux C Sockets - Binded socket receives packets from another interface?
body: <p>I'm working with Linux(Debian) and C.</p>
  
  <p>I have opened a socket to the interfaces - eth0 and eth1 with the following function:</p>
  
  <pre><code>    // Opening the socket
      mSocket = socket(device-&gt;ifa_addr-&gt;sa_family, SOCK_RAW | SOCK_NONBLOCK,
                  htons(ETH_P_ALL));
      if (mSocket == -1)
      {
          throw CException("socket is bad");
      }
  
      // Adding flag to the socket to work only on a specific interface
      if (setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE,
              (void *) &amp;mIfreq, sizeof(mIfreq)) &lt; 0)
      {
          throw CException(
                  "Failed to add binding to specific interface flag");
      }
  </code></pre>
  
  <p>while ifreq holds the interface name:</p>
  
  <pre><code>    strncpy(mIfreq.ifr_ifrn.ifrn_name,device-&gt;ifa_name, IF_NAMESIZE);
  </code></pre>
  
  <p>The next thing I do is receiving packets using the following code:</p>
  
  <pre><code>    recvSize = recvfrom(mSocket, buffer, ETH_FRAME_LEN, 0, NULL,
                      NULL);
  </code></pre>
  
  <p>questions:</p>
  
  <p><strong>1)</strong> why if both interfaces aren't connected to anything non of the sockets can be opened</p>
  
  <p><strong>2*)</strong> why (WLOG) if eth0 is connected and eth1 isn't I can open sockets to each of them</p>
  
  <p><strong>2.1*)</strong> why by reading from eth1 socket I get all packets received in eth0</p>
  
  <p><strong>(wireshark on eth1 isn't showing any packet as it should be)</strong></p>
  
  <p>[my guess - is it linux fault?(doing stuff behind my back?)]</p>
  
  <p>Thanks in advance,</p>
  
  <p>Despair</p>