We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

tomwasd
Posts: 2
Joined: Mon Sep 02, 2019 10:15 am

Two network interfaces at once

Mon Sep 02, 2019 10:26 am

Hi,

I have a raspberry pi connected via wifi to my home broadband connection and also via a 4G dongle to a sim card.

If I run ifconfig I can see that I have eth1 (the USB dongle) and an associated IP address and wlan0 and an associated IP address.

If I'm connected to both networks and run:

Code: Select all

wget -q0- http://checkip.dyndns.com --bind-address [wlan0 ip]
I get back my home broadband IP.

However, if I run

Code: Select all

wget -q0- http://checkip.dyndns.com --bind-address [eth1 ip]
It times out.

If I disconnect from wifi so that I'm only connected via the USB dongle and re-run:

Code: Select all

wget -q0- http://checkip.dyndns.com --bind-address [eth1 ip]
I correctly get back the 4G ip.

It appears as if when both network interfaces are connected I cannot use the USB dongle's internet connection.

I was hoping someone could shed some light as to what's going on and how I can resolve this so I can make requests on both network interfaces? Please let me know if any more information is required - somewhat out of my depth with this!

Thanks.

epoch1970
Posts: 8664
Joined: Thu May 05, 2016 9:33 am
Location: France

Re: Two network interfaces at once

Mon Sep 02, 2019 3:59 pm

What happens is that you have 2 default routes (routes to everywhere, to the Internet) defined, one via the network to which the WiFi interface is connected, one via the 4G network via the USB adapter.
By default when Linux has 2 possible and identical routes to a destination, it always uses the same, which is the one with the lowest “metric” value attached to it.
If you run “route” or “ip route” you should see the 2 default routes and the metric values.

On Raspbian the default metric values are defined by dhcpcd. Values are 200 + ifindex (index number of the interface) for Ethernet adapters and 300 + ifindex for WiFi interfaces. For the built-in interfaces in a Pi 3 or 4 this results to metric 202 for eth0 and 303 for wlan0, giving priority to using Ethernet when it is connected.

In your case I presume the WiFi interface has metric 303, I would have expected your “eth1” interface, given its name, would get metric “204”. However from your description I doubt this is the case, it looks like WiFi has the lowest metric and is preferred by the system.

If you add something like

Code: Select all


interface eth1
metric 0

to dhcpcd.conf, the interface eth1 will be preferred instead of the wireless one. 4G will be use for all non-local traffic.

Alternatively, if you remove the default route defined via the WiFi interface, Linux will have a single default route via eth1, so metric won't matter.
To do this you’d need to configure dhcpcd to reject the default route sent by the DHCP server on wlan0. I don’t know how to do that by heart, please refer to the dhcpcd.conf manpage or other forum posts.

Be sure to reset networking when you make changes to dhcpcd.conf. Reboot in case of doubt.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

tomwasd
Posts: 2
Joined: Mon Sep 02, 2019 10:15 am

Re: Two network interfaces at once

Tue Sep 03, 2019 6:17 am

Hey @epoch1970

Thank you for taking the time to reply - much appreciated :)

I've previously tried changing the metric values and removing the default routes but unfortunately didn't have much luck. As I understand it (I may be wrong!) if I change the metric value of one of the interfaces and/or remove a default route will it not just prefer one or the other?

I'm guessing I still wouldn't be able to specify which interface to use when making requests as only the one with the lowest metric value would work?

Is there a way where I can set things up so that I can make external requests to the internet using either-or - i.e. using the --bind-address option on wget?

epoch1970
Posts: 8664
Joined: Thu May 05, 2016 9:33 am
Location: France

Re: Two network interfaces at once

Tue Sep 03, 2019 9:46 am

There is a default value for metric, it is 0, the highest priority. Route metric is always taken into account, even if you don’t tweak it.

The overriding issue: Linux implements by default the weak network host model. BSD, OSX, modern Wins implement the strong network host model.
  • With the weak host model, interfaces do not matter, only the machine. For outgoing communication the route selection process considers all possible routes to destination and selects one. That route will happen to be via interface X, Y or Z. Any old port in a storm. Your bind option runs afoul of that logic.
  • With the strong host model, route selection starts with an interface. Your bind option would work assuming the interface has the appropriate route defined.
You can force Linux to use the strong host model, that’s called “Linux Advanced Routing (and Traffic Control)” or “Policy Routing”.
This gives you routing tables per interface in addition to the global default one, and a set of rules that specifies the order in which traffic should be matched with the routing tables.

A more modern way of dealing with this would be to segregate interfaces in network namespaces. Struggling to find a good link... please disregard the WireGuard specific stuff in here.
Each network namespace has its interfaces, routing tables, iptables rules, etc. so you could keep the Linux default routing strategy. However the wget command above would then need to look like

Code: Select all

ip netns exec my4Gns wget -q0- http://checkip.dyndns.com
Sorry for that ;)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

epoch1970
Posts: 8664
Joined: Thu May 05, 2016 9:33 am
Location: France

Re: Two network interfaces at once

Tue Sep 03, 2019 10:07 am

On last thing. In case you wish to put wlan0 in a separate namespace, you have to put the whole phy in the namespace, not wlan0 itself.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “Networking and servers”