Skip to content

resolved: bind socket to the interface - #12004

Closed
antoinefaure wants to merge 1 commit into
systemd:masterfrom
antoinefaure:dev/afaure/iface
Closed

antoinefaure wants to merge 1 commit into
systemd:masterfrom
antoinefaure:dev/afaure/iface

Conversation

@antoinefaure

@antoinefaure antoinefaure commented Mar 14, 2019 •

Copy link
Copy Markdown

This pull request resolves #11935

TL;DR :
When using a DNS server associated to a link, make sure to use this link to send the request.
IP_UNICAST_IF is overridden by the configured routes, and therefore doesn't guarantee that the request will go on the desired interface.

To use SO_BINDTODEVICE we need to keep the capability CAP_NET_RAW

@antoinefaure

antoinefaure commented Mar 14, 2019 •

Copy link
Copy Markdown
Author

Hello,
I don't know why the CI is all red, it seems to come from networkd, although I haven't touched it..

ERROR: ld.so: object 'libeatmydata.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
[...]
networkd-test.py     FAIL non-zero exit status 1```
Any ideas please ?

Comment thread src/resolve/resolved-dns-scope.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IP_UNICAST_IF has no effect on anything but IPv4 UDP. I would remove this. We don't know what effect it might have on stream sockets (and similar) in the future.

@dvdhrm

dvdhrm commented Mar 15, 2019

Copy link
Copy Markdown
Contributor

TL;DR :
When using a DNS server associated to a link, make sure to use this link to send the request.
IP_UNICAST_IF is overridden by the configured routes, and therefore doesn't guarantee that the request will go on the desired interface.

To use SO_BINDTODEVICE we need to keep the capability CAP_NET_RAW

You say TLDR, but where is the long version?

Secondly: Newer kernels provide SO_BINDTOIFINDEX. It would be cool to write a helper that uses this and falls back to SO_BINDTODEVICE if it fails. It could look similar like the one in n-dhcp4:

https://github.com/nettools/n-dhcp4/blob/master/src/util/socket.c#L49

Lastly, can somebody elaborate what exactly the per-interface option in resolved is doing? SO_BINDTODEVICE is mostly meant for link-level applications, isn't it? So if you never want routing to be applied, anyway. But in the DNS case, why would you want to skip routing? Also, why would your routing tables contradict the way you want resolved to work?

The patch looks good to me, but I would prefer having some more context on why exactly it is needed?

Thanks!
David

@antoinefaure

Copy link
Copy Markdown
Author

Hello @dvdhrm

The long version is the issue #11935

I will have a look at SO_BINDTOIFINDEX and come back to you asap.

Concerning the per-interface resolution: you can receive DNS servers through DHCP, so you can have specific servers that are joinable only through one interface. In my case it was problematic using a 3G dongle on an embedded system that also has an ethernet connection. The network operator provides us with a DNS server on a public IP (94.x.x.x), but the resolutions are accepted only through the 3G network. At the moment, when I ask resolved to resolve a hostname using this server, it will try to do it through the ethernet link, and our requests are dropped.
In short we have a dns server associated to the link ppp0 (or whatever it's name) but it will be contacted through eth0.

Do you have any idea why the CI is broken ? It seems unrelated to what I've modified :s

@dvdhrm

dvdhrm commented Mar 15, 2019

Copy link
Copy Markdown
Contributor

Do you have any idea why the CI is broken ? It seems unrelated to what I've modified :s

Meh, ignore it, someone with sufficient access will restart it ;)

Can you explain why SO_BINDTODEVICE works but IP_UNICAST_IF does not? Both are used as output-interface to the routing lookup, and both should affect the routing selection to only return routes for that interface (fib_table_lookup()). I don't see how one can work but the other does not.

IP_UNICAST_IF has no other side-effects, while SO_BINDTODEVICE affects all kinds of other knobs. So preferably we would use the former, as it actually is intended to do exactly what we want: Specify an interface to make the routing ignore any routes on other interfaces.

If you just happended to try both and one worked, that is fine. I just want to understand why exactly this is needed.

@yuwata

yuwata commented Mar 15, 2019

Copy link
Copy Markdown
Member

Do you have any idea why the CI is broken ? It seems unrelated to what I've modified :s

Meh, ignore it, someone with sufficient access will restart it ;)

No no, please do not ignore the CI failures. These are caused by this PR. The test script networkd-test.py also tests resolved.

======================================================================
ERROR: test_resolved_etc_hosts (__main__.DnsmasqClientTest)
resolved queries to /etc/hosts
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/autopkgtest.2pNVl7/build.SWB/systemd/test/networkd-test.py", line 370, in do_test
    self.iface, '--timeout=%i' % online_timeout])
  File "/usr/lib/python3.6/subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/lib/systemd/systemd-networkd-wait-online', '--interface', 'test_eth42', '--timeout=10']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/autopkgtest.2pNVl7/build.SWB/systemd/test/networkd-test.py", line 697, in test_resolved_etc_hosts
    self.do_test(coldplug=None, ipv6=True)
  File "/tmp/autopkgtest.2pNVl7/build.SWB/systemd/test/networkd-test.py", line 424, in do_test
    self.print_server_log()
  File "/tmp/autopkgtest.2pNVl7/build.SWB/systemd/test/networkd-test.py", line 590, in print_server_log
    with open(self.dnsmasq_log) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpavt01c33/dnsmasq.log'

----------------------------------------------------------------------
Ran 36 tests in 275.406s

FAILED (errors=1, skipped=2)
autopkgtest [10:02:04]: test networkd-test.py: -----------------------]
autopkgtest [10:02:04]: test networkd-test.py:  - - - - - - - - - - results - - - - - - - - - -
networkd-test.py     FAIL non-zero exit status 1
autopkgtest [10:02:04]: test networkd-test.py:  - - - - - - - - - - stderr - - - - - - - - - -

@yuwata yuwata added resolve ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR labels Mar 15, 2019
@b1tninja

Copy link
Copy Markdown
Contributor

master...b1tninja:dns_bind_addr

any desire to collaborate on this? I got the ifname moved into the manager struct, just need to wire up config parsing....

does anyone see any usefulness in looking up the interface by address?

An empty string will unbind the socket from the device/interface, but would a keyword / other config param be more user friendly? Since currently the default is "lo"

@dvdhrm

dvdhrm commented Mar 20, 2019

Copy link
Copy Markdown
Contributor

@antoinefaure So I checked the kernel sources again IP_UNICAST_IF should do exactly what we want. I will try setting up a virtual test network to reproduce your issues. Do you have a minimal description of the setup where you experience these issues?

@poettering

Copy link
Copy Markdown
Member

Secondly: Newer kernels provide SO_BINDTOIFINDEX. It would be cool to write a helper that uses this and falls back to SO_BINDTODEVICE if it fails. It could look similar like the one in n-dhcp4:

(Just to mention that: #12024 moved systemd over to using SO_BINDTOIFINDEX where available)

@antoinefaure

antoinefaure commented Mar 20, 2019 •

Copy link
Copy Markdown
Author

Hello,

I force pushed with the following changes:

@antoinefaure

Copy link
Copy Markdown
Author

@dvdhrm When using IP_UNICAST_IF the request is not always sent using the DNS server's interface. It is quite easy to reproduce :

  • Connect your computer with 2 interfaces (e.g. ethernet + wifi)
  • Add a DNS server to resolved on the wireless interface (which is not the best default route)
  • Start a capture with wireshark or tcpdump for example
  • Ask resolved to resolve a hostname on the wireless interface (which, again, should not be the best default route)
  • You'll see that the request will not be sent through the wireless interface but through the default route (ethernet)

This is not very problematic on a computer where the 2 interfaces have access to internet, but as described in the issue #11935 this is problematic when the secondary connection is a 3G dongle for example.

I think IP_UNICAST_IF does respect the routing rules, whereas SO_BINDTODEVICE does not but instead guarantees to use the asked interface. As @poettering said in the issue, this is poorly documented and the difference between the 2 socket options is not very clear

When using a DNS server associated to a link, make sure to use this link to send the request.
IP_UNICAST_IF is overridden by the configured routes, and therefore doesn't guarantee that the request will go on the desired interface.
To use SO_BINDTODEVICE we need to keep the capability CAP_NET_RAW
@poettering

Copy link
Copy Markdown
Member

I posted #17535 now which contains a commit that issues the SO_BINDTOINDEX sockopt before calling connect() and then undoes it right after. THis should give the right behaviour, as it means the routing decision done in connect() will take the binding into account, but the other SO_BINDTOINDE semantics we won't be affected by.

Ideally this would be fixed in the kernel, i.e. connect() should really take the IP_UNICAST_IF ifindex into account when doing its routing decision, but after checking the kernel sources, it currently really does not.

Anyway, closing in favour of #17535

@poettering poettering closed this Nov 6, 2020
poettering added a commit to poettering/systemd that referenced this pull request Dec 2, 2020
Apparently, IF_UNICAST_IF does not influence the routing decisions done
during connect(). But SO_BINDTODEVICE/SO_BINDTOINDEX does, which however
brings a lot of other semantics with it, we are not so interested in
(i.e. it doesn't not allow packets from any other iface to us, even if
routing otherwise allows it).

Hence, let's bind to the ifindex immediately before the connect() and
unbind right after again, so that we get the semantics we want, but not
the ones we don't.

Fixes: systemd#11935
Replaces: systemd#12004
mikhailnov pushed a commit to mikhailnov/systemd that referenced this pull request Jan 16, 2021
Apparently, IF_UNICAST_IF does not influence the routing decisions done
during connect(). But SO_BINDTODEVICE/SO_BINDTOINDEX does, which however
brings a lot of other semantics with it, we are not so interested in
(i.e. it doesn't not allow packets from any other iface to us, even if
routing otherwise allows it).

Hence, let's bind to the ifindex immediately before the connect() and
unbind right after again, so that we get the semantics we want, but not
the ones we don't.

Fixes: systemd#11935
Replaces: systemd#12004
(cherry picked from commit d301c52)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-fails/needs-rework 🔥 Please rework this, the CI noticed an issue with the PR resolve

Development

Successfully merging this pull request may close these issues.

Resolved: wrong interface used

5 participants