1

My Linux (kernel 3.17) computer has more than one NIC: eth0, eth1, (and/or eth2...)

I have several PC connected to each of them:

PC0 <---> eth0 (192.168.1.100)
PC1 <---> eth1 (192.168.1.101)
PC2 <---> eth2 (192.168.1.102)

An application on the Linux computer started threads to listen to 192.168.1.100 (and the other two). Upon receiving a packet from PC0, the thread replies. How do I guarantee the reply never go through eth1 or eth2 although they have same subnet mask? Similarly, upon receiving packet from pc1, the reply should never go through eth0 or eth2.

Ideally, to achieve what I said, I should use three completely different subnet, such as 10.0.0.1, and 172...., but my colleague suggests it could work, so I am here to find peer opinions. The kernel is in my control, I build it, so any solution to make it work can be proposed.

Explanation

Why three NIC on same subnet? Eth0 is the main IP address that is statically assigned during Linux boot. It shouldn't change but could change if needed. The question is: what if the field people accidentally assigned eth1 and eth2 to have same subnet with eth0? Will my application reply only to eth0 if the socket listens on eth0 at a certain port?

Possible Routing Table

the rule is to go through the NIC if it is where the request is from. I copied from somewhere

ip route add 172.16.10.0/24 dev eth0 src 172.16.10.10 table 10
ip route add default via 172.16.10.251 table 10
ip route add 172.16.10.0/24 dev eth1 src 172.16.10.20 table 20
ip route add default via 172.16.10.251 table 20
ip route add 172.16.10.0/24 dev eth0 src 172.16.10.10
ip route add 172.16.10.0/24 dev eth1 src 172.16.10.20
ip route add default via 172.16.10.251
ip rule add from 172.16.10.10 table 10
ip rule add from 172.16.10.20 table 20
ip route flush cache
7
  • The set-up looks all wrong. Give us more detail (IP addresses and the routing table). Oct 25, 2015 at 2:47
  • 1
    Agreed. Why three separate network cards on the same subnet?
    – EEAA
    Oct 25, 2015 at 2:52
  • Do you build the code that's listening for connections? It can control which interface it uses. Oct 25, 2015 at 3:54
  • @KonradGajewski I added explanation. I wanted to know what should be in the routing table if I don't want PC0 to send anything to PC1, or PC2.
    – Splash
    Oct 25, 2015 at 4:17
  • @RyanBabchishin, I know when I listen I can specify an IP, can I also specify the hardware name such as eth0?
    – Splash
    Oct 25, 2015 at 4:18

1 Answer 1

5

Usually, an application binds to an IP address, not to a specific interface. (The exception is applications that mess with the network directly, such as e.g. tcpdump/wireshark etc - but that's not your case, so disregard this.) Most applications can bind either to only one IP address or to all of them (but usually not to more-than-one-but-not-all of them). Which of these apply to your particular application will depend on how it's coded.

When a client connects to this socket, the connection is identified by four items:

  1. Your own IP address (e.g. 192.168.1.100)
  2. Your own port (e.g. 8080)
  3. The client's IP address
  4. The client's port

Any responses sent to the data you are receiving on this connection will be sent using these four characteristics - i.e. they will not be sent from another IP or interface on your server.

So, as long as what your application does is to listen on an {IP,port}, accept connections on that {IP,port} and answering back to the client, there shouldn't be any problems.

Where you might run into problems is if your application will sometimes initiate its own connection. In that case, it will (usually) let the computer decide which interface to use. And if your computer considers all three interfaces equally valid, you may get traffic going out on the wrong one.

It is still possible to make your computer always use a particular interface - exactly how you do it will depend on your particular OS/distro. The term to search for is "unix source routing".

4
  • Your binding statement matches what I know. Thanks. I wanted to reply a packet through the same NIC that the request comes in. So if request0 comes through eth0, then reply0 should go through eth0. If request 1 comes through eth1, then reply1 should go through eth1. If there a routing table configuration to do that?
    – Splash
    Oct 25, 2015 at 13:35
  • @Splash I'm not sure I understand your question there. The routing table has to be correctly configured in order for the connection to be established to begin with, otherwise the TCP handshake will fail.
    – Jenny D
    Oct 25, 2015 at 16:10
  • I am stating that maybe the return could go through eth1 if eth0 and eth1 are on the same subnet, if routing table is not configured specifically.
    – Splash
    Oct 25, 2015 at 18:50
  • 1
    @Splash If the incoming packet was addressed to 192.168.1.100, then the return packet must have that as its source address, or the receiving computer would reject it. Unless you've done something weird to the routing table, the packet will go out using whichever adapter is assigned that specific address, in this case, eth0. As Jenny said, the only issue is that, if your computer makes an outgoing connection, the OS could choose any of the three adapters... but will then exclusively use that specific adapter for the rest of the connection (again, unless you've messed with the routing table). Sep 17, 2019 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.