Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linux, how to specify which ethernet interface data is transmitted on

I'm working on a Linux based server system in which there are two network interfaces, both on the same subnet (for now, lets just say they are 172.17.32.10 & 172.17.32.11). When I send data to a host on the network, I would like to specify which interface on my server the data is transmitted on. I need to be able to switch from one interface to the other (or maybe even transmit on both) in software (static routing rules won't work for this application).

I found a related question in StackOverflow that suggested using the netlink library to modify routes on the fly. This intuitively seems like it should work, but I was wondering if there were any other options to accomplish this same result.

like image 894
Steve Hawkins Avatar asked Oct 06 '08 00:10

Steve Hawkins


People also ask

How do I know which network interface to use?

Open up the Task Manager, go to the Networking tab, and you can see which adapters are being utilized. Show activity on this post. You can identify the adapter by MAC address (Physical Address) using the ipconfig /all command.

How do I change the network interface in Linux?

From the desktop. If you are working with a Linux system using a GUI, you can configure the network interface via an icon in the far upper right of the screen. The function of this icon is very similar to the windows “TV Set” down in the lower right of the screen in the taskbar of a Windows desktop system.

How do I see network interfaces in Linux?

netstat command – It is used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. ifconfig command – It is used to display or configure a network interface. nmcli command – A command to show or configure a network interface on Linux.

How do I find my Ethernet interface name Linux?

1. Log in to the system as root and run ifconfig -a plumb in a command shell. The command discovers all installed network interfaces.


1 Answers

No offense intended, but the answer about using bind() is quite wrong. bind() will control the source IP address placed within the packet IP header. It does not control which interface will be used to send the packet: the kernel's routing table will be consulted to determine which interface has the lowest cost to reach a particular destination. (*see note)

Instead, you should use an SO_BINDTODEVICE sockopt. This does two things:

  • Packets will always egress from the interface you specified, regardless of what the kernel routing tables says.
  • Only packets arriving on the specified interface will be handed to the socket. Packets arriving on other interfaces will not.

If you have multiple interfaces you want to switch between, I'd suggest creating one socket per interface. Because you'll also only receive packets to the interface you've bound to, you'll need to add all of these sockets to your select()/poll()/whatever you use.

#include <net/if.h>

struct ifreq ifr;

memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, "eth1", sizeof(ifr.ifr_name));
if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE,
            (void *)&ifr, sizeof(ifr)) < 0) {
    perror("SO_BINDTODEVICE failed");
}

(*note) Bind() to an interface IP address can lead to confusing but nonetheless correct behavior. For example if you bind() to the IP address for eth1, but the routing table sends the packet out eth0, then a packet will appear on the eth0 wire but carrying the source IP address of the eth1 interface. This is weird but allowed, though packets sent back to the eth1 IP address would be routed back to eth1. You can test this using a Linux system with two iP interfaces. I have one, and did test it, and bind() is not effective in steering the packet out a physical interface.

Though technically allowed, depending on topology this may nonetheless not work. To dampen distributed denial of service attacks where the attackers use forged IP source addresses, many routers now perform Reverse Path Forwarding (RPF) checks. Packets with a source IP address on the "wrong" path may be dropped.

like image 105
DGentry Avatar answered Oct 09 '22 07:10

DGentry